github.com/wawandco/oxpecker-plugins@v0.1.1/tools/pop/builder_test.go (about) 1 package pop 2 3 import ( 4 "bytes" 5 "fmt" 6 "io/ioutil" 7 "os" 8 "path/filepath" 9 "testing" 10 ) 11 12 func Test_findConfig_Exists_config_folder(t *testing.T) { 13 dir := t.TempDir() 14 err := os.Chdir(dir) 15 if err != nil { 16 t.Error(err) 17 } 18 19 config := ` 20 development: 21 pool: 2 22 url: something 23 ` 24 err = os.Mkdir("config", 0700) 25 if err != nil { 26 t.Errorf("should be no error creating folder, got %v", err) 27 } 28 fmt.Println(dir) 29 30 err = ioutil.WriteFile(filepath.Join("config", "database.yml"), []byte(config), 0600) 31 if err != nil { 32 t.Error(err) 33 } 34 35 p := &Plugin{} 36 b, err := p.findConfig() 37 if err != nil { 38 t.Errorf("should be no error but got %v", err) 39 } 40 41 if !bytes.Contains(b, []byte("url: something")) { 42 t.Error("should contain url:something") 43 } 44 } 45 46 func Test_findConfig_notThere(t *testing.T) { 47 dir := t.TempDir() 48 err := os.Chdir(dir) 49 if err != nil { 50 t.Error(err) 51 } 52 53 p := &Plugin{} 54 _, err = p.findConfig() 55 if err == nil { 56 t.Errorf("should be error but got nil") 57 } 58 } 59 60 func Test_RunAfterBuild(t *testing.T) { 61 dir := t.TempDir() 62 err := os.Chdir(dir) 63 if err != nil { 64 t.Error(err) 65 } 66 67 err = os.MkdirAll("config", 0700) 68 if err != nil { 69 t.Error(err) 70 } 71 72 path := filepath.Join("config", "gen_database.go") 73 err = ioutil.WriteFile(path, []byte{}, 0600) 74 if err != nil { 75 t.Error(err) 76 } 77 78 p := &Plugin{} 79 err = p.RunAfterBuild(dir, []string{}) 80 if err != nil { 81 t.Error(err) 82 } 83 84 if _, err := os.Stat(path); err == nil { 85 t.Errorf("%v should not exist after build", path) 86 } 87 } 88 89 func Test_RunAfterBuild_NoFile(t *testing.T) { 90 dir := t.TempDir() 91 err := os.Chdir(dir) 92 if err != nil { 93 t.Error(err) 94 } 95 96 p := &Plugin{} 97 err = p.RunAfterBuild(dir, []string{}) 98 if err != nil { 99 t.Error(err) 100 } 101 102 path := filepath.Join("config", "gen_database.go") 103 if _, err := os.Stat(path); err == nil { 104 t.Errorf("%v should not exist after build", path) 105 } 106 }