|
| 1 | +package commit_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "errors" |
| 5 | + "strings" |
| 6 | + "testing" |
| 7 | + |
| 8 | + "github.com/conventionalcommit/commitlint/commit" |
| 9 | + "github.com/conventionalcommit/syntax" |
| 10 | +) |
| 11 | + |
| 12 | +func TestSyntaxAdapter(t *testing.T) { |
| 13 | + p := commit.NewParser() |
| 14 | + for _, newline := range []string{"\n", "\r\n"} { |
| 15 | + input := strings.ReplaceAll("feat(store)!: replace index \n\nRetain records.\n\nTrace: #91\n continuation\nRefs #92\nRefs #93\nBREAKING-CHANGE: remove legacy keys\n", "\n", newline) |
| 16 | + c, err := p.Parse(input) |
| 17 | + if err != nil { |
| 18 | + t.Fatal(err) |
| 19 | + } |
| 20 | + if c.Message() != input || c.Header() != "feat(store)!: replace index " || c.Type() != "feat" || c.Scope() != "store" || c.Description() != "replace index " || c.Body() != "Retain records." || !c.IsBreakingChange() { |
| 21 | + t.Fatalf("unexpected fields: %#v", c) |
| 22 | + } |
| 23 | + want := [][2]string{{"Trace", "#91" + newline + " continuation"}, {"Refs", "92"}, {"Refs", "93"}, {"BREAKING-CHANGE", "remove legacy keys"}} |
| 24 | + notes := c.Notes() |
| 25 | + if len(notes) != len(want) { |
| 26 | + t.Fatalf("notes = %d", len(notes)) |
| 27 | + } |
| 28 | + for i, entry := range want { |
| 29 | + if notes[i].Token() != entry[0] || notes[i].Value() != entry[1] { |
| 30 | + t.Fatalf("note %d = %q: %q", i, notes[i].Token(), notes[i].Value()) |
| 31 | + } |
| 32 | + } |
| 33 | + notes[0] = nil |
| 34 | + if c.Notes()[0].Token() != "Trace" { |
| 35 | + t.Fatal("returned slice changed the result") |
| 36 | + } |
| 37 | + if !strings.HasPrefix(c.Footer(), "Trace: #91"+newline) { |
| 38 | + t.Fatalf("footer = %q", c.Footer()) |
| 39 | + } |
| 40 | + } |
| 41 | +} |
| 42 | + |
| 43 | +func TestSyntaxAdapterErrors(t *testing.T) { |
| 44 | + p := commit.NewParser() |
| 45 | + for _, tt := range []struct { |
| 46 | + input string |
| 47 | + code syntax.ErrorCode |
| 48 | + offset int |
| 49 | + }{ |
| 50 | + {" feat: expose records", syntax.InvalidType, 0}, |
| 51 | + {"feat:expose records", syntax.InvalidSeparator, 5}, |
| 52 | + {"feat: expose records\nbody", syntax.InvalidSectionSeparator, 21}, |
| 53 | + {"feat: expose records\n\nRefs #9\nEmpty: ", syntax.InvalidFooterValue, 37}, |
| 54 | + } { |
| 55 | + c, err := p.Parse(tt.input) |
| 56 | + var pe *syntax.ParseError |
| 57 | + if c != nil || !errors.As(err, &pe) || pe.Code != tt.code || pe.Offset != tt.offset { |
| 58 | + t.Fatalf("input %q: result=%v error=%v", tt.input, c, err) |
| 59 | + } |
| 60 | + } |
| 61 | +} |
| 62 | + |
| 63 | +func TestSyntaxAdapterWithoutNotes(t *testing.T) { |
| 64 | + c, err := commit.NewParser().Parse("feat: expose records\n") |
| 65 | + if err != nil { |
| 66 | + t.Fatal(err) |
| 67 | + } |
| 68 | + if len(c.Notes()) != 0 || c.Message() != "feat: expose records\n" { |
| 69 | + t.Fatal("unexpected empty-footer result") |
| 70 | + } |
| 71 | +} |
| 72 | + |
| 73 | +func TestSyntaxInspectionAdapter(t *testing.T) { |
| 74 | + p := commit.NewParser() |
| 75 | + input := "feat:expose records\nBody text." |
| 76 | + c, diagnostics := commit.Inspect(input) |
| 77 | + if c == nil || len(diagnostics) != 2 || c.Type() != "feat" || c.Body() != "Body text." { |
| 78 | + t.Fatalf("result=%v diagnostics=%v", c, diagnostics) |
| 79 | + } |
| 80 | + var pe *syntax.ParseError |
| 81 | + if !errors.As(diagnostics[0], &pe) || pe.Code != syntax.InvalidSeparator { |
| 82 | + t.Fatal("typed diagnostic lost") |
| 83 | + } |
| 84 | + if strict, err := p.Parse(input); strict != nil || err == nil { |
| 85 | + t.Fatal("strict parsing recovered invalid input") |
| 86 | + } |
| 87 | + if c, diagnostics := commit.Inspect("invalid header"); c != nil || len(diagnostics) != 1 { |
| 88 | + t.Fatal("ambiguous header recovered") |
| 89 | + } |
| 90 | +} |
| 91 | + |
| 92 | +func TestCommitHelpers(t *testing.T) { |
| 93 | + parsed, err := commit.NewParser().Parse("feat!: drop v1\n\nBody text.\n\nBREAKING CHANGE: gone\nRefs #9") |
| 94 | + if err != nil { |
| 95 | + t.Fatal(err) |
| 96 | + } |
| 97 | + if !parsed.HasHeaderBang() || !parsed.HasBreakingFooter() || parsed.BodyBlankLines() != 1 || parsed.FooterBlankLines() != 1 { |
| 98 | + t.Fatal("syntax-backed Commit methods reported incorrect facts on valid breaking commit") |
| 99 | + } |
| 100 | + if parsed.NoteCount() != 2 || parsed.NoteAt(0).Token() != "BREAKING CHANGE" || parsed.NoteAt(1).Token() != "Refs" { |
| 101 | + t.Fatalf("unexpected notes: count=%d", parsed.NoteCount()) |
| 102 | + } |
| 103 | + if got := commit.FirstLine("Initial commit\r\nignored"); got != "Initial commit" { |
| 104 | + t.Fatalf("FirstLine = %q", got) |
| 105 | + } |
| 106 | + var lines []string |
| 107 | + commit.ForEachLine("a\r\nb\nc", func(line string) bool { |
| 108 | + lines = append(lines, line) |
| 109 | + return len(lines) < 2 |
| 110 | + }) |
| 111 | + if len(lines) != 2 || lines[0] != "a" || lines[1] != "b" { |
| 112 | + t.Fatalf("ForEachLine = %q", lines) |
| 113 | + } |
| 114 | + _, diags := commit.Inspect("feat: ok\n\nRefs #9\nEmpty: ") |
| 115 | + if line, col, ok := commit.DiagnosticPosition("feat: ok\n\nRefs #9\nEmpty: ", diags[0]); !ok || line != 4 || col != 8 { |
| 116 | + t.Fatalf("DiagnosticPosition = (%d, %d, %v)", line, col, ok) |
| 117 | + } |
| 118 | + if _, _, ok := commit.DiagnosticPosition("feat: ok", errors.New("custom error")); ok { |
| 119 | + t.Fatal("expected false for non-syntax error") |
| 120 | + } |
| 121 | +} |
| 122 | + |
| 123 | +var parsed commit.Commit |
| 124 | +var parseErr error |
| 125 | +var parsedNotes []commit.Note |
| 126 | + |
| 127 | +func BenchmarkSyntaxAdapter(b *testing.B) { |
| 128 | + p := commit.NewParser() |
| 129 | + for _, tt := range []struct { |
| 130 | + name, input string |
| 131 | + notes bool |
| 132 | + }{ |
| 133 | + {"header", "feat: expose records", false}, |
| 134 | + {"footers", "feat: expose records\n\nRefs #9\nTrace: #91", false}, |
| 135 | + {"footers_with_notes", "feat: expose records\n\nRefs #9\nTrace: #91", true}, |
| 136 | + } { |
| 137 | + b.Run(tt.name, func(b *testing.B) { |
| 138 | + b.ReportAllocs() |
| 139 | + for i := 0; i < b.N; i++ { |
| 140 | + parsed, parseErr = p.Parse(tt.input) |
| 141 | + if tt.notes && parseErr == nil { |
| 142 | + parsedNotes = parsed.Notes() |
| 143 | + } |
| 144 | + } |
| 145 | + if parseErr != nil { |
| 146 | + b.Fatal(parseErr) |
| 147 | + } |
| 148 | + }) |
| 149 | + } |
| 150 | +} |
0 commit comments