Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Ability for ADD to unTar a file #792

Merged
merged 3 commits into from
Jan 21, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pkg/util/fs_util.go
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ func unTar(r io.Reader, dest string) ([]string, error) {
if err := ExtractFile(dest, hdr, tr); err != nil {
return nil, err
}
extractedFiles = append(extractedFiles, dest)
extractedFiles = append(extractedFiles, filepath.Join(dest, filepath.Clean(hdr.Name)))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@loganprice Can you please add tests for it.

  • That would help prevent future breakage.
  • It would help reviewers understand the impact of the change.

Thanks
Tejal

Copy link
Contributor Author

@loganprice loganprice Sep 30, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@tejal29 Unit test have been added

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@tejal29 just curious to then PRs get reviewed? I added a unit test as requested.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looking now @loganprice

}
return extractedFiles, nil
}
Expand Down
81 changes: 81 additions & 0 deletions pkg/util/fs_util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -528,6 +528,87 @@ func dirHeader(name string, mode int64) *tar.Header {
}
}

func createUncompressedTar(fileContents map[string]string, tarFileName, testDir string) error {
if err := testutil.SetupFiles(testDir, fileContents); err != nil {
return err
}
tarFile, err := os.Create(filepath.Join(testDir, tarFileName))
if err != nil {
return err
}
t := NewTar(tarFile)
defer t.Close()
for file := range fileContents {
filePath := filepath.Join(testDir, file)
if err := t.AddFileToTar(filePath); err != nil {
return err
}
}
return nil
}

func Test_unTar(t *testing.T) {
tcs := []struct {
name string
setupTarContents map[string]string
tarFileName string
destination string
expectedFileList []string
errorExpected bool
}{
{
name: "multfile tar",
setupTarContents: map[string]string{
"foo/file1": "hello World",
"bar/file1": "hello World",
"bar/file2": "hello World",
"file1": "hello World",
},
tarFileName: "test.tar",
destination: "/",
expectedFileList: []string{"foo/file1", "bar/file1", "bar/file2", "file1"},
errorExpected: false,
},
{
name: "empty tar",
setupTarContents: map[string]string{},
tarFileName: "test.tar",
destination: "/",
expectedFileList: nil,
errorExpected: false,
},
}
for _, tc := range tcs {
t.Run(tc.name, func(t *testing.T) {
testDir, err := ioutil.TempDir("", "")
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(testDir)
if err := createUncompressedTar(tc.setupTarContents, tc.tarFileName, testDir); err != nil {
t.Fatal(err)
}
file, err := os.Open(filepath.Join(testDir, tc.tarFileName))
if err != nil {
t.Fatal(err)
}
fileList, err := unTar(file, tc.destination)
if err != nil {
t.Fatal(err)
}
// update expectedFileList to take into factor temp directory
for i, file := range tc.expectedFileList {
tc.expectedFileList[i] = filepath.Join(testDir, file)
}
// sort both slices to ensure objects are in the same order for deep equals
sort.Strings(tc.expectedFileList)
sort.Strings(fileList)
testutil.CheckErrorAndDeepEqual(t, tc.errorExpected, err, tc.expectedFileList, fileList)

})
}
}

func TestExtractFile(t *testing.T) {
type tc struct {
name string
Expand Down