Monorepo for Tangled
tangled.org
1package repo
2
3import (
4 "strings"
5 "testing"
6)
7
8func TestValidateRenameInput(t *testing.T) {
9 const validTID = "3jzfcijpj2z2a"
10 cases := []struct {
11 name string
12 currentName string
13 currentRkey string
14 raw string
15 wantName string
16 wantErrSub string
17 }{
18 {"happy path", "foo", "", "bar", "bar", ""},
19 {"trims surrounding whitespace", "foo", "", " bar ", "bar", ""},
20 {"strips .git suffix", "foo", "", "bar.git", "bar", ""},
21 {"empty after trim", "foo", "", " ", "", "cannot be empty"},
22 {"raw empty", "foo", "", "", "", "cannot be empty"},
23 {"path traversal slash", "foo", "", "../bar", "", "invalid path"},
24 {"invalid character", "foo", "", "ba r", "", "alphanumeric"},
25 {"same name as current with non-TID rkey", "foo", "foo", "foo", "", "matches the current name"},
26 {"same name as current with TID rkey allowed", "foo", validTID, "foo", "foo", ""},
27 {"case-only diff is not a no-op", "foo", "", "Foo", "Foo", ""},
28 {"strip-git collides with current", "foo", "", "foo.git", "", "matches the current name"},
29 }
30 for _, tc := range cases {
31 t.Run(tc.name, func(t *testing.T) {
32 got, err := validateRenameInput(tc.currentName, tc.currentRkey, tc.raw)
33 if tc.wantErrSub == "" {
34 if err != nil {
35 t.Fatalf("err = %v, want nil", err)
36 }
37 if got != tc.wantName {
38 t.Errorf("name = %q, want %q", got, tc.wantName)
39 }
40 return
41 }
42 if err == nil {
43 t.Fatalf("err = nil, want error containing %q", tc.wantErrSub)
44 }
45 if got != "" {
46 t.Errorf("name = %q, want empty on error", got)
47 }
48 if !strings.Contains(err.Error(), tc.wantErrSub) {
49 t.Errorf("err = %q, want substring %q", err.Error(), tc.wantErrSub)
50 }
51 })
52 }
53}