| 1 | package desctpl |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "os" |
| 6 | "os/exec" |
| 7 | "path/filepath" |
| 8 | "strings" |
| 9 | "testing" |
| 10 | ) |
| 11 | |
| 12 | func TestExpand(t *testing.T) { |
| 13 | body := "%{source_branch} -> %{target_branch}\n%{closes_issue}\n%{first_commit}\n%{all_commits}\n%{first_multiline_commit}\n%{co_authored_by}" |
| 14 | got := Expand(body, Vars{ |
| 15 | Source: "12-fix", Target: "master", |
| 16 | Commits: []Commit{ |
| 17 | {Subject: "Merge branch", Merge: true, Body: "Co-authored-by: Sam <sam@example>"}, |
| 18 | {Subject: "Fix login", Body: "Details\n\nCo-authored-by: Tim <tim@example>"}, |
| 19 | {Subject: "klein"}, |
| 20 | {Subject: "enorm", Body: strings.Repeat("x", hugeBody+1)}, |
| 21 | }, |
| 22 | }) |
| 23 | for _, want := range []string{"12-fix -> master", "Closes #12", "Fix login\n\nDetails", "klein", "Co-authored-by: Sam <sam@example>", "Co-authored-by: Tim <tim@example>"} { |
| 24 | if !strings.Contains(got, want) { |
| 25 | t.Errorf("mist %q in\n%s", want, got) |
| 26 | } |
| 27 | } |
| 28 | if strings.Contains(got, "Merge branch") || strings.Contains(got, "enorm") { |
| 29 | t.Fatalf("merge of enorm hoort er niet in:\n%s", got) |
| 30 | } |
| 31 | if Expand("%{closes_issue}", Vars{Source: "feature"}) != "" { |
| 32 | t.Fatal("branch zonder nummer") |
| 33 | } |
| 34 | } |
| 35 | |
| 36 | func TestListFallback(t *testing.T) { |
| 37 | dir := t.TempDir() |
| 38 | git := func(args ...string) { |
| 39 | t.Helper() |
| 40 | cmd := exec.Command("git", args...) |
| 41 | cmd.Dir = dir |
| 42 | out, err := cmd.CombinedOutput() |
| 43 | if err != nil { |
| 44 | t.Fatalf("git %s: %v\n%s", strings.Join(args, " "), err, out) |
| 45 | } |
| 46 | } |
| 47 | git("init", "-b", "master") |
| 48 | git("config", "user.email", "t@example") |
| 49 | git("config", "user.name", "t") |
| 50 | write(t, filepath.Join(dir, ".gitlab", "issue_templates", "Bug.md"), "gitlab bug\n") |
| 51 | write(t, filepath.Join(dir, ".gitlab", "issue_templates", "Legacy.md"), "oud\n") |
| 52 | write(t, filepath.Join(dir, ".gitlab", "issue_templates", "default.md"), "gitlab default\n") |
| 53 | write(t, filepath.Join(dir, ".bluegit", "issue_templates", "Bug.md"), "bluegit bug\n") |
| 54 | write(t, filepath.Join(dir, ".bluegit", "issue_templates", "Default.md"), "bluegit default\n") |
| 55 | git("add", ".") |
| 56 | git("commit", "-m", "sjablonen") |
| 57 | list, err := List(context.Background(), "git", dir, "master", "issue") |
| 58 | if err != nil { |
| 59 | t.Fatal(err) |
| 60 | } |
| 61 | if len(list) != 4 { |
| 62 | t.Fatalf("lijst %v", names(list)) |
| 63 | } |
| 64 | if list[0].Name != "Default" || !strings.Contains(list[0].Body, "bluegit default") { |
| 65 | t.Fatalf("default %#v", list[0]) |
| 66 | } |
| 67 | other, ok := Find(list, "default") |
| 68 | if !ok || !strings.Contains(other.Body, "gitlab default") { |
| 69 | t.Fatalf("default.md %#v", other) |
| 70 | } |
| 71 | bug, ok := Find(list, "Bug") |
| 72 | if !ok || !strings.Contains(bug.Body, "bluegit bug") || strings.Contains(bug.Body, "gitlab") { |
| 73 | t.Fatalf("bug %#v", bug) |
| 74 | } |
| 75 | legacy, ok := Find(list, "Legacy") |
| 76 | if !ok || !strings.Contains(legacy.Body, "oud") { |
| 77 | t.Fatal(legacy) |
| 78 | } |
| 79 | def, ok := Default(list) |
| 80 | if !ok || def.Name != "Default" { |
| 81 | t.Fatal(def) |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | func TestListAbsent(t *testing.T) { |
| 86 | dir := t.TempDir() |
| 87 | git := func(args ...string) { |
| 88 | t.Helper() |
| 89 | cmd := exec.Command("git", args...) |
| 90 | cmd.Dir = dir |
| 91 | out, err := cmd.CombinedOutput() |
| 92 | if err != nil { |
| 93 | t.Fatalf("git %s: %v\n%s", strings.Join(args, " "), err, out) |
| 94 | } |
| 95 | } |
| 96 | git("init", "-b", "master") |
| 97 | git("config", "user.email", "t@example") |
| 98 | git("config", "user.name", "t") |
| 99 | write(t, filepath.Join(dir, "README.md"), "alleen dit\n") |
| 100 | git("add", ".") |
| 101 | git("commit", "-m", "zonder sjablonen") |
| 102 | for _, kind := range []string{"issue", "mr"} { |
| 103 | list, err := List(context.Background(), "git", dir, "master", kind) |
| 104 | if err != nil { |
| 105 | t.Fatalf("%s: %v", kind, err) |
| 106 | } |
| 107 | if len(list) != 0 { |
| 108 | t.Fatalf("%s lijst %v", kind, names(list)) |
| 109 | } |
| 110 | } |
| 111 | for _, root := range []string{".bluegit", ".gitlab"} { |
| 112 | if _, err := os.Stat(filepath.Join(dir, root)); !os.IsNotExist(err) { |
| 113 | t.Fatalf("%s werd aangemaakt: %v", root, err) |
| 114 | } |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | func names(list []Template) []string { |
| 119 | var out []string |
| 120 | for _, t := range list { |
| 121 | out = append(out, t.Name) |
| 122 | } |
| 123 | return out |
| 124 | } |
| 125 | |
| 126 | func write(t *testing.T, path, body string) { |
| 127 | t.Helper() |
| 128 | if err := os.MkdirAll(filepath.Dir(path), 0o755); err != nil { |
| 129 | t.Fatal(err) |
| 130 | } |
| 131 | if err := os.WriteFile(path, []byte(body), 0o644); err != nil { |
| 132 | t.Fatal(err) |
| 133 | } |
| 134 | } |