github.com/rgonomic/rgo@v0.2.2-0.20220708095818-4747f0905d6e/internal/rgo/rgo_test.go (about) 1 // Copyright ©2020 The rgonomic Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 package rgo 6 7 import ( 8 "bytes" 9 "flag" 10 "fmt" 11 "io/ioutil" 12 "os" 13 "os/exec" 14 "path/filepath" 15 "strings" 16 "testing" 17 18 "github.com/pkg/diff" 19 "github.com/pkg/diff/write" 20 ) 21 22 var regenerate = flag.Bool("regen", false, "regenerate golden data from current state") 23 24 func TestRgo(t *testing.T) { 25 tmpdir, err := ioutil.TempDir("", "rgo-*") 26 if err != nil { 27 t.Fatalf("failed to make temporary build directory: %v", err) 28 } 29 rgo := filepath.Join(tmpdir, "rgo") 30 cmd := exec.Command("go", "build", "-o", rgo, "github.com/rgonomic/rgo") 31 out, err := cmd.CombinedOutput() 32 if err != nil { 33 t.Fatalf("failed to build rgo: %v\n%s", err, out) 34 } 35 t.Cleanup(func() { 36 err := os.RemoveAll(tmpdir) 37 if err != nil { 38 t.Fatalf("failed to clean up build directory: %v", err) 39 } 40 }) 41 42 dir, err := os.Open("testdata") 43 if err != nil { 44 t.Fatalf("failed open testdata directory: %v", err) 45 } 46 infos, err := dir.Readdir(-1) 47 if err != nil { 48 t.Fatalf("failed open get package names: %v", err) 49 } 50 for _, fi := range infos { 51 if !fi.IsDir() { 52 continue 53 } 54 pkg := fi.Name() 55 t.Run("init:"+pkg, func(t *testing.T) { 56 cmd := exec.Command(rgo, "init", fmt.Sprintf("-dry-run=%t", !*regenerate)) 57 cmd.Dir = filepath.Join("testdata", pkg) 58 var buf bytes.Buffer 59 cmd.Stdout = &buf 60 err := cmd.Run() 61 if err != nil { 62 t.Fatalf("failed to run rgo init: %v", err) 63 } 64 65 if *regenerate { 66 return 67 } 68 69 got := buf.Bytes() 70 want, err := ioutil.ReadFile(filepath.Join("testdata", pkg, "rgo.json")) 71 if err != nil { 72 t.Fatalf("failed to read golden data: %v", err) 73 } 74 75 if !bytes.Equal(got, want) { 76 t.Errorf("unexpected rgo.json:\ngot:\n%s\nwant:\n%s", got, want) 77 } 78 }) 79 80 t.Run("build:"+pkg, func(t *testing.T) { 81 if strings.Contains(pkg, "uintptr") { 82 t.Skipf("skipping unhandled type %q", "uintptr") 83 } 84 cmd := exec.Command(rgo, "build", "-dry-run") 85 cmd.Dir = filepath.Join("testdata", pkg) 86 var buf bytes.Buffer 87 cmd.Stdout = &buf 88 err := cmd.Run() 89 if err != nil { 90 t.Fatalf("failed to run rgo build: %v", err) 91 } 92 93 got := buf.Bytes() 94 golden := filepath.Join("testdata", pkg, "golden.txtar") 95 if *regenerate { 96 err := ioutil.WriteFile(golden, got, 0o664) 97 if err != nil { 98 t.Fatalf("failed to write golden data: %v", err) 99 } 100 return 101 } 102 103 want, err := ioutil.ReadFile(golden) 104 if err != nil { 105 t.Fatalf("failed to read golden data: %v", err) 106 } 107 108 if !bytes.Equal(got, want) { 109 var buf bytes.Buffer 110 err := diff.Text("got", "want", got, want, &buf, write.TerminalColor()) 111 if err != nil { 112 t.Errorf("unexpected error: %v", err) 113 } 114 t.Errorf("unexpected generated code:\n%s", &buf) 115 } 116 }) 117 } 118 }