Skip to content

Commit

Permalink
fix: Update root detection (#1049)
Browse files Browse the repository at this point in the history
Now the .regal dir will be used as the root, unless it does not exist.
If this is the case, then the roots will be used as the args to the fix
cmd.

Signed-off-by: Charlie Egan <[email protected]>
  • Loading branch information
charlieegan3 authored Sep 4, 2024
1 parent 02d9538 commit fef46d7
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 10 deletions.
17 changes: 17 additions & 0 deletions cmd/fix.go
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,23 @@ func fix(args []string, params *fixCommandParams) error {
ignore = params.ignoreFiles.v
}

// create a list of absolute paths, these will be used for the file from
// this point in order to be able to use the roots for format reporting.
absArgs := make([]string, len(args))

for i, arg := range args {
if filepath.IsAbs(arg) {
absArgs[i] = arg

continue
}

absArgs[i], err = filepath.Abs(arg)
if err != nil {
return fmt.Errorf("failed to get absolute path for %s: %w", arg, err)
}
}

filtered, err := config.FilterIgnoredPaths(args, ignore, true, "")
if err != nil {
return fmt.Errorf("failed to filter ignored paths: %w", err)
Expand Down
5 changes: 3 additions & 2 deletions e2e/cli_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -779,6 +779,7 @@ func TestFix(t *testing.T) {
td := t.TempDir()

initialState := map[string]string{
".regal/config.yaml": "", // needed to find the root in the right place
"foo/main.rego": `package wow
import rego.v1
Expand Down Expand Up @@ -821,13 +822,13 @@ test_allow {
mustWriteToFile(t, filepath.Join(td, file), string(content))
}

err := regal(&stdout, &stderr)("fix", td)
err := regal(&stdout, &stderr)("fix", filepath.Join(td, "foo"), filepath.Join(td, "bar"))

// 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
In project root: %[1]s
bar/main.rego -> wow/foo-bar/baz/main.rego:
- directory-package-mismatch
Expand Down
29 changes: 21 additions & 8 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -675,22 +675,30 @@ func (rule *Rule) mapToConfig(result any) error {
}

func GetPotentialRoots(paths ...string) ([]string, error) {
var err error

dirMap := make(map[string]struct{})

for _, path := range paths {
abs, err := filepath.Abs(path)
if err != nil {
return nil, fmt.Errorf("failed to get absolute path for %s: %w", path, err)
absDirPaths := make([]string, len(paths))

for i, path := range paths {
abs := path

if !filepath.IsAbs(abs) {
abs, err = filepath.Abs(path)
if err != nil {
return nil, fmt.Errorf("failed to get absolute path for %s: %w", path, err)
}
}

if isDir(abs) {
dirMap[abs] = struct{}{}
absDirPaths[i] = abs
} else {
dirMap[filepath.Dir(abs)] = struct{}{}
absDirPaths[i] = filepath.Dir(abs)
}
}

for _, dir := range util.Keys(dirMap) {
for _, dir := range absDirPaths {
brds, err := FindBundleRootDirectories(dir)
if err != nil {
return nil, fmt.Errorf("failed to find bundle root directories in %s: %w", dir, err)
Expand All @@ -701,7 +709,12 @@ func GetPotentialRoots(paths ...string) ([]string, error) {
}
}

return util.Keys(dirMap), nil
foundRoots := util.Keys(dirMap)
if len(foundRoots) == 0 {
return absDirPaths, nil
}

return foundRoots, nil
}

func isDir(path string) bool {
Expand Down

0 comments on commit fef46d7

Please sign in to comment.