func testConfig(name string, t *testing.T) { obj, err := configParser().ReadFile(name) if err != nil { t.Fatal(err) } lowLevelConf, err := serverconfig.GenLowLevelConfig(&serverconfig.Config{Obj: obj}) if err != nil { t.Fatalf("test %s: GenLowLevelConfig: %v", name, err) } baseName := strings.Replace(filepath.Base(name), ".json", "", 1) wantFile := strings.Replace(name, ".json", "-want.json", 1) wantConf, err := configParser().ReadFile(wantFile) if err != nil { t.Fatalf("test %s: ReadFile: %v", name, err) } var got, want bytes.Buffer prettyPrint(&got, lowLevelConf.Obj, 0) prettyPrint(&want, wantConf, 0) if got.String() != want.String() { tempGot := tempFile(baseName+"-got", got.Bytes()) tempWant := tempFile(baseName+"-want", want.Bytes()) defer os.Remove(tempGot.Name()) defer os.Remove(tempWant.Name()) diff, err := exec.Command("diff", "-u", tempWant.Name(), tempGot.Name()).Output() if err != nil { t.Logf("test %s diff failure: %v", name, err) } t.Errorf("test %s configurations differ.\nGot:\n%s\nWant:\n%s\nDiff:\n%s", name, &got, &want, diff) } }
func testConfig(name string, t *testing.T) { obj, err := configParser().ReadFile(name) if err != nil { t.Fatal(err) } wantedError := func() error { slurp, err := ioutil.ReadFile(strings.Replace(name, ".json", ".err", 1)) if os.IsNotExist(err) { return nil } if err != nil { t.Fatalf("Error reading .err file: %v", err) } return errors.New(string(slurp)) } lowLevelConf, err := serverconfig.GenLowLevelConfig(&serverconfig.Config{Obj: obj}) if g, w := strings.TrimSpace(fmt.Sprint(err)), strings.TrimSpace(fmt.Sprint(wantedError())); g != w { t.Fatalf("test %s: got GenLowLevelConfig error %q; want %q", name, g, w) } if err != nil { return } wantFile := strings.Replace(name, ".json", "-want.json", 1) wantConf, err := configParser().ReadFile(wantFile) if err != nil { t.Fatalf("test %s: ReadFile: %v", name, err) } var got, want bytes.Buffer prettyPrint(t, &got, lowLevelConf.Obj, 0) prettyPrint(t, &want, wantConf, 0) if got.String() != want.String() { if *updateGolden { contents, err := json.MarshalIndent(lowLevelConf.Obj, "", "\t") if err != nil { t.Fatal(err) } contents = canonicalizeGolden(t, contents) if err := ioutil.WriteFile(wantFile, contents, 0644); err != nil { t.Fatal(err) } } t.Errorf("test %s configurations differ.\nGot:\n%s\nWant:\n%s\nDiff (got -> want), %s:\n%s", name, &got, &want, name, test.Diff(got.Bytes(), want.Bytes())) } }
func testConfig(name string, t *testing.T) { obj, err := configParser().ReadFile(name) if err != nil { t.Fatal(err) } wantedError := func() error { slurp, err := ioutil.ReadFile(strings.Replace(name, ".json", ".err", 1)) if os.IsNotExist(err) { return nil } if err != nil { t.Fatalf("Error reading .err file: %v", err) } return errors.New(string(slurp)) } lowLevelConf, err := serverconfig.GenLowLevelConfig(&serverconfig.Config{Obj: obj}) if g, w := strings.TrimSpace(fmt.Sprint(err)), strings.TrimSpace(fmt.Sprint(wantedError())); g != w { t.Fatalf("test %s: got GenLowLevelConfig error %q; want %q", name, g, w) } if err != nil { return } baseName := strings.Replace(filepath.Base(name), ".json", "", 1) wantFile := strings.Replace(name, ".json", "-want.json", 1) wantConf, err := configParser().ReadFile(wantFile) if err != nil { t.Fatalf("test %s: ReadFile: %v", name, err) } var got, want bytes.Buffer prettyPrint(&got, lowLevelConf.Obj, 0) prettyPrint(&want, wantConf, 0) if got.String() != want.String() { tempGot := tempFile(baseName+"-got", got.Bytes()) tempWant := tempFile(baseName+"-want", want.Bytes()) defer os.Remove(tempGot.Name()) defer os.Remove(tempWant.Name()) diff, err := exec.Command("diff", "-u", tempWant.Name(), tempGot.Name()).Output() if err != nil { t.Logf("test %s diff failure: %v", name, err) } t.Errorf("test %s configurations differ.\nGot:\n%s\nWant:\n%s\nDiff:\n%s", name, &got, &want, diff) } }