|
| 1 | +// +build windows |
| 2 | + |
| 3 | +package archive |
| 4 | + |
| 5 | +import ( |
| 6 | + "testing" |
| 7 | +) |
| 8 | + |
| 9 | +func TestCanonicalTarNameForPath(t *testing.T) { |
| 10 | + cases := []struct { |
| 11 | + in, expected string |
| 12 | + shouldFail bool |
| 13 | + }{ |
| 14 | + {"foo", "foo", false}, |
| 15 | + {"foo/bar", "___", true}, // unix-styled windows path must fail |
| 16 | + {`foo\bar`, "foo/bar", false}, |
| 17 | + {`foo\bar`, "foo/bar/", false}, |
| 18 | + } |
| 19 | + for _, v := range cases { |
| 20 | + if out, err := canonicalTarNameForPath(v.in); err != nil && !v.shouldFail { |
| 21 | + t.Fatalf("cannot get canonical name for path: %s: %v", v.in, err) |
| 22 | + } else if v.shouldFail && err == nil { |
| 23 | + t.Fatalf("canonical path call should have pailed with error. in=%s out=%s", v.in, out) |
| 24 | + } else if !v.shouldFail && out != v.expected { |
| 25 | + t.Fatalf("wrong canonical tar name. expected:%s got:%s", v.expected, out) |
| 26 | + } |
| 27 | + } |
| 28 | +} |
| 29 | + |
| 30 | +func TestCanonicalTarName(t *testing.T) { |
| 31 | + cases := []struct { |
| 32 | + in string |
| 33 | + isDir bool |
| 34 | + expected string |
| 35 | + }{ |
| 36 | + {"foo", false, "foo"}, |
| 37 | + {"foo", true, "foo/"}, |
| 38 | + {`foo\bar`, false, "foo/bar"}, |
| 39 | + {`foo\bar`, true, "foo/bar/"}, |
| 40 | + } |
| 41 | + for _, v := range cases { |
| 42 | + if out, err := canonicalTarName(v.in, v.isDir); err != nil { |
| 43 | + t.Fatalf("cannot get canonical name for path: %s: %v", v.in, err) |
| 44 | + } else if out != v.expected { |
| 45 | + t.Fatalf("wrong canonical tar name. expected:%s got:%s", v.expected, out) |
| 46 | + } |
| 47 | + } |
| 48 | +} |
0 commit comments