diff --git a/internal/provider/data_source_archive_file_test.go b/internal/provider/data_source_archive_file_test.go index 8ec32870..ddfec65e 100644 --- a/internal/provider/data_source_archive_file_test.go +++ b/internal/provider/data_source_archive_file_test.go @@ -960,8 +960,8 @@ func TestAccArchiveFile_SymlinkFile_Absolute_ExcludeSymlinkDirectories(t *testin }) } -// TestAccArchiveFile_SymlinkDirectory_Relative_ExcludeSymlinkDirectories verifies that an error is generated when -// trying to use a symlink to a directory. +// TestAccArchiveFile_SymlinkDirectory_Relative_ExcludeSymlinkDirectories verifies that an empty archive +// is generated when trying to archive a directory which only contains a symlink to a directory. func TestAccArchiveFile_SymlinkDirectory_Relative_ExcludeSymlinkDirectories(t *testing.T) { td := t.TempDir() @@ -980,14 +980,19 @@ func TestAccArchiveFile_SymlinkDirectory_Relative_ExcludeSymlinkDirectories(t *t exclude_symlink_directories = true } `, filepath.ToSlash("test-fixtures/test-symlink-dir"), filepath.ToSlash(f)), - ExpectError: regexp.MustCompile(`.*error creating archive: error archiving directory: error reading file for`), + Check: r.ComposeTestCheckFunc( + r.TestCheckResourceAttrWith("data.archive_file.foo", "output_path", func(value string) error { + ensureContents(t, value, map[string][]byte{}) + return nil + }), + ), }, }, }) } -// TestAccArchiveFile_SymlinkDirectory_Absolute_ExcludeSymlinkDirectories verifies that an error is generated when -// trying to use a symlink to a directory. +// TestAccArchiveFile_SymlinkDirectory_Absolute_ExcludeSymlinkDirectories verifies that an empty archive +// is generated when trying to archive a directory which only contains a symlink to a directory. func TestAccArchiveFile_SymlinkDirectory_Absolute_ExcludeSymlinkDirectories(t *testing.T) { td := t.TempDir() @@ -1011,7 +1016,12 @@ func TestAccArchiveFile_SymlinkDirectory_Absolute_ExcludeSymlinkDirectories(t *t exclude_symlink_directories = true } `, filepath.ToSlash(symlinkDirWithRegularFilesAbs), filepath.ToSlash(f)), - ExpectError: regexp.MustCompile(`.*error creating archive: error archiving directory: error reading file for`), + Check: r.ComposeTestCheckFunc( + r.TestCheckResourceAttrWith("data.archive_file.foo", "output_path", func(value string) error { + ensureContents(t, value, map[string][]byte{}) + return nil + }), + ), }, }, }) @@ -1181,8 +1191,8 @@ func TestAccArchiveFile_SymlinkDirectoryWithSymlinkFile_Absolute_ExcludeSymlinkD }) } -// TestAccArchiveFile_DirectoryWithSymlinkDirectory_Relative_ExcludeSymlinkDirectories verifies that an error is -// generated when trying to a directory which contains a symlink to a directory. +// TestAccArchiveFile_DirectoryWithSymlinkDirectory_Relative_ExcludeSymlinkDirectories verifies that an empty archive +// is generated when trying to archive a directory which only contains a symlink to a directory. func TestAccArchiveFile_DirectoryWithSymlinkDirectory_Relative_ExcludeSymlinkDirectories(t *testing.T) { td := t.TempDir() @@ -1197,18 +1207,22 @@ func TestAccArchiveFile_DirectoryWithSymlinkDirectory_Relative_ExcludeSymlinkDir type = "zip" source_dir = "%s" output_path = "%s" - output_file_mode = "0666" exclude_symlink_directories = true } `, filepath.ToSlash("test-fixtures/test-dir-with-symlink-dir"), filepath.ToSlash(f)), - ExpectError: regexp.MustCompile(`.*error creating archive: error archiving directory: error reading file for`), + Check: r.ComposeTestCheckFunc( + r.TestCheckResourceAttrWith("data.archive_file.foo", "output_path", func(value string) error { + ensureContents(t, value, map[string][]byte{}) + return nil + }), + ), }, }, }) } -// TestAccArchiveFile_IncludeDirectoryWithSymlinkDirectory_Absolute_ExcludeSymlinkDirectories verifies that an error is -// generated when trying to a directory which contains a symlink to a directory. +// TestAccArchiveFile_IncludeDirectoryWithSymlinkDirectory_Absolute_ExcludeSymlinkDirectories verifies that an empty archive +// is generated when trying to archive a directory which only contains a symlink to a directory. func TestAccArchiveFile_IncludeDirectoryWithSymlinkDirectory_Absolute_ExcludeSymlinkDirectories(t *testing.T) { td := t.TempDir() @@ -1228,23 +1242,29 @@ func TestAccArchiveFile_IncludeDirectoryWithSymlinkDirectory_Absolute_ExcludeSym type = "zip" source_dir = "%s" output_path = "%s" - output_file_mode = "0666" exclude_symlink_directories = true } `, filepath.ToSlash(symlinkDirInRegularDirAbs), filepath.ToSlash(f)), - ExpectError: regexp.MustCompile(`.*error creating archive: error archiving directory: error reading file for`), + Check: r.ComposeTestCheckFunc( + r.TestCheckResourceAttrWith("data.archive_file.foo", "output_path", func(value string) error { + ensureContents(t, value, map[string][]byte{}) + return nil + }), + ), }, }, }) } -// TestAccArchiveFile_Multiple_Relative_ExcludeSymlinkDirectories verifies that an error is -// generated when trying to a directory which contains a symlink to a directory. +// TestAccArchiveFile_Multiple_Relative_ExcludeSymlinkDirectories verifies that +// symlinked directories are excluded. func TestAccArchiveFile_Multiple_Relative_ExcludeSymlinkDirectories(t *testing.T) { td := t.TempDir() f := filepath.Join(td, "zip_file_acc_test.zip") + var fileSize string + r.ParallelTest(t, r.TestCase{ ProtoV5ProviderFactories: protoV5ProviderFactories(), Steps: []r.TestStep{ @@ -1258,14 +1278,32 @@ func TestAccArchiveFile_Multiple_Relative_ExcludeSymlinkDirectories(t *testing.T exclude_symlink_directories = true } `, filepath.ToSlash("test-fixtures"), filepath.ToSlash(f)), - ExpectError: regexp.MustCompile(`.*error creating archive: error archiving directory: error reading file for`), + Check: r.ComposeTestCheckFunc( + testAccArchiveFileSize(f, &fileSize), + r.TestCheckResourceAttrPtr("data.archive_file.foo", "output_size", &fileSize), + r.TestCheckResourceAttrWith("data.archive_file.foo", "output_path", func(value string) error { + ensureContents(t, value, map[string][]byte{ + "test-dir/test-dir1/file1.txt": []byte("This is file 1"), + "test-dir/test-dir1/file2.txt": []byte("This is file 2"), + "test-dir/test-dir1/file3.txt": []byte("This is file 3"), + "test-dir/test-dir2/file1.txt": []byte("This is file 1"), + "test-dir/test-dir2/file2.txt": []byte("This is file 2"), + "test-dir/test-dir2/file3.txt": []byte("This is file 3"), + "test-dir/test-file.txt": []byte("This is test content"), + "test-dir-with-symlink-file/test-file.txt": []byte("This is test content"), + "test-dir-with-symlink-file/test-symlink.txt": []byte("This is test content"), + }) + ensureFileMode(t, value, "0666") + return nil + }), + ), }, }, }) } -// TestAccArchiveFile_Multiple_Absolute_ExcludeSymlinkDirectories verifies that an error is -// generated when trying to a directory which contains a symlink to a directory. +// TestAccArchiveFile_Multiple_Relative_ExcludeSymlinkDirectories verifies that +// symlinked directories are excluded. func TestAccArchiveFile_Multiple_Absolute_ExcludeSymlinkDirectories(t *testing.T) { td := t.TempDir() @@ -1276,6 +1314,8 @@ func TestAccArchiveFile_Multiple_Absolute_ExcludeSymlinkDirectories(t *testing.T t.Fatal(err) } + var fileSize string + r.ParallelTest(t, r.TestCase{ ProtoV5ProviderFactories: protoV5ProviderFactories(), Steps: []r.TestStep{ @@ -1289,7 +1329,25 @@ func TestAccArchiveFile_Multiple_Absolute_ExcludeSymlinkDirectories(t *testing.T exclude_symlink_directories = true } `, filepath.ToSlash(multipleDirsAndFilesAbs), filepath.ToSlash(f)), - ExpectError: regexp.MustCompile(`.*error creating archive: error archiving directory: error reading file for`), + Check: r.ComposeTestCheckFunc( + testAccArchiveFileSize(f, &fileSize), + r.TestCheckResourceAttrPtr("data.archive_file.foo", "output_size", &fileSize), + r.TestCheckResourceAttrWith("data.archive_file.foo", "output_path", func(value string) error { + ensureContents(t, value, map[string][]byte{ + "test-dir/test-dir1/file1.txt": []byte("This is file 1"), + "test-dir/test-dir1/file2.txt": []byte("This is file 2"), + "test-dir/test-dir1/file3.txt": []byte("This is file 3"), + "test-dir/test-dir2/file1.txt": []byte("This is file 1"), + "test-dir/test-dir2/file2.txt": []byte("This is file 2"), + "test-dir/test-dir2/file3.txt": []byte("This is file 3"), + "test-dir/test-file.txt": []byte("This is test content"), + "test-dir-with-symlink-file/test-file.txt": []byte("This is test content"), + "test-dir-with-symlink-file/test-symlink.txt": []byte("This is test content"), + }) + ensureFileMode(t, value, "0666") + return nil + }), + ), }, }, }) diff --git a/internal/provider/zip_archiver.go b/internal/provider/zip_archiver.go index 9b149529..c5248444 100644 --- a/internal/provider/zip_archiver.go +++ b/internal/provider/zip_archiver.go @@ -146,23 +146,25 @@ func (a *ZipArchiver) createWalkFunc(basePath string, indirname string, opts Arc } if info.Mode()&os.ModeSymlink == os.ModeSymlink { - if !opts.ExcludeSymlinkDirectories { - realPath, err := filepath.EvalSymlinks(path) - if err != nil { - return err - } + realPath, err := filepath.EvalSymlinks(path) + if err != nil { + return err + } - realInfo, err := os.Stat(realPath) - if err != nil { - return err - } + realInfo, err := os.Stat(realPath) + if err != nil { + return err + } - if realInfo.IsDir() { + if realInfo.IsDir() { + if !opts.ExcludeSymlinkDirectories { return filepath.Walk(realPath, a.createWalkFunc(archivePath, realPath, opts)) + } else { + return filepath.SkipDir } - - info = realInfo } + + info = realInfo } fh, err := zip.FileInfoHeader(info) diff --git a/internal/provider/zip_archiver_test.go b/internal/provider/zip_archiver_test.go index 3d45f324..a46de0d8 100644 --- a/internal/provider/zip_archiver_test.go +++ b/internal/provider/zip_archiver_test.go @@ -9,7 +9,6 @@ import ( "io" "os" "path/filepath" - "regexp" "strconv" "testing" "time" @@ -218,11 +217,8 @@ func TestZipArchiver_Dir_ExcludeSymlinkDirectories(t *testing.T) { ExcludeSymlinkDirectories: true, }) - regex := regexp.MustCompile(`error reading file for archival: read test-fixtures(\/|\\)test-dir-with-symlink-dir(\/|\\)test-symlink-dir: `) - found := regex.Match([]byte(err.Error())) - - if !found { - t.Fatalf("expedted error to match %q, got: %s", regex.String(), err.Error()) + if err != nil { + t.Errorf("expected no error: %s", err) } } @@ -270,11 +266,8 @@ func TestZipArchiver_Dir_Exclude_ExcludeSymlinkDirectories(t *testing.T) { ExcludeSymlinkDirectories: true, }) - regex := regexp.MustCompile(`error reading file for archival: read test-fixtures(\/|\\)test-dir-with-symlink-dir(\/|\\)test-symlink-dir: `) - found := regex.Match([]byte(err.Error())) - - if !found { - t.Fatalf("expedted error to match %q, got: %s", regex.String(), err.Error()) + if err != nil { + t.Errorf("expected no error: %s", err) } }