Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions internal/comments/comments_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -800,3 +800,32 @@ func TestCommentValueString(t *testing.T) {
})
}
}

func TestIsRuleComment(t *testing.T) {
type testCaseT struct {
ctype comments.Type
expected bool
}

testCases := []testCaseT{
{comments.RuleOwnerType, true},
{comments.DisableType, true},
{comments.SnoozeType, true},
{comments.RuleSetType, true},
{comments.IgnoreLineType, false},
{comments.FileOwnerType, false},
}

for _, tc := range testCases {
t.Run(fmt.Sprintf("%v", tc.ctype), func(t *testing.T) {
require.Equal(t, tc.expected, comments.IsRuleComment(tc.ctype))
})
}
}

func TestOwnerError(t *testing.T) {
oe := comments.OwnerError{
Diagnostic: diags.Diagnostic{Message: "test error"},
}
require.Equal(t, "test error", oe.Error())
}
28 changes: 28 additions & 0 deletions internal/config/options/selector_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (

"github.com/stretchr/testify/require"

"github.com/cloudflare/pint/internal/checks"
"github.com/cloudflare/pint/internal/config/options"
)

Expand Down Expand Up @@ -59,3 +60,30 @@ func TestSelectorSettings(t *testing.T) {
})
}
}

func TestSelectorSettingsGetSeverity(t *testing.T) {
type testCaseT struct {
conf options.SelectorSettings
fallback checks.Severity
expected checks.Severity
}

testCases := []testCaseT{
{
conf: options.SelectorSettings{Severity: "info"},
fallback: checks.Bug,
expected: checks.Information,
},
{
conf: options.SelectorSettings{},
fallback: checks.Bug,
expected: checks.Bug,
},
}

for _, tc := range testCases {
t.Run(fmt.Sprintf("%v", tc.conf), func(t *testing.T) {
require.Equal(t, tc.expected, tc.conf.GetSeverity(tc.fallback))
})
}
}
48 changes: 48 additions & 0 deletions internal/diags/position_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -195,3 +195,51 @@ expr: |
})
}
}

func TestLineRangeString(t *testing.T) {
type testCaseT struct {
expected string
lr LineRange
}

testCases := []testCaseT{
{lr: LineRange{First: 1, Last: 1}, expected: "1"},
{lr: LineRange{First: 1, Last: 2}, expected: "1-2"},
}

for _, tc := range testCases {
t.Run(tc.expected, func(t *testing.T) {
require.Equal(t, tc.expected, tc.lr.String())
})
}
}

func TestLineRangeExpand(t *testing.T) {
lr := LineRange{First: 1, Last: 3}
require.Equal(t, []int{1, 2, 3}, lr.Expand())
}

func TestPositionRangesLines(t *testing.T) {
prs := PositionRanges{
{Line: 2},
{Line: 5},
{Line: 3},
}
lr := prs.Lines()
require.Equal(t, 2, lr.First)
require.Equal(t, 5, lr.Last)
}

func TestPositionRangesAddOffset(t *testing.T) {
prs := PositionRanges{
{Line: 1, FirstColumn: 2, LastColumn: 3},
{Line: 2, FirstColumn: 3, LastColumn: 4},
}
prs.AddOffset(10, 20)
require.Equal(t, 11, prs[0].Line)
require.Equal(t, 22, prs[0].FirstColumn)
require.Equal(t, 23, prs[0].LastColumn)
require.Equal(t, 12, prs[1].Line)
require.Equal(t, 23, prs[1].FirstColumn)
require.Equal(t, 24, prs[1].LastColumn)
}
33 changes: 33 additions & 0 deletions internal/diags/problems_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,3 +158,36 @@ expr: |
})
}
}

func TestCountDigits(t *testing.T) {
require.Equal(t, 1, countDigits(1))
require.Equal(t, 2, countDigits(10))
require.Equal(t, 3, countDigits(100))
}

func TestLineCoverage(t *testing.T) {
diags := []Diagnostic{
{Pos: PositionRanges{{Line: 1}, {Line: 2}}},
{Pos: PositionRanges{{Line: 2}, {Line: 3}}},
}
require.Equal(t, []int{1, 2, 3}, lineCoverage(diags))
}

func TestInjectDiagnosticsKind(t *testing.T) {
input := "expr: foo(bar) by()"
diags := []Diagnostic{
{FirstColumn: 1, LastColumn: 13, Message: "this is bad", Kind: Issue},
{FirstColumn: 1, LastColumn: 13, Message: "this is context", Kind: Context},
}
key, val := parseYaml(input)
pos := NewPositionRange(strings.Split(input, "\n"), val, key.Column+2)
for i := range diags {
diags[i].Pos = pos
}
out := InjectDiagnostics(input, diags, output.None)
expected := `1 | expr: foo(bar) by()
^^^^^^^^^^^^^ this is bad
this is context
`
require.Equal(t, expected, out)
}
28 changes: 28 additions & 0 deletions internal/log/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@ package log

import (
"bytes"
"context"
"encoding/json"
"errors"
"log/slog"
"strconv"
"testing"
"time"

"github.com/stretchr/testify/require"
)
Expand Down Expand Up @@ -94,6 +96,32 @@ func TestHandler(t *testing.T) {
},
expected: "level=INFO msg=bar group=[{\"Key\":\"with\",\"Value\":{}}]\n",
},
{
noColor: true,
run: func(l *slog.Logger) {
l.WithGroup("group").Info("bar", slog.String("with", "true"))
},
expected: "level=INFO msg=bar with=true\n",
},
{
noColor: true,
run: func(l *slog.Logger) {
h := l.Handler()
r := slog.NewRecord(time.Now(), slog.LevelInfo, "bar", 0)
_ = h.Handle(context.Background(), r)
},
expected: "level=INFO msg=bar\n",
},
{
noColor: true,
run: func(l *slog.Logger) {
h := l.Handler()
r := slog.NewRecord(time.Now(), slog.LevelError, "bar", 0)
r.AddAttrs(slog.Any("err", errors.New("error")))
_ = h.Handle(context.Background(), r)
},
expected: "level=ERROR msg=bar err=error\n",
},
}

for i, tc := range testCases {
Expand Down
12 changes: 12 additions & 0 deletions internal/output/ranges_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,18 @@ func TestFormatLineRangeString(t *testing.T) {
lines: []int{13, 12, 3, 5, 4, 7},
output: "3-5 7 12-13",
},
{
lines: []int{},
output: "",
},
{
lines: []int{1},
output: "1",
},
{
lines: []int{-1, 0, 1},
output: "",
},
}

for i, tc := range testCases {
Expand Down
Loading
Loading