From d2744ea005f31a4e84a56a42f1b4fa4bba80ca6b Mon Sep 17 00:00:00 2001 From: "Kirill Che." Date: Fri, 6 Dec 2024 13:56:08 +0400 Subject: [PATCH] fix: windows file paths separator --- .github/workflows/go.yml | 2 +- ast/file_test.go | 3 ++- ast/parser_test.go | 27 +++++++++++++++------------ ast/testhelper.go | 9 ++++++++- generator_test.go | 3 +-- 5 files changed, 27 insertions(+), 17 deletions(-) diff --git a/.github/workflows/go.yml b/.github/workflows/go.yml index 216715e..15d0a61 100644 --- a/.github/workflows/go.yml +++ b/.github/workflows/go.yml @@ -24,7 +24,7 @@ jobs: - name: Build run: go build -v ./... - name: Test - run: go test -v ./... + run: go test -v -test.v ./... - name: Check examples run: | ./_examples/clean.sh diff --git a/ast/file_test.go b/ast/file_test.go index aa66f6b..2c4adb6 100644 --- a/ast/file_test.go +++ b/ast/file_test.go @@ -2,12 +2,13 @@ package ast import ( "go/ast" + "path/filepath" "testing" ) func TestFileVisitor(t *testing.T) { fset, pkg, docs := loadTestFileSet(t) - fh, fv, file := testFileVisitor(fset, pkg, "testdata/onetype.go", docs) + fh, fv, file := testFileVisitor(t, fset, pkg, filepath.Join("testdata", "onetype.go"), docs) ast.Walk(fv, file) types := make([]*TypeSpec, 0) diff --git a/ast/parser_test.go b/ast/parser_test.go index b131948..9e66862 100644 --- a/ast/parser_test.go +++ b/ast/parser_test.go @@ -3,7 +3,6 @@ package ast import ( "fmt" "os" - "path" "path/filepath" "strings" "testing" @@ -14,7 +13,7 @@ import ( ) func TestDataParser(t *testing.T) { - files, err := filepath.Glob("testdata/parser/*.txtar") + files, err := filepath.Glob(filepath.Join("testdata", "parser", "*.txtar")) if err != nil { t.Fatalf("failed to list testdata files: %s", err) } @@ -23,11 +22,17 @@ func TestDataParser(t *testing.T) { t.Fatal("no testdata files found") } + // normalize files + for i, file := range files { + files[i] = filepath.Clean(file) + } + for _, file := range files { file := file t.Run(filepath.Base(file), func(t *testing.T) { - t.Parallel() + // TODO: enable parallel tests after fixing #43 + // t.Parallel() ar, err := txtar.ParseFile(file) if err != nil { @@ -35,9 +40,7 @@ func TestDataParser(t *testing.T) { } dir := t.TempDir() - if err := extractTxtar(ar, dir); err != nil { - t.Fatalf("failed to extract txtar: %s", err) - } + extractTxtar(t, ar, dir) tc := readTestCase(t, dir) testParser(t, dir, tc) @@ -305,25 +308,25 @@ func checkTypeRef(t *testing.T, prefix string, expect, res *FieldTypeRef) { } } -//--- +func extractTxtar(t *testing.T, ar *txtar.Archive, dir string) { + t.Helper() -func extractTxtar(ar *txtar.Archive, dir string) error { for _, file := range ar.Files { name := filepath.Join(dir, file.Name) + t.Logf("Extracting %q to %q", file.Name, name) if err := os.MkdirAll(filepath.Dir(name), 0o777); err != nil { - return err + t.Fatalf("failed to create dir: %s", err) } if err := os.WriteFile(name, file.Data, 0o666); err != nil { - return err + t.Fatalf("failed to write file: %s", err) } } - return nil } func readTestCase(t *testing.T, dir string) parserTestCase { t.Helper() - testCaseFile, err := os.Open(path.Join(dir, "testcase.yaml")) + testCaseFile, err := os.Open(filepath.Join(dir, "testcase.yaml")) if err != nil { t.Fatalf("failed to open testcase file: %s", err) } diff --git a/ast/testhelper.go b/ast/testhelper.go index b9f0f37..ef3bd18 100644 --- a/ast/testhelper.go +++ b/ast/testhelper.go @@ -5,6 +5,7 @@ import ( "go/doc" "go/parser" "go/token" + "testing" ) type T interface { @@ -98,10 +99,16 @@ func (h *testFileHandler) onFile(f *FileSpec) interface { } //nolint:staticcheck -func testFileVisitor(fset *token.FileSet, pkg *ast.Package, fileName string, +func testFileVisitor(t *testing.T, + fset *token.FileSet, pkg *ast.Package, fileName string, docs *doc.Package, ) (*testFileHandler, *fileVisitor, *ast.File) { + t.Helper() + fileAst := pkg.Files[fileName] + if fileAst == nil { + t.Fatalf("file %q not found", fileName) + } fileTkn := fset.File(fileAst.Pos()) fileSpec := &FileSpec{ Name: fileTkn.Name(), diff --git a/generator_test.go b/generator_test.go index 2f03d31..6e5d2f1 100644 --- a/generator_test.go +++ b/generator_test.go @@ -5,7 +5,6 @@ import ( "bytes" "io" "os" - "path" "path/filepath" "strings" "testing" @@ -53,7 +52,7 @@ func TestGenerator(t *testing.T) { var out bytes.Buffer runGenerator(t, gen, spec, dir, &out) - expectFile, err := os.Open(path.Join(dir, "expect.txt")) + expectFile, err := os.Open(filepath.Join(dir, "expect.txt")) if err != nil { t.Fatalf("failed to open expect.txt: %s", err) }