From 6cc0956be3e65b326f9f2f8683c370ea94990f96 Mon Sep 17 00:00:00 2001 From: sbp-bvanb Date: Sat, 2 Nov 2024 18:42:51 +0100 Subject: [PATCH] fix: [#5] Ensure that file names get tested in the root --- .prolayout.yml | 8 ++++++++ internal/analyzer/analyzer.go | 31 ++++++++++++++++++++++++------- 2 files changed, 32 insertions(+), 7 deletions(-) diff --git a/.prolayout.yml b/.prolayout.yml index 08530dc..b46580b 100644 --- a/.prolayout.yml +++ b/.prolayout.yml @@ -1,6 +1,14 @@ --- module: "github.com/wimspaargaren/prolayout" root: + - name: "" + files: + - name: main.go + - name: ".test" - name: "bar" - name: "internal" + dirs: + - name: analyzer + - name: errors + - name: model - name: "tests" diff --git a/internal/analyzer/analyzer.go b/internal/analyzer/analyzer.go index d71acf3..d222234 100644 --- a/internal/analyzer/analyzer.go +++ b/internal/analyzer/analyzer.go @@ -2,6 +2,7 @@ package analyzer import ( + "fmt" "regexp" "strings" @@ -51,26 +52,30 @@ func (r *runner) assess(pass *analysis.Pass) error { func (r *runner) assessDir(pass *analysis.Pass) (*model.Dir, error) { module := r.Root.Module + // fmt.Println("----1---", pass.Pkg.Path()) + packagePathWithoutModule := strings.ReplaceAll(pass.Pkg.Path(), module, "") packagePathWithoutModule = strings.TrimPrefix(packagePathWithoutModule, "/") - packageSplittedPerFolder := splitPath(packagePathWithoutModule) + packageSplitPerFolder := splitPath(packagePathWithoutModule) dirs := r.Root.Root dir := &model.Dir{} - for _, folder := range packageSplittedPerFolder { - if len(dirs) == 0 || strings.HasSuffix(folder, ".test") { - return nil, nil - } + for _, folder := range packageSplitPerFolder { + // if len(dirs) == 0 || strings.HasSuffix(folder, ".test") { + // return nil, nil + // } + fmt.Println("-------42-------", folder) res, ok, err := matchDir(dirs, folder) if err != nil { return nil, err } if !ok { - if len(pass.Files) == 0 || packagePathWithoutModule == "" { + // if len(pass.Files) == 0 || packagePathWithoutModule == "" { + if len(pass.Files) == 0 { continue } - pass.ReportRangef(pass.Files[0], "package not allowed: %s, %s not found in allowed names: [%s]", packagePathWithoutModule, folder, strings.Join(dirsNames(dirs), ",")) + pass.ReportRangef(pass.Files[0], "folder not allowed: %s, %s not found in allowed names: [%s]", packagePathWithoutModule, folder, strings.Join(dirsNames(dirs), ",")) break } dir = res @@ -119,6 +124,18 @@ func (r *runner) matchFiles(files []*model.File, name string) (bool, error) { func matchDir(dir []*model.Dir, name string) (*model.Dir, bool, error) { for _, d := range dir { + // if d.Name == "" { + fmt.Println("-------43----", d.Name, name, "===") + // } + + if d.Name == "" { + if name == "" { + return d, true, nil + } + // return nil, false, nil + continue + } + match, err := regexp.MatchString(d.Name, name) if err != nil { return nil, false, errors.ErrInvalidDirNameRegex{DirName: d.Name}