What changed:
- net/http reads HTTPS_PROXY and its siblings once per process, at its
first routed request. A test binary that lets the machine's stand froze
that read with the developer's shell: on a machine with https_proxy set,
whichever test reached net/http first (a plugin host's catalog read was
the earliest single one) fixed the machine's proxy into every later
route, and a test that set its own afterwards read the machine's —
TestSearchFollowsTheSearchersProxy's Auto ask follows ALL_PROXY alone
for exactly that reason, and its searcher went to a proxy that cannot
reach the test's servers (red in the full package run, green alone).
Offline's refused hosts were carried for real by the machine's proxy on
loopback, red-lining TestOffline the same way.
- testenv.ClearProxy unsets the eight variables. Isolate calls it, and so
does the gateway's own TestMain (the package isolates itself) and this
package's new TestMain. TestIsolate now sets a proxy variable and holds
Isolate to having cleared it.
Run: TestSearchFollowsTheSearchersProxy alone green, red with
TestClinePluginPinsDeepSeek run before it, green again with the fix
(TestClinePluginPinsDeepSeek|TestSearchFollowsTheSearchersProxy); without
ClearProxy the pair is red, TestOffline fails with "models.dev: <nil>"
(the request went through the machine's proxy) and TestIsolate fails with
"HTTPS_PROXY is still set"; both green with it. go test -tags nogui
./internal/testenv ./internal/gateway (the gateway package whole) green.
Review: reproduced on main (3f5702421) with https_proxy, http_proxy and
HTTPS_PROXY set to http://127.0.0.1:1 for the test run: the pair above
fails with "proxyconnect tcp: dial tcp 127.0.0.1:1" (CONNECTs []), and
TestOffline fails the same way, i.e. a vendor host went to the machine's
proxy instead of Offline's refusal. With the change both packages are ok
under the same variables; taking ClearProxy out of Isolate turns
TestIsolate red ("HTTPS_PROXY is still set"), and out of the gateway's
TestMain turns the pair red again. The gateway's is the only TestMain
that isolates itself without testenv.
Co-authored-by: factory-droid[bot] <138933559+factory-droid[bot]@users.noreply.github.com>
73 lines
2.2 KiB
Go
73 lines
2.2 KiB
Go
package testenv
|
|
|
|
import (
|
|
"os"
|
|
"path/filepath"
|
|
"testing"
|
|
)
|
|
|
|
func TestIsolate(t *testing.T) {
|
|
for _, k := range append(folderVars, "CODEX_HOME", "PATH", Marker) {
|
|
t.Setenv(k, os.Getenv(k)) // restored after the test
|
|
}
|
|
os.Setenv("CODEX_HOME", filepath.Join(t.TempDir(), "codex"))
|
|
t.Setenv("HTTPS_PROXY", "http://127.0.0.1:9") // the machine's own, which net/http would freeze into its first read
|
|
home, err := Isolate()
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
t.Cleanup(func() { remove(home) })
|
|
for _, k := range proxyVars {
|
|
if _, ok := os.LookupEnv(k); ok {
|
|
t.Errorf("%s is still set", k)
|
|
}
|
|
}
|
|
for _, f := range []func() (string, error){os.UserHomeDir, os.UserConfigDir, os.UserCacheDir} {
|
|
if p, err := f(); err != nil || !within(p, home) {
|
|
t.Errorf("%q (%v) is outside %q", p, err, home)
|
|
}
|
|
}
|
|
if _, ok := os.LookupEnv("CODEX_HOME"); ok {
|
|
t.Error("CODEX_HOME is still set")
|
|
}
|
|
remove(home)
|
|
if _, err := os.Stat(home); !os.IsNotExist(err) {
|
|
t.Errorf("the test home is still there: %v", err)
|
|
}
|
|
}
|
|
|
|
// A binary started again by a test keeps that test's homes, all in the
|
|
// temporary folder; one pointing anywhere else is refused.
|
|
func TestCheckChild(t *testing.T) {
|
|
sandbox, fixture := t.TempDir(), t.TempDir()
|
|
for _, k := range append(folderVars, "CODEX_HOME") {
|
|
t.Setenv(k, "")
|
|
}
|
|
SetHome(t, fixture)
|
|
t.Setenv("CODEX_HOME", filepath.Join(fixture, ".codex"))
|
|
if err := checkChild(sandbox); err != nil {
|
|
t.Errorf("a fixture home in the temporary folder: %v", err)
|
|
}
|
|
outside, _ := filepath.Abs(filepath.Join(filepath.VolumeName(sandbox)+string(filepath.Separator), "not-temp", "home"))
|
|
t.Setenv("CODEX_HOME", outside)
|
|
if err := checkChild(sandbox); err == nil {
|
|
t.Error("CODEX_HOME outside the temporary folder passed")
|
|
}
|
|
t.Setenv("CODEX_HOME", "")
|
|
if err := checkChild(outside); err == nil {
|
|
t.Error("a sandbox outside the temporary folder passed")
|
|
}
|
|
}
|
|
|
|
func TestRemoveTouchesOnlyItsOwn(t *testing.T) {
|
|
dir := t.TempDir()
|
|
other := filepath.Join(dir, prefix+"x")
|
|
os.MkdirAll(other, 0o755)
|
|
remove(other) // not in os.TempDir() itself
|
|
remove("")
|
|
remove(".")
|
|
if _, err := os.Stat(other); err != nil {
|
|
t.Errorf("remove deleted a folder it didn't make: %v", err)
|
|
}
|
|
}
|