-
Notifications
You must be signed in to change notification settings - Fork 47
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add unit-tests coverage for dup_pattern checker (#43)
* Change library for golden files
- Loading branch information
Showing
10 changed files
with
198 additions
and
13 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
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,65 @@ | ||
package check_test | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
|
||
"github.com/mszostok/codeowners-validator/internal/check" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestDuplicatedPattern(t *testing.T) { | ||
tests := map[string]struct { | ||
codeownersInput string | ||
expectedIssues []check.Issue | ||
}{ | ||
"Should report info about duplicated entries": { | ||
codeownersInput: ` | ||
* @global-owner1 @global-owner2 | ||
/build/logs/ @doctocat | ||
/build/logs/ @doctocat | ||
/script @mszostok | ||
/script m.t@g.com | ||
`, | ||
expectedIssues: []check.Issue{ | ||
{ | ||
Severity: check.Error, | ||
LineNo: nil, | ||
Message: `Pattern "/build/logs/" is defined 2 times in lines: | ||
* 4: with owners: [@doctocat] | ||
* 5: with owners: [@doctocat]`, | ||
}, | ||
{ | ||
Severity: check.Error, | ||
LineNo: nil, | ||
Message: `Pattern "/script" is defined 2 times in lines: | ||
* 7: with owners: [@mszostok] | ||
* 8: with owners: [m.t@g.com]`, | ||
}, | ||
}, | ||
}, | ||
"Should not report any issues with correct CODEOWNERS file": { | ||
codeownersInput: validCODEOWNERS, | ||
expectedIssues: nil, | ||
}, | ||
} | ||
|
||
for tn, tc := range tests { | ||
t.Run(tn, func(t *testing.T) { | ||
// given | ||
sut := check.NewDuplicatedPattern() | ||
|
||
// when | ||
out, err := sut.Check(context.TODO(), loadInput(tc.codeownersInput)) | ||
|
||
// then | ||
require.NoError(t, err) | ||
assert.ElementsMatch(t, tc.expectedIssues, out.Issues) | ||
}) | ||
} | ||
|
||
} |
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,30 @@ | ||
package check_test | ||
|
||
import ( | ||
"strings" | ||
|
||
"github.com/mszostok/codeowners-validator/internal/check" | ||
"github.com/mszostok/codeowners-validator/pkg/codeowners" | ||
) | ||
|
||
var validCODEOWNERS = ` | ||
# These owners will be the default owners for everything | ||
* @global-owner1 @global-owner2 | ||
# js owner | ||
*.js @js-owner | ||
*.go docs@example.com | ||
/build/logs/ @doctocat | ||
/script m.t@g.com | ||
` | ||
|
||
func loadInput(in string) check.Input { | ||
r := strings.NewReader(in) | ||
|
||
return check.Input{ | ||
CodeownerEntries: codeowners.ParseCodeowners(r), | ||
} | ||
} |
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,34 @@ | ||
package context_test | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
|
||
contextutil "github.com/mszostok/codeowners-validator/internal/context" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestShouldExit(t *testing.T) { | ||
t.Run("Should notify about exit if context is canceled", func(t *testing.T) { | ||
// given | ||
ctx, cancel := context.WithCancel(context.Background()) | ||
|
||
// when | ||
cancel() | ||
shouldExit := contextutil.ShouldExit(ctx) | ||
|
||
// then | ||
assert.True(t, shouldExit) | ||
}) | ||
|
||
t.Run("Should return false if context is not canceled", func(t *testing.T) { | ||
// given | ||
ctx := context.Background() | ||
|
||
// when | ||
shouldExit := contextutil.ShouldExit(ctx) | ||
|
||
// then | ||
assert.False(t, shouldExit) | ||
}) | ||
} |
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,46 @@ | ||
package envconfig_test | ||
|
||
import ( | ||
"os" | ||
"testing" | ||
|
||
"github.com/mszostok/codeowners-validator/internal/envconfig" | ||
|
||
"github.com/stretchr/testify/require" | ||
"gotest.tools/assert" | ||
) | ||
|
||
type testConfig struct { | ||
Key1 string | ||
} | ||
|
||
func TestInit(t *testing.T) { | ||
t.Run("Should read env variable without prefix", func(t *testing.T) { | ||
// given | ||
var cfg testConfig | ||
|
||
require.NoError(t, os.Setenv("KEY1", "test-value")) | ||
|
||
// when | ||
err := envconfig.Init(&cfg) | ||
|
||
// then | ||
require.NoError(t, err) | ||
assert.Equal(t, "test-value", cfg.Key1) | ||
}) | ||
|
||
t.Run("Should read env variable with prefix", func(t *testing.T) { | ||
// given | ||
var cfg testConfig | ||
|
||
require.NoError(t, os.Setenv("ENVS_PREFIX", "TEST_PREFIX")) | ||
require.NoError(t, os.Setenv("TEST_PREFIX_KEY1", "test-value")) | ||
|
||
// when | ||
err := envconfig.Init(&cfg) | ||
|
||
// then | ||
require.NoError(t, err) | ||
assert.Equal(t, "test-value", cfg.Key1) | ||
}) | ||
} |
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
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