Skip to content

Commit

Permalink
Refactor and test PathContainsArchive
Browse files Browse the repository at this point in the history
  • Loading branch information
mholt committed Dec 7, 2024
1 parent 998c962 commit 5e373c5
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 6 deletions.
12 changes: 6 additions & 6 deletions fs.go
Original file line number Diff line number Diff line change
Expand Up @@ -919,15 +919,15 @@ var archiveExtensions = []string{
".tar.lz",
}

// FilepathContainsArchive returns true if the path contains an archive file (i.e.
// PathContainsArchive returns true if the path contains an archive file (i.e.
// whether the path traverses into an archive) solely by lexical analysis (no
// reading of files or headers is performed). Such a path is not typically
// usable by the OS, but can be used by the DeepFS type. Examples:
// "/foo/example.zip/path/in/archive" or "C:\example.zip\path\in\archive"
func FilepathContainsArchive(path string) bool {
pathPlusSep := path + string(filepath.Separator)
// usable by the OS, but can be used by the DeepFS type. Slash must be the
// path component separator. Example: "/foo/example.zip/path/in/archive"
func PathContainsArchive(path string) bool {
pathPlusSep := path + "/"
for _, ext := range archiveExtensions {
if strings.Contains(pathPlusSep, ext+string(filepath.Separator)) {
if strings.Contains(pathPlusSep, ext+"/") {
return true
}
}
Expand Down
37 changes: 37 additions & 0 deletions fs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,43 @@ func TestSplitPath(t *testing.T) {
}
}

func TestPathContainsArchive(t *testing.T) {
for i, testCase := range []struct {
input string
expected bool
}{
{
input: "",
expected: false,
},
{
input: "foo",
expected: false,
},
{
input: "foo.zip",
expected: true,
},
{
input: "a/b/c.tar.gz",
expected: true,
},
{
input: "a/b/c.tar.gz/d",
expected: true,
},
{
input: "a/b/c.txt",
expected: false,
},
} {
actual := PathContainsArchive(testCase.input)
if actual != testCase.expected {
t.Errorf("Test %d (input=%q): expected %v but got %v", i, testCase.input, testCase.expected, actual)
}
}
}

var (
//go:embed testdata/test.zip
testZIP []byte
Expand Down

0 comments on commit 5e373c5

Please sign in to comment.