github.com/vugu/vugu@v0.3.5/gen/missing-fixer_test.go (about) 1 package gen 2 3 import ( 4 "io/ioutil" 5 "os" 6 "os/exec" 7 "path/filepath" 8 "runtime" 9 "strings" 10 "testing" 11 ) 12 13 func TestMissingFixer(t *testing.T) { 14 15 // NOTE: for more complex testing, see TestRun which is easier to add more general cases to. 16 17 tmpDir, err := ioutil.TempDir("", "TestMissingFixer") 18 if err != nil { 19 t.Fatal(err) 20 } 21 defer os.RemoveAll(tmpDir) 22 23 must := func(err error) { 24 if err != nil { 25 _, file, line, _ := runtime.Caller(1) 26 t.Fatalf("from %s:%d: %v", file, line, err) 27 } 28 } 29 vuguAbs, _ := filepath.Abs("..") 30 31 must(ioutil.WriteFile(filepath.Join(tmpDir, "go.mod"), []byte("module missingfixertest\n\nreplace github.com/vugu/vugu => "+vuguAbs+"\n"), 0644)) 32 must(ioutil.WriteFile(filepath.Join(tmpDir, "events.go"), []byte("package main\n\n//vugugen:event Something\n//vugugen:event SomeOtherThing\n//vugugen:event SomeOtherThing\n"), 0644)) 33 must(ioutil.WriteFile(filepath.Join(tmpDir, "root.vugu"), []byte("<div>root</div>"), 0644)) 34 must(ioutil.WriteFile(filepath.Join(tmpDir, "root_vgen.go"), []byte("package main\n\nimport \"github.com/vugu/vugu\"\n\nfunc (c *Root)Build(vgin *vugu.BuildIn) (vgout *vugu.BuildOut) {return nil}"), 0644)) 35 // a second component that does include it's own struct definition 36 must(ioutil.WriteFile(filepath.Join(tmpDir, "comp1.vugu"), []byte("<div>comp1</div>"), 0644)) 37 must(ioutil.WriteFile(filepath.Join(tmpDir, "comp1_vgen.go"), []byte("package main\n\nimport \"github.com/vugu/vugu\"\n\ntype Comp1 struct{}\n\nfunc (c *Comp1)Build(vgin *vugu.BuildIn) (vgout *vugu.BuildOut) {return nil}"), 0644)) 38 // a file with an event where the event type is declared but not the handler interface or func 39 must(ioutil.WriteFile(filepath.Join(tmpDir, "epart.go"), []byte("package main\n\n//vugugen:event Part\ntype PartEvent struct { A string }\n"), 0644)) 40 41 // // TEMP 42 // must(ioutil.WriteFile(filepath.Join(tmpDir, "root_vgen.go"), []byte(` 43 // package main 44 45 // import "github.com/vugu/vugu" 46 47 // type Root struct {} 48 49 // func (c *Root)Build(vgin *vugu.BuildIn) (vgout *vugu.BuildOut) {return nil} 50 51 // `), 0644)) 52 53 mf := newMissingFixer(tmpDir, "main", map[string]string{ 54 "root.vugu": "root_vgen.go", 55 // "comp1.vugu": "comp1_vgen.go", 56 }) 57 err = mf.run() 58 if err != nil { 59 t.Fatal(err) 60 } 61 62 b, err := ioutil.ReadFile(filepath.Join(tmpDir, "0_missing_vgen.go")) 63 must(err) 64 t.Logf("0_missing_vgen.go result:\n%s", b) 65 s := string(b) 66 checks := []string{ 67 "type Root struct", 68 69 "!type Comp1 struct", 70 71 "type SomethingEvent struct", 72 "type SomethingHandler interface", 73 "type SomethingFunc func", 74 "func (f SomethingFunc) SomethingHandle(", 75 "var _ SomethingHandler =", 76 77 // "type PartEvent struct", // should exist only in epart.go 78 79 // "type SomeOtherThingEvent interface", 80 // "type SomeOtherThingHandler interface", 81 // "type SomeOtherThingHandlerFunc func", 82 // "func (f SomeOtherThingHandlerFunc) SomeOtherThingHandle(", 83 // "var _ SomeOtherThingHandler =", 84 } 85 for _, check := range checks { 86 if check[0] == '!' { 87 if strings.Contains(s, check[1:]) { 88 t.Errorf("found unexpected %q", check[1:]) 89 } 90 } else if !strings.Contains(s, check) { 91 t.Errorf("missing %q", check) 92 } 93 } 94 95 if t.Failed() { 96 return 97 } 98 // if the above worked, try compiling it 99 must(ioutil.WriteFile(filepath.Join(tmpDir, "main.go"), []byte("package main\n\nfunc main(){}\n"), 0644)) 100 101 cmd := exec.Command("go", "mod", "tidy") 102 cmd.Dir = tmpDir 103 cmd.CombinedOutput() 104 cmd = exec.Command("go", "build", "-o", "a.out", ".") 105 cmd.Dir = tmpDir 106 b, err = cmd.CombinedOutput() 107 if err != nil { 108 t.Logf("build output: %s", b) 109 t.Fatal(err) 110 } 111 112 // ensure the output is there 113 _, err = os.Stat(filepath.Join(tmpDir, "a.out")) 114 if err != nil { 115 t.Fatal(err) 116 } 117 118 }