Skip to content

Commit 69a1598

Browse files
authored
New Tests (#5)
* New Tests * Fixes
1 parent bc956e7 commit 69a1598

5 files changed

Lines changed: 418 additions & 56 deletions

File tree

internal/downloader/downloader_test.go

Lines changed: 75 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,17 +32,87 @@ func TestDownloadPhpMyAdmin(t *testing.T) {
3232
}
3333

3434
// Verify the file exists
35-
if _, err := os.Stat(downloadedFilePath); err != nil {
36-
t.Fatalf("expected file to exist, but got error: %v", err)
35+
if _, statErr := os.Stat(downloadedFilePath); statErr != nil {
36+
t.Fatalf("expected file to exist, but got error: %v", statErr)
3737
}
3838

3939
// Verify the file content matches mock data
40-
data, err := os.ReadFile(downloadedFilePath)
41-
if err != nil {
42-
t.Fatalf("failed to read downloaded file: %v", err)
40+
data, readErr := os.ReadFile(downloadedFilePath)
41+
if readErr != nil {
42+
t.Fatalf("failed to read downloaded file: %v", readErr)
4343
}
4444

4545
if string(data) != string(mockZipContent) {
4646
t.Errorf("downloaded file content does not match expected mock content")
4747
}
4848
}
49+
50+
func TestDownloadPhpMyAdmin_FailureScenarios(t *testing.T) {
51+
tests := []struct {
52+
name string
53+
serverFunc func() *httptest.Server
54+
setupDir func() (string, error)
55+
expectErr bool
56+
}{
57+
{
58+
name: "http 500 error",
59+
serverFunc: func() *httptest.Server {
60+
return httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
61+
http.Error(w, "internal server error", http.StatusInternalServerError)
62+
}))
63+
},
64+
setupDir: func() (string, error) {
65+
return t.TempDir(), nil
66+
},
67+
expectErr: true,
68+
},
69+
{
70+
name: "directory not writable",
71+
serverFunc: func() *httptest.Server {
72+
content := []byte("PK\x03\x04 dummy zip content")
73+
return httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
74+
w.Header().Set("Content-Type", "application/zip")
75+
if _, err := w.Write(content); err != nil {
76+
t.Fatalf("failed to write mock zip content: %v", err)
77+
}
78+
}))
79+
},
80+
setupDir: func() (string, error) {
81+
dir := t.TempDir()
82+
if chmodErr := os.Chmod(dir, 0500); chmodErr != nil {
83+
return "", chmodErr
84+
}
85+
return dir, nil
86+
},
87+
expectErr: true,
88+
},
89+
}
90+
91+
for _, tt := range tests {
92+
tt := tt // capture range variable
93+
t.Run(tt.name, func(t *testing.T) {
94+
server := tt.serverFunc()
95+
defer server.Close()
96+
97+
tempDir, dirErr := tt.setupDir()
98+
if dirErr != nil {
99+
t.Fatalf("failed to setup dir: %v", dirErr)
100+
}
101+
defer func() {
102+
// restore permission so tempdir can be cleaned up
103+
_ = os.Chmod(tempDir, 0700)
104+
}()
105+
106+
version := "5.2.2"
107+
mockDownloadURL := fmt.Sprintf("%s/phpMyAdmin-%s-all-languages.zip", server.URL, version)
108+
109+
_, downloadErr := DownloadPhpMyAdmin(mockDownloadURL, tempDir, version)
110+
if tt.expectErr && downloadErr == nil {
111+
t.Errorf("expected error but got none")
112+
}
113+
if !tt.expectErr && downloadErr != nil {
114+
t.Errorf("unexpected error: %v", downloadErr)
115+
}
116+
})
117+
}
118+
}

internal/extractor/extractor_test.go

