Skip to content

Commit

Permalink
Refactor tests: t.Fatal, t.TempDir, t.Parallel (#107)
Browse files Browse the repository at this point in the history
- Use t.Fatal instead of panic.
- Make test running in parallel.
- Use t.TempDir instead of "/tmp".

Co-authored-by: Vyacheslav Pryimak <[email protected]>
  • Loading branch information
alexandear and incu6us authored Feb 27, 2023
1 parent 9e9be45 commit 3532668
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 43 deletions.
7 changes: 6 additions & 1 deletion helper/helper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import (
)

func TestDetermineProjectName(t *testing.T) {
t.Parallel()

type args struct {
projectName string
filePath string
Expand All @@ -25,7 +27,7 @@ func TestDetermineProjectName(t *testing.T) {
filePath: func() string {
dir, err := os.Getwd()
if err != nil {
panic(err)
t.Fatal(err)
}

return dir
Expand All @@ -47,7 +49,10 @@ func TestDetermineProjectName(t *testing.T) {
},
}
for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
t.Parallel()

got, err := DetermineProjectName(tt.args.projectName, tt.args.filePath)
if (err != nil) != tt.wantErr {
t.Errorf("DetermineProjectName() error = %v, wantErr %v", err, tt.wantErr)
Expand Down
11 changes: 10 additions & 1 deletion pkg/astutil/astutil_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import (
)

func TestUsesImport(t *testing.T) {
t.Parallel()

type args struct {
fileData string
path string
Expand Down Expand Up @@ -146,10 +148,12 @@ func main(){
},
}
for _, tt := range tests {

tt := tt
fileData := tt.args.fileData

t.Run(tt.name, func(t *testing.T) {
t.Parallel()

fset := token.NewFileSet()
f, err := parser.ParseFile(fset, "", []byte(fileData), parser.ParseComments)
require.NoError(t, err)
Expand All @@ -162,6 +166,8 @@ func main(){
}

func TestLoadPackageDeps(t *testing.T) {
t.Parallel()

type args struct {
dir string
filename string
Expand Down Expand Up @@ -200,7 +206,10 @@ func TestLoadPackageDeps(t *testing.T) {
}

for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
t.Parallel()

f, err := parser.ParseFile(
token.NewFileSet(),
fmt.Sprintf("%s/%s", tt.args.dir, tt.args.filename),
Expand Down
4 changes: 4 additions & 0 deletions pkg/module/error_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package module
import "testing"

func TestPathIsNotSetError_Error(t *testing.T) {
t.Parallel()

tests := []struct {
name string
want string
Expand All @@ -24,6 +26,8 @@ func TestPathIsNotSetError_Error(t *testing.T) {
}

func TestUndefinedModuleError_Error(t *testing.T) {
t.Parallel()

tests := []struct {
name string
want string
Expand Down
73 changes: 32 additions & 41 deletions pkg/module/module_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,13 @@ package module

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

func TestGoModRootPathAndName(t *testing.T) {
t.Parallel()

type args struct {
dir string
}
Expand All @@ -21,9 +24,8 @@ func TestGoModRootPathAndName(t *testing.T) {
dir: func() string {
dir, err := os.Getwd()
if err != nil {
panic(err)
t.Fatal(err)
}

return dir
}(),
},
Expand All @@ -49,7 +51,10 @@ func TestGoModRootPathAndName(t *testing.T) {
}

for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
t.Parallel()

goModRootPath, err := GoModRootPath(tt.args.dir)
if err != nil && !tt.wantErr {
t.Errorf("GoModRootPath() error = %v, wantErr %v", err, tt.wantErr)
Expand All @@ -70,71 +75,52 @@ func TestGoModRootPathAndName(t *testing.T) {
}

func TestName(t *testing.T) {
type args struct {
goModRootPath string
}
t.Parallel()

tests := []struct {
name string
prepareFn func()
args args
prepareFn func() string
want string
wantErr bool
}{
{
name: "read empty go.mod",
prepareFn: func() {
const f = "/tmp/go.mod"

if _, err := os.Stat(f); os.IsExist(err) {
if err := os.Remove(f); err != nil {
panic(err)
}
}

_, err := os.Create(f)
prepareFn: func() string {
dir := t.TempDir()
_, err := os.Create(filepath.Join(dir, "go.mod"))
if err != nil {
panic(err)
t.Fatal(err)
}
},
args: args{
goModRootPath: "/tmp",
return dir
},
want: "",
wantErr: true,
},
{
name: "check failed parsing of go.mod",
prepareFn: func() {
const f = "/tmp/go.mod"

if _, err := os.Stat(f); os.IsExist(err) {
if err := os.Remove(f); err != nil {
panic(err)
}
}

file, err := os.Create(f)
prepareFn: func() string {
dir := t.TempDir()
file, err := os.Create(filepath.Join(dir, "go.mod"))
if err != nil {
panic(err)
t.Fatal(err)
}

if _, err := file.WriteString("mod test"); err != nil {
panic(err)
t.Fatal(err)
}
},
args: args{
goModRootPath: "/tmp",
return dir
},
want: "",
wantErr: true,
},
}
for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
t.Parallel()

tt.prepareFn()

got, err := Name(tt.args.goModRootPath)
goModRootPath := tt.prepareFn()
got, err := Name(goModRootPath)
if (err != nil) != tt.wantErr {
t.Errorf("Name() error = %v, wantErr %v", err, tt.wantErr)
return
Expand All @@ -147,6 +133,8 @@ func TestName(t *testing.T) {
}

func TestDetermineProjectName(t *testing.T) {
t.Parallel()

type args struct {
projectName string
filePath string
Expand All @@ -164,9 +152,9 @@ func TestDetermineProjectName(t *testing.T) {
filePath: func() string {
dir, err := os.Getwd()
if err != nil {
panic(err)
t.Fatal(err)
}
return dir + "/module.go"
return filepath.Join(dir, "module.go")
}(),
},
want: "github.com/incu6us/goimports-reviser/v3",
Expand All @@ -184,7 +172,10 @@ func TestDetermineProjectName(t *testing.T) {
},
}
for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
t.Parallel()

got, err := DetermineProjectName(tt.args.projectName, tt.args.filePath)
if (err != nil) != tt.wantErr {
t.Errorf("DetermineProjectName() error = %v, wantErr %v", err, tt.wantErr)
Expand Down

0 comments on commit 3532668

Please sign in to comment.