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 all 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
2 changes: 1 addition & 1 deletion .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,4 @@
]
}
]
}
}
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
59 changes: 33 additions & 26 deletions pkg/fixer/fixer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/open-policy-agent/opa/v1/ast"
"github.com/open-policy-agent/opa/v1/format"

"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,10 +21,9 @@ func TestFixer(t *testing.T) {
policies := map[string][]byte{
"test/main.rego": []byte(`package test

allow {
allow if {
true #no space
}

deny = true
`),
}
Expand All @@ -48,14 +48,12 @@ deny = true
}

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"},
// use-assigment-operator is correct in formatting so does not appear.
"test/main.rego": {"no-whitespace-comment", "opa-fmt"},
}
expectedFileContents := map[string][]byte{
"test/main.rego": []byte(`package test

import rego.v1

allow := true

# no space
Expand All @@ -65,7 +63,7 @@ deny := true
}

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 +93,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 +135,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 +178,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 +205,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)
}
}
}
13 changes: 3 additions & 10 deletions pkg/fixer/fixes/fixes.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package fixes

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

"github.com/styrainc/regal/internal/lsp/clients"
"github.com/styrainc/regal/pkg/config"
Expand All @@ -14,10 +13,9 @@ func NewDefaultFixes() []Fix {
return []Fix{
&Fmt{},
&Fmt{
// this effectively maps the fix for violations from the
// use-rego-v1 rule to just format the file.
Copy link
Member

Choose a reason for hiding this comment

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

Good!

NameOverride: "use-rego-v1",
OPAFmtOpts: format.Opts{
RegoVersion: ast.RegoV0CompatV1,
},
},
&UseAssignmentOperator{},
&NoWhitespaceComment{},
Expand All @@ -29,12 +27,7 @@ func NewDefaultFixes() []Fix {
// Notably, this does not include fixers that move files around.
func NewDefaultFormatterFixes() []Fix {
return []Fix{
&Fmt{
NameOverride: "use-rego-v1",
OPAFmtOpts: format.Opts{
RegoVersion: ast.RegoV0CompatV1,
},
},
&Fmt{},
&UseAssignmentOperator{},
&NoWhitespaceComment{},
}
Expand Down
Loading
Loading