-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: Improve error handling and fix wrong compress dir for windows (#367
) * fix: Improve error handling and fix wrong compress dir for windows * refactor: Refactor fileutil package for pass linter - Refactored the `CompressDir` function in the `fileutil` package for improved error handling and handling of edge cases - Improved error messages for various file operations - Added test file and created tests for the `CompressDir` function in the `fileutil` package
- Loading branch information
Showing
3 changed files
with
108 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package fileutil | ||
|
||
import ( | ||
"os" | ||
"path/filepath" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func setupTestDir(t *testing.T, files []string) string { | ||
t.Helper() // Marks the function as a helper function. | ||
|
||
tempDir, err := os.MkdirTemp("", "testCompressDir") | ||
require.NoError(t, err, "failed to create a temporary directory") | ||
|
||
for _, file := range files { | ||
filePath := filepath.Join(tempDir, file) | ||
err := os.WriteFile(filePath, []byte("test content"), 0o644) | ||
require.NoError(t, err, "failed to create a test file") | ||
} | ||
return tempDir | ||
} | ||
|
||
func TestCompressDir(t *testing.T) { | ||
t.Run("Normal Operation", func(t *testing.T) { | ||
tempDir := setupTestDir(t, []string{"file1.txt", "file2.txt", "file3.txt"}) | ||
defer os.RemoveAll(tempDir) | ||
|
||
err := CompressDir(tempDir) | ||
assert.NoError(t, err, "compressDir should not return an error") | ||
|
||
// Check if the zip file exists | ||
zipFile := filepath.Join(tempDir, filepath.Base(tempDir)+".zip") | ||
assert.FileExists(t, zipFile, "zip file should be created") | ||
}) | ||
|
||
t.Run("Directory Does Not Exist", func(t *testing.T) { | ||
err := CompressDir("/path/to/nonexistent/directory") | ||
assert.Error(t, err, "should return an error for non-existent directory") | ||
}) | ||
|
||
t.Run("Empty Directory", func(t *testing.T) { | ||
tempDir, err := os.MkdirTemp("", "testEmptyDir") | ||
require.NoError(t, err, "failed to create empty test directory") | ||
defer os.RemoveAll(tempDir) | ||
|
||
err = CompressDir(tempDir) | ||
assert.Error(t, err, "should return an error for an empty directory") | ||
}) | ||
} |