Skip to content

Commit 69a3052

Browse files
committed
Fix tests; use topDir function
1 parent a186391 commit 69a3052

File tree

2 files changed

+27
-21
lines changed

2 files changed

+27
-21
lines changed

archives.go

+21-13
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ func (f FileInfo) Stat() (fs.FileInfo, error) { return f.FileInfo, nil }
5757
// archive recursively, rooted at the named directory. They should use the
5858
// platform's path separator (backslash on Windows; slash on everything else).
5959
// For convenience, map keys that end in a separator ('/', or '\' on Windows)
60-
// will enumerate contents only without adding the folder itself to the archive.
60+
// will enumerate contents only, without adding the folder itself to the archive.
6161
//
6262
// Map values should typically use slash ('/') as the separator regardless of
6363
// the platform, as most archive formats standardize on that rune as the
@@ -153,11 +153,24 @@ func nameOnDiskToNameInArchive(nameOnDisk, rootOnDisk, rootInArchive string) str
153153
// one function is easier to reason about and test. I suspect the performance
154154
// penalty is insignificant.
155155
if strings.HasSuffix(rootOnDisk, string(filepath.Separator)) {
156+
// "map keys that end in a separator will enumerate contents only,
157+
// without adding the folder itself to the archive."
156158
rootInArchive = trimTopDir(rootInArchive)
157159
} else if rootInArchive == "" {
160+
// "map values that are empty string are interpreted as the base name
161+
// of the file (sans path) in the root of the archive"
158162
rootInArchive = filepath.Base(rootOnDisk)
159163
}
164+
if rootInArchive == "." {
165+
// an in-archive root of "." is an escape hatch for the above rule
166+
// where an empty in-archive root means to use the base name of the
167+
// file; if the user does not want this, they can specify a "." to
168+
// still put it in the root of the archive
169+
rootInArchive = ""
170+
}
160171
if strings.HasSuffix(rootInArchive, "/") {
172+
// "map values that end in a slash will use the base name of the file in
173+
// that folder of the archive."
161174
rootInArchive += filepath.Base(rootOnDisk)
162175
}
163176
truncPath := strings.TrimPrefix(nameOnDisk, rootOnDisk)
@@ -167,27 +180,22 @@ func nameOnDiskToNameInArchive(nameOnDisk, rootOnDisk, rootInArchive string) str
167180
// trimTopDir strips the top or first directory from the path.
168181
// It expects a forward-slashed path.
169182
//
170-
// For example, "a/b/c" => "b/c".
183+
// Examples: "a/b/c" => "b/c", "/a/b/c" => "b/c"
171184
func trimTopDir(dir string) string {
172-
if len(dir) > 0 && dir[0] == '/' {
173-
dir = dir[1:]
174-
}
175-
if pos := strings.Index(dir, "/"); pos >= 0 {
176-
return dir[pos+1:]
177-
}
178-
return dir
185+
return strings.TrimPrefix(dir, topDir(dir)+"/")
179186
}
180187

181188
// topDir returns the top or first directory in the path.
182189
// It expects a forward-slashed path.
183190
//
184-
// For example, "a/b/c" => "a".
191+
// Examples: "a/b/c" => "a", "/a/b/c" => "/a"
185192
func topDir(dir string) string {
193+
var start int
186194
if len(dir) > 0 && dir[0] == '/' {
187-
dir = dir[1:]
195+
start = 1
188196
}
189-
if pos := strings.Index(dir, "/"); pos >= 0 {
190-
return dir[:pos]
197+
if pos := strings.Index(dir[start:], "/"); pos >= 0 {
198+
return dir[:pos+start]
191199
}
192200
return dir
193201
}

archives_test.go

+6-8
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import (
88
)
99

1010
func TestTrimTopDir(t *testing.T) {
11-
for _, tc := range []struct {
11+
for i, test := range []struct {
1212
input string
1313
want string
1414
}{
@@ -17,11 +17,10 @@ func TestTrimTopDir(t *testing.T) {
1717
{input: "abc/def", want: "def"},
1818
{input: "/abc/def", want: "def"},
1919
} {
20-
tc := tc
21-
t.Run(tc.input, func(t *testing.T) {
22-
got := trimTopDir(tc.input)
23-
if got != tc.want {
24-
t.Errorf("want: '%s', got: '%s')", tc.want, got)
20+
t.Run(test.input, func(t *testing.T) {
21+
got := trimTopDir(test.input)
22+
if got != test.want {
23+
t.Errorf("Test %d: want: '%s', got: '%s')", i, test.want, got)
2524
}
2625
})
2726
}
@@ -35,9 +34,8 @@ func TestTopDir(t *testing.T) {
3534
{input: "a/b/c", want: "a"},
3635
{input: "a", want: "a"},
3736
{input: "abc/def", want: "abc"},
38-
{input: "/abc/def", want: "abc"},
37+
{input: "/abc/def", want: "/abc"},
3938
} {
40-
tc := tc
4139
t.Run(tc.input, func(t *testing.T) {
4240
got := topDir(tc.input)
4341
if got != tc.want {

0 commit comments

Comments
 (0)