-
-
Notifications
You must be signed in to change notification settings - Fork 565
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add tiny go.mod parser instead of third party package.
- Loading branch information
Showing
2 changed files
with
77 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
package main | ||
|
||
import ( | ||
"strings" | ||
"testing" | ||
) | ||
|
||
|
@@ -37,20 +38,67 @@ func TestGenerator_goaPackage(t *testing.T) { | |
} | ||
|
||
func TestParseGoModGoaPackage(t *testing.T) { | ||
input := `module calc | ||
cases := []struct { | ||
Name string | ||
Mod string | ||
Package string | ||
Expected string | ||
}{ | ||
{ | ||
Name: "simple require", | ||
Mod: "require goa.design/goa/v3 v3.0.3-0.20190704022140-85024ebc66dc", | ||
Package: "goa.design/goa/v3", | ||
Expected: "goa.design/goa/[email protected]", | ||
}, | ||
{ | ||
Name: "in require block", | ||
Mod: `module calc | ||
go 1.12 | ||
require ( | ||
github.com/smartystreets/assertions v1.0.0 // indirect | ||
github.com/ikawaha/kagome v1.0.0 // indirect | ||
goa.design/goa/v3 v3.0.3-0.20190704022140-85024ebc66dc | ||
goa.design/plugins/v3 v3.0.1 | ||
) | ||
` | ||
expected := `goa.design/goa/[email protected]` | ||
pkg, err := parseGoModGoaPackage("goa.design/goa/v3", []byte(input)) | ||
if err != nil { | ||
t.Fatalf("unexpected error, %v", err) | ||
`, | ||
Package: "goa.design/goa/v3", | ||
Expected: `goa.design/goa/[email protected]`, | ||
}, | ||
{ | ||
Name: "not found", | ||
Mod: `module calc | ||
go 1.12 | ||
require ( | ||
github.com/ikawaha/kagome v1.0.0 // indirect | ||
goa.design/plugins/v3 v3.0.1 | ||
) | ||
`, | ||
Package: "goa.design/goa/v3", | ||
Expected: "goa.design/goa/v3", | ||
}, | ||
{ | ||
Name: "with replace", | ||
Mod: `module calc | ||
go 1.12 | ||
replace goa.design/goa/v3 => ../../../goa.design/goa | ||
require ( | ||
github.com/ikawaha/kagome v1.0.0 // indirect | ||
goa.design/goa/v3 v3.0.3-0.20190704022140-85024ebc66dc | ||
goa.design/plugins/v3 v3.0.1 | ||
) | ||
`, | ||
Package: "goa.design/goa/v3", | ||
Expected: `goa.design/goa/[email protected]`, | ||
}, | ||
} | ||
if pkg != expected { | ||
t.Errorf("expected %v, got %v", expected, pkg) | ||
for _, c := range cases { | ||
t.Run(c.Name, func(t *testing.T) { | ||
pkg, err := parseGoModGoaPackage("goa.design/goa/v3", strings.NewReader(c.Mod)) | ||
if err != nil { | ||
t.Fatalf("unexpected error, %v", err) | ||
} | ||
if pkg != c.Expected { | ||
t.Errorf("expected %v, got %v", c.Expected, pkg) | ||
} | ||
}) | ||
} | ||
} |