github.com/wawandco/oxplugins@v0.7.11/tools/packr/build_test.go (about) 1 package packr 2 3 import ( 4 "bytes" 5 "context" 6 "io/ioutil" 7 "os" 8 "testing" 9 ) 10 11 func TestRunBeforeBuild(t *testing.T) { 12 t.Run("Empty", func(t *testing.T) { 13 dir := t.TempDir() 14 err := os.Chdir(dir) 15 if err != nil { 16 t.Error(err) 17 } 18 19 err = ioutil.WriteFile("go.mod", []byte("module sample/thing"), 0644) 20 if err != nil { 21 t.Error(err) 22 } 23 24 p := &Plugin{} 25 err = p.RunBeforeBuild(context.Background(), dir, []string{}) 26 if err != nil { 27 t.Error(err) 28 } 29 30 _, err = os.Stat("thing.go") 31 if os.IsNotExist(err) { 32 t.Fatal("thing.go should be generated!") 33 } 34 35 content, err := ioutil.ReadFile("thing.go") 36 if err != nil { 37 t.Error(err) 38 } 39 40 if !bytes.Contains(content, []byte("package thing")) { 41 t.Errorf("file should contain `package thing`") 42 } 43 }) 44 45 t.Run("Exists", func(t *testing.T) { 46 dir := t.TempDir() 47 err := os.Chdir(dir) 48 if err != nil { 49 t.Error(err) 50 } 51 52 err = ioutil.WriteFile("go.mod", []byte("module sample/thing"), 0644) 53 if err != nil { 54 t.Error(err) 55 } 56 57 err = ioutil.WriteFile("thing.go", []byte("package other"), 0644) 58 if err != nil { 59 t.Fatal(err) 60 } 61 62 p := &Plugin{} 63 err = p.RunBeforeBuild(context.Background(), dir, []string{}) 64 if err != nil { 65 t.Error(err) 66 } 67 68 content, err := ioutil.ReadFile("thing.go") 69 if err != nil { 70 t.Error(err) 71 } 72 73 if !bytes.Contains(content, []byte("package other")) { 74 t.Errorf("file should contain `package other`") 75 } 76 }) 77 }