Skip to content

Commit

Permalink
feat: use standard tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ldez authored and DanilXO committed Oct 24, 2024
1 parent 9cf0cd8 commit f590c10
Show file tree
Hide file tree
Showing 8 changed files with 1,072 additions and 96 deletions.
8 changes: 1 addition & 7 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,9 @@ module github.com/DanilXO/filen

go 1.22.0

require (
github.com/stretchr/testify v1.9.0
golang.org/x/tools v0.26.0
)
require golang.org/x/tools v0.26.0

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
golang.org/x/mod v0.21.0 // indirect
golang.org/x/sync v0.8.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
10 changes: 0 additions & 10 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,18 +1,8 @@
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
golang.org/x/mod v0.21.0 h1:vvrHzRwRfVKSiLrG+d4FMl/Qi4ukBCE6kZlTUkDYRT0=
golang.org/x/mod v0.21.0/go.mod h1:6SkKJ3Xj0I0BrPOZoBy3bdMptDDU9oJrpohJ3eWZ1fY=
golang.org/x/sync v0.8.0 h1:3NFvSEYkUoMifnESzZl15y791HH1qU2xm6eCJU5ZPXQ=
golang.org/x/sync v0.8.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
golang.org/x/tools v0.26.0 h1:v/60pFQmzmT9ExmjDv2gGIfi3OqfKoEP6I5+umXlbnQ=
golang.org/x/tools v0.26.0/go.mod h1:TPVVj70c7JJ3WCazhD8OdXcZg/og+b9+tH/KxylGwH0=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
74 changes: 27 additions & 47 deletions pkg/filen/analyzer_test.go
Original file line number Diff line number Diff line change
@@ -1,82 +1,62 @@
package filen
package filen_test

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

"github.com/stretchr/testify/require"
"github.com/DanilXO/filen/pkg/filen"
"golang.org/x/tools/go/analysis/analysistest"
)

type silentTest struct {
*testing.T
Errors []string
}

func (t *silentTest) Errorf(format string, args ...interface{}) {
t.Errors = append(t.Errors, fmt.Sprintf(format, args...))
}

func TestAll(t *testing.T) {
wd, err := os.Getwd()
if err != nil {
t.Fatalf("Failed to get wd: %s", err)
}
func TestName(t *testing.T) {
testdata := analysistest.TestData()

testCases := []struct {
name string
runConfig *Runner
countOfInvalidFiles int
desc string
config *filen.Runner
pkg string
}{
{
name: "all_files_are_valid",
runConfig: &Runner{
desc: "no report",
pkg: "a",
config: &filen.Runner{
MaxLines: 500,
MinLines: 1,
MinLines: 5,
IgnoreComments: false,
},
countOfInvalidFiles: 0,
},
{
name: "sample_is_too_big",
runConfig: &Runner{
MaxLines: 10,
MinLines: 1,
desc: "default max lines exceeded",
pkg: "b",
config: &filen.Runner{
MaxLines: 500,
MinLines: 5,
IgnoreComments: false,
},
countOfInvalidFiles: 1,
},
{
name: "sample_is_too_small",
runConfig: &Runner{
desc: "default min lines exceeded",
pkg: "c",
config: &filen.Runner{
MaxLines: 500,
MinLines: 40,
MinLines: 5,
IgnoreComments: false,
},
countOfInvalidFiles: 1,
},
{
name: "sample_is_too_small_without_comments",
runConfig: &Runner{
desc: "no report when ignore comments",
pkg: "d",
config: &filen.Runner{
MaxLines: 500,
MinLines: 26,
MinLines: 5,
IgnoreComments: true,
},
countOfInvalidFiles: 1,
},
}

for _, testCase := range testCases {
t.Run(testCase.name, func(t *testing.T) {
silentTest := &silentTest{T: t}

testdata := filepath.Join(filepath.Dir(filepath.Dir(wd)), "testdata")

analysistest.Run(silentTest, testdata, NewAnalyzer(testCase.runConfig), "samples")
for _, test := range testCases {
t.Run(test.desc, func(t *testing.T) {

require.Len(t, silentTest.Errors, testCase.countOfInvalidFiles)
analysistest.RunWithSuggestedFixes(t, testdata, filen.NewAnalyzer(test.config), test.pkg)
})
}
}
39 changes: 39 additions & 0 deletions pkg/filen/testdata/src/a/sample.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package a

import "fmt"

// foo0 test function
func foo0() string {
fmt.Println("foo0 is a test")
return "bar"
}

// foo1 test function
func foo1() string {
fmt.Println("foo1 is a test")
return "bar"
}

// foo2 test function
func foo2() string {
fmt.Println("foo2 is a test")
return "bar"
}

// foo3 test function
func foo3() string {
fmt.Println("foo3 is a test")
return "bar"
}

// foo4 test function
func foo4() string {
fmt.Println("foo4 is a test")
return "bar"
}

// foo5 test function
func foo5() string {
fmt.Println("foo5 is a test")
return "bar"
}
Loading

0 comments on commit f590c10

Please sign in to comment.