github.com/wawandco/oxplugins@v0.7.11/tools/actions/generator_test.go (about) 1 package action 2 3 import ( 4 "context" 5 "fmt" 6 "io/ioutil" 7 "log" 8 "os" 9 "path/filepath" 10 "regexp" 11 "testing" 12 ) 13 14 func Test_ActionGenerator(t *testing.T) { 15 g := Generator{} 16 17 t.Run("generate action", func(t *testing.T) { 18 dir := t.TempDir() 19 modelsPath := filepath.Join(dir, "app", "actions") 20 if err := os.MkdirAll(modelsPath, os.ModePerm); err != nil { 21 t.Errorf("creating templates folder should not be error, but got %v", err) 22 } 23 24 if err := g.Generate(context.Background(), dir, []string{"generate", "action", "users"}); err != nil { 25 t.Errorf("should not be error, but got %v", err) 26 } 27 28 // Validating Files existence 29 if !g.exists(filepath.Join(modelsPath, "user.go")) { 30 t.Error("'user.go' file does not exists on the path") 31 } 32 33 if !g.exists(filepath.Join(modelsPath, "user_test.go")) { 34 t.Error("'user_test.go' file does not exists on the path") 35 } 36 }) 37 t.Run("generate action and checking the content", func(t *testing.T) { 38 dir := t.TempDir() 39 modelsPath := filepath.Join(dir, "app", "actions") 40 if err := os.MkdirAll(modelsPath, os.ModePerm); err != nil { 41 t.Errorf("creating templates folder should not be error, but got %v", err) 42 } 43 44 if err := g.Generate(context.Background(), dir, []string{"generate", "action", "users"}); err != nil { 45 t.Errorf("should not be error, but got %v", err) 46 } 47 48 // Validating Files existence 49 if !g.exists(filepath.Join(modelsPath, "user.go")) { 50 t.Error("'user.go' file does not exists on the path") 51 } 52 53 if !g.exists(filepath.Join(modelsPath, "user_test.go")) { 54 t.Error("'user_test.go' file does not exists on the path") 55 } 56 content, err := ioutil.ReadFile(filepath.Join(modelsPath, "user.go")) 57 if err != nil { 58 log.Fatal(err) 59 } 60 text := string(content) 61 matched, err := regexp.MatchString(`func User`, text) 62 63 if !matched { 64 fmt.Println(text) 65 t.Fatalf("File's content is not correct, %v", err) 66 } 67 }) 68 }