-
Notifications
You must be signed in to change notification settings - Fork 49
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
235 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package check_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/mszostok/codeowners-validator/internal/check" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestAPIBuilder(t *testing.T) { | ||
var bldr *check.OutputBuilder = nil | ||
|
||
t.Run("Does not panic on ReportIssue when builder is nil", func(t *testing.T) { | ||
assert.NotPanics(t, func() { | ||
issue := bldr.ReportIssue("test") | ||
assert.Nil(t, issue) | ||
}) | ||
}) | ||
|
||
t.Run("Does not panic on Output when builder is nil", func(t *testing.T) { | ||
assert.NotPanics(t, func() { | ||
out := bldr.Output() | ||
assert.Empty(t, out) | ||
}) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package check_test | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"testing" | ||
|
||
"github.com/mszostok/codeowners-validator/internal/check" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestRespectingCanceledContext(t *testing.T) { | ||
must := func(checker check.Checker, err error) check.Checker { | ||
require.NoError(t, err) | ||
return checker | ||
} | ||
|
||
checkers := []check.Checker{ | ||
check.NewDuplicatedPattern(), | ||
check.NewFileExist(), | ||
check.NewValidSyntax(), | ||
check.NewNotOwnedFile(check.NotOwnedFileConfig{}), | ||
must(check.NewValidOwner(check.ValidOwnerConfig{Repository: "org/repo"}, nil)), | ||
} | ||
|
||
for _, checker := range checkers { | ||
sut := checker | ||
t.Run(checker.Name(), func(t *testing.T) { | ||
// given: canceled context | ||
ctx, cancel := context.WithCancel(context.Background()) | ||
cancel() | ||
|
||
// when | ||
out, err := sut.Check(ctx, loadInput(validCODEOWNERS)) | ||
|
||
// then | ||
assert.True(t, errors.Is(err, context.Canceled)) | ||
assert.Empty(t, out) | ||
}) | ||
} | ||
} |
2 changes: 2 additions & 0 deletions
2
...ata/TestTTYPrinterPrintCheckResult/Should_print_OK_status_on_empty_issues_list.golden.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
==> Executing Foo Checker (1s) | ||
Check OK |
5 changes: 5 additions & 0 deletions
5
...inter/testdata/TestTTYPrinterPrintCheckResult/Should_print_all_reported_issues.golden.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
==> Executing Foo Checker (1s) | ||
[err] line 42: Simulate error in line 42 | ||
[war] line 2020: Simulate warning in line 2020 | ||
[err] Error without line number | ||
[war] Warning without line number |
2 changes: 2 additions & 0 deletions
2
...estdata/TestTTYPrinterPrintSummary/Should_print_'no'_when_there_is_no_failures.golden.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
|
||
20 check(s) executed, no failure(s) |
2 changes: 2 additions & 0 deletions
2
...al/printer/testdata/TestTTYPrinterPrintSummary/Should_print_number_of_failures.golden.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
|
||
20 check(s) executed, 10 failure(s) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
package printer | ||
|
||
import ( | ||
"bytes" | ||
"io" | ||
"testing" | ||
"time" | ||
|
||
"github.com/mszostok/codeowners-validator/internal/check" | ||
"github.com/mszostok/codeowners-validator/internal/ptr" | ||
|
||
"github.com/sebdah/goldie/v2" | ||
) | ||
|
||
func TestTTYPrinterPrintCheckResult(t *testing.T) { | ||
t.Run("Should print all reported issues", func(t *testing.T) { | ||
// given | ||
tty := TTYPrinter{} | ||
|
||
buff := &bytes.Buffer{} | ||
restore := overrideWriter(buff) | ||
defer restore() | ||
|
||
// when | ||
tty.PrintCheckResult("Foo Checker", time.Second, check.Output{ | ||
Issues: []check.Issue{ | ||
{ | ||
Severity: check.Error, | ||
LineNo: ptr.Uint64Ptr(42), | ||
Message: "Simulate error in line 42", | ||
}, | ||
{ | ||
Severity: check.Warning, | ||
LineNo: ptr.Uint64Ptr(2020), | ||
Message: "Simulate warning in line 2020", | ||
}, | ||
{ | ||
Severity: check.Error, | ||
Message: "Error without line number", | ||
}, | ||
{ | ||
Severity: check.Warning, | ||
Message: "Warning without line number", | ||
}, | ||
}, | ||
}) | ||
|
||
// then | ||
g := goldie.New(t, goldie.WithNameSuffix(".golden.txt")) | ||
g.Assert(t, t.Name(), buff.Bytes()) | ||
}) | ||
|
||
t.Run("Should print OK status on empty issues list", func(t *testing.T) { | ||
// given | ||
tty := TTYPrinter{} | ||
|
||
buff := &bytes.Buffer{} | ||
restore := overrideWriter(buff) | ||
defer restore() | ||
|
||
// when | ||
tty.PrintCheckResult("Foo Checker", time.Second, check.Output{ | ||
Issues: nil, | ||
}) | ||
|
||
// then | ||
g := goldie.New(t, goldie.WithNameSuffix(".golden.txt")) | ||
g.Assert(t, t.Name(), buff.Bytes()) | ||
}) | ||
} | ||
|
||
func TestTTYPrinterPrintSummary(t *testing.T) { | ||
t.Run("Should print number of failures", func(t *testing.T) { | ||
// given | ||
tty := TTYPrinter{} | ||
|
||
buff := &bytes.Buffer{} | ||
restore := overrideWriter(buff) | ||
defer restore() | ||
|
||
// when | ||
tty.PrintSummary(20, 10) | ||
|
||
// then | ||
g := goldie.New(t, goldie.WithNameSuffix(".golden.txt")) | ||
g.Assert(t, t.Name(), buff.Bytes()) | ||
}) | ||
|
||
t.Run("Should print 'no' when there is no failures", func(t *testing.T) { | ||
// given | ||
tty := TTYPrinter{} | ||
|
||
buff := &bytes.Buffer{} | ||
restore := overrideWriter(buff) | ||
defer restore() | ||
|
||
// when | ||
tty.PrintSummary(20, 0) | ||
|
||
// then | ||
g := goldie.New(t, goldie.WithNameSuffix(".golden.txt")) | ||
g.Assert(t, t.Name(), buff.Bytes()) | ||
}) | ||
} | ||
|
||
func overrideWriter(in io.Writer) func() { | ||
old := writer | ||
writer = in | ||
return func() { writer = old } | ||
} |