unittests: Fixes unit tests for Windows (part 11)

Currently, there are some unit tests that are failing on
Windows due to various reasons:

- Cannot remove a directory if there's a file open in that directory.
- Paths may have / or \ on Windows.
This commit is contained in:
Claudiu Belu 2024-04-18 08:26:57 +00:00
parent e342ab05bb
commit e90cfb83ed
3 changed files with 12 additions and 24 deletions

View File

@ -428,6 +428,8 @@ func hasPathPrefix(path, prefix string) bool {
if prefix == "" || path == prefix {
return true
}
path = filepath.Clean(path)
prefix = filepath.Clean(prefix)
if !strings.HasSuffix(path, string(filepath.Separator)) {
prefix += string(filepath.Separator)
}

View File

@ -19,7 +19,6 @@ package main
import (
"path/filepath"
"reflect"
goruntime "runtime"
"strings"
"testing"
@ -121,10 +120,6 @@ func TestHasTestFiles(t *testing.T) {
}
func TestPackageDir(t *testing.T) {
if goruntime.GOOS == "windows" {
// TODO: remove skip once the failing test has been fixed.
t.Skip("Skip failing test on Windows.")
}
cases := []struct {
input *packages.Package
expect string
@ -134,13 +129,13 @@ func TestPackageDir(t *testing.T) {
GoFiles: []string{"/src/prj/file.go"},
IgnoredFiles: []string{"/otherdir/file.go"},
},
expect: "/src/prj",
expect: filepath.Clean("/src/prj"),
}, {
input: &packages.Package{
PkgPath: "example.com/foo/bar/qux",
IgnoredFiles: []string{"/src/prj/file.go"},
},
expect: "/src/prj",
expect: filepath.Clean("/src/prj"),
}, {
input: &packages.Package{
PkgPath: "example.com/foo/bar/qux",
@ -157,10 +152,6 @@ func TestPackageDir(t *testing.T) {
}
func TestHasPathPrefix(t *testing.T) {
if goruntime.GOOS == "windows" {
// TODO: remove skip once the failing test has been fixed.
t.Skip("Skip failing test on Windows.")
}
cases := []struct {
base string
pfx string
@ -228,10 +219,6 @@ func checkAllErrorStrings(t *testing.T, errs []error, expect []string) {
}
func TestSimpleForward(t *testing.T) {
if goruntime.GOOS == "windows" {
// TODO: remove skip once the failing test has been fixed.
t.Skip("Skip failing test on Windows.")
}
pkgs, err := loadPkgs("./testdata/simple-fwd/aaa")
if err != nil {
t.Fatalf("unexpected failure: %v", err)

View File

@ -23,7 +23,6 @@ import (
"net"
"os"
"path/filepath"
goruntime "runtime"
"testing"
"k8s.io/client-go/tools/clientcmd"
@ -203,10 +202,6 @@ func writeTestKubeconfig(t *testing.T, dir, name string, caCert *x509.Certificat
}
func TestFileExists(t *testing.T) {
if goruntime.GOOS == "windows" {
// TODO: remove skip once the failing test has been fixed.
t.Skip("Skip failing test on Windows.")
}
tmpdir, err := os.MkdirTemp("", "")
if err != nil {
t.Fatalf("Couldn't create tmpdir: %v", err)
@ -221,6 +216,10 @@ func TestFileExists(t *testing.T) {
if err != nil {
t.Fatalf("Couldn't create tmpfile: %v", err)
}
if err := tmpfile.Close(); err != nil {
t.Fatalf("Couldn't close tmpfile: %v", err)
}
tests := []struct {
name string
filename string
@ -308,10 +307,6 @@ func TestPKICertificateReadWriterExists(t *testing.T) {
}
func TestKubeConfigReadWriterExists(t *testing.T) {
if goruntime.GOOS == "windows" {
// TODO: remove skip once the failing test has been fixed.
t.Skip("Skip failing test on Windows.")
}
tmpdir, err := os.MkdirTemp("", "")
if err != nil {
t.Fatalf("Couldn't create tmpdir: %v", err)
@ -326,6 +321,10 @@ func TestKubeConfigReadWriterExists(t *testing.T) {
if err != nil {
t.Fatalf("Couldn't create tmpfile: %v", err)
}
if err := tmpfile.Close(); err != nil {
t.Fatalf("Couldn't close tmpfile: %v", err)
}
tests := []struct {
name string
kubeConfigFilePath string