Skip to content

Commit

Permalink
Return error when generating an archive would result in an empty arch…
Browse files Browse the repository at this point in the history
…ive (#296)
  • Loading branch information
bendbennett committed Jan 17, 2024
1 parent 437d727 commit b3ddb4c
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 27 deletions.
5 changes: 5 additions & 0 deletions .changes/unreleased/BUG FIXES-20240117-101851.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
kind: BUG FIXES
body: 'resource/archive_file: Return error when generated archive would be empty'
time: 2024-01-17T10:18:51.941981Z
custom:
Issue: "298"
5 changes: 5 additions & 0 deletions .changes/unreleased/BUG FIXES-20240117-101923.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
kind: BUG FIXES
body: 'data-source/archive_file: Return error when generated archive would be empty'
time: 2024-01-17T10:19:23.907477Z
custom:
Issue: "298"
28 changes: 4 additions & 24 deletions internal/provider/data_source_archive_file_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -980,12 +980,7 @@ func TestAccArchiveFile_SymlinkDirectory_Relative_ExcludeSymlinkDirectories(t *t
exclude_symlink_directories = true
}
`, filepath.ToSlash("test-fixtures/test-symlink-dir"), filepath.ToSlash(f)),
Check: r.ComposeTestCheckFunc(
r.TestCheckResourceAttrWith("data.archive_file.foo", "output_path", func(value string) error {
ensureContents(t, value, map[string][]byte{})
return nil
}),
),
ExpectError: regexp.MustCompile(`.*error creating archive: error archiving directory: archive has not been\ncreated as it would be empty`),
},
},
})
Expand Down Expand Up @@ -1016,12 +1011,7 @@ func TestAccArchiveFile_SymlinkDirectory_Absolute_ExcludeSymlinkDirectories(t *t
exclude_symlink_directories = true
}
`, filepath.ToSlash(symlinkDirWithRegularFilesAbs), filepath.ToSlash(f)),
Check: r.ComposeTestCheckFunc(
r.TestCheckResourceAttrWith("data.archive_file.foo", "output_path", func(value string) error {
ensureContents(t, value, map[string][]byte{})
return nil
}),
),
ExpectError: regexp.MustCompile(`.*error creating archive: error archiving directory: archive has not been\ncreated as it would be empty`),
},
},
})
Expand Down Expand Up @@ -1210,12 +1200,7 @@ func TestAccArchiveFile_DirectoryWithSymlinkDirectory_Relative_ExcludeSymlinkDir
exclude_symlink_directories = true
}
`, filepath.ToSlash("test-fixtures/test-dir-with-symlink-dir"), filepath.ToSlash(f)),
Check: r.ComposeTestCheckFunc(
r.TestCheckResourceAttrWith("data.archive_file.foo", "output_path", func(value string) error {
ensureContents(t, value, map[string][]byte{})
return nil
}),
),
ExpectError: regexp.MustCompile(`.*error creating archive: error archiving directory: archive has not been\ncreated as it would be empty`),
},
},
})
Expand Down Expand Up @@ -1245,12 +1230,7 @@ func TestAccArchiveFile_IncludeDirectoryWithSymlinkDirectory_Absolute_ExcludeSym
exclude_symlink_directories = true
}
`, filepath.ToSlash(symlinkDirInRegularDirAbs), filepath.ToSlash(f)),
Check: r.ComposeTestCheckFunc(
r.TestCheckResourceAttrWith("data.archive_file.foo", "output_path", func(value string) error {
ensureContents(t, value, map[string][]byte{})
return nil
}),
),
ExpectError: regexp.MustCompile(`.*error creating archive: error archiving directory: archive has not been\ncreated as it would be empty`),
},
},
})
Expand Down
25 changes: 22 additions & 3 deletions internal/provider/zip_archiver.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,15 +107,28 @@ func (a *ZipArchiver) ArchiveDir(indirname string, opts ArchiveDirOpts) error {
opts.Excludes[i] = filepath.FromSlash(opts.Excludes[i])
}

// Determine whether an empty archive would be generated.
isArchiveEmpty := true

err = filepath.Walk(indirname, a.createWalkFunc("", indirname, opts, &isArchiveEmpty, true))
if err != nil {
return err
}

// Return an error if an empty archive would be generated.
if isArchiveEmpty {
return fmt.Errorf("archive has not been created as it would be empty")
}

if err := a.open(); err != nil {
return err
}
defer a.close()

return filepath.Walk(indirname, a.createWalkFunc("", indirname, opts))
return filepath.Walk(indirname, a.createWalkFunc("", indirname, opts, &isArchiveEmpty, false))
}

func (a *ZipArchiver) createWalkFunc(basePath string, indirname string, opts ArchiveDirOpts) func(path string, info os.FileInfo, err error) error {
func (a *ZipArchiver) createWalkFunc(basePath, indirname string, opts ArchiveDirOpts, isArchiveEmpty *bool, dryRun bool) func(path string, info os.FileInfo, err error) error {
return func(path string, info os.FileInfo, err error) error {
if err != nil {
return fmt.Errorf("error encountered during file walk: %s", err)
Expand Down Expand Up @@ -158,7 +171,7 @@ func (a *ZipArchiver) createWalkFunc(basePath string, indirname string, opts Arc

if realInfo.IsDir() {
if !opts.ExcludeSymlinkDirectories {
return filepath.Walk(realPath, a.createWalkFunc(archivePath, realPath, opts))
return filepath.Walk(realPath, a.createWalkFunc(archivePath, realPath, opts, isArchiveEmpty, dryRun))
} else {
return filepath.SkipDir
}
Expand All @@ -167,6 +180,12 @@ func (a *ZipArchiver) createWalkFunc(basePath string, indirname string, opts Arc
info = realInfo
}

*isArchiveEmpty = false

if dryRun {
return nil
}

fh, err := zip.FileInfoHeader(info)
if err != nil {
return fmt.Errorf("error creating file header: %s", err)
Expand Down

0 comments on commit b3ddb4c

Please sign in to comment.