Monorepo for Tangled
tangled.org
1package models
2
3import (
4 "strings"
5 "testing"
6)
7
8func TestValidateRepoName_ValidRkeys(t *testing.T) {
9 valid := []string{
10 "myrepo",
11 "MyRepo",
12 "my-repo",
13 "my_repo",
14 "my.repo",
15 "a",
16 "repo123",
17 strings.Repeat("a", 100),
18 }
19 for _, name := range valid {
20 if err := ValidateRepoName(name); err != nil {
21 t.Errorf("ValidateRepoName(%q) = %v, want nil", name, err)
22 }
23 }
24}
25
26func TestValidateRepoName_InvalidRkeys(t *testing.T) {
27 cases := []struct {
28 input string
29 substr string
30 }{
31 {"", "empty"},
32 {strings.Repeat("a", 101), "100 characters"},
33 {"has space", "alphanumeric"},
34 {"has/slash", "invalid path"},
35 {"has\\backslash", "invalid path"},
36 {".dotprefix", "invalid path"},
37 {"dotsuffix.", "invalid path"},
38 {"two..dots", "sequential dots"},
39 {"../traversal", "invalid path"},
40 {"self", "reserved"},
41 {"SELF", "reserved"},
42 }
43 for _, tc := range cases {
44 err := ValidateRepoName(tc.input)
45 if err == nil {
46 t.Errorf("ValidateRepoName(%q) = nil, want error containing %q", tc.input, tc.substr)
47 continue
48 }
49 if !strings.Contains(strings.ToLower(err.Error()), strings.ToLower(tc.substr)) {
50 t.Errorf("ValidateRepoName(%q) = %q, want substring %q", tc.input, err.Error(), tc.substr)
51 }
52 }
53}
54
55func TestStripGitExt(t *testing.T) {
56 cases := []struct{ in, want string }{
57 {"repo.git", "repo"},
58 {"repo", "repo"},
59 {"repo.git.git", "repo.git"},
60 {".git", ""},
61 }
62 for _, tc := range cases {
63 if got := StripGitExt(tc.in); got != tc.want {
64 t.Errorf("StripGitExt(%q) = %q, want %q", tc.in, got, tc.want)
65 }
66 }
67}
68
69func TestCosmeticName_NilWhenMatchesRkey(t *testing.T) {
70 r := Repo{Name: "myrepo", Rkey: "myrepo"}
71 rec := r.AsRecord()
72 if rec.Name != nil {
73 t.Errorf("cosmeticName should be nil when Name == Rkey, got %q", *rec.Name)
74 }
75}
76
77func TestCosmeticName_PresentWhenDiffers(t *testing.T) {
78 r := Repo{Name: "MyRepo", Rkey: "myrepo", Knot: "k"}
79 rec := r.AsRecord()
80 if rec.Name == nil {
81 t.Fatal("cosmeticName should be non-nil when Name != Rkey")
82 }
83 if *rec.Name != "MyRepo" {
84 t.Errorf("cosmeticName = %q, want %q", *rec.Name, "MyRepo")
85 }
86}
87
88func TestRepoSlug(t *testing.T) {
89 cases := []struct {
90 name string
91 repo Repo
92 want string
93 }{
94 {"name set distinct from rkey", Repo{Name: "anemone", Rkey: "3kabc"}, "anemone"},
95 {"name equals rkey", Repo{Name: "scallop", Rkey: "scallop"}, "scallop"},
96 {"name empty falls to rkey", Repo{Name: "", Rkey: "whelk"}, "whelk"},
97 {"both empty", Repo{}, ""},
98 }
99 for _, c := range cases {
100 t.Run(c.name, func(t *testing.T) {
101 if got := c.repo.Slug(); got != c.want {
102 t.Errorf("Slug() = %q, want %q", got, c.want)
103 }
104 })
105 }
106}