-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Charlie Egan <[email protected]>
- Loading branch information
1 parent
c243dd3
commit 3e9faac
Showing
9 changed files
with
267 additions
and
47 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
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,43 @@ | ||
package fixes | ||
|
||
import ( | ||
"bytes" | ||
) | ||
|
||
type NoWhitespaceComment struct{} | ||
|
||
func (*NoWhitespaceComment) Key() string { | ||
return "no-whitespace-comment" | ||
} | ||
|
||
func (*NoWhitespaceComment) WholeFile() bool { | ||
return false | ||
} | ||
|
||
func (*NoWhitespaceComment) Fix(in []byte, opts *RuntimeOptions) (bool, []byte, error) { | ||
lines := bytes.Split(in, []byte("\n")) | ||
|
||
// this fix must have locations | ||
if len(opts.Locations) == 0 { | ||
return false, nil, nil | ||
} | ||
|
||
for _, loc := range opts.Locations { | ||
if loc.Row > len(lines) { | ||
return false, nil, nil | ||
} | ||
|
||
if loc.Col != 1 { | ||
// current impl only understands the first column | ||
return false, nil, nil | ||
} | ||
|
||
line := lines[loc.Row-1] | ||
|
||
if bytes.HasPrefix(line, []byte("#")) && !bytes.HasPrefix(line, []byte("# ")) { | ||
lines[loc.Row-1] = bytes.Replace(line, []byte("#"), []byte("# "), 1) | ||
} | ||
} | ||
|
||
return true, bytes.Join(lines, []byte("\n")), nil | ||
} |
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,120 @@ | ||
package fixes | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/open-policy-agent/opa/ast" | ||
) | ||
|
||
func TestNoWhitespaceComment(t *testing.T) { | ||
t.Parallel() | ||
|
||
testCases := map[string]struct { | ||
runtimeOptions *RuntimeOptions | ||
|
||
beforeFix []byte | ||
afterFix []byte | ||
|
||
fixExpected bool | ||
}{ | ||
"no change needed": { | ||
beforeFix: []byte(`package test\n | ||
# this is a comment | ||
`), | ||
afterFix: []byte(`package test\n | ||
# this is a comment | ||
`), | ||
fixExpected: false, | ||
runtimeOptions: &RuntimeOptions{}, | ||
}, | ||
"no change made because no location": { | ||
beforeFix: []byte(`package test\n | ||
#this is a comment | ||
`), | ||
afterFix: []byte(`package test\n | ||
#this is a comment | ||
`), | ||
fixExpected: false, | ||
runtimeOptions: &RuntimeOptions{}, | ||
}, | ||
"single change": { | ||
beforeFix: []byte(`package test\n | ||
#this is a comment | ||
`), | ||
afterFix: []byte(`package test\n | ||
# this is a comment | ||
`), | ||
fixExpected: true, | ||
runtimeOptions: &RuntimeOptions{ | ||
Locations: []ast.Location{ | ||
{ | ||
Row: 3, | ||
Col: 1, // this is what the rule outputs at the moment | ||
}, | ||
}, | ||
}, | ||
}, | ||
"many changes": { | ||
beforeFix: []byte(`package test\n | ||
#this is a comment | ||
#this is a comment | ||
#this is a comment | ||
`), | ||
afterFix: []byte(`package test\n | ||
# this is a comment | ||
# this is a comment | ||
# this is a comment | ||
`), | ||
fixExpected: true, | ||
runtimeOptions: &RuntimeOptions{ | ||
Locations: []ast.Location{ | ||
{ | ||
Row: 3, | ||
Col: 1, | ||
}, | ||
{ | ||
Row: 4, | ||
Col: 1, | ||
}, | ||
{ | ||
Row: 5, | ||
Col: 1, | ||
}, | ||
}, | ||
}, | ||
}, | ||
} | ||
|
||
for testName, tc := range testCases { | ||
tc := tc | ||
|
||
nwc := NoWhitespaceComment{} | ||
|
||
t.Run(testName, func(t *testing.T) { | ||
t.Parallel() | ||
|
||
fixed, fixedContent, err := nwc.Fix(tc.beforeFix, tc.runtimeOptions) | ||
if err != nil { | ||
t.Fatalf("unexpected error: %v", err) | ||
} | ||
|
||
if tc.fixExpected && !fixed { | ||
t.Fatalf("expected fix to be applied") | ||
} | ||
|
||
if tc.fixExpected && string(fixedContent) != string(tc.afterFix) { | ||
t.Fatalf("unexpected content, got:\n%s---\nexpected:\n%s---", | ||
string(fixedContent), | ||
string(tc.afterFix)) | ||
} | ||
}) | ||
} | ||
} |
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
Oops, something went wrong.