Skip to content

Commit

Permalink
fix: Remove mandatory fixes (#1356)
Browse files Browse the repository at this point in the history
This functionality is no longer used

Signed-off-by: Charlie Egan <[email protected]>
  • Loading branch information
charlieegan3 authored Jan 21, 2025
1 parent 0a17534 commit 760b484
Show file tree
Hide file tree
Showing 3 changed files with 0 additions and 132 deletions.
1 change: 0 additions & 1 deletion internal/lsp/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -2040,7 +2040,6 @@ func (l *LanguageServer) handleTextDocumentFormatting(

f := fixer.NewFixer()
f.RegisterFixes(fixes.NewDefaultFormatterFixes()...)
f.RegisterMandatoryFixes([]fixes.Fix{&fixes.Fmt{}}...)

if roots, err := config.GetPotentialRoots(
l.workspacePath(),
Expand Down
11 changes: 0 additions & 11 deletions pkg/fixer/fixer.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,17 +64,6 @@ func (f *Fixer) RegisterFixes(fixes ...fixes.Fix) {
}
}

// RegisterMandatoryFixes sets fixes which will be run before other registered
// fixes, against all files which are not ignored, regardless of linter
// violations.
func (f *Fixer) RegisterMandatoryFixes(fixes ...fixes.Fix) {
for _, fix := range fixes {
f.registeredMandatoryFixes[fix.Name()] = fix

delete(f.registeredFixes, fix.Name())
}
}

// RegisterRoots sets the roots of the files that will be fixed.
// Certain fixes may require the nearest root of the file to be known,
// as fix operations could involve things like moving files, which
Expand Down
120 changes: 0 additions & 120 deletions pkg/fixer/fixer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"testing"

"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"
Expand Down Expand Up @@ -122,125 +121,6 @@ deny := true
}
}

func TestFixerWithRegisterMandatoryFixes(t *testing.T) {
t.Parallel()

policies := map[string]string{
"/root/main/main.rego": `package test
allow {
true #no space
}
deny = true
`,
}

memfp := fileprovider.NewInMemoryFileProvider(policies)

input, err := memfp.ToInput(map[string]ast.RegoVersion{
"/root/main": ast.RegoV0,
})
if err != nil {
t.Fatalf("failed to create input: %v", err)
}

l := linter.NewLinter().
WithEnableAll(true).
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
// RegisterMandatoryFixes
f.RegisterMandatoryFixes(
&fixes.Fmt{
NameOverride: "use-rego-v1",
OPAFmtOpts: format.Opts{
RegoVersion: ast.RegoV0CompatV1,
},
},
)

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

expectedFileFixedViolations := map[string][]string{
"/root/main/main.rego": {"use-rego-v1"},
}
expectedFileContents := map[string]string{
// note that since only the rego-v1-format fix is run, the
// no-whitespace-comment fix is not applied
"/root/main/main.rego": `package test
import rego.v1
allow := true
#no space
deny := true
`,
}

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

fpFiles, err := memfp.List()
if err != nil {
t.Fatalf("failed to list files: %v", err)
}

for _, file := range fpFiles {
// check that the content is correct
expectedContent, ok := expectedFileContents[file]
if !ok {
t.Fatalf("unexpected file %s", file)
}

content, err := memfp.Get(file)
if err != nil {
t.Fatalf("failed to get file %s: %v", file, err)
}

if content != expectedContent {
t.Fatalf(
"unexpected content for %s:\ngot:\n%s---\nexpected:\n%s---",
file,
content,
expectedContent,
)
}

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 was fixed %s", file)
}

slices.Sort(expectedFixes)
slices.Sort(fixes)

if !slices.Equal(expectedFixes, fixes) {
t.Fatalf("unexpected fixes for %s:\ngot: %v\nexpected: %v", file, fixes, expectedFixes)
}
}
}

func TestFixViolations(t *testing.T) {
t.Parallel()

Expand Down

0 comments on commit 760b484

Please sign in to comment.