Skip to content

Commit

Permalink
Fixes for tests on windows.
Browse files Browse the repository at this point in the history
* Converted some test data to using filepath.Join()
* GCS paths no longer assert using filepath.Join()
* REPL tests for file paths assert on the error to avoid os output differences, see:

golang/go#46734

PiperOrigin-RevId: 646274646
  • Loading branch information
evan-gordon authored and copybara-github committed Jun 25, 2024
1 parent 56c40f3 commit 9513400
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 6 deletions.
40 changes: 35 additions & 5 deletions cmd/repl/repl_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@
package main

import (
"errors"
"os"
"path/filepath"
"strings"
"testing"
)
Expand All @@ -34,14 +37,14 @@ func TestValidateFlags(t *testing.T) {
},
{
name: "cql file suffix is valid",
cqlInputText: "/tmp/cql.cql",
cqlInputText: filepath.Join("tmp", "cql.cql"),
bundleFileText: "",
valuesetDirText: "",
},
{
name: "bundle json suffix is valid",
cqlInputText: "",
bundleFileText: "/tmp/bundle.json",
bundleFileText: filepath.Join("tmp", "bundle.json"),
valuesetDirText: "",
},
{
Expand Down Expand Up @@ -70,23 +73,23 @@ func TestValidateFlagsError(t *testing.T) {
}{
{
name: "cql file without cql suffix returns error",
cqlInputText: "/tmp/cql.txt",
cqlInputText: filepath.Join("tmp", "cql.txt"),
bundleFileText: "",
valuesetDirText: "",
wantErr: "--cql_file flag is required to be a valid .cql",
},
{
name: "bundle json file without json suffix returns error",
cqlInputText: "",
bundleFileText: "/tmp/bundle.txt",
bundleFileText: filepath.Join("tmp", "bundle.txt"),
valuesetDirText: "",
wantErr: "--bundle_file when specified, is required to be a valid json file",
},
{
name: "invalid directory returns error",
cqlInputText: "",
bundleFileText: "",
valuesetDirText: "/my/fake/dir",
valuesetDirText: filepath.Join("my", "fake", "dir"),
wantErr: "no such file or directory",
},
}
Expand All @@ -98,3 +101,30 @@ func TestValidateFlagsError(t *testing.T) {
})
}
}

func TestValidateFlagPathError(t *testing.T) {
// Path errors can return different text for windows and linux, so we need to assert on the error
// itself, not the error text.
tests := []struct {
name string
cqlInputText string
bundleFileText string
valuesetDirText string
wantErr error
}{
{
name: "invalid directory returns not exist error",
cqlInputText: "",
bundleFileText: "",
valuesetDirText: filepath.Join("my", "fake", "dir"),
wantErr: os.ErrNotExist,
},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
if err := validateFlags(tc.cqlInputText, tc.bundleFileText, tc.valuesetDirText); !errors.Is(err, tc.wantErr) {
t.Errorf("validateFlags() returned unexpected error (-want +got):\n%s, %s", tc.wantErr, err.Error())
}
})
}
}
4 changes: 3 additions & 1 deletion internal/iohelpers/files_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -208,9 +208,11 @@ func TestWriteFileGCS(t *testing.T) {
}
}

// gcsPath returns the full GCS path for a given suffixPath.
// Since these are not real file paths, we don't need to use filepath.Join.
func gcsPath(t *testing.T, suffixPath string) string {
t.Helper()
return "gs://" + filepath.Join(testBucketName, suffixPath)
return "gs://" + testBucketName + "/" + suffixPath
}

func gcsObject(t *testing.T, content string) testhelpers.GCSObjectEntry {
Expand Down

0 comments on commit 9513400

Please sign in to comment.