Skip to content

Commit

Permalink
added go module parser tests
Browse files Browse the repository at this point in the history
  • Loading branch information
engelmi committed Nov 9, 2022
1 parent 29168ff commit 7e7424c
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 0 deletions.
63 changes: 63 additions & 0 deletions pkg/domain/goModuleParser_gomod_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package domain_test

import (
"fmt"
"os"
"path"
"testing"

"github.com/GoTestTools/limgo/pkg/domain"
"github.com/GoTestTools/limgo/pkg/model/module"
"github.com/google/go-cmp/cmp"
)

func TestParseGoMod(t *testing.T) {

testcases := []struct {
file string
expectGoMod module.GoMod
expectError bool
}{
{
file: "empty_file",
expectError: true,
},
{
file: "missing_go_version",
expectError: true,
},
{
file: "missing_module",
expectError: true,
},
{
file: "valid_gomod",
expectGoMod: module.GoMod{
ModuleName: "github.com/GoTestTools/limgo",
GoVersion: "1.19",
},
expectError: false,
},
}

for _, testcase := range testcases {
t.Run(fmt.Sprintf("with %s", testcase.file), func(t *testing.T) {
file, err := os.Open(path.Join("testdata", "goModuleParser_gomod", testcase.file))
if err != nil {
t.Fatalf("Unexpected error occurred when opening file '%s': %v", testcase.file, err)
}

goMod, err := domain.ParseGoMod(file)
if testcase.expectError && err == nil {
t.Fatalf("Expected error, but got none")
}
if !testcase.expectError && err != nil {
t.Fatalf("Expected no error, but got %v", err)
}

if diff := cmp.Diff(testcase.expectGoMod, goMod); diff != "" {
t.Fatalf("Detected difference in parsed GoMod: %s", diff)
}
})
}
}
Empty file.
5 changes: 5 additions & 0 deletions pkg/domain/testdata/goModuleParser_gomod/missing_go_version
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module github.com/GoTestTools/limgo

require github.com/go-errors/errors v1.4.2

require github.com/google/go-cmp v0.5.9
5 changes: 5 additions & 0 deletions pkg/domain/testdata/goModuleParser_gomod/missing_module
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
go 1.19

require github.com/go-errors/errors v1.4.2

require github.com/google/go-cmp v0.5.9
7 changes: 7 additions & 0 deletions pkg/domain/testdata/goModuleParser_gomod/valid_gomod
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module github.com/GoTestTools/limgo

go 1.19

require github.com/go-errors/errors v1.4.2

require github.com/google/go-cmp v0.5.9

0 comments on commit 7e7424c

Please sign in to comment.