From 9299f65993324a5e920bb1f72c037e834fd4140c Mon Sep 17 00:00:00 2001 From: Jan Eitzinger Date: Tue, 25 Apr 2023 09:27:22 +0200 Subject: [PATCH] Start to integrate file based testing --- internal/repository_test/importFlag.go | 74 +++++++++++++------------- 1 file changed, 37 insertions(+), 37 deletions(-) diff --git a/internal/repository_test/importFlag.go b/internal/repository_test/importFlag.go index 4486131..2b5d737 100644 --- a/internal/repository_test/importFlag.go +++ b/internal/repository_test/importFlag.go @@ -4,52 +4,52 @@ // license that can be found in the LICENSE file. package repository_test -import "testing" +import ( + "bytes" + "go/format" + "os" + "path/filepath" + "testing" +) func TestHandleImportFlag(t *testing.T) { r := setupRepo(t) - paths, err := filepath.Glob(filepath.Join("testdata", "*.input")) - if err != nil { - t.Fatal(err) - } + tests, err := filepath.Glob(filepath.Join("testdata", "*.input")) + if err != nil { + t.Fatal(err) + } - for _, path := range paths { - _, filename := filepath.Split(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) - } - }) - } + for _, path := range tests { + _, filename := filepath.Split(path) + testname := filename[:len(filename)-len(filepath.Ext(path))] + 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" err := HandleImportFlag(s)