Skip to content

Commit

Permalink
Teach toTar to omit extraneous leading directory paths
Browse files Browse the repository at this point in the history
Imagine a directory structure like so:

  /tmp
  └── reports
      ├── result-1.xml
      └── subdir
          └── result-2.xml

Prior to this change, when toTar was given a path like "/tmp/reports",
the resulting tar would have the following structure:

  /tmp
  └── reports
      ├── result-1.xml
      └── subdir
          └── result-2.xml

With the changes in this commit, the resulting tar has the following
structure:

  .
  ├── result-1.xml
  └── subdir
      └── result-2.xml

In other words, the resulting tar file omits the extraneous /tmp and
/tmp/reports directories.

This also resolves an issue that would lead to invalid tar files when
using certain relative path structures (e.g., "../../reports").
  • Loading branch information
jasonrudolph committed Oct 11, 2020
1 parent 4bbefa1 commit b0f1731
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 11 deletions.
13 changes: 9 additions & 4 deletions cmd/submit.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ func toTar(dir string) (dest string, err error) {
}

return tarfile.Name(), filepath.Walk(dir,
func(path string, info os.FileInfo, err error) error {
func(srcpath string, info os.FileInfo, err error) error {
if err != nil {
return err
}
Expand All @@ -175,12 +175,17 @@ func toTar(dir string) (dest string, err error) {
return nil
}

header, err := tar.FileInfoHeader(info, path)
destpath, err := filepath.Rel(dir, srcpath)
if err != nil {
return err
}

header.Name = path
header, err := tar.FileInfoHeader(info, destpath)
if err != nil {
return err
}

header.Name = destpath
if err := writer.WriteHeader(header); err != nil {
return err
}
Expand All @@ -189,7 +194,7 @@ func toTar(dir string) (dest string, err error) {
return nil
}

file, err := os.Open(path)
file, err := os.Open(srcpath)
if err != nil {
return err
}
Expand Down
13 changes: 6 additions & 7 deletions cmd/submit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -315,25 +315,24 @@ func Test_toTarGz(t *testing.T) {
// === Verify original directory content matches resulting directory content
assertEqualContent(t,
"testdata/example-test-results/buildpulse.yml",
filepath.Join(dir, "testdata/example-test-results/buildpulse.yml"),
filepath.Join(dir, "buildpulse.yml"),
)
assertEqualContent(t,
"testdata/example-test-results/junit/browserstack/example-1.xml",
filepath.Join(dir, "testdata/example-test-results/junit/browserstack/example-1.xml"),
filepath.Join(dir, "junit/browserstack/example-1.xml"),
)
assertEqualContent(t,
"testdata/example-test-results/junit/browserstack/example-2.XML",
filepath.Join(dir, "testdata/example-test-results/junit/browserstack/example-2.XML"),
filepath.Join(dir, "junit/browserstack/example-2.XML"),
)
assertEqualContent(t,
"testdata/example-test-results/junit/browsertest/example-3.xml",
filepath.Join(dir, "testdata/example-test-results/junit/browsertest/example-3.xml"),
filepath.Join(dir, "junit/browsertest/example-3.xml"),
)

// === Verify tarball excludes files other than buildpulse.yml and XML reports
nonXMLPath := "testdata/example-test-results/junit/browsertest/example-3.txt"
assert.FileExists(t, nonXMLPath)
assert.NoFileExists(t, filepath.Join(dir, nonXMLPath))
assert.FileExists(t, "testdata/example-test-results/junit/browsertest/example-3.txt")
assert.NoFileExists(t, filepath.Join(dir, "junit/browsertest/example-3.txt"))
}

func unzip(src io.Reader, dest io.Writer) error {
Expand Down

0 comments on commit b0f1731

Please sign in to comment.