Skip to content

Commit cbda10c

Browse files
authored
Add unit-tests coverage for dup_pattern checker (#43)
* Change library for golden files
1 parent c915fc0 commit cbda10c

File tree

10 files changed

+198
-13
lines changed

10 files changed

+198
-13
lines changed

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ require (
99
github.com/kr/text v0.2.0 // indirect
1010
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e // indirect
1111
github.com/pkg/errors v0.9.1
12+
github.com/sebdah/goldie/v2 v2.3.0
1213
github.com/sergi/go-diff v1.1.0 // indirect
1314
github.com/sirupsen/logrus v1.4.2
1415
github.com/spf13/afero v1.2.2

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,8 @@ github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
6161
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
6262
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
6363
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
64+
github.com/sebdah/goldie/v2 v2.3.0 h1:qOrofCLbWLjF2PDEL/BueSdFC8V8VyRKccKmqf/89ws=
65+
github.com/sebdah/goldie/v2 v2.3.0/go.mod h1:oZ9fp0+se1eapSRjfYbsV/0Hqhbuu3bJVvKI/NNtssI=
6466
github.com/sergi/go-diff v1.0.0 h1:Kpca3qRNrduNnOQeazBd0ysaKrUJiIuISHxogkT9RPQ=
6567
github.com/sergi/go-diff v1.0.0/go.mod h1:0CfEIISq7TuYL3j771MWULgwwjU+GofnZX9QAmXWZgo=
6668
github.com/sergi/go-diff v1.1.0 h1:we8PVUC3FE2uYfodKH/nBHMSetSfHDR6scGdBi+erh0=

internal/check/duplicated_pattern.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,16 +36,16 @@ func (d *DuplicatedPattern) Check(ctx context.Context, in Input) (Output, error)
3636

3737
for name, entries := range patterns {
3838
if len(entries) > 1 {
39-
msg := fmt.Sprintf("Pattern %q is defined %d times in lines: \n%s", name, len(entries), ListFormatFunc(entries))
39+
msg := fmt.Sprintf("Pattern %q is defined %d times in lines: \n%s", name, len(entries), d.listFormatFunc(entries))
4040
output.ReportIssue(msg)
4141
}
4242
}
4343

4444
return output, nil
4545
}
4646

47-
// ListFormatFunc is a basic formatter that outputs a bullet point list of the pattern.
48-
func ListFormatFunc(es []codeowners.Entry) string {
47+
// listFormatFunc is a basic formatter that outputs a bullet point list of the pattern.
48+
func (d *DuplicatedPattern) listFormatFunc(es []codeowners.Entry) string {
4949
points := make([]string, len(es))
5050
for i, err := range es {
5151
points[i] = fmt.Sprintf(" * %d: with owners: %s", err.LineNo, err.Owners)
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
package check_test
2+
3+
import (
4+
"context"
5+
"testing"
6+
7+
"github.com/mszostok/codeowners-validator/internal/check"
8+
9+
"github.com/stretchr/testify/assert"
10+
"github.com/stretchr/testify/require"
11+
)
12+
13+
func TestDuplicatedPattern(t *testing.T) {
14+
tests := map[string]struct {
15+
codeownersInput string
16+
expectedIssues []check.Issue
17+
}{
18+
"Should report info about duplicated entries": {
19+
codeownersInput: `
20+
* @global-owner1 @global-owner2
21+
22+
/build/logs/ @doctocat
23+
/build/logs/ @doctocat
24+
25+
/script @mszostok
26+
27+
`,
28+
expectedIssues: []check.Issue{
29+
{
30+
Severity: check.Error,
31+
LineNo: nil,
32+
Message: `Pattern "/build/logs/" is defined 2 times in lines:
33+
* 4: with owners: [@doctocat]
34+
* 5: with owners: [@doctocat]`,
35+
},
36+
{
37+
Severity: check.Error,
38+
LineNo: nil,
39+
Message: `Pattern "/script" is defined 2 times in lines:
40+
* 7: with owners: [@mszostok]
41+
* 8: with owners: [[email protected]]`,
42+
},
43+
},
44+
},
45+
"Should not report any issues with correct CODEOWNERS file": {
46+
codeownersInput: validCODEOWNERS,
47+
expectedIssues: nil,
48+
},
49+
}
50+
51+
for tn, tc := range tests {
52+
t.Run(tn, func(t *testing.T) {
53+
// given
54+
sut := check.NewDuplicatedPattern()
55+
56+
// when
57+
out, err := sut.Check(context.TODO(), loadInput(tc.codeownersInput))
58+
59+
// then
60+
require.NoError(t, err)
61+
assert.ElementsMatch(t, tc.expectedIssues, out.Issues)
62+
})
63+
}
64+
65+
}

internal/check/helpers_test.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package check_test
2+
3+
import (
4+
"strings"
5+
6+
"github.com/mszostok/codeowners-validator/internal/check"
7+
"github.com/mszostok/codeowners-validator/pkg/codeowners"
8+
)
9+
10+
var validCODEOWNERS = `
11+
# These owners will be the default owners for everything
12+
* @global-owner1 @global-owner2
13+
14+
# js owner
15+
*.js @js-owner
16+
17+
18+
19+
/build/logs/ @doctocat
20+
21+
22+
`
23+
24+
func loadInput(in string) check.Input {
25+
r := strings.NewReader(in)
26+
27+
return check.Input{
28+
CodeownerEntries: codeowners.ParseCodeowners(r),
29+
}
30+
}

internal/context/check_test.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package context_test
2+
3+
import (
4+
"context"
5+
"testing"
6+
7+
contextutil "github.com/mszostok/codeowners-validator/internal/context"
8+
"github.com/stretchr/testify/assert"
9+
)
10+
11+
func TestShouldExit(t *testing.T) {
12+
t.Run("Should notify about exit if context is canceled", func(t *testing.T) {
13+
// given
14+
ctx, cancel := context.WithCancel(context.Background())
15+
16+
// when
17+
cancel()
18+
shouldExit := contextutil.ShouldExit(ctx)
19+
20+
// then
21+
assert.True(t, shouldExit)
22+
})
23+
24+
t.Run("Should return false if context is not canceled", func(t *testing.T) {
25+
// given
26+
ctx := context.Background()
27+
28+
// when
29+
shouldExit := contextutil.ShouldExit(ctx)
30+
31+
// then
32+
assert.False(t, shouldExit)
33+
})
34+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package envconfig_test
2+
3+
import (
4+
"os"
5+
"testing"
6+
7+
"github.com/mszostok/codeowners-validator/internal/envconfig"
8+
9+
"github.com/stretchr/testify/require"
10+
"gotest.tools/assert"
11+
)
12+
13+
type testConfig struct {
14+
Key1 string
15+
}
16+
17+
func TestInit(t *testing.T) {
18+
t.Run("Should read env variable without prefix", func(t *testing.T) {
19+
// given
20+
var cfg testConfig
21+
22+
require.NoError(t, os.Setenv("KEY1", "test-value"))
23+
24+
// when
25+
err := envconfig.Init(&cfg)
26+
27+
// then
28+
require.NoError(t, err)
29+
assert.Equal(t, "test-value", cfg.Key1)
30+
})
31+
32+
t.Run("Should read env variable with prefix", func(t *testing.T) {
33+
// given
34+
var cfg testConfig
35+
36+
require.NoError(t, os.Setenv("ENVS_PREFIX", "TEST_PREFIX"))
37+
require.NoError(t, os.Setenv("TEST_PREFIX_KEY1", "test-value"))
38+
39+
// when
40+
err := envconfig.Init(&cfg)
41+
42+
// then
43+
require.NoError(t, err)
44+
assert.Equal(t, "test-value", cfg.Key1)
45+
})
46+
}

internal/github/client.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package github
33
import (
44
"context"
55
"net/http"
6+
"time"
67

78
"github.com/mszostok/codeowners-validator/pkg/url"
89

@@ -11,9 +12,10 @@ import (
1112
)
1213

1314
type ClientConfig struct {
14-
AccessToken string `envconfig:"optional"`
15-
BaseURL string `envconfig:"optional"`
16-
UploadURL string `envconfig:"optional"`
15+
AccessToken string `envconfig:"optional"`
16+
BaseURL string `envconfig:"optional"`
17+
UploadURL string `envconfig:"optional"`
18+
HTTPRequestTimeout time.Duration `envconfig:"default=30s"`
1719
}
1820

1921
func NewClient(ctx context.Context, cfg ClientConfig) (ghClient *github.Client, err error) {
@@ -23,6 +25,7 @@ func NewClient(ctx context.Context, cfg ClientConfig) (ghClient *github.Client,
2325
&oauth2.Token{AccessToken: cfg.AccessToken},
2426
))
2527
}
28+
httpClient.Timeout = cfg.HTTPRequestTimeout
2629

2730
baseURL, uploadURL := cfg.BaseURL, cfg.UploadURL
2831

pkg/codeowners/owners.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ func NewFromPath(path string) ([]Entry, error) {
3232
return nil, err
3333
}
3434

35-
entries := parseCodeowners(r)
35+
entries := ParseCodeowners(r)
3636
return entries, nil
3737
}
3838

@@ -66,7 +66,7 @@ func openCodeownersFile(dir string) (io.Reader, error) {
6666
return nil, fmt.Errorf("No CODEOWNERS found in the root, docs/, or .github/ directory of the repository %s", dir)
6767
}
6868

69-
func parseCodeowners(r io.Reader) []Entry {
69+
func ParseCodeowners(r io.Reader) []Entry {
7070
var e []Entry
7171
s := bufio.NewScanner(r)
7272
no := uint64(0)

tests/integration/integration_test.go

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@ import (
77
"testing"
88
"time"
99

10+
"github.com/sebdah/goldie/v2"
1011
"github.com/stretchr/testify/assert"
1112
"github.com/stretchr/testify/require"
12-
"gotest.tools/golden"
1313
)
1414

1515
const (
@@ -24,7 +24,7 @@ const (
2424
// to the golden file.
2525
//
2626
// To update golden file, run:
27-
// go test ./tests/integration/... -v -test.update-golden -tags=integration -run=^TestCheckHappyPath$
27+
// go test ./tests/integration/... -v -update -tags=integration -run=^TestCheckHappyPath$
2828
func TestCheckSuccess(t *testing.T) {
2929
type Envs map[string]string
3030
tests := []struct {
@@ -83,7 +83,9 @@ func TestCheckSuccess(t *testing.T) {
8383
require.NoError(t, err)
8484
assert.Equal(t, result.ExitCode, 0)
8585
normalizedOutput := normalizeTimeDurations(result.Stdout)
86-
golden.Assert(t, normalizedOutput, t.Name()+".golden.txt")
86+
87+
g := goldie.New(t, goldie.WithNameSuffix(".golden.txt"))
88+
g.Assert(t, t.Name(), []byte(normalizedOutput))
8789
})
8890
}
8991
}
@@ -95,7 +97,7 @@ func TestCheckSuccess(t *testing.T) {
9597
// to the golden file.
9698
//
9799
// To update golden file, run:
98-
// go test ./tests/integration/... -v -test.update-golden -tags=integration -run=^TestCheckFailures$
100+
// go test ./tests/integration/... -v -update -tags=integration -run=^TestCheckFailures$
99101
func TestCheckFailures(t *testing.T) {
100102
type Envs map[string]string
101103
tests := []struct {
@@ -157,7 +159,9 @@ func TestCheckFailures(t *testing.T) {
157159
assert.Equal(t, 3, result.ExitCode)
158160

159161
normalizedOutput := normalizeTimeDurations(result.Stdout)
160-
golden.Assert(t, normalizedOutput, t.Name()+".golden.txt")
162+
163+
g := goldie.New(t, goldie.WithNameSuffix(".golden.txt"))
164+
g.Assert(t, t.Name(), []byte(normalizedOutput))
161165
})
162166
}
163167
}

0 commit comments

Comments
 (0)