From 5e373c52f8aa113d79291d148ef269c3d8fe5ed9 Mon Sep 17 00:00:00 2001 From: Matthew Holt Date: Sat, 7 Dec 2024 10:53:49 -0700 Subject: [PATCH] Refactor and test PathContainsArchive --- fs.go | 12 ++++++------ fs_test.go | 37 +++++++++++++++++++++++++++++++++++++ 2 files changed, 43 insertions(+), 6 deletions(-) diff --git a/fs.go b/fs.go index fa0a79d..a189d5d 100644 --- a/fs.go +++ b/fs.go @@ -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 } } diff --git a/fs_test.go b/fs_test.go index c049524..d814fd0 100644 --- a/fs_test.go +++ b/fs_test.go @@ -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