Explorar el Código

fix(makefile): clean up cross-submodule go.mod churn after make test (#6378)

Co-authored-by: Gergely Bräutigam <gergely.brautigam@sap.com>
Signed-off-by: Alexander Chernov <alexander@chernov.it>
Alexander Chernov hace 3 semanas
padre
commit
a38e5b08e0
Se han modificado 2 ficheros con 64 adiciones y 2 borrados
  1. 8 2
      Makefile
  2. 56 0
      hack/modfiles.sh

+ 8 - 2
Makefile

@@ -106,9 +106,15 @@ go-work:
 	@$(OK) created go workspace
 
 .PHONY: test
-test: generate envtest go-work ## Run tests
+test: generate envtest ## Run tests
 	@$(INFO) go test unit-tests
-	KUBEBUILDER_ASSETS="$(shell $(ENVTEST) use $(KUBERNETES_VERSION) -p path --bin-dir $(LOCALBIN))" go test -tags $(PROVIDER) work -v -race -coverprofile cover.out
+	@set -e; \
+	snap=$$(mktemp); \
+	./hack/modfiles.sh snapshot "$$snap"; \
+	trap "./hack/modfiles.sh restore $$snap" EXIT INT TERM; \
+	$(MAKE) go-work; \
+	KUBEBUILDER_ASSETS="$$($(ENVTEST) use $(KUBERNETES_VERSION) -p path --bin-dir $(LOCALBIN))" \
+	    go test -tags $(PROVIDER) work -v -race -coverprofile cover.out
 	@$(OK) go test unit-tests
 
 .PHONY: test.e2e

+ 56 - 0
hack/modfiles.sh

@@ -0,0 +1,56 @@
+#!/usr/bin/env bash
+set -euo pipefail
+
+# modfiles.sh: snapshot or restore the set of dirty go.mod / go.sum files.
+#
+# Used by the `test` Makefile target to undo the cross-submodule churn that
+# `go work sync` introduces. The snapshot subcommand records which go.mod /
+# go.sum files were ALREADY modified before the test run; the restore
+# subcommand reverts any go.mod / go.sum files that became dirty during the
+# run but were not in the snapshot, leaving the developer's intentional
+# changes alone.
+#
+# Usage:
+#   hack/modfiles.sh snapshot <snapshot-file>
+#   hack/modfiles.sh restore  <snapshot-file>
+#
+# Both subcommands are no-ops when run outside a git checkout.
+
+cmd="${1:-}"
+snap="${2:-}"
+
+if [[ -z "$cmd" || -z "$snap" ]]; then
+    echo "Usage: $0 {snapshot|restore} <snapshot-file>" >&2
+    exit 2
+fi
+
+# Print every dirty path (including renames and untracked) whose basename is
+# go.mod or go.sum, one per line, sorted and unique. Tolerates non-git
+# checkouts by swallowing git's error and emitting nothing.
+list_dirty_modfiles() {
+    { git status --porcelain 2>/dev/null || true; } \
+        | awk '{print $NF}' \
+        | awk '$0 ~ /(^|\/)(go\.mod|go\.sum)$/' \
+        | sort -u
+}
+
+case "$cmd" in
+    snapshot)
+        list_dirty_modfiles > "$snap"
+        ;;
+    restore)
+        [[ -f "$snap" ]] || exit 0
+        post=$(list_dirty_modfiles)
+        if [[ -n "$post" ]]; then
+            to_restore=$(printf '%s\n' "$post" | grep -Fxvf "$snap" || true)
+            if [[ -n "$to_restore" ]]; then
+                printf '%s\n' "$to_restore" | xargs git checkout --
+            fi
+        fi
+        rm -f "$snap"
+        ;;
+    *)
+        echo "Unknown command: $cmd" >&2
+        exit 2
+        ;;
+esac