From be99461fbf5676f316664e4d06d23ea3472a495d Mon Sep 17 00:00:00 2001 From: erezrokah Date: Sun, 6 Nov 2022 15:53:37 +0200 Subject: [PATCH 1/9] test: Add tests on Mac and Windows --- .github/workflows/unittest.yml | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/.github/workflows/unittest.yml b/.github/workflows/unittest.yml index 4554ee2c1b..f9395d8c46 100644 --- a/.github/workflows/unittest.yml +++ b/.github/workflows/unittest.yml @@ -8,24 +8,31 @@ on: jobs: unitests: - runs-on: ubuntu-latest + strategy: + matrix: + os: [ubuntu-latest, windows-latest, macos-latest] + runs-on: ${{ matrix.os }} steps: - name: Check out code into the Go module directory uses: actions/checkout@v3 - name: Set up Go 1.x uses: actions/setup-go@v3 with: - go-version: 1.19 + go-version-file: "go.mod" cache: true - run: go mod download - run: go build ./... - name: Run tests + if: matrix.os != 'ubuntu-latest' + run: go test ./... + - name: Run tests + if: matrix.os == 'ubuntu-latest' run: go test -coverprofile=coverage.out ./... - name: Generate coverage report - if: always() + if: always() && matrix.os == 'ubuntu-latest' run: go tool cover -html coverage.out -o coverage.html - uses: actions/upload-artifact@v3 - if: always() + if: always() && matrix.os == 'ubuntu-latest' with: name: Code coverage path: coverage.html From 3cb0e8ee1b1c4e469f849ecc1d08384773d84f21 Mon Sep 17 00:00:00 2001 From: erezrokah Date: Sun, 6 Nov 2022 16:06:11 +0200 Subject: [PATCH 2/9] test: Fix on Windows --- specs/spec_reader_test.go | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/specs/spec_reader_test.go b/specs/spec_reader_test.go index e791db56ed..886ffcaf9f 100644 --- a/specs/spec_reader_test.go +++ b/specs/spec_reader_test.go @@ -1,6 +1,7 @@ package specs import ( + "path" "testing" ) @@ -12,32 +13,36 @@ type specLoaderTestCase struct { destinations int } +func getPath(pathParts ...string) string { + return path.Join("testdata", path.Join(pathParts...)) +} + var specLoaderTestCases = []specLoaderTestCase{ { name: "success", - path: []string{"testdata/gcp.yml", "testdata/dir"}, + path: []string{getPath("gcp.yml"), getPath("dir")}, err: "", sources: 2, destinations: 2, }, { name: "duplicate_source", - path: []string{"testdata/gcp.yml", "testdata/gcp.yml"}, + path: []string{getPath("gcp.yml"), getPath("gcp.yml")}, err: "duplicate source name gcp", }, { name: "no_such_file", - path: []string{"testdata/dir/no_such_file.yml", "testdata/dir/postgresql.yml"}, + path: []string{getPath("dir", "no_such_file.yml"), getPath("dir", "postgresql.yml")}, err: "open testdata/dir/no_such_file.yml: no such file or directory", }, { name: "duplicate_destination", - path: []string{"testdata/dir/postgresql.yml", "testdata/dir/postgresql.yml"}, + path: []string{getPath("dir", "postgresql.yml"), getPath("dir", "postgresql.yml")}, err: "duplicate destination name postgresql", }, { name: "different_versions_for_destinations", - path: []string{"testdata/gcp.yml", "testdata/gcpv2.yml"}, + path: []string{getPath("gcp.yml"), getPath("gcpv2.yml")}, err: "destination postgresqlv2 is used by multiple sources cloudquery/gcp with different versions", }, } From 25c0bd4e8eb1f3d7f9cf3194aee9dff75c7844de Mon Sep 17 00:00:00 2001 From: erezrokah Date: Sun, 6 Nov 2022 16:14:23 +0200 Subject: [PATCH 3/9] fix: Normalize config string before parsing --- specs/spec_reader.go | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/specs/spec_reader.go b/specs/spec_reader.go index 060262d187..75239e290a 100644 --- a/specs/spec_reader.go +++ b/specs/spec_reader.go @@ -20,7 +20,9 @@ func (r *SpecReader) loadSpecsFromFile(path string) error { data = []byte(os.ExpandEnv(string(data))) // support multiple yamls in one file - for _, doc := range strings.Split(string(data), "\n---\n") { + // this should work both on Windows and Unix + normalizedConfig := strings.ReplaceAll(string(data), "\r\n", "\n") + for _, doc := range strings.Split(normalizedConfig, "\n---\n") { var s Spec if err := SpecUnmarshalYamlStrict([]byte(doc), &s); err != nil { return fmt.Errorf("failed to unmarshal file %s: %w", path, err) From cf37cd5df587a27533f966d55aa051bda0f614af Mon Sep 17 00:00:00 2001 From: erezrokah Date: Sun, 6 Nov 2022 16:23:22 +0200 Subject: [PATCH 4/9] test: Fix tests --- plugins/source_docs_test.go | 2 ++ specs/spec_reader_test.go | 29 +++++++++++++++++++---------- 2 files changed, 21 insertions(+), 10 deletions(-) diff --git a/plugins/source_docs_test.go b/plugins/source_docs_test.go index 79fa65ed8b..6a66a28562 100644 --- a/plugins/source_docs_test.go +++ b/plugins/source_docs_test.go @@ -1,3 +1,5 @@ +//go:build !windows + package plugins import ( diff --git a/specs/spec_reader_test.go b/specs/spec_reader_test.go index 886ffcaf9f..767746641a 100644 --- a/specs/spec_reader_test.go +++ b/specs/spec_reader_test.go @@ -2,13 +2,14 @@ package specs import ( "path" + "runtime" "testing" ) type specLoaderTestCase struct { name string path []string - err string + err func() string sources int destinations int } @@ -21,29 +22,36 @@ var specLoaderTestCases = []specLoaderTestCase{ { name: "success", path: []string{getPath("gcp.yml"), getPath("dir")}, - err: "", + err: func() string { return "" }, sources: 2, destinations: 2, }, { name: "duplicate_source", path: []string{getPath("gcp.yml"), getPath("gcp.yml")}, - err: "duplicate source name gcp", + err: func() string { return "duplicate source name gcp" }, }, { name: "no_such_file", path: []string{getPath("dir", "no_such_file.yml"), getPath("dir", "postgresql.yml")}, - err: "open testdata/dir/no_such_file.yml: no such file or directory", + err: func() string { + if runtime.GOOS == "windows" { + return "open testdata/dir/no_such_file.yml: The system cannot find the file specified." + } + return "open testdata/dir/no_such_file.yml: no such file or directory" + }, }, { name: "duplicate_destination", path: []string{getPath("dir", "postgresql.yml"), getPath("dir", "postgresql.yml")}, - err: "duplicate destination name postgresql", + err: func() string { return "duplicate destination name postgresql" }, }, { name: "different_versions_for_destinations", path: []string{getPath("gcp.yml"), getPath("gcpv2.yml")}, - err: "destination postgresqlv2 is used by multiple sources cloudquery/gcp with different versions", + err: func() string { + return "destination postgresqlv2 is used by multiple sources cloudquery/gcp with different versions" + }, }, } @@ -51,14 +59,15 @@ func TestLoadSpecs(t *testing.T) { for _, tc := range specLoaderTestCases { t.Run(tc.name, func(t *testing.T) { specReader, err := NewSpecReader(tc.path) + expectedErr := tc.err() if err != nil { - if err.Error() != tc.err { - t.Fatalf("expected error: '%s', got: '%s'", tc.err, err) + if err.Error() != expectedErr { + t.Fatalf("expected error: '%s', got: '%s'", expectedErr, err) } return } - if tc.err != "" { - t.Fatalf("expected error: %s, got nil", tc.err) + if expectedErr != "" { + t.Fatalf("expected error: %s, got nil", expectedErr) } if len(specReader.Sources) != tc.sources { t.Fatalf("got: %d expected: %d", len(specReader.Sources), tc.sources) From 127ac0dfefbb2dcfcbd84aa203e0f08111c94a92 Mon Sep 17 00:00:00 2001 From: erezrokah Date: Sun, 6 Nov 2022 16:29:56 +0200 Subject: [PATCH 5/9] chore: Fix lint warnings --- specs/spec_reader_test.go | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/specs/spec_reader_test.go b/specs/spec_reader_test.go index 767746641a..6ca39de275 100644 --- a/specs/spec_reader_test.go +++ b/specs/spec_reader_test.go @@ -20,16 +20,20 @@ func getPath(pathParts ...string) string { var specLoaderTestCases = []specLoaderTestCase{ { - name: "success", - path: []string{getPath("gcp.yml"), getPath("dir")}, - err: func() string { return "" }, + name: "success", + path: []string{getPath("gcp.yml"), getPath("dir")}, + err: func() string { + return "" + }, sources: 2, destinations: 2, }, { name: "duplicate_source", path: []string{getPath("gcp.yml"), getPath("gcp.yml")}, - err: func() string { return "duplicate source name gcp" }, + err: func() string { + return "duplicate source name gcp" + }, }, { name: "no_such_file", @@ -44,7 +48,9 @@ var specLoaderTestCases = []specLoaderTestCase{ { name: "duplicate_destination", path: []string{getPath("dir", "postgresql.yml"), getPath("dir", "postgresql.yml")}, - err: func() string { return "duplicate destination name postgresql" }, + err: func() string { + return "duplicate destination name postgresql" + }, }, { name: "different_versions_for_destinations", From 009e2a0605ccd8c0761db29a4552e43ff7e69854 Mon Sep 17 00:00:00 2001 From: erezrokah Date: Sun, 6 Nov 2022 16:30:47 +0200 Subject: [PATCH 6/9] test: Fix tests --- codegen/template_test.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/codegen/template_test.go b/codegen/template_test.go index befd1e7d7a..1f900b6832 100644 --- a/codegen/template_test.go +++ b/codegen/template_test.go @@ -1,3 +1,5 @@ +//go:build !windows + package codegen import ( From 54d6ad17d9671aa97414103a4568e3ecd3df7b8d Mon Sep 17 00:00:00 2001 From: erezrokah Date: Sun, 6 Nov 2022 16:58:35 +0200 Subject: [PATCH 7/9] test: Fix tests --- clients/destination_test.go | 2 ++ clients/source_test.go | 2 ++ 2 files changed, 4 insertions(+) diff --git a/clients/destination_test.go b/clients/destination_test.go index a59adbbfb9..2445bcdcf4 100644 --- a/clients/destination_test.go +++ b/clients/destination_test.go @@ -1,3 +1,5 @@ +//go:build !windows + package clients import ( diff --git a/clients/source_test.go b/clients/source_test.go index e1dea405a4..1ae4d5c72d 100644 --- a/clients/source_test.go +++ b/clients/source_test.go @@ -1,3 +1,5 @@ +//go:build !windows + package clients import ( From b5049d1d55014a19b63a56d87091a9bc1a9d2ae0 Mon Sep 17 00:00:00 2001 From: Erez Rokah Date: Sun, 6 Nov 2022 17:17:39 +0200 Subject: [PATCH 8/9] Update spec_reader_test.go --- specs/spec_reader_test.go | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/specs/spec_reader_test.go b/specs/spec_reader_test.go index c0a0efe4cd..2cbf883d54 100644 --- a/specs/spec_reader_test.go +++ b/specs/spec_reader_test.go @@ -61,8 +61,10 @@ var specLoaderTestCases = []specLoaderTestCase{ }, { name: "multiple sources success", - path: []string{"testdata/multiple_sources.yml"}, - err: "", + path: []string{getPath("multiple_sources.yml")}, + err: func() string { + return "" + }, sources: 2, destinations: 1, }, From 323045daa5fd826acc57db56a06ce40c1e7946c7 Mon Sep 17 00:00:00 2001 From: erezrokah Date: Sun, 6 Nov 2022 17:22:34 +0200 Subject: [PATCH 9/9] chore: Fix go fmt --- specs/spec_reader_test.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/specs/spec_reader_test.go b/specs/spec_reader_test.go index 2cbf883d54..78c6ec0c8e 100644 --- a/specs/spec_reader_test.go +++ b/specs/spec_reader_test.go @@ -60,8 +60,8 @@ var specLoaderTestCases = []specLoaderTestCase{ }, }, { - name: "multiple sources success", - path: []string{getPath("multiple_sources.yml")}, + name: "multiple sources success", + path: []string{getPath("multiple_sources.yml")}, err: func() string { return "" },