Skip to content

Commit

Permalink
fix: [#5] Add unit testing
Browse files Browse the repository at this point in the history
  • Loading branch information
sbp-bvanb committed Nov 1, 2024
1 parent 9300617 commit cb058e1
Show file tree
Hide file tree
Showing 9 changed files with 111 additions and 15 deletions.
5 changes: 0 additions & 5 deletions .github/workflows/golang.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,6 @@ jobs:
\(cmd\/prolayout\)
golangci-lint-version: v1.61.0
testing-type: ${{ matrix.testing-type }}
token: ${{ secrets.GITHUB_TOKEN }}
trivy-action-db: |-
ghcr.io/schubergphilis/mcvs-scanner-trivy-db:2
trivy-action-java-db: |-
ghcr.io/schubergphilis/mcvs-scanner-trivy-java-db:1
strategy:
matrix:
testing-type:
Expand Down
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
.task
.vscode
coverage.html
functioncoverage.out
profile.cov
reports
2 changes: 2 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,8 @@ linters:
- nilnil
- nlreturn
- paralleltest
- revive
- testpackage
- varnamelen
- wsl

Expand Down
2 changes: 2 additions & 0 deletions .prolayout.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,5 @@
module: "github.com/wimspaargaren/prolayout"
root:
- name: "bar"
- name: "internal"
- name: "tests"
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ fashion.

## Example configuration file

```YAML
```yml
module: "github.com/wimspaargaren/prolayout"
root:
- name: "cmd"
Expand All @@ -35,3 +35,5 @@ root:
files:
- name: ".*_test.go"
```
and run `prolayout ./...`
11 changes: 7 additions & 4 deletions internal/analyzer/analyzer.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,24 +58,27 @@ 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
}

res, ok, err := matchDir(dirs, folder)
if err != nil {
return nil, err
}

if len(pass.Files) == 0 || packagePathWithoutModule == "" {
continue
}

if !ok {
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
}

Expand Down
51 changes: 51 additions & 0 deletions internal/analyzer/analyzer_test.go
Original file line number Diff line number Diff line change
@@ -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)
})
}
}
24 changes: 19 additions & 5 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@
package main

import (
"fmt"
"log"
"os"
"path/filepath"

"golang.org/x/tools/go/analysis/singlechecker"
"gopkg.in/yaml.v3"
Expand All @@ -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))
}
24 changes: 24 additions & 0 deletions main_test.go
Original file line number Diff line number Diff line change
@@ -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'")
}

0 comments on commit cb058e1

Please sign in to comment.