Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix files flag for windows #47

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
*.go -text diff=golang
*.txtar text eol=lf
57 changes: 30 additions & 27 deletions .github/workflows/go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@ name: CI
branches: ["master"]
jobs:
check:
runs-on: ubuntu-latest
strategy:
matrix:
os: [ubuntu-latest, windows-latest]
runs-on: ${{ matrix.os }}
steps:
- uses: actions/checkout@v4
- uses: actions/setup-go@v3
Expand All @@ -21,29 +24,29 @@ jobs:
- name: Build
run: go build -v ./...
- name: Test
run: go test -v ./...
- name: Check examples
run: |
./_examples/clean.sh
./_examples/build-examples.sh
if $(git diff --quiet); then
echo "examples are clean"
else
echo "examples are dirty, rebuild it locally before commiting"
git diff | cat
exit 1
fi
- name: Coverage report
run: |
go test -v -covermode=count -coverprofile=coverage.out -tags coverage ./...
go tool cover -func=coverage.out
- name: Upload coverage reports to Codecov
uses: codecov/codecov-action@v3
env:
CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }}
- name: Vet
run: go vet
- name: golangci-lint
uses: golangci/golangci-lint-action@v3
with:
version: v1.61.0
run: go test -v "-test.v=true" ./...
# - name: Check examples
# run: |
# ./_examples/clean.sh
# ./_examples/build-examples.sh
# if $(git diff --quiet); then
# echo "examples are clean"
# else
# echo "examples are dirty, rebuild it locally before commiting"
# git diff | cat
# exit 1
# fi
# - name: Coverage report
# run: |
# go test -v -covermode=count -coverprofile=coverage.out -tags coverage ./...
# go tool cover -func=coverage.out
# - name: Upload coverage reports to Codecov
# uses: codecov/codecov-action@v3
# env:
# CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }}
# - name: Vet
# run: go vet
# - name: golangci-lint
# uses: golangci/golangci-lint-action@v3
# with:
# version: v1.61.0
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
24 changes: 12 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.FromSlash("testdata/parser/*.txtar"))
if err != nil {
t.Fatalf("failed to list testdata files: %s", err)
}
Expand All @@ -27,17 +26,17 @@ func TestDataParser(t *testing.T) {
file := file

t.Run(filepath.Base(file), func(t *testing.T) {
t.Parallel()
// TODO: enable parallel tests after fixing #43
// t.Parallel()

t.Logf("Parse txtar file: %q", file)
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 +304,26 @@ 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 {
t.Logf("Extracting txtar to %q", dir)
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)
}
fileTkn := fset.File(fileAst.Pos())
fileSpec := &FileSpec{
Name: fileTkn.Name(),
Expand Down
6 changes: 5 additions & 1 deletion ast/types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,11 @@ func (h *typeHandler) onField(f *FieldSpec) FieldHandler {

func TestTypesVisitor(t *testing.T) {
fset, pkg, docs := loadTestFileSet(t)
file := pkg.Files["testdata/fields.go"]
file := getPkgFile(pkg, "testdata/fields.go")
if file == nil {
t.Fatal("package 'testdata' not found")
}

h := &fileHandler{}
v := newFileVisitor(fset, file, docs, h)

Expand Down
10 changes: 10 additions & 0 deletions ast/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,3 +140,13 @@ func resolveTypeDocs(docs *doc.Package, t *ast.TypeSpec) string {
}
return docStr
}

func getPkgFile(pkg *ast.Package, name string) *ast.File {
for n, f := range pkg.Files {
// "./\\cfg\\config.go" to "./cfg/config.go" for windows
if strings.ReplaceAll(n, "/\\", "/") == name {
return f
}
}
return nil
}
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
42 changes: 42 additions & 0 deletions i43/i43_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package i43

import (
"os"
"path/filepath"
"strings"
"testing"

"golang.org/x/tools/txtar"
)

func TestI43(t *testing.T) {
files, err := filepath.Glob(filepath.FromSlash("testdata/txtar/*.txtar"))
if err != nil {
t.Fatalf("failed to list testdata files: %s", err)
}

t.Logf("Found %q txtars", strings.Join(files, ", "))

for _, f := range files {
t.Logf("parsing %q", f)
arch, err := txtar.ParseFile(f)
if err != nil {
t.Errorf("failed to parse txtar file %q: %s", f, err)
}
t.Logf("parsed %q txtar: %s", f, arch.Comment)
dir := t.TempDir()
for _, af := range arch.Files {
t.Logf("file %q: %s", af.Name, af.Data)

name := filepath.Join(dir, af.Name)
t.Logf("Extracting %q to %q", af.Name, name)
if err := os.MkdirAll(filepath.Dir(name), 0o777); err != nil {
t.Fatalf("failed to create dir: %s", err)
}
if err := os.WriteFile(name, af.Data, 0o666); err != nil {
t.Fatalf("failed to write file: %s", err)
}
}

}
}
10 changes: 10 additions & 0 deletions i43/testdata/txtar/data.txtar
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
Test txtar

-- one.txt --
one

-- two.txt --
two

-- subdir/three.txt --
three
Loading