Skip to content

Commit c43cdbf

Browse files
committed
TestIsArchivePathInvalidFile: avoid shelling out
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
1 parent eb19964 commit c43cdbf

1 file changed

Lines changed: 16 additions & 28 deletions

File tree

archive_test.go

Lines changed: 16 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import (
88
"fmt"
99
"io"
1010
"os"
11-
"os/exec"
1211
"path/filepath"
1312
"runtime"
1413
"strings"
@@ -80,22 +79,6 @@ func createTarFromFiles(t *testing.T, tarPath string, files ...string) {
8079
assert.NilError(t, f.Close())
8180
}
8281

83-
// toUnixPath converts the given path to a unix-path, using forward-slashes, and
84-
// with the drive-letter replaced (e.g. "C:\temp\file.txt" becomes "/c/temp/file.txt").
85-
// It is a no-op on non-Windows platforms.
86-
func toUnixPath(p string) string {
87-
if runtime.GOOS != "windows" {
88-
return p
89-
}
90-
p = filepath.ToSlash(p)
91-
92-
vol := strings.TrimPrefix(filepath.VolumeName(p), "//?/")
93-
if len(vol) == 2 && vol[1] == ':' {
94-
return "/" + strings.ToLower(vol[:1]) + p[len(filepath.VolumeName(p)):]
95-
}
96-
return p
97-
}
98-
9982
func TestIsArchivePathDir(t *testing.T) {
10083
tmp := t.TempDir()
10184
assert.NilError(t, os.Mkdir(filepath.Join(tmp, "archivedir"), 0o755))
@@ -106,17 +89,22 @@ func TestIsArchivePathDir(t *testing.T) {
10689

10790
func TestIsArchivePathInvalidFile(t *testing.T) {
10891
tmp := t.TempDir()
109-
cmd := exec.Command("sh", "-c", fmt.Sprintf("dd if=/dev/zero bs=1024 count=1 of=%[1]s/archive && gzip --stdout %[1]s/archive > %[1]s/archive.gz", toUnixPath(tmp)))
110-
output, err := cmd.CombinedOutput()
111-
if err != nil {
112-
t.Fatalf("Failed to create archive file (%v): %s", err, output)
113-
}
114-
if IsArchivePath(filepath.Join(tmp, "archive")) {
115-
t.Fatalf("Incorrectly recognised invalid tar path as archive")
116-
}
117-
if IsArchivePath(filepath.Join(tmp, "archive.gz")) {
118-
t.Fatalf("Incorrectly recognised invalid compressed tar path as archive")
119-
}
92+
93+
archive := filepath.Join(tmp, "archive")
94+
assert.NilError(t, os.WriteFile(archive, []byte("hello"), 0o644))
95+
96+
archiveGz := archive + ".gz"
97+
f, err := os.Create(archiveGz)
98+
assert.NilError(t, err)
99+
100+
gw := gzip.NewWriter(f)
101+
_, err = gw.Write([]byte("hello"))
102+
assert.NilError(t, err)
103+
assert.NilError(t, gw.Close())
104+
assert.NilError(t, f.Close())
105+
106+
assert.Check(t, !IsArchivePath(archive), "incorrectly recognised invalid tar path as archive")
107+
assert.Check(t, !IsArchivePath(archiveGz), "incorrectly recognised invalid compressed tar path as archive")
120108
}
121109

122110
func TestIsArchivePathTar(t *testing.T) {

0 commit comments

Comments
 (0)