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 88220dd commit f70d30e
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 6 deletions.
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
1 change: 1 addition & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ linters:
- nilnil
- nlreturn
- paralleltest
- testpackage
- varnamelen
- wsl

Expand Down
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 ./...`
3 changes: 3 additions & 0 deletions internal/analyzer/analyzer.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,9 @@ func (r *runner) assessDir(pass *analysis.Pass) (*model.Dir, error) {
if err != nil {
return nil, err
}
// if len(pass.Files) == 0 {
// 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
Expand Down
1 change: 1 addition & 0 deletions internal/analyzer/analyzer_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package analyzer
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"}},
}
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.ymldoes-not-exist: no such file or directory'")
}

0 comments on commit f70d30e

Please sign in to comment.