Skip to content

Commit

Permalink
fix: windows file paths separator
Browse files Browse the repository at this point in the history
  • Loading branch information
g4s8 committed Dec 6, 2024
1 parent 95332a7 commit d2744ea
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 17 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion ast/file_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
27 changes: 15 additions & 12 deletions ast/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package ast
import (
"fmt"
"os"
"path"
"path/filepath"
"strings"
"testing"
Expand All @@ -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)
}
Expand All @@ -23,21 +22,25 @@ 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 {
t.Fatalf("failed to parse txtar file: %s", err)
}

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)
Expand Down Expand Up @@ -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)
}
Expand Down
9 changes: 8 additions & 1 deletion ast/testhelper.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"go/doc"
"go/parser"
"go/token"
"testing"
)

type T interface {
Expand Down Expand Up @@ -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)
}

Check warning on line 111 in ast/testhelper.go

View check run for this annotation

Codecov / codecov/patch

ast/testhelper.go#L110-L111

Added lines #L110 - L111 were not covered by tests
fileTkn := fset.File(fileAst.Pos())
fileSpec := &FileSpec{
Name: fileTkn.Name(),
Expand Down
3 changes: 1 addition & 2 deletions generator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"bytes"
"io"
"os"
"path"
"path/filepath"
"strings"
"testing"
Expand Down Expand Up @@ -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)
}
Expand Down

0 comments on commit d2744ea

Please sign in to comment.