|
1 | 1 | package archives |
2 | 2 |
|
3 | 3 | import ( |
| 4 | +"context" |
4 | 5 | "fmt" |
5 | 6 | "os" |
6 | 7 | "path/filepath" |
7 | 8 | "reflect" |
8 | 9 | "runtime" |
| 10 | +"sort" |
9 | 11 | "strings" |
10 | 12 | "testing" |
11 | 13 | ) |
@@ -266,6 +268,13 @@ func TestNameOnDiskToNameInArchive(t *testing.T) { |
266 | 268 | } |
267 | 269 | } |
268 | 270 |
|
| 271 | +func fixSeparators(path string) string { |
| 272 | +if runtime.GOOS == "windows" { |
| 273 | +return strings.ReplaceAll(path, "/", "\\") |
| 274 | +} |
| 275 | +return path |
| 276 | +} |
| 277 | + |
269 | 278 | func TestFollowSymlink(t *testing.T) { |
270 | 279 | // Create temp directory for tests |
271 | 280 | tmpDir := t.TempDir() |
@@ -510,3 +519,46 @@ func TestFollowSymlink(t *testing.T) { |
510 | 519 | } |
511 | 520 | }) |
512 | 521 | } |
| 522 | + |
| 523 | +func TestFilesFromDisk_SymlinkOutsideFileNamesMap(t *testing.T) { |
| 524 | +tmpDir := t.TempDir() |
| 525 | +otherTmpDir := t.TempDir() |
| 526 | + |
| 527 | +testDirName := "test_dir" |
| 528 | +testDir := filepath.Join(otherTmpDir, testDirName) |
| 529 | +if err := os.Mkdir(testDir, 0755); err != nil { |
| 530 | +t.Fatal(err) |
| 531 | +} |
| 532 | + |
| 533 | +testFileName := "test.txt" |
| 534 | +testFile := filepath.Join(testDir, testFileName) |
| 535 | +if err := os.WriteFile(testFile, []byte("test content"), 0644); err != nil { |
| 536 | +t.Fatal(err) |
| 537 | +} |
| 538 | + |
| 539 | +symlinkDirName := "symlink_dir" |
| 540 | +symlinkDir := filepath.Join(tmpDir, symlinkDirName) |
| 541 | +if err := os.Symlink(testDir, symlinkDir); err != nil { |
| 542 | +t.Fatal(err) |
| 543 | +} |
| 544 | + |
| 545 | +files, err := FilesFromDisk(context.Background(), &FromDiskOptions{ |
| 546 | +FollowSymlinks: true, |
| 547 | +}, map[string]string{symlinkDir: ""}) |
| 548 | +if err != nil { |
| 549 | +t.Fatal(err) |
| 550 | +} |
| 551 | + |
| 552 | +sort.Slice(files, func(i, j int) bool { |
| 553 | +return files[i].NameInArchive < files[j].NameInArchive |
| 554 | +}) |
| 555 | + |
| 556 | +if files[0].NameInArchive != symlinkDirName { |
| 557 | +t.Fatalf("expected file name '%s', got '%s'", symlinkDirName, files[0].NameInArchive) |
| 558 | +} |
| 559 | + |
| 560 | +testFilePath := fmt.Sprintf("%s/%s", symlinkDirName, testFileName) |
| 561 | +if files[1].NameInArchive != testFilePath { |
| 562 | +t.Fatalf("expected file name '%s', got '%s'", testFilePath, files[1].NameInArchive) |
| 563 | +} |
| 564 | +} |
0 commit comments