From d92baa5de580b20ff841989d42983ef43fc8bd8a Mon Sep 17 00:00:00 2001 From: retoor Date: Sat, 24 Oct 2026 04:46:35 +0200 Subject: [PATCH] fix(clients): hermetic tests ignore .env.json.local unless MOLO_LIVE_NET is set Co-Authored-By: Claude Sonnet 5.5 --- example/example.mk | 2 +- src/google/SPEC.md | 3 +++ src/google/credentials.cpp | 5 +++++ src/google/google.mk | 2 +- 4 files changed, 10 insertions(+), 2 deletions(-) diff --git a/example/example.mk b/example/example.mk index cff54dc..9dc15d8 100644 --- a/example/example.mk +++ b/example/example.mk @@ -117,7 +117,7 @@ example-test: example b=$(EXAMPLE_BUILD)/tests/$$n; \ if $(CXX) $(EXAMPLE_CXXFLAGS) -I$(EXAMPLE_DIR) -I$(EXAMPLE_TESTDIR) -Isrc \ $$t $(EXAMPLE_BUILD)/libexample.a $(EXAMPLE_REAL_LIBS) -lwolfssl -o $$b > $$b.log 2>&1; \ - then $$b >> $$b.log 2>&1; else echo "COMPILE FAILED: $$t" >> $$b.log; false; fi; \ + then MOLO_ENV_FILES=off $$b >> $$b.log 2>&1; else echo "COMPILE FAILED: $$t" >> $$b.log; false; fi; \ echo $$? > $$b.rc' < $(EXAMPLE_BUILD)/tests/list.txt pass=0; fail=0; \ for l in `ls $(EXAMPLE_BUILD)/tests/*.log | sort`; do \ diff --git a/src/google/SPEC.md b/src/google/SPEC.md index 7e5dc22..c8e6be2 100644 --- a/src/google/SPEC.md +++ b/src/google/SPEC.md @@ -58,6 +58,9 @@ client, credentials, errors, or caps. APIs that refuse keys) use the same seam: `Credentials::bearer`; a `GOOGLE_ACCESS_TOKEN` (same lookup order) takes precedence over a key. Tokens expire after about an hour; refresh is the caller's job (gap). +- Test runs set `MOLO_ENV_FILES=off`: config files are ignored so a developer's + `.env.json.local` cannot change test outcomes. `MOLO_LIVE_NET=1` re-enables + them for the opt-in live checks. - No key: `configured()` is false and every call throws `NotConfiguredError` (HTTP 503, status `NOT_CONFIGURED`) before any network traffic. Loud, not silent. diff --git a/src/google/credentials.cpp b/src/google/credentials.cpp index 046267c..06f8ba4 100644 --- a/src/google/credentials.cpp +++ b/src/google/credentials.cpp @@ -43,6 +43,11 @@ std::string from_json_file(const char* path, const char* name) { // secrets), then .env.json, both in the working directory. std::string Credentials::env(const char* name) { if (const char* v = std::getenv(name); v && *v) return v; + // Test runs set MOLO_ENV_FILES=off so a developer's .env.json.local never + // leaks into hermetic tests; opt-in live runs (MOLO_LIVE_NET) need it. + const char* files = std::getenv("MOLO_ENV_FILES"); + if (files && std::string(files) == "off" && !std::getenv("MOLO_LIVE_NET")) + return ""; std::string local = from_json_file(".env.json.local", name); if (!local.empty()) return local; return from_json_file(".env.json", name); diff --git a/src/google/google.mk b/src/google/google.mk index eb6330a..c74e4e3 100644 --- a/src/google/google.mk +++ b/src/google/google.mk @@ -48,7 +48,7 @@ $(GOOGLE_LIB): $(GOOGLE_OBJS) # The clients test harness (framework.h plus main.cpp) is reused, not copied. google-test: $(GOOGLE_TEST_BIN) - $(GOOGLE_TEST_BIN) + MOLO_ENV_FILES=off $(GOOGLE_TEST_BIN) $(GOOGLE_TEST_BIN): $(GOOGLE_OBJS) $(GOOGLE_TEST_SRCS) $(CLIENTS_LIB) $(OPERABILITY_LIB) $(ROOT)/tests/src/clients/main.cpp @mkdir -p $(dir $@)