Skip to content

Commit

Permalink
Merge pull request #1091 from cloudflare/tests
Browse files Browse the repository at this point in the history
Add more test coverage
  • Loading branch information
prymitive committed Aug 27, 2024
2 parents 654267a + 0db239f commit 80986de
Show file tree
Hide file tree
Showing 16 changed files with 307 additions and 26 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/benchmark.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ permissions:
pull-requests: write

jobs:
test-go:
benchmark:
runs-on: ubuntu-latest
steps:
- name: Check out code
Expand Down
2 changes: 2 additions & 0 deletions .github/workflows/compile.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ on:
types:
- published

permissions: read-all

jobs:
build:
runs-on: ubuntu-latest
Expand Down
3 changes: 1 addition & 2 deletions .github/workflows/deps.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
name: "Dependency Review"
on: [pull_request]

permissions:
contents: read
permissions: read-all

jobs:
dependency-review:
Expand Down
10 changes: 6 additions & 4 deletions .github/workflows/docker.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,10 @@ on:
types:
- published

permissions: read-all

jobs:
amd64:
docker-amd64:
runs-on: ubuntu-latest
steps:
- name: Check out code
Expand All @@ -27,7 +29,7 @@ jobs:
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3

- name: Build and push
- name: Build
id: docker_build
uses: docker/build-push-action@v6
with:
Expand All @@ -36,7 +38,7 @@ jobs:
platforms: linux/amd64
push: false

arm64:
docker-arm64:
if: github.event_name != 'pull_request'
runs-on: ubuntu-latest
steps:
Expand All @@ -52,7 +54,7 @@ jobs:
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3

- name: Build and push
- name: Build
id: docker_build
uses: docker/build-push-action@v6
with:
Expand Down
2 changes: 2 additions & 0 deletions .github/workflows/docs.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
name: Spellcheck
on: [pull_request]

permissions: read-all

jobs:
spellcheck:
name: Spellcheck
Expand Down
4 changes: 3 additions & 1 deletion .github/workflows/examples.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@ on:
branches:
- main

permissions: read-all

jobs:
test-go:
examples:
runs-on: ubuntu-latest
steps:
- name: Check out code
Expand Down
2 changes: 2 additions & 0 deletions .github/workflows/go-mod-tidy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ on:
branches:
- main

permissions: read-all

jobs:
go-mod-tidy:
runs-on: ubuntu-latest
Expand Down
2 changes: 2 additions & 0 deletions .github/workflows/goreleaser.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ on:
branches:
- main

permissions: read-all

jobs:
goreleaser-config:
runs-on: ubuntu-latest
Expand Down
2 changes: 2 additions & 0 deletions .github/workflows/lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ on:
branches:
- main

permissions: read-all

jobs:
golangci-lint:
runs-on: ubuntu-latest
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/pages.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ concurrency:
cancel-in-progress: true

jobs:
build:
pages:
runs-on: ubuntu-latest
steps:
- name: Checkout
Expand Down
2 changes: 2 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ on:
branches:
- main

permissions: read-all

