Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fixer: Prep for 1.0 tests #1319

Merged
merged 5 commits into from
Jan 8, 2025
Merged
Show file tree
Hide file tree
Changes from 4 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
3 changes: 2 additions & 1 deletion .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,5 @@
]
}
]
}
}

81 changes: 55 additions & 26 deletions e2e/cli_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -288,8 +288,6 @@ func TestLintV0WithRegoV1ImportViolations(t *testing.T) {
t.Errorf("expected stderr %q, got %q", exp, act)
}

//fmt.Println(stdout.String())

var rep report.Report
if err = json.Unmarshal(stdout.Bytes(), &rep); err != nil {
t.Fatalf("expected JSON response, got %v", stdout.String())
Expand Down Expand Up @@ -836,11 +834,17 @@ func TestFix(t *testing.T) {
td := t.TempDir()

initialState := map[string]string{
".regal/config.yaml": "", // needed to find the root in the right place
".regal/config.yaml": `
project:
rego-version: 1
roots:
- path: v0
rego-version: 0
- path: v1
rego-version: 0
`,
"foo/main.rego": `package wow
import rego.v1
#comment
allow if {
Expand All @@ -849,28 +853,34 @@ allow if {
`,
"foo/main_test.rego": `package wow_test
test_allow {
test_allow if {
true
}
`,
"foo/foo.rego": `package foo
import rego.v1
# present and correct
allow if {
input.admin
}
`,
"bar/main.rego": `package wow["foo-bar"].baz
import rego.v1
`,
"bar/main_test.rego": `package wow["foo-bar"].baz_test
test_allow {
test_allow if {
true
}
`,
"v0/main.rego": `package v0
#comment
allow { true }
`,
"v1/main.rego": `package v1
#comment
allow if { true }
`,
"unrelated.txt": `foobar`,
}
Expand All @@ -880,29 +890,46 @@ test_allow {
}

// --force is required to make the changes when there is no git repo
err := regal(&stdout, &stderr)("fix", "--force", filepath.Join(td, "foo"), filepath.Join(td, "bar"))
err := regal(&stdout, &stderr)(
"fix",
"--force",
filepath.Join(td, "foo"),
filepath.Join(td, "bar"),
filepath.Join(td, "v0"),
filepath.Join(td, "v1"),
)

// 0 exit status is expected as all violations should have been fixed
expectExitCode(t, err, 0, &stdout, &stderr)

exp := fmt.Sprintf(`8 fixes applied:
In project root: %s
exp := fmt.Sprintf(`12 fixes applied:
In project root: %[1]s
bar/main.rego -> wow/foo-bar/baz/main.rego:
- directory-package-mismatch
bar/main_test.rego -> wow/foo-bar/baz/main_test.rego:
- directory-package-mismatch
- use-rego-v1
- opa-fmt
foo/main.rego -> wow/main.rego:
- directory-package-mismatch
- use-rego-v1
- opa-fmt
- no-whitespace-comment
foo/main_test.rego -> wow/main_test.rego:
- directory-package-mismatch
- use-rego-v1
- opa-fmt
In project root: %[1]s/v0
main.rego:
- opa-fmt
- no-whitespace-comment
In project root: %[1]s/v1
main.rego:
- opa-fmt
- no-whitespace-comment
`, td)

if act := stdout.String(); exp != act {
Expand All @@ -916,37 +943,39 @@ foo/main_test.rego -> wow/main_test.rego:
expectedState := map[string]string{
"foo/foo.rego": `package foo
import rego.v1
# present and correct
allow if {
input.admin
}
`,
"wow/foo-bar/baz/main.rego": `package wow["foo-bar"].baz
import rego.v1
`,
"wow/foo-bar/baz/main_test.rego": `package wow["foo-bar"].baz_test
import rego.v1
test_allow := true
`,
"wow/main.rego": `package wow
import rego.v1
# comment
allow := true
`,
"wow/main_test.rego": `package wow_test
test_allow := true
`,
"v0/main.rego": `package v0
import rego.v1
test_allow := true
# comment
allow := true
`,
"v1/main.rego": `package v1
# comment
allow := true
`,
"unrelated.txt": `foobar`,
}
Expand Down
9 changes: 1 addition & 8 deletions internal/lsp/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -2165,14 +2165,7 @@ func (l *LanguageServer) handleTextDocumentFormatting(

f := fixer.NewFixer()
f.RegisterFixes(fixes.NewDefaultFormatterFixes()...)
f.RegisterMandatoryFixes(
&fixes.Fmt{
NameOverride: "use-rego-v1",
OPAFmtOpts: format.Opts{
RegoVersion: ast.RegoV0CompatV1,
},
},
)
f.RegisterMandatoryFixes([]fixes.Fix{&fixes.Fmt{}}...)

if roots, err := config.GetPotentialRoots(
l.workspacePath(),
Expand Down
85 changes: 49 additions & 36 deletions pkg/fixer/fixer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,15 @@ package fixer
import (
"bytes"
"context"
"os"
"slices"
"testing"

"github.com/open-policy-agent/opa/v1/ast"
"github.com/open-policy-agent/opa/v1/format"
"github.com/open-policy-agent/opa/v1/topdown"

"github.com/styrainc/regal/pkg/config"
"github.com/styrainc/regal/pkg/fixer/fileprovider"
"github.com/styrainc/regal/pkg/fixer/fixes"
"github.com/styrainc/regal/pkg/linter"
Expand All @@ -20,11 +23,7 @@ func TestFixer(t *testing.T) {
policies := map[string][]byte{
"test/main.rego": []byte(`package test

allow {
true #no space
}

deny = true
allow = true #no space
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

didn’t it used to be that way before because the formatter would rewrite it to
allow = true
and we wanted to make sure we didn’t have that in our output?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, originally I didn't want to have the complicated formatting logic in these tests, but I think it's fine and have reverted this in b174300.

`),
}

Expand All @@ -37,35 +36,40 @@ deny = true

l := linter.NewLinter().
WithEnableAll(true).
WithInputModules(&input)
WithInputModules(&input).
WithPrintHook(topdown.NewPrintHook(os.Stderr)).
WithUserConfig(config.Config{
Capabilities: &config.Capabilities{
Features: []string{"rego_v1_import"},
},
})

f := NewFixer()
f.RegisterFixes(fixes.NewDefaultFixes()...)
f.RegisterFixes(
[]fixes.Fix{
&fixes.UseAssignmentOperator{},
&fixes.NoWhitespaceComment{},
&fixes.DirectoryPackageMismatch{},
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So we don't want formatting here?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See above.

}...,
)

fixReport, err := f.Fix(context.Background(), &l, memfp)
if err != nil {
t.Fatalf("failed to fix: %v", err)
}

expectedFileFixedViolations := map[string][]string{
// use-assignment-operator is not expected since use-rego-v1 also addresses this in this example
"test/main.rego": {"no-whitespace-comment", "use-rego-v1"},
"test/main.rego": {"no-whitespace-comment", "use-assignment-operator"},
}
expectedFileContents := map[string][]byte{
"test/main.rego": []byte(`package test

import rego.v1

allow := true

# no space

deny := true
allow := true # no space
`),
}

if got, exp := fixReport.TotalFixes(), uint(2); got != exp {
t.Fatalf("expected %d fixed files, got %d", exp, got)
t.Fatalf("expected a total of %d fixes, got %d", exp, got)
}

fpFiles, err := memfp.List()
Expand Down Expand Up @@ -95,19 +99,21 @@ deny := true
// check that the fixed violations are correct
fxs := fixReport.FixesForFile(file)

var fixes []string
for _, fx := range fxs {
fixes = append(fixes, fx.Title)
}

expectedFixes, ok := expectedFileFixedViolations[file]
if !ok {
t.Fatalf("unexpected file waas fixed %s", file)
t.Fatalf("unexpected file was fixed %s", file)
}

if len(fxs) != len(expectedFixes) {
t.Fatalf("unexpected number of fixes for %s:\ngot: %v\nexpected: %v", file, fxs, expectedFixes)
}
slices.Sort(expectedFixes)
slices.Sort(fixes)

for _, fx := range fxs {
if !slices.Contains(expectedFixes, fx.Title) {
t.Fatalf("expected fixes to contain %s:\ngot: %v", fx.Title, expectedFixes)
}
if !slices.Equal(expectedFixes, fixes) {
t.Fatalf("unexpected fixes for %s:\ngot: %v\nexpected: %v", file, fixes, expectedFixes)
}
}
}
Expand Down Expand Up @@ -135,7 +141,12 @@ deny = true

l := linter.NewLinter().
WithEnableAll(true).
WithInputModules(&input)
WithInputModules(&input).
WithUserConfig(config.Config{
Capabilities: &config.Capabilities{
Features: []string{"rego_v1_import"},
},
})

f := NewFixer()
// No fixes are registered here, we are only testing the functionality of
Expand Down Expand Up @@ -173,7 +184,7 @@ deny := true
}

if got, exp := fixReport.TotalFixes(), uint(1); got != exp {
t.Fatalf("expected %d fixed files, got %d", exp, got)
t.Fatalf("expected %d fixes, got %d", exp, got)
}

fpFiles, err := memfp.List()
Expand All @@ -200,22 +211,24 @@ deny := true
string(expectedContent))
}

// check that the fixed violations are correct
fxs := fixReport.FixesForFile(file)

// check that the fixed violations are correct
var fixes []string
for _, fx := range fxs {
fixes = append(fixes, fx.Title)
}

expectedFixes, ok := expectedFileFixedViolations[file]
if !ok {
t.Fatalf("unexpected file waas fixed %s", file)
t.Fatalf("unexpected file was fixed %s", file)
}

if len(fxs) != len(expectedFixes) {
t.Fatalf("unexpected number of fixes for %s:\ngot: %v\nexpected: %v", file, fxs, expectedFixes)
}
slices.Sort(expectedFixes)
slices.Sort(fixes)

for _, fx := range fxs {
if !slices.Contains(expectedFixes, fx.Title) {
t.Fatalf("expected fixes to contain %s:\ngot: %v", fx.Title, expectedFixes)
}
if !slices.Equal(expectedFixes, fixes) {
t.Fatalf("unexpected fixes for %s:\ngot: %v\nexpected: %v", file, fixes, expectedFixes)
}
}
}
Loading
Loading