Skip to content

Commit e86ad24

Browse files
fix(cli): fix import path with --nomod (#2775)
Co-authored-by: 包子 <[email protected]>
1 parent 69d7322 commit e86ad24

File tree

3 files changed

+154
-3
lines changed

3 files changed

+154
-3
lines changed

cmd/kratos/internal/project/new.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ func (p *Project) New(ctx context.Context, dir string, layout string, branch str
3939
}
4040
fmt.Printf("🚀 Creating service %s, layout repo is %s, please wait a moment.\n\n", p.Name, layout)
4141
repo := base.NewRepo(layout, branch)
42-
if err := repo.CopyTo(ctx, to, p.Path, []string{".git", ".github"}); err != nil {
42+
if err := repo.CopyTo(ctx, to, p.Name, []string{".git", ".github"}); err != nil {
4343
return err
4444
}
4545
e := os.Rename(

cmd/kratos/internal/project/project.go

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ func run(_ *cobra.Command, args []string) {
6666
name = args[0]
6767
}
6868
projectName, workingDir := processProjectParams(name, wd)
69-
p := &Project{Name: projectName, Path: projectName}
69+
p := &Project{Name: projectName}
7070
done := make(chan error, 1)
7171
go func() {
7272
if !nomod {
@@ -79,9 +79,16 @@ func run(_ *cobra.Command, args []string) {
7979
return
8080
}
8181

82+
p.Path, err = filepath.Rel(projectRoot, filepath.Join(workingDir, projectName))
83+
if err != nil {
84+
done <- fmt.Errorf("🚫 failed to get relative path: %v", err)
85+
return
86+
}
87+
8288
mod, e := base.ModulePath(filepath.Join(projectRoot, "go.mod"))
8389
if e != nil {
84-
panic(e)
90+
done <- fmt.Errorf("🚫 failed to parse `go.mod`: %v", e)
91+
return
8592
}
8693
// Get the relative path for adding a project based on Go modules
8794
p.Path = filepath.Join(strings.TrimPrefix(workingDir, projectRoot+"/"), p.Name)
Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
1+
package project
2+
3+
import (
4+
"fmt"
5+
"go/parser"
6+
"go/token"
7+
"os"
8+
"path/filepath"
9+
"testing"
10+
11+
"github.com/go-kratos/kratos/cmd/kratos/v2/internal/base"
12+
)
13+
14+
// TestCmdNew tests the `kratos new` command.
15+
func TestCmdNew(t *testing.T) {
16+
cwd := changeCurrentDir(t)
17+
projectName := "helloworld"
18+
19+
// create a new project
20+
CmdNew.SetArgs([]string{projectName})
21+
if err := CmdNew.Execute(); err != nil {
22+
t.Fatalf("executing command: %v", err)
23+
}
24+
25+
// check that the expected files were created
26+
for _, file := range []string{
27+
"go.mod",
28+
"go.sum",
29+
"README.md",
30+
"cmd/helloworld/main.go",
31+
} {
32+
if _, err := os.Stat(filepath.Join(cwd, projectName, file)); err != nil {
33+
t.Errorf("expected file %s to exist", file)
34+
}
35+
}
36+
37+
// check that the go.mod file contains the expected module name
38+
assertGoMod(t, filepath.Join(cwd, projectName, "go.mod"), projectName)
39+
40+
assertImportsInclude(t, filepath.Join(cwd, projectName, "cmd", projectName, "wire.go"), fmt.Sprintf(`"%s/internal/biz"`, projectName))
41+
}
42+
43+
// TestCmdNewNoMod tests the `kratos new` command with the --nomod flag.
44+
func TestCmdNewNoMod(t *testing.T) {
45+
cwd := changeCurrentDir(t)
46+
47+
// create a new project
48+
CmdNew.SetArgs([]string{"project"})
49+
if err := CmdNew.Execute(); err != nil {
50+
t.Fatalf("executing command: %v", err)
51+
}
52+
53+
// add new app with --nomod flag
54+
CmdNew.SetArgs([]string{"--nomod", "project/app/user"})
55+
if err := CmdNew.Execute(); err != nil {
56+
t.Fatalf("executing command: %v", err)
57+
}
58+
59+
// check that the expected files were created
60+
for _, file := range []string{
61+
"go.mod",
62+
"go.sum",
63+
"README.md",
64+
"cmd/project/main.go",
65+
"app/user/cmd/user/main.go",
66+
} {
67+
if _, err := os.Stat(filepath.Join(cwd, "project", file)); err != nil {
68+
t.Errorf("expected file %s to exist", file)
69+
}
70+
}
71+
72+
assertImportsInclude(t, filepath.Join(cwd, "project/app/user/cmd/user/wire.go"), `"project/app/user/internal/biz"`)
73+
}
74+
75+
// assertImportsInclude checks that the file at path contains the expected import.
76+
func assertImportsInclude(t *testing.T, path, expected string) {
77+
t.Helper()
78+
79+
got, err := imports(path)
80+
if err != nil {
81+
t.Fatalf("getting imports: %v", err)
82+
}
83+
84+
for _, imp := range got {
85+
if imp == expected {
86+
return
87+
}
88+
}
89+
90+
t.Errorf("expected imports to include %s, got %v", expected, got)
91+
}
92+
93+
// imports returns the imports in the file at path.
94+
func imports(path string) ([]string, error) {
95+
fset := token.NewFileSet()
96+
f, err := parser.ParseFile(fset, path, nil, parser.ImportsOnly)
97+
if err != nil {
98+
return nil, err
99+
}
100+
101+
imports := make([]string, 0, len(f.Imports))
102+
for _, s := range f.Imports {
103+
imports = append(imports, s.Path.Value)
104+
}
105+
106+
return imports, nil
107+
}
108+
109+
// assertGoMod checks that the go.mod file contains the expected module name.
110+
func assertGoMod(t *testing.T, path, expected string) {
111+
t.Helper()
112+
113+
got, err := base.ModulePath(path)
114+
if err != nil {
115+
t.Fatalf("getting module path: %v", err)
116+
}
117+
118+
if got != expected {
119+
t.Errorf("expected module name %s, got %s", expected, got)
120+
}
121+
}
122+
123+
// change the working directory to the tempdir
124+
func changeCurrentDir(t *testing.T) string {
125+
t.Helper()
126+
127+
tmp := t.TempDir()
128+
129+
oldCWD, err := os.Getwd()
130+
if err != nil {
131+
t.Fatalf("getting working directory: %v", err)
132+
}
133+
134+
if err := os.Chdir(tmp); err != nil {
135+
t.Fatalf("changing working directory: %v", err)
136+
}
137+
t.Cleanup(func() {
138+
if err := os.Chdir(oldCWD); err != nil {
139+
t.Fatalf("restoring working directory: %v", err)
140+
}
141+
})
142+
143+
return tmp
144+
}

0 commit comments

Comments
 (0)