aboutsummaryrefslogtreecommitdiff
path: root/internal/cli/path_prefix_test.go
diff options
context:
space:
mode:
Diffstat (limited to 'internal/cli/path_prefix_test.go')
-rw-r--r--internal/cli/path_prefix_test.go52
1 files changed, 52 insertions, 0 deletions
diff --git a/internal/cli/path_prefix_test.go b/internal/cli/path_prefix_test.go
new file mode 100644
index 0000000..bdb4e42
--- /dev/null
+++ b/internal/cli/path_prefix_test.go
@@ -0,0 +1,52 @@
+package cli
+
+import (
+ "errors"
+ "testing"
+)
+
+func TestPathPrefixValueSet(t *testing.T) {
+ cases := map[string]string{
+ "": "",
+ "/": "",
+ "foo": "foo",
+ "/foo": "foo",
+ "foo/": "foo",
+ "/foo/": "foo",
+ "foo//bar": "foo/bar",
+ "foo/./bar": "foo/bar",
+ "foo/bar/..": "foo",
+ "a/b/../c": "a/c",
+ }
+ for in, want := range cases {
+ var p string
+ v := &pathPrefixValue{p: &p}
+ if err := v.Set(in); err != nil {
+ t.Errorf("Set(%q): unexpected error %v", in, err)
+ continue
+ }
+ if p != want {
+ t.Errorf("Set(%q) = %q, want %q", in, p, want)
+ }
+ }
+}
+
+func TestPathPrefixValueSetEscapes(t *testing.T) {
+ for _, in := range []string{"..", "../x", "/../x", "foo/../../x"} {
+ var p string
+ v := &pathPrefixValue{p: &p}
+ var ue usageError
+ if err := v.Set(in); !errors.As(err, &ue) {
+ t.Errorf("Set(%q): expected usageError, got %v", in, err)
+ }
+ }
+}
+
+func TestPrefixedPath(t *testing.T) {
+ if got := prefixedPath("", "assets/x.js"); got != "assets/x.js" {
+ t.Errorf("empty prefix: got %q", got)
+ }
+ if got := prefixedPath("foo", "assets/x.js"); got != "foo/assets/x.js" {
+ t.Errorf("prefixed: got %q", got)
+ }
+}