From 2c4c3fd7490c3d3f57598d5cb8820715b02d7b3d Mon Sep 17 00:00:00 2001
From: Sunny <darkowlzz@protonmail.com>
Date: Mon, 28 Feb 2022 08:56:08 +0530
Subject: [PATCH] test: ignore golden template files with -update

Add a template values check in the `assertGoldenTemplateFile()` function
to only update golden files if they aren't templates. A note is printed
when an update to a template golden file is needed and `-update` flag
can't update it.

Signed-off-by: Sunny <darkowlzz@protonmail.com>
---
 CONTRIBUTING.md       | 12 ++++++++++++
 Makefile              |  6 ++++--
 cmd/flux/main_test.go | 11 ++++++++---
 3 files changed, 24 insertions(+), 5 deletions(-)

diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md
index cafc531e..58ce5039 100644
--- a/CONTRIBUTING.md
+++ b/CONTRIBUTING.md
@@ -103,6 +103,18 @@ files used in the test, pass `-update` flag to the test as:
 make e2e TEST_ARGS="-update"
 ```
 
+Since not all packages use golden files for testing, `-update` argument must be
+passed only for the packages that use golden files. Use the variables
+`TEST_PKG_PATH` for unit tests and `E2E_TEST_PKG_PATH` for e2e tests, to set the
+path of the target test package:
+
+```bash
+# Unit test
+make test TEST_PKG_PATH="./cmd/flux" TEST_ARGS="-update"
+# e2e test
+make e2e E2E_TEST_PKG_PATH="./cmd/flux" TEST_ARGS="-update"
+```
+
 Teardown the e2e environment with:
 
 ```bash
diff --git a/Makefile b/Makefile
index 21149468..5fd6f05f 100644
--- a/Makefile
+++ b/Makefile
@@ -35,11 +35,13 @@ cleanup-kind:
 	rm $(TEST_KUBECONFIG)
 
 KUBEBUILDER_ASSETS?="$(shell $(ENVTEST) --arch=$(ENVTEST_ARCH) use -i $(ENVTEST_KUBERNETES_VERSION) --bin-dir=$(ENVTEST_ASSETS_DIR) -p path)"
+TEST_PKG_PATH="./..."
 test: $(EMBEDDED_MANIFESTS_TARGET) tidy fmt vet install-envtest
-	KUBEBUILDER_ASSETS="$(KUBEBUILDER_ASSETS)" go test ./... -coverprofile cover.out --tags=unit
+	KUBEBUILDER_ASSETS="$(KUBEBUILDER_ASSETS)" go test $(TEST_PKG_PATH) -coverprofile cover.out --tags=unit $(TEST_ARGS)
 
+E2E_TEST_PKG_PATH="./cmd/flux/..."
 e2e: $(EMBEDDED_MANIFESTS_TARGET) tidy fmt vet
-	TEST_KUBECONFIG=$(TEST_KUBECONFIG) go test ./cmd/flux/... -coverprofile e2e.cover.out --tags=e2e -v -failfast $(TEST_ARGS)
+	TEST_KUBECONFIG=$(TEST_KUBECONFIG) go test $(E2E_TEST_PKG_PATH) -coverprofile e2e.cover.out --tags=e2e -v -failfast $(TEST_ARGS)
 
 test-with-kind: install-envtest
 	make setup-kind
diff --git a/cmd/flux/main_test.go b/cmd/flux/main_test.go
index 5bc3f1ff..4408c8d7 100644
--- a/cmd/flux/main_test.go
+++ b/cmd/flux/main_test.go
@@ -304,10 +304,15 @@ func assertGoldenTemplateFile(goldenFile string, templateValues map[string]strin
 		if assertErr := assertGoldenValue(expectedOutput)(output, err); assertErr != nil {
 			// Update the golden files if comparision fails and the update flag is set.
 			if *update && output != "" {
-				if err := os.WriteFile(goldenFile, []byte(output), 0644); err != nil {
-					return fmt.Errorf("failed to update golden file '%s': %v", goldenFile, err)
+				// Skip update if there are template values.
+				if len(templateValues) > 0 {
+					fmt.Println("NOTE: -update flag passed but golden template files can't be updated, please update it manually")
+				} else {
+					if err := os.WriteFile(goldenFile, []byte(output), 0644); err != nil {
+						return fmt.Errorf("failed to update golden file '%s': %v", goldenFile, err)
+					}
+					return nil
 				}
-				return nil
 			}
 			return fmt.Errorf("Mismatch from golden file '%s': %v", goldenFile, assertErr)
 		}
-- 
GitLab