github.com/d0sbit/gocode@v0.0.0-20211001144653-a968ce917518/cmd/gocode_sqlcrud/main_test.go (about) 1 package main 2 3 import ( 4 "flag" 5 "io/ioutil" 6 "os" 7 "os/exec" 8 "path/filepath" 9 "testing" 10 ) 11 12 func TestMaineExec(t *testing.T) { 13 14 // tests with execution of `go test` - requires "docker run ..." etc to work 15 16 var modDir string 17 modDir = t.TempDir() 18 modDir, _ = ioutil.TempDir("", "TestMaine") 19 t.Logf("modDir: %s", modDir) 20 must(t, os.MkdirAll(filepath.Join(modDir, "migrations"), 0755)) 21 must(t, os.WriteFile(filepath.Join(modDir, "migrations/20210101000000_temp.sql"), []byte(` 22 -- +goose Up 23 CREATE TABLE a ( 24 id varchar(128) NOT NULL, 25 name varchar(255) NOT NULL, 26 PRIMARY KEY(id) 27 ); 28 29 -- +goose Down 30 DROP TABLE a; 31 `), 0644)) 32 must(t, os.Mkdir(filepath.Join(modDir, "a"), 0755)) 33 must(t, os.WriteFile(filepath.Join(modDir, "go.mod"), []byte("module test1\n"), 0644)) 34 must(t, os.WriteFile(filepath.Join(modDir, "a/types.go"), []byte(`package a 35 36 type A struct { 37 ID string `+"`db:\"id\"`"+` 38 Name string `+"`db:\"name\"`"+` 39 } 40 41 func (a *A) IDAssign() { a.ID = IDString() } 42 43 `), 0644)) 44 45 // run the code generator 46 must(t, os.Chdir(modDir)) 47 flset := flag.NewFlagSet(os.Args[0], flag.PanicOnError) 48 ret := maine(flset, []string{"-package=a", "-type=A"}) 49 if ret != 0 { 50 t.Errorf("ret = %d", ret) 51 } 52 53 cmd := exec.Command("go", "get", "./...") 54 cmd.Dir = modDir 55 b, err := cmd.CombinedOutput() 56 t.Logf("go get cmd output: %s", b) 57 must(t, err) 58 59 cmd = exec.Command("go", "test", "-v", "./...") 60 cmd.Dir = modDir 61 b, err = cmd.CombinedOutput() 62 t.Logf("test cmd output: %s", b) 63 must(t, err) 64 65 } 66 67 // func TestMaineDryRun(t *testing.T) { 68 69 // var modDir string 70 // modDir = t.TempDir() 71 // // modDir, _ = ioutil.TempDir("", "TestMaine") 72 // t.Logf("modDir: %s", modDir) 73 // must(t, os.Mkdir(filepath.Join(modDir, "a"), 0755)) 74 // must(t, os.WriteFile(filepath.Join(modDir, "go.mod"), []byte("module test1\n"), 0644)) 75 // must(t, os.WriteFile(filepath.Join(modDir, "a/types.go"), []byte(`package a 76 77 // import "go.mongodb.org/mongo-driver/bson/primitive" 78 79 // type A struct { 80 // ID primitive.ObjectID `+"`bson:\"_id\"`"+` 81 // Name string `+"`bson:\"name\"`"+` 82 // } 83 // `), 0644)) 84 85 // // run the code generator 86 // must(t, os.Chdir(modDir)) 87 // flset := flag.NewFlagSet(os.Args[0], flag.PanicOnError) 88 // ret := maine(flset, []string{"-package=a", "-type=A", "-dry-run=html", "-json"}) 89 // if ret != 0 { 90 // t.Errorf("ret = %d", ret) 91 // } 92 93 // } 94 95 // func TestMaineSubDirs(t *testing.T) { 96 97 // // make sure subdirs in the package folder don't cause an issue 98 99 // var modDir string 100 // modDir = t.TempDir() 101 // // modDir, _ = ioutil.TempDir("", "TestMaine") 102 // t.Logf("modDir: %s", modDir) 103 // must(t, os.Mkdir(filepath.Join(modDir, "a"), 0755)) 104 // must(t, os.Mkdir(filepath.Join(modDir, "a/tmp"), 0755)) 105 // must(t, os.WriteFile(filepath.Join(modDir, "go.mod"), []byte("module test1\n"), 0644)) 106 // must(t, os.WriteFile(filepath.Join(modDir, "a/types.go"), []byte(`package a 107 108 // import "go.mongodb.org/mongo-driver/bson/primitive" 109 110 // type A struct { 111 // ID primitive.ObjectID `+"`bson:\"_id\"`"+` 112 // Name string `+"`bson:\"name\"`"+` 113 // } 114 // `), 0644)) 115 116 // // run the code generator 117 // must(t, os.Chdir(modDir)) 118 // flset := flag.NewFlagSet(os.Args[0], flag.PanicOnError) 119 // ret := maine(flset, []string{"-package=a", "-type=A", "-dry-run=html", "-json"}) 120 // if ret != 0 { 121 // t.Errorf("ret = %d", ret) 122 // } 123 124 // } 125 126 // func TestMaineOneDir(t *testing.T) { 127 128 // // confirm that it works when the package dir is empty 129 // // and everything is directly in the module dir 130 131 // var modDir string 132 // modDir = t.TempDir() 133 // // modDir, _ = ioutil.TempDir("", "TestMaine") 134 // t.Logf("modDir: %s", modDir) 135 // must(t, os.WriteFile(filepath.Join(modDir, "go.mod"), []byte("module test1\n"), 0644)) 136 // must(t, os.WriteFile(filepath.Join(modDir, "types.go"), []byte(`package a 137 138 // import "go.mongodb.org/mongo-driver/bson/primitive" 139 140 // type A struct { 141 // ID primitive.ObjectID `+"`bson:\"_id\"`"+` 142 // Name string `+"`bson:\"name\"`"+` 143 // } 144 // `), 0644)) 145 146 // // run the code generator 147 // must(t, os.Chdir(modDir)) 148 // flset := flag.NewFlagSet(os.Args[0], flag.PanicOnError) 149 // ret := maine(flset, []string{"-package=.", "-type=A", "-dry-run=html", "-json"}) 150 // if ret != 0 { 151 // t.Errorf("ret = %d", ret) 152 // } 153 154 // } 155 156 func must(t *testing.T, err error) { 157 t.Helper() 158 if err != nil { 159 t.Fatal(err) 160 } 161 }