-
-
Notifications
You must be signed in to change notification settings - Fork 414
Compress all files selected #821
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
6835752
First Solution for compress Files
c012965
solve: compress all selected files
fd42dcf
Deleted reduntant error , edit function getZipArchiveName, comment ou…
7d5af0e
added tests for zipSources function
1506966
Rewrote Test, deleted commented function, fixed spacing
larsn03 3c6bf68
Added process-management to zipSources Func
larsn03 27a06ae
Fixed linter errors and addressed double transversal bottleneck
larsn03 e97a1bd
Fixed linter errors and fixed testsuite/ReadMe.md
larsn03 49ddace
fix: Remove commented out code
lazysegtree 5b4dc0e
fix: refactor compressFile function
lazysegtree b80be69
fix: Cleanup in setupFunc() for TestZipSources
lazysegtree 3f5cdf7
feat: Add compress and extract test in handle file operation
lazysegtree f2b0b8e
fix: Fix test duplication, and added test for empty panel
lazysegtree File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,138 @@ | ||
| package internal | ||
|
|
||
| import ( | ||
| "archive/zip" | ||
| "io" | ||
| "os" | ||
| "path/filepath" | ||
| "strings" | ||
| "testing" | ||
|
|
||
| "github.com/stretchr/testify/require" | ||
| ) | ||
|
|
||
| func TestZipSources(t *testing.T) { | ||
| tests := []struct { | ||
| name string | ||
| setupFunc func(t *testing.T, tempDir string) ([]string, error) | ||
| expectedFiles map[string]string | ||
| expectError bool | ||
| }{ | ||
| { | ||
| name: "multiple directories with subdirectories", | ||
| setupFunc: func(t *testing.T, tempDir string) ([]string, error) { | ||
| testDir1 := filepath.Join(tempDir, "testdir1") | ||
| testDir2 := filepath.Join(tempDir, "testdir2") | ||
| subDir := filepath.Join(testDir1, "subdir") | ||
| setupDirectories(t, testDir1, testDir2, subDir) | ||
| setupFilesWithData(t, []byte("Content of file1"), filepath.Join(testDir1, "file1.txt")) | ||
| setupFilesWithData(t, []byte("Content of file2"), filepath.Join(subDir, "file2.txt")) | ||
| setupFilesWithData(t, []byte("Content of file3"), filepath.Join(testDir2, "file3.txt")) | ||
|
|
||
| return []string{testDir1, testDir2}, nil | ||
| }, | ||
| expectedFiles: map[string]string{ | ||
| "testdir1/": "", | ||
| "testdir1/file1.txt": "Content of file1", | ||
| "testdir1/subdir/": "", | ||
| "testdir1/subdir/file2.txt": "Content of file2", | ||
| "testdir2/": "", | ||
| "testdir2/file3.txt": "Content of file3", | ||
| }, | ||
| expectError: false, | ||
| }, | ||
| { | ||
| name: "single file", | ||
| setupFunc: func(t *testing.T, tempDir string) ([]string, error) { | ||
| testFile := filepath.Join(tempDir, "single.txt") | ||
| setupFilesWithData(t, []byte("Single file content"), testFile) | ||
| return []string{testFile}, nil | ||
| }, | ||
| expectedFiles: map[string]string{ | ||
| "single.txt": "Single file content", | ||
| }, | ||
| expectError: false, | ||
| }, | ||
| { | ||
| name: "empty list", | ||
| setupFunc: func(_ *testing.T, _ string) ([]string, error) { | ||
| return []string{}, nil | ||
| }, | ||
| expectedFiles: map[string]string{}, | ||
| expectError: false, | ||
| }, | ||
| { | ||
| name: "non-existent source", | ||
| setupFunc: func(_ *testing.T, _ string) ([]string, error) { | ||
| return []string{"/non/existent/path"}, nil | ||
| }, | ||
| expectedFiles: nil, | ||
| expectError: true, | ||
| }, | ||
| } | ||
|
|
||
| for _, tt := range tests { | ||
| t.Run(tt.name, func(t *testing.T) { | ||
| tempDir := t.TempDir() | ||
| sources, err := tt.setupFunc(t, tempDir) | ||
| if err != nil { | ||
| t.Fatalf("Setup failed: %v", err) | ||
| } | ||
|
|
||
| targetZip := filepath.Join(tempDir, "test.zip") | ||
| err = zipSources(sources, targetZip) | ||
|
|
||
| if tt.expectError { | ||
| require.Error(t, err, "zipSources should return error") | ||
| return | ||
| } | ||
|
|
||
| require.NoError(t, err, "zipSources should not return error") | ||
|
|
||
| zipReader, err := zip.OpenReader(targetZip) | ||
| require.NoError(t, err, "should be able to open ZIP file") | ||
| defer zipReader.Close() | ||
|
|
||
| require.Equal(t, len(tt.expectedFiles), len(zipReader.File), "ZIP should contain expected number of files") | ||
|
|
||
| foundFiles := make(map[string]string) | ||
| for _, file := range zipReader.File { | ||
| foundFiles[file.Name] = "" | ||
| if !strings.HasSuffix(file.Name, "/") { | ||
| rc, err := file.Open() | ||
| require.NoError(t, err, "should be able to open file %s in ZIP", file.Name) | ||
|
|
||
| content, err := io.ReadAll(rc) | ||
| rc.Close() | ||
| require.NoError(t, err, "should be able to read file %s", file.Name) | ||
|
|
||
| foundFiles[file.Name] = string(content) | ||
| } | ||
| } | ||
|
|
||
| for expectedFile, expectedContent := range tt.expectedFiles { | ||
| foundContent, exists := foundFiles[expectedFile] | ||
| require.True(t, exists, "expected file %s should be found in ZIP", expectedFile) | ||
| if expectedContent != "" { | ||
| require.Equal(t, expectedContent, foundContent, "content should match for file %s", expectedFile) | ||
| } | ||
| } | ||
|
|
||
| for foundFile := range foundFiles { | ||
| _, expected := tt.expectedFiles[foundFile] | ||
| require.True(t, expected, "unexpected file %s found in ZIP", foundFile) | ||
| } | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| func TestZipSourcesInvalidTarget(t *testing.T) { | ||
| tempDir := t.TempDir() | ||
| testFile := filepath.Join(tempDir, "test.txt") | ||
| err := os.WriteFile(testFile, []byte("test"), 0644) | ||
| require.NoError(t, err, "should be able to create test file") | ||
|
|
||
| invalidTarget := "/invalid/path/test.zip" | ||
| err = zipSources([]string{testFile}, invalidTarget) | ||
| require.Error(t, err, "zipSources should return error for invalid target") | ||
| } |
This file contains hidden or 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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.