Skip to content

Commit

Permalink
fix(cli): fix import path with --nomod
Browse files Browse the repository at this point in the history
  • Loading branch information
haoqixu committed May 10, 2023
1 parent f03f5f8 commit fbcf8d2
Show file tree
Hide file tree
Showing 3 changed files with 154 additions and 3 deletions.
2 changes: 1 addition & 1 deletion cmd/kratos/internal/project/new.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func (p *Project) New(ctx context.Context, dir string, layout string, branch str
}
fmt.Printf("🚀 Creating service %s, layout repo is %s, please wait a moment.\n\n", p.Name, layout)
repo := base.NewRepo(layout, branch)
if err := repo.CopyTo(ctx, to, p.Path, []string{".git", ".github"}); err != nil {
if err := repo.CopyTo(ctx, to, p.Name, []string{".git", ".github"}); err != nil {
return err
}
e := os.Rename(
Expand Down
11 changes: 9 additions & 2 deletions cmd/kratos/internal/project/project.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ func run(_ *cobra.Command, args []string) {
name = args[0]
}
projectName, workingDir := processProjectParams(name, wd)
p := &Project{Name: projectName, Path: projectName}
p := &Project{Name: projectName}
done := make(chan error, 1)
go func() {
if !nomod {
Expand All @@ -79,9 +79,16 @@ func run(_ *cobra.Command, args []string) {
return
}

p.Path, err = filepath.Rel(projectRoot, filepath.Join(workingDir, projectName))
if err != nil {
done <- fmt.Errorf("🚫 failed to get relative path: %v", err)
return
}

mod, e := base.ModulePath(filepath.Join(projectRoot, "go.mod"))
if e != nil {
panic(e)
done <- fmt.Errorf("🚫 failed to parse `go.mod`: %v", e)
return
}
done <- p.Add(ctx, workingDir, repoURL, branch, mod)
}()
Expand Down
144 changes: 144 additions & 0 deletions cmd/kratos/internal/project/project_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
package project

import (
"fmt"
"go/parser"
"go/token"
"os"
"path/filepath"
"testing"

"github.com/go-kratos/kratos/cmd/kratos/v2/internal/base"
)

// TestCmdNew tests the `kratos new` command.
func TestCmdNew(t *testing.T) {
cwd := changeCurrentDir(t)
projectName := "helloworld"

// create a new project
CmdNew.SetArgs([]string{projectName})
if err := CmdNew.Execute(); err != nil {
t.Fatalf("executing command: %v", err)
}

// check that the expected files were created
for _, file := range []string{
"go.mod",
"go.sum",
"README.md",
"cmd/helloworld/main.go",
} {
if _, err := os.Stat(filepath.Join(cwd, projectName, file)); err != nil {
t.Errorf("expected file %s to exist", file)
}
}

// check that the go.mod file contains the expected module name
assertGoMod(t, filepath.Join(cwd, projectName, "go.mod"), projectName)

assertImportsInclude(t, filepath.Join(cwd, projectName, "cmd", projectName, "wire.go"), fmt.Sprintf(`"%s/internal/biz"`, projectName))
}

// TestCmdNewNoMod tests the `kratos new` command with the --nomod flag.
func TestCmdNewNoMod(t *testing.T) {
cwd := changeCurrentDir(t)

// create a new project
CmdNew.SetArgs([]string{"project"})
if err := CmdNew.Execute(); err != nil {
t.Fatalf("executing command: %v", err)
}

// add new app with --nomod flag
CmdNew.SetArgs([]string{"--nomod", "project/app/user"})
if err := CmdNew.Execute(); err != nil {
t.Fatalf("executing command: %v", err)
}

// check that the expected files were created
for _, file := range []string{
"go.mod",
"go.sum",
"README.md",
"cmd/project/main.go",
"app/user/cmd/user/main.go",
} {
if _, err := os.Stat(filepath.Join(cwd, "project", file)); err != nil {
t.Errorf("expected file %s to exist", file)
}
}

assertImportsInclude(t, filepath.Join(cwd, "project/app/user/cmd/user/wire.go"), `"project/app/user/internal/biz"`)
}

// assertImportsInclude checks that the file at path contains the expected import.
func assertImportsInclude(t *testing.T, path, expected string) {
t.Helper()

got, err := imports(path)
if err != nil {
t.Fatalf("getting imports: %v", err)
}

for _, imp := range got {
if imp == expected {
return
}
}

t.Errorf("expected imports to include %s, got %v", expected, got)
}

// imports returns the imports in the file at path.
func imports(path string) ([]string, error) {
fset := token.NewFileSet()
f, err := parser.ParseFile(fset, path, nil, parser.ImportsOnly)
if err != nil {
return nil, err
}

imports := make([]string, 0, len(f.Imports))
for _, s := range f.Imports {
imports = append(imports, s.Path.Value)
}

return imports, nil
}

// assertGoMod checks that the go.mod file contains the expected module name.
func assertGoMod(t *testing.T, path, expected string) {
t.Helper()

got, err := base.ModulePath(path)
if err != nil {
t.Fatalf("getting module path: %v", err)
}

if got != expected {
t.Errorf("expected module name %s, got %s", expected, got)
}
}

// change the working directory to the tempdir
func changeCurrentDir(t *testing.T) string {
t.Helper()

tmp := t.TempDir()

oldCWD, err := os.Getwd()
if err != nil {
t.Fatalf("getting working directory: %v", err)
}

if err := os.Chdir(tmp); err != nil {
t.Fatalf("changing working directory: %v", err)
}
t.Cleanup(func() {
if err := os.Chdir(oldCWD); err != nil {
t.Fatalf("restoring working directory: %v", err)
}
})

return tmp
}

0 comments on commit fbcf8d2

Please sign in to comment.