github.com/Desuuuu/genqlient@v0.5.3/internal/integration/util.go (about) 1 package integration 2 3 import ( 4 "bytes" 5 "fmt" 6 "os" 7 "path/filepath" 8 "runtime" 9 "testing" 10 11 "github.com/Desuuuu/genqlient/generate" 12 ) 13 14 // RepoRoot returns the root of the genqlient repository, 15 func RepoRoot(t *testing.T) string { 16 _, thisFile, _, ok := runtime.Caller(0) 17 if !ok { 18 t.Fatal("runtime.Caller non-ok") 19 } 20 21 root := filepath.Dir(filepath.Dir(filepath.Dir(thisFile))) 22 if _, err := os.Stat(filepath.Join(root, ".gitignore")); err != nil { 23 t.Fatal(fmt.Errorf("doesn't look like repo root: %v", err)) 24 } 25 return root 26 } 27 28 // RunGenerateTest checks that running genqlient with the given 29 // repo-root-relative config file would not produce any changes to the 30 // checked-in files. 31 func RunGenerateTest(t *testing.T, relConfigFilename string) { 32 configFilename := filepath.Join(RepoRoot(t), relConfigFilename) 33 config, err := generate.ReadAndValidateConfig(configFilename) 34 if err != nil { 35 t.Fatal(err) 36 } 37 38 generated, err := generate.Generate(config) 39 if err != nil { 40 t.Fatal(err) 41 } 42 43 for filename, content := range generated { 44 expectedContent, err := os.ReadFile(filename) 45 if err != nil { 46 t.Fatal(err) 47 } 48 49 if !bytes.Equal(content, expectedContent) { 50 t.Errorf("mismatch in %s", filename) 51 if testing.Verbose() { 52 t.Errorf("got:\n%s\nwant:\n%s\n", content, expectedContent) 53 } 54 } 55 } 56 }