Skip to content

Commit a86dbb8

Browse files
committed
test: improve coverage
1 parent c8a914a commit a86dbb8

File tree

1 file changed

+87
-1
lines changed

1 file changed

+87
-1
lines changed

internal/librariangen/generate/generator_test.go

Lines changed: 87 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -374,11 +374,93 @@ func TestRestructureOutput(t *testing.T) {
374374
}
375375
}
376376

377-
func TestUnzip(t *testing.T) {
377+
func TestCopyAndMerge(t *testing.T) {
378378
e := newTestEnv(t)
379379
defer e.cleanup(t)
380380

381+
// 1. Setup: Create source and destination directories with nested structures.
382+
srcDir := filepath.Join(e.tmpDir, "src")
383+
destDir := filepath.Join(e.tmpDir, "dest")
384+
sourceFiles := map[string]string{
385+
"com/google/foo.java": "",
386+
"com/google/bar/baz.java": "",
387+
"com/google/bar/qux/quux.java": "",
388+
}
389+
destFiles := map[string]string{
390+
"com/google/existing.java": "",
391+
"com/google/bar/another.java": "",
392+
}
393+
for path, content := range sourceFiles {
394+
fullPath := filepath.Join(srcDir, path)
395+
if err := os.MkdirAll(filepath.Dir(fullPath), 0755); err != nil {
396+
t.Fatalf("failed to create source directory for %s: %v", path, err)
397+
}
398+
if err := os.WriteFile(fullPath, []byte(content), 0644); err != nil {
399+
t.Fatalf("failed to write source file for %s: %v", path, err)
400+
}
401+
}
402+
for path, content := range destFiles {
403+
fullPath := filepath.Join(destDir, path)
404+
if err := os.MkdirAll(filepath.Dir(fullPath), 0755); err != nil {
405+
t.Fatalf("failed to create dest directory for %s: %v", path, err)
406+
}
407+
if err := os.WriteFile(fullPath, []byte(content), 0644); err != nil {
408+
t.Fatalf("failed to write dest file for %s: %v", path, err)
409+
}
410+
}
411+
412+
// 2. Execute: Call the function under test.
413+
if err := copyAndMerge(srcDir, destDir); err != nil {
414+
t.Fatalf("copyAndMerge() failed: %v", err)
415+
}
416+
417+
// 3. Verify: Check that all files were merged correctly.
418+
for path := range sourceFiles {
419+
fullPath := filepath.Join(destDir, path)
420+
if _, err := os.Stat(fullPath); err != nil {
421+
t.Errorf("source file not merged: %v", err)
422+
}
423+
}
424+
for path := range destFiles {
425+
fullPath := filepath.Join(destDir, path)
426+
if _, err := os.Stat(fullPath); err != nil {
427+
t.Errorf("destination file was deleted: %v", err)
428+
}
429+
}
430+
}
431+
432+
func TestUnzip(t *testing.T) {
433+
t.Run("happy path", func(t *testing.T) {
434+
e := newTestEnv(t)
435+
defer e.cleanup(t)
436+
// Create a valid zip file.
437+
zipPath := filepath.Join(e.outputDir, "valid.zip")
438+
f, err := os.Create(zipPath)
439+
if err != nil {
440+
t.Fatalf("failed to create zip file: %v", err)
441+
}
442+
defer f.Close()
443+
zipWriter := zip.NewWriter(f)
444+
if _, err := zipWriter.Create("file.txt"); err != nil {
445+
t.Fatalf("failed to create file in zip: %v", err)
446+
}
447+
zipWriter.Close()
448+
449+
// Unzip the file.
450+
destDir := filepath.Join(e.outputDir, "unzip-dest")
451+
if err := unzip(zipPath, destDir); err != nil {
452+
t.Fatalf("unzip() failed: %v", err)
453+
}
454+
455+
// Check that the file was unzipped.
456+
if _, err := os.Stat(filepath.Join(destDir, "file.txt")); err != nil {
457+
t.Errorf("file not unzipped: %v", err)
458+
}
459+
})
460+
381461
t.Run("invalid zip file", func(t *testing.T) {
462+
e := newTestEnv(t)
463+
defer e.cleanup(t)
382464
invalidZipPath := filepath.Join(e.outputDir, "invalid.zip")
383465
if err := os.WriteFile(invalidZipPath, []byte("not a zip file"), 0644); err != nil {
384466
t.Fatalf("failed to write invalid zip file: %v", err)
@@ -389,6 +471,8 @@ func TestUnzip(t *testing.T) {
389471
})
390472

391473
t.Run("permission denied", func(t *testing.T) {
474+
e := newTestEnv(t)
475+
defer e.cleanup(t)
392476
// Create a valid zip file.
393477
validZipPath := filepath.Join(e.outputDir, "valid.zip")
394478
if err := os.WriteFile(validZipPath, []byte{}, 0644); err != nil {
@@ -423,6 +507,8 @@ func TestUnzip(t *testing.T) {
423507
})
424508

425509
t.Run("zip slip vulnerability", func(t *testing.T) {
510+
e := newTestEnv(t)
511+
defer e.cleanup(t)
426512
// Create a zip file with a malicious file path.
427513
maliciousZipPath := filepath.Join(e.outputDir, "malicious.zip")
428514
f, err := os.Create(maliciousZipPath)

0 commit comments

Comments
 (0)