Skip to content

cmd/mcp_test: TestMCP_StartWriteable leaks FUNC_ENABLE_MCP_WRITE environment variable #3686

@Elvand-Lie

Description

@Elvand-Lie

Bug

TestMCP_StartWriteable in cmd/mcp_test.go (line 54) sets the FUNC_ENABLE_MCP_WRITE environment variable using os.Setenv but never cleans it up:

_ = os.Setenv("FUNC_ENABLE_MCP_WRITE", "true")

Since Go runs all tests in a package within the same process, this polluted environment variable persists for every subsequent test in the cmd package.

Impact

  • Creates a fragile dependency on test execution order
  • Running with go test -shuffle=on could cause flaky failures if a future test checks this variable
  • The first assertion in the same test function (testing the default-off behavior, lines 40-51) only passes because it runs before the Setenv call, not because the env var is actually unset

Fix

Replace os.Setenv with t.Setenv, which automatically restores the original value when the test finishes. This also allows removing the now-unused "os" import.

 import (
 	"context"
-	"os"
 	"testing"

 	fn "knative.dev/func/pkg/functions"
 	"knative.dev/func/pkg/mock"
 	. "knative.dev/func/pkg/testing"
 )
 	// Ensure it is set to true on proper truthy value
-	_ = os.Setenv("FUNC_ENABLE_MCP_WRITE", "true")
+	t.Setenv("FUNC_ENABLE_MCP_WRITE", "true")

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions