github.com/mloves0824/enron/cmd/enron@v0.0.0-20230830012320-113bbf6be3c8/internal/project/project_test.go (about) 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/mloves0824/enron/cmd/enron/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 }