From 1319fa3cde8323429f43543a161f5f918a973c88 Mon Sep 17 00:00:00 2001 From: ihopenre-eng <247072151+ihopenre-eng@users.noreply.github.com> Date: Tue, 21 Jul 2026 23:15:34 +0700 Subject: [PATCH] test(utils): fix tests that assert nothing and run them in CI The utils package is not part of `make test`, which runs `./pkg/...` only, so its tests never ran in CI. Three of them fail today. TestValidateFilePaths and TestReadFile create a temp fixture and remove it with `defer` while their subtests call `t.Parallel()`. A parallel subtest is paused until its parent returns, so the deferred cleanup deletes the fixture before any subtest reads it, and the "valid file path" cases assert an error that only proves the file is gone. This fails on every platform: --- FAIL: TestValidateFilePaths (0.01s) --- FAIL: TestReadFile (0.00s) Move both to t.Cleanup, which runs after parallel subtests finish, the pattern TestFindFileInDir in this same file already uses. TestGetAbsPath additionally fails on Windows: "/absolute/path" has no volume name, so filepath.IsAbs reports false for it and GetAbsPath joins it with baseDir. Pick the absolute path per platform. Add ./utils/... to the test target so these stay honest. Signed-off-by: ihopenre-eng <247072151+ihopenre-eng@users.noreply.github.com> --- Makefile | 2 +- utils/utils_test.go | 20 ++++++++++++++++---- 2 files changed, 17 insertions(+), 5 deletions(-) diff --git a/Makefile b/Makefile index c6b853a233..2eb792f2b4 100644 --- a/Makefile +++ b/Makefile @@ -147,7 +147,7 @@ test-deps: ################################################################################ .PHONY: test test: test-deps - gotestsum --jsonfile $(TEST_OUTPUT_FILE) --format standard-quiet -- ./pkg/... $(COVERAGE_OPTS) + gotestsum --jsonfile $(TEST_OUTPUT_FILE) --format standard-quiet -- ./pkg/... ./utils/... $(COVERAGE_OPTS) ################################################################################ # E2E Tests for Kubernetes # diff --git a/utils/utils_test.go b/utils/utils_test.go index 95a0d7ec1f..c11476cbac 100644 --- a/utils/utils_test.go +++ b/utils/utils_test.go @@ -174,7 +174,9 @@ func TestGetVersionAndImageVariant(t *testing.T) { func TestValidateFilePaths(t *testing.T) { dirName := createTempDir(t, "test_validate_paths") - defer cleanupTempDir(t, dirName) + t.Cleanup(func() { + cleanupTempDir(t, dirName) + }) validFile := createTempFile(t, dirName, "valid_test_file.yaml") testcases := []struct { name string @@ -212,6 +214,14 @@ func TestGetAbsPath(t *testing.T) { assert.NoError(t, err) baseDir := filepath.Dir(ex) + // An absolute path is platform specific: on Windows a rooted path such as + // "/absolute/path" carries no volume name, so filepath.IsAbs reports false + // for it and GetAbsPath joins it with baseDir instead of returning it. + absolutePath := "/absolute/path" + if runtime.GOOS == "windows" { + absolutePath = `C:\absolute\path` + } + testcases := []struct { name string input string @@ -234,8 +244,8 @@ func TestGetAbsPath(t *testing.T) { }, { name: "absolute path", - input: "/absolute/path", - expected: "/absolute/path", + input: absolutePath, + expected: absolutePath, }, } @@ -311,7 +321,9 @@ func TestResolveHomeDir(t *testing.T) { func TestReadFile(t *testing.T) { fileName := createTempFile(t, "", "test_read_file") - defer cleanupTempDir(t, fileName) + t.Cleanup(func() { + cleanupTempDir(t, fileName) + }) testcases := []struct { name string input string