jobs:
test-go:
runs-on: ubuntu-latest
Expand Down
2 changes: 0 additions & 2 deletions cmd/pint/scan.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,6 @@ func checkRules(ctx context.Context, workers int, isOffline bool, gen *config.Pr
go func() {
for _, entry := range entries {
switch {
case entry.State == discovery.Excluded:
continue
case entry.PathError != nil && entry.State == discovery.Removed:
continue
case entry.Rule.Error.Err != nil && entry.State == discovery.Removed:
Expand Down
3 changes: 0 additions & 3 deletions internal/discovery/discovery.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,6 @@ func (c ChangeType) String() string {
return "removed"
case Moved:
return "moved"
case Excluded:
return "excluded"
default:
return "---"
}
Expand All @@ -63,7 +61,6 @@ const (
Modified
Removed
Moved
Excluded
)

type Path struct {
Expand Down
2 changes: 1 addition & 1 deletion internal/discovery/git_branch.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ func (f GitBranchFinder) Find(allEntries []Entry) (entries []Entry, err error) {
case me.hasBefore && me.hasAfter:
switch {
case me.isIdentical && !me.wasMoved:
me.after.State = Excluded
me.after.State = Noop
me.after.ModifiedLines = []int{}
slog.Debug(
"Rule content was not modified on HEAD, identical rule present before",
Expand Down
97 changes: 87 additions & 10 deletions internal/discovery/git_branch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ groups:
finder: discovery.NewGitBranchFinder(git.RunGit, git.NewPathFilter(includeAll, nil, nil), "main", 4),
entries: []discovery.Entry{
{
State: discovery.Excluded,
State: discovery.Noop,
Path: discovery.Path{
Name: "rules.yml",
SymlinkTarget: "rules.yml",
Expand Down Expand Up @@ -472,7 +472,7 @@ groups:
Rule: mustParse(6, "- record: up:count:2a\n expr: count(up)\n"),
},
{
State: discovery.Excluded,
State: discovery.Noop,
Path: discovery.Path{
Name: "rules.yml",
SymlinkTarget: "rules.yml",
Expand Down Expand Up @@ -535,7 +535,7 @@ groups:
Rule: mustParse(1, "- alert: rule1\n expr: sum(foo) by(job)\n for: 0s\n"),
},
{
State: discovery.Excluded,
State: discovery.Noop,
Path: discovery.Path{
Name: "rules.yml",
SymlinkTarget: "rules.yml",
Expand Down Expand Up @@ -566,7 +566,7 @@ groups:
finder: discovery.NewGitBranchFinder(git.RunGit, git.NewPathFilter(includeAll, nil, includeAll), "main", 4),
entries: []discovery.Entry{
{
State: discovery.Excluded,
State: discovery.Noop,
Path: discovery.Path{
Name: "rules.yml",
SymlinkTarget: "rules.yml",
Expand Down Expand Up @@ -606,7 +606,7 @@ groups:
finder: discovery.NewGitBranchFinder(git.RunGit, git.NewPathFilter(includeAll, nil, includeAll), "main", 4),
entries: []discovery.Entry{
{
State: discovery.Excluded,
State: discovery.Noop,
Path: discovery.Path{
Name: "rules.yml",
SymlinkTarget: "rules.yml",
Expand Down Expand Up @@ -650,7 +650,7 @@ groups:
finder: discovery.NewGitBranchFinder(git.RunGit, git.NewPathFilter(includeAll, nil, includeAll), "main", 4),
entries: []discovery.Entry{
{
State: discovery.Excluded,
State: discovery.Noop,
Path: discovery.Path{
Name: "rules.yml",
SymlinkTarget: "rules.yml",
Expand All @@ -659,7 +659,7 @@ groups:
Rule: mustParse(1, "- alert: rule1\n expr: sum(foo) by(job)\n"),
},
{
State: discovery.Excluded,
State: discovery.Noop,
Path: discovery.Path{
Name: "rules.yml",
SymlinkTarget: "rules.yml",
Expand Down Expand Up @@ -756,7 +756,7 @@ groups:
finder: discovery.NewGitBranchFinder(git.RunGit, git.NewPathFilter(includeAll, nil, includeAll), "main", 4),
entries: []discovery.Entry{
{
State: discovery.Excluded,
State: discovery.Noop,
Path: discovery.Path{
Name: "rules.yml",
SymlinkTarget: "rules.yml",
Expand All @@ -765,7 +765,7 @@ groups:
Rule: mustParse(1, "- alert: rule1\n expr: sum(foo) by(job)\n"),
},
{
State: discovery.Excluded,
State: discovery.Noop,
Path: discovery.Path{
Name: "rules.yml",
SymlinkTarget: "rules.yml",
Expand Down Expand Up @@ -887,7 +887,7 @@ groups:
finder: discovery.NewGitBranchFinder(git.RunGit, git.NewPathFilter(includeAll, nil, includeAll), "main", 4),
entries: []discovery.Entry{
{
State: discovery.Excluded,
State: discovery.Noop,
Path: discovery.Path{
Name: "rules.yml",
SymlinkTarget: "rules.yml",
Expand Down Expand Up @@ -1127,6 +1127,83 @@ groups:
},
},
},
{
title: "add two dups",
setup: func(t *testing.T) {
commitFile(t, "rules.yml", `
- alert: rule1
expr: sum(foo) by(job)
- alert: rule2
expr: sum(foo) by(job)
- alert: rule3
expr: sum(foo) by(job)
`, "v1")

_, err := git.RunGit("checkout", "-b", "v2")
require.NoError(t, err, "git checkout v2")

commitFile(t, "rules.yml", `
- alert: rule1
expr: sum(foo) by(job)
- alert: rule2
expr: sum(foo) by(job)
- alert: rule3
expr: sum(foo) by(job)
- alert: rule1
expr: sum(foo) by(job)
- alert: rule1
expr: sum(foo) by(job)
`, "v2")
},
finder: discovery.NewGitBranchFinder(git.RunGit, git.NewPathFilter(includeAll, nil, includeAll), "main", 4),
entries: []discovery.Entry{
{
State: discovery.Noop,
Path: discovery.Path{
Name: "rules.yml",
SymlinkTarget: "rules.yml",
},
ModifiedLines: []int{},
Rule: mustParse(1, "- alert: rule1\n expr: sum(foo) by(job)\n"),
},
{
State: discovery.Noop,
Path: discovery.Path{
Name: "rules.yml",
SymlinkTarget: "rules.yml",
},
ModifiedLines: []int{},
Rule: mustParse(3, "- alert: rule2\n expr: sum(foo) by(job)\n"),
},
{
State: discovery.Noop,
Path: discovery.Path{
Name: "rules.yml",
SymlinkTarget: "rules.yml",
},
ModifiedLines: []int{},
Rule: mustParse(5, "- alert: rule3\n expr: sum(foo) by(job)\n"),
},
{
State: discovery.Added,
Path: discovery.Path{
Name: "rules.yml",
SymlinkTarget: "rules.yml",
},
ModifiedLines: []int{8, 9},
Rule: mustParse(7, "- alert: rule1\n expr: sum(foo) by(job)\n"),
},
{
State: discovery.Added,
Path: discovery.Path{
Name: "rules.yml",
SymlinkTarget: "rules.yml",
},
ModifiedLines: []int{10, 11},
Rule: mustParse(9, "- alert: rule1\n expr: sum(foo) by(job)\n"),
},
},
},
}

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

0 comments on commit 80986de

Please sign in to comment.