Start to integrate file based testing

This commit is contained in:
Jan Eitzinger 2023-04-25 09:27:22 +02:00
parent 5a09277d1c
commit 9299f65993

View File

@ -4,52 +4,52 @@
// license that can be found in the LICENSE file. // license that can be found in the LICENSE file.
package repository_test package repository_test
import "testing" import (
"bytes"
"go/format"
"os"
"path/filepath"
"testing"
)
func TestHandleImportFlag(t *testing.T) { func TestHandleImportFlag(t *testing.T) {
r := setupRepo(t) r := setupRepo(t)
paths, err := filepath.Glob(filepath.Join("testdata", "*.input")) tests, err := filepath.Glob(filepath.Join("testdata", "*.input"))
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
for _, path := range paths { for _, path := range tests {
_, filename := filepath.Split(path) _, filename := filepath.Split(path)
testname := filename[:len(filename)-len(filepath.Ext(path))] testname := filename[:len(filename)-len(filepath.Ext(path))]
// Each path turns into a test: the test name is the filename without the
// extension.
t.Run(testname, func(t *testing.T) {
source, err := os.ReadFile(path)
if err != nil {
t.Fatal("error reading source file:", err)
}
// >>> This is the actual code under test.
output, err := format.Source(source)
if err != nil {
t.Fatal("error formatting:", err)
}
// <<<
// Each input file is expected to have a "golden output" file, with the
// same path except the .input extension is replaced by .golden
goldenfile := filepath.Join("testdata", testname+".golden")
want, err := os.ReadFile(goldenfile)
if err != nil {
t.Fatal("error reading golden file:", err)
}
if !bytes.Equal(output, want) {
t.Errorf("\n==== got:\n%s\n==== want:\n%s\n", output, want)
}
})
}
t.Run(testname, func(t *testing.T) {
source, err := os.ReadFile(path)
if err != nil {
t.Fatal("error reading source file:", err)
}
// >>> This is the actual code under test.
output, err := format.Source(source)
if err != nil {
t.Fatal("error formatting:", err)
}
// <<<
// Each input file is expected to have a "golden output" file, with the
// same path except the .input extension is replaced by .golden
goldenfile := filepath.Join("testdata", testname+".golden")
want, err := os.ReadFile(goldenfile)
if err != nil {
t.Fatal("error reading golden file:", err)
}
if !bytes.Equal(output, want) {
t.Errorf("\n==== got:\n%s\n==== want:\n%s\n", output, want)
}
})
}
s := "../../test/repo/meta1.json:../../test/repo/data1.json" s := "../../test/repo/meta1.json:../../test/repo/data1.json"
err := HandleImportFlag(s) err := HandleImportFlag(s)