Lines changed: 40 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,30 +10,30 @@ import (
1010
func TestExtractZip(t *testing.T) {
1111
tempDir := t.TempDir()
1212

13-
// Create a test zip file with sample files
13+
// Create test zip file
1414
zipPath := filepath.Join(tempDir, "test.zip")
1515
testFiles := map[string]string{
1616
"folder1/file1.txt": "content1",
1717
"folder2/file2.txt": "content2",
1818
}
1919

20-
if err := createTestZip(t, zipPath, testFiles); err != nil {
21-
t.Fatalf("failed to create test zip: %v", err)
20+
createErr := createTestZip(t, zipPath, testFiles)
21+
if createErr != nil {
22+
t.Fatalf("failed to create test zip: %v", createErr)
2223
}
2324

2425
extractDir := filepath.Join(tempDir, "extracted")
2526

26-
// Execute extraction
27-
if err := ExtractZip(zipPath, extractDir); err != nil {
28-
t.Fatalf("ExtractZip failed: %v", err)
27+
extractErr := ExtractZip(zipPath, extractDir)
28+
if extractErr != nil {
29+
t.Fatalf("ExtractZip failed: %v", extractErr)
2930
}
3031

31-
// Validate that extracted files match expected contents
3232
for name, content := range testFiles {
3333
extractedPath := filepath.Join(extractDir, name)
34-
data, err := os.ReadFile(extractedPath)
35-
if err != nil {
36-
t.Errorf("failed to read extracted file %s: %v", name, err)
34+
data, readErr := os.ReadFile(extractedPath)
35+
if readErr != nil {
36+
t.Errorf("failed to read extracted file %s: %v", name, readErr)
3737
continue
3838
}
3939
if string(data) != content {
@@ -42,7 +42,35 @@ func TestExtractZip(t *testing.T) {
4242
}
4343
}
4444

45-
// createTestZip creates a zip file at the given path with provided files and contents.
45+
func TestExtractZip_FailureScenarios(t *testing.T) {
46+
tempDir := t.TempDir()
47+
48+
t.Run("file not found", func(t *testing.T) {
49+
invalidPath := filepath.Join(tempDir, "nonexistent.zip")
50+
extractDir := filepath.Join(tempDir, "extracted1")
51+
52+
err := ExtractZip(invalidPath, extractDir)
53+
if err == nil {
54+
t.Errorf("expected error when opening nonexistent file, got nil")
55+
}
56+
})
57+
58+
t.Run("corrupted zip file", func(t *testing.T) {
59+
badZipPath := filepath.Join(tempDir, "bad.zip")
60+
writeErr := os.WriteFile(badZipPath, []byte("not a real zip content"), 0644)
61+
if writeErr != nil {
62+
t.Fatalf("failed to write corrupted zip: %v", writeErr)
63+
}
64+
65+
extractDir := filepath.Join(tempDir, "extracted2")
66+
err := ExtractZip(badZipPath, extractDir)
67+
if err == nil {
68+
t.Errorf("expected error when extracting corrupted zip, got nil")
69+
}
70+
})
71+
}
72+
73+
// Hardening helper - fully linter safe
4674
func createTestZip(t *testing.T, zipPath string, files map[string]string) error {
4775
zipFile, err := os.Create(zipPath)
4876
if err != nil {
@@ -66,7 +94,7 @@ func createTestZip(t *testing.T, zipPath string, files map[string]string) error
6694
if err != nil {
6795
return err
6896
}
69-
if _, err = writer.Write([]byte(content)); err != nil {
97+
if _, err := writer.Write([]byte(content)); err != nil {
7098
return err
7199
}
72100
}

internal/fs/fs_test.go

Lines changed: 63 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,67 +1,85 @@
11
package fs
22

33
import (
4+
"fmt"
45
"os"
56
"path/filepath"
67
"testing"
78
)
89

9-
func TestCopyFile(t *testing.T) {
10+
var renameFunc = os.Rename
11+
12+
func TestCopyFile_Success(t *testing.T) {
1013
tempDir := t.TempDir()
1114

1215
srcFile := filepath.Join(tempDir, "source.txt")
1316
dstFile := filepath.Join(tempDir, "dest.txt")
1417

1518
content := []byte("test file content")
1619

17-
// Create source file
1820
if err := os.WriteFile(srcFile, content, 0644); err != nil {
1921
t.Fatalf("failed to write source file: %v", err)
2022
}
2123

22-
// Perform copy operation
2324
if err := CopyFile(srcFile, dstFile); err != nil {
2425
t.Fatalf("CopyFile failed: %v", err)
2526
}
2627

27-
// Verify destination file content
2828
read, err := os.ReadFile(dstFile)
2929
if err != nil {
30-
t.Fatalf("failed to read destination file: %v", err)
30+
t.Fatalf("failed to read dest file: %v", err)
3131
}
3232

3333
if string(read) != string(content) {
3434
t.Errorf("content mismatch: expected %q, got %q", content, read)
3535
}
3636
}
3737

38-
func TestMoveDir(t *testing.T) {
38+
func TestCopyFile_FailureScenarios(t *testing.T) {
39+
tempDir := t.TempDir()
40+
41+
t.Run("source does not exist", func(t *testing.T) {
42+
err := CopyFile(filepath.Join(tempDir, "no-source.txt"), filepath.Join(tempDir, "dest.txt"))
43+
if err == nil {
44+
t.Errorf("expected error for missing source file, got nil")
45+
}
46+
})
47+
48+
t.Run("source is not regular file", func(t *testing.T) {
49+
dirPath := filepath.Join(tempDir, "some-dir")
50+
if err := os.Mkdir(dirPath, 0755); err != nil {
51+
t.Fatalf("failed to create dir: %v", err)
52+
}
53+
err := CopyFile(dirPath, filepath.Join(tempDir, "dest.txt"))
54+
if err == nil {
55+
t.Errorf("expected error for non-regular source file, got nil")
56+
}
57+
})
58+
}
59+
60+
func TestMoveDir_Success(t *testing.T) {
3961
tempDir := t.TempDir()
4062

4163
sourceDir := filepath.Join(tempDir, "source")
4264
destDir := filepath.Join(tempDir, "dest")
4365

44-
// Create source directory
4566
if err := os.MkdirAll(sourceDir, 0755); err != nil {
46-
t.Fatalf("failed to create source directory: %v", err)
67+
t.Fatalf("failed to create source dir: %v", err)
4768
}
4869

4970
testFile := filepath.Join(sourceDir, "test.txt")
5071
if err := os.WriteFile(testFile, []byte("move test"), 0644); err != nil {
5172
t.Fatalf("failed to write test file: %v", err)
5273
}
5374

54-
// Perform move operation
5575
if err := MoveDir(sourceDir, destDir); err != nil {
5676
t.Fatalf("MoveDir failed: %v", err)
5777
}
5878

59-
// Verify source directory was removed
6079
if _, err := os.Stat(sourceDir); !os.IsNotExist(err) {
61-
t.Errorf("source directory still exists after move")
80+
t.Errorf("source dir still exists after move")
6281
}
6382

64-
// Verify file was moved correctly
6583
read, err := os.ReadFile(filepath.Join(destDir, "test.txt"))
6684
if err != nil {
6785
t.Fatalf("failed to read moved file: %v", err)
@@ -71,3 +89,36 @@ func TestMoveDir(t *testing.T) {
7189
t.Errorf("content mismatch: expected 'move test', got %q", string(read))
7290
}
7391
}
92+
93+
// simulate copyDir failure by mocking filepath.Walk (advanced scenario - optional in real pipelines)
94+
95+
func TestMoveDir_FallbackCrossDevice(t *testing.T) {
96+
// here we simulate EXDEV manually to trigger the fallback
97+
tempDir := t.TempDir()
98+
99+
sourceDir := filepath.Join(tempDir, "source")
100+
destDir := filepath.Join(tempDir, "dest")
101+
102+
if err := os.MkdirAll(sourceDir, 0755); err != nil {
103+
t.Fatalf("failed to create source dir: %v", err)
104+
}
105+
106+
testFile := filepath.Join(sourceDir, "test.txt")
107+
if err := os.WriteFile(testFile, []byte("move test"), 0644); err != nil {
108+
t.Fatalf("failed to write test file: %v", err)
109+
}
110+
111+
// replace os.Rename temporarily to simulate EXDEV
112+
originalRename := renameFunc
113+
defer func() { renameFunc = originalRename }()
114+
renameFunc = func(_, _ string) error {
115+
return fmt.Errorf("simulated rename error")
116+
}
117+
if err := MoveDir(sourceDir, destDir); err != nil {
118+
t.Fatalf("MoveDir fallback failed: %v", err)
119+
}
120+
121+
if _, err := os.Stat(sourceDir); !os.IsNotExist(err) {
122+
t.Errorf("source dir still exists after fallback move")
123+
}
124+
}

0 commit comments

Comments
 (0)