From aa7b9dcf8db0c4ab718b40b2e2a83c2524088f1e Mon Sep 17 00:00:00 2001 From: sbp-bvanb Date: Fri, 1 Nov 2024 08:56:29 +0100 Subject: [PATCH] fix: [#5] Add unit testing --- .github/workflows/golang.yml | 2 +- .gitignore | 3 ++ .golangci.yml | 7 ++-- .prolayout.yml | 2 ++ README.md | 4 ++- internal/analyzer/analyzer.go | 9 +++--- internal/analyzer/analyzer_test.go | 51 ++++++++++++++++++++++++++++++ main.go | 24 +++++++++++--- main_test.go | 24 ++++++++++++++ 9 files changed, 113 insertions(+), 13 deletions(-) create mode 100644 internal/analyzer/analyzer_test.go create mode 100644 main_test.go diff --git a/.github/workflows/golang.yml b/.github/workflows/golang.yml index e7d019d..36849ed 100644 --- a/.github/workflows/golang.yml +++ b/.github/workflows/golang.yml @@ -18,7 +18,7 @@ jobs: - uses: actions/checkout@v4.2.2 - uses: schubergphilis/mcvs-golang-action@v0.9.4 with: - code-coverage-expected: 62.3 + code-coverage-expected: 71.2 gci: "false" golang-unit-tests-exclusions: |- \(cmd\/prolayout\) diff --git a/.gitignore b/.gitignore index b5b4faa..f4a1928 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,6 @@ .task .vscode +coverage.html +functioncoverage.out +profile.cov reports diff --git a/.golangci.yml b/.golangci.yml index b0dfc56..69e2dec 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -130,6 +130,7 @@ linters: - nilnil - nlreturn - paralleltest + - testpackage - varnamelen - wsl @@ -147,14 +148,16 @@ issues: - path: _test\.go linters: - funlen - - linters: - staticcheck text: "SA9003:" - - linters: - lll text: "^//go:generate " + - linters: + - revive + path: internal/analyzer/analyzer.go + text: "cyclomatic: function \\(\\*runner\\).assessDir has cyclomatic complexity 8 \\(\\> max enabled 7\\)" exclude-use-default: false diff --git a/.prolayout.yml b/.prolayout.yml index 491db75..08530dc 100644 --- a/.prolayout.yml +++ b/.prolayout.yml @@ -2,3 +2,5 @@ module: "github.com/wimspaargaren/prolayout" root: - name: "bar" + - name: "internal" + - name: "tests" diff --git a/README.md b/README.md index abb3a03..9469260 100644 --- a/README.md +++ b/README.md @@ -21,7 +21,7 @@ fashion. ## Example configuration file -```YAML +```yml module: "github.com/wimspaargaren/prolayout" root: - name: "cmd" @@ -35,3 +35,5 @@ root: files: - name: ".*_test.go" ``` + +and run `prolayout ./...` diff --git a/internal/analyzer/analyzer.go b/internal/analyzer/analyzer.go index 38be6a0..d71acf3 100644 --- a/internal/analyzer/analyzer.go +++ b/internal/analyzer/analyzer.go @@ -58,10 +58,7 @@ func (r *runner) assessDir(pass *analysis.Pass) (*model.Dir, error) { dir := &model.Dir{} for _, folder := range packageSplittedPerFolder { - if len(dirs) == 0 { - return nil, nil - } - if strings.HasSuffix(folder, ".test") { + if len(dirs) == 0 || strings.HasSuffix(folder, ".test") { return nil, nil } @@ -70,12 +67,16 @@ func (r *runner) assessDir(pass *analysis.Pass) (*model.Dir, error) { return nil, err } if !ok { + if len(pass.Files) == 0 || packagePathWithoutModule == "" { + continue + } pass.ReportRangef(pass.Files[0], "package not allowed: %s, %s not found in allowed names: [%s]", packagePathWithoutModule, folder, strings.Join(dirsNames(dirs), ",")) break } dir = res dirs = res.Dirs } + return dir, nil } diff --git a/internal/analyzer/analyzer_test.go b/internal/analyzer/analyzer_test.go new file mode 100644 index 0000000..5bf3595 --- /dev/null +++ b/internal/analyzer/analyzer_test.go @@ -0,0 +1,51 @@ +package analyzer + +import ( + "go/types" + "testing" + + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" + "golang.org/x/tools/go/analysis" + + "github.com/wimspaargaren/prolayout/internal/model" +) + +func TestAssessDir(t *testing.T) { + testCases := []struct { + name string + pass *analysis.Pass + expected *model.Dir + hasError bool + }{ + { + name: "if folder contains a '.test' suffix, then skip assessDir and return nil twice", + pass: &analysis.Pass{ + Pkg: types.NewPackage("github.com/wimspaargaren/prolayout/tests.test", "main"), + }, + expected: nil, + hasError: false, + }, + { + name: "if folder contains a '.something' suffix, then loop through and return 'dir *model.Dir' at the end", + pass: &analysis.Pass{ + Pkg: types.NewPackage("github.com/wimspaargaren/prolayout/tests.something", "main"), + }, + expected: &model.Dir{Name: "", Files: []*model.File(nil), Dirs: []*model.Dir(nil)}, + hasError: false, + }, + } + + for _, tc := range testCases { + t.Run(tc.name, func(t *testing.T) { + r := &runner{Root: model.Root{Root: []*model.Dir{{Name: "internal"}}}} + dir, err := r.assessDir(tc.pass) + if tc.hasError { + assert.Error(t, err) + return + } + require.NoError(t, err) + assert.Equal(t, tc.expected, dir) + }) + } +} diff --git a/main.go b/main.go index 5011e0f..f07663d 100644 --- a/main.go +++ b/main.go @@ -2,8 +2,10 @@ package main import ( + "fmt" "log" "os" + "path/filepath" "golang.org/x/tools/go/analysis/singlechecker" "gopkg.in/yaml.v3" @@ -12,15 +14,27 @@ import ( "github.com/wimspaargaren/prolayout/internal/model" ) -func main() { - data, err := os.ReadFile(".prolayout.yml") +const proLayoutFile = ".prolayout.yml" + +func readAndUnmarshalProLayoutYML(proLayoutFile string) (*model.Root, error) { + data, err := os.ReadFile(filepath.Clean(proLayoutFile)) if err != nil { - log.Fatalf("error: %v", err) + return nil, fmt.Errorf("'%w'", err) } t := model.Root{} err = yaml.Unmarshal(data, &t) if err != nil { - log.Fatalf("error: %v", err) + return nil, fmt.Errorf("'%w'", err) } - singlechecker.Main(analyzer.New(t)) + + return &t, nil +} + +func main() { + unmarshalledProLayoutYML, err := readAndUnmarshalProLayoutYML(proLayoutFile) + if err != nil { + log.Fatalf("failed to unmarshal '%s': '%v'", proLayoutFile, err) + } + + singlechecker.Main(analyzer.New(*unmarshalledProLayoutYML)) } diff --git a/main_test.go b/main_test.go new file mode 100644 index 0000000..e467330 --- /dev/null +++ b/main_test.go @@ -0,0 +1,24 @@ +package main + +import ( + "testing" + + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" + + "github.com/wimspaargaren/prolayout/internal/model" +) + +func TestReadAndUnmarshalProLayoutYML(t *testing.T) { + exp := model.Root{ + Module: "github.com/wimspaargaren/prolayout", + Root: []*model.Dir{{Name: "bar"}, {Name: "internal"}, {Name: "tests"}}, + } + act, err := readAndUnmarshalProLayoutYML(proLayoutFile) + require.NoError(t, err) + assert.Equal(t, exp, *act) + + act, err = readAndUnmarshalProLayoutYML(proLayoutFile + "-does-not-exist") + require.Empty(t, act) + assert.EqualError(t, err, "'open .prolayout.yml-does-not-exist: no such file or directory'") +}