reexec: improve test-coverage, and use blackbox testing#207
reexec: improve test-coverage, and use blackbox testing#207
Conversation
|
Looks like we need to remove this one;
|
d26c6e6 to
40674b3
Compare
40674b3 to
a58e720
Compare
| // TestingTB is the minimal subset of [testing.TB] used by this package. | ||
| type TestingTB interface { | ||
| Helper() | ||
| Cleanup(func()) | ||
| } |
There was a problem hiding this comment.
I went for a local interface after all; #208 (comment) because this package is imported in reexec.go ("production" code)
| // OverrideArgv0 overrides the argv0 value observed by reexec.Self for the | ||
| // lifetime of the calling test and restores it via [testing.TB.Cleanup]. | ||
| // | ||
| // The override is process-global. Tests using OverrideArgv0 must not run in | ||
| // parallel with other tests that call reexec.Self. OverrideArgv0 panics if an | ||
| // override is already active. | ||
| func OverrideArgv0(t TestingTB, argv0 string) { | ||
| t.Helper() | ||
|
|
||
| s := argv0 | ||
| if !argv0Override.CompareAndSwap(nil, &s) { | ||
| panic("testing: test using reexecoverride.OverrideArgv0 cannot use t.Parallel") | ||
| } | ||
|
|
||
| t.Cleanup(func() { | ||
| if !argv0Override.CompareAndSwap(&s, nil) { | ||
| panic("testing: cleanup for reexecoverride.OverrideArgv0 detected parallel use of reexec.Self") | ||
| } | ||
| }) | ||
| } |
There was a problem hiding this comment.
Implemented your suggestion here, @kolyshkin 👍
Signed-off-by: Sebastiaan van Stijn <[email protected]>
341240a to
e3e0efe
Compare
Signed-off-by: Sebastiaan van Stijn <[email protected]>
Try to use blackbox testing; add a internal/reexecoverride.OverrideArgv0 utility to override `os.Arg[0]` for testing, and to disable the Linux "/proc/self/exe" fast-path to allow us to test the "naive" path resolution. Signed-off-by: Sebastiaan van Stijn <[email protected]>
e3e0efe to
ad92e49
Compare
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.
Comments suppressed due to low confidence (1)
reexec/reexec.go:99
- naiveSelf returns exec.LookPath(name) directly. LookPath can return a relative path when PATH contains relative entries (e.g., "."), so Self() may return a non-absolute path even when resolving via PATH. Consider normalizing the LookPath result (e.g., make it absolute when it is not already) before returning it.
if filepath.Base(name) == name {
if lp, err := exec.LookPath(name); err == nil {
return lp
}
}
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| t.Errorf("did not resolve via PATH; got %q", resolved) | ||
| } | ||
| if !filepath.IsAbs(resolved) { | ||
| t.Errorf("expected absolute path; got %q", resolved) | ||
| } |
There was a problem hiding this comment.
This subtest assumes reexec.Self() returns an absolute path when resolving via PATH, but naiveSelf currently returns exec.LookPath's result as-is. If PATH contains relative entries, LookPath may return a relative path, making this assertion flaky. Either normalize LookPath's result in reexec.Self()/naiveSelf or relax the test to only require that the returned path is different/resolves to an executable.
There was a problem hiding this comment.
exec.LookPath should return an absolute path, no? @copilot code review[agent]
// LookPath searches for an executable named file in the current path,
// following the conventions of the host operating system.
// If file contains a slash, it is tried directly and the default path is not consulted.
// Otherwise, on success the result is an absolute path.
//
// LookPath returns an error satisfying [errors.Is](err, [ErrDot])
// if the resolved path is relative to the current directory.
// See the package documentation for more details.
No description provided.