From 18c57349301035046cad6df216e300d09c3a2780 Mon Sep 17 00:00:00 2001 From: 180909 <734461790@qq.com> Date: Tue, 6 Sep 2022 13:50:00 +0800 Subject: [PATCH] test(cmd): add ModulePath test (#2337) --- cmd/kratos/internal/base/mod_test.go | 34 +++++++++++++++++++++++++++- 1 file changed, 33 insertions(+), 1 deletion(-) diff --git a/cmd/kratos/internal/base/mod_test.go b/cmd/kratos/internal/base/mod_test.go index 9ce7e3ccb..577896b03 100644 --- a/cmd/kratos/internal/base/mod_test.go +++ b/cmd/kratos/internal/base/mod_test.go @@ -1,6 +1,9 @@ package base -import "testing" +import ( + "os" + "testing" +) func TestModuleVersion(t *testing.T) { v, err := ModuleVersion("golang.org/x/mod") @@ -9,3 +12,32 @@ func TestModuleVersion(t *testing.T) { } t.Log(v) } + +func TestModulePath(t *testing.T) { + if err := os.Mkdir("/tmp/test_mod", os.ModePerm); err != nil { + t.Fatal(err) + } + + defer os.RemoveAll("/tmp/test_mod") + + f, err := os.Create("/tmp/test_mod/go.mod") + if err != nil { + t.Fatal(err) + } + + mod := `module github.com/go-kratos/kratos/v2 + +go 1.16` + _, err = f.WriteString(mod) + if err != nil { + t.Fatal(err) + } + + p, err := ModulePath("/tmp/test_mod/go.mod") + if err != nil { + t.Fatal(err) + } + if p != "github.com/go-kratos/kratos/v2" { + t.Fatalf("want: %s, got: %s", "module github.com/go-kratos/kratos/v2", p) + } +}