ProbeSkills kept each GitHub probe's download, a magpie-skill-* folder in
the temp dir, cached for 15 minutes. An expired one went only at the
*next* probe, so a magpie that quit, or a test that ended, left it there
for good. One go test ./internal/library left 22. This machine's temp dir
had 5477, and with old test homes they filled the disk under a full run
(gateway tests failed "no space left on device").
- keepProbe: each cached probe has a timer that removes its folder when
probeLife is up. A probe of the same input replaces it and removes the
old folder at once, outside the lock.
- sweepStaleDownloads, at each probe: a magpie-skill-* in the temp dir over
an hour old goes. No live one is that old, since a probe is kept 15
minutes and a fetch takes seconds.
- internal/library's TestMain gives the tests a temp dir of their own
(TMPDIR, and TMP/TEMP for Windows), ends the cached probes, and fails a
run that leaves a skill download there. A test binary started again by a
test keeps its parent's temp dir.
Run (on origin/main 8b89b2e36): go test -count=1 -timeout 60m ./... all ok;
gofmt clean; go vet darwin, GOOS=linux -tags nogui, GOOS=windows clean.
Broken by hand, each went red:
- a replaced probe's folder kept (TestProbeDownloadEndsWithIt)
- no removal at expiry (TestProbeDownloadEndsWithIt)
- the sweep ignores age, or the prefix, or doesn't run
(TestStaleDownloadsSwept)
- TestMain doesn't end the probes: "the tests left 16 skill downloads"
Not tried: the Windows box, though TMP/TEMP are set for it.
57 lines
1.3 KiB
Go
57 lines
1.3 KiB
Go
package library
|
|
|
|
import (
|
|
"fmt"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/yetone/magpie/internal/testenv"
|
|
)
|
|
|
|
// The tests run in a home of their own, never the user's real files, with
|
|
// a temp dir of their own: what they leave in it is theirs, and a skill's
|
|
// download left there fails the run (#2321).
|
|
func TestMain(m *testing.M) {
|
|
if os.Getenv(testenv.Marker) != "" {
|
|
testenv.Main(m) // started again by a test: in its temp dir already
|
|
}
|
|
tmp, err := os.MkdirTemp("", "magpie-library-test-")
|
|
if err != nil {
|
|
fmt.Fprintln(os.Stderr, err)
|
|
os.Exit(1)
|
|
}
|
|
for _, k := range []string{"TMPDIR", "TMP", "TEMP"} { // TMP, TEMP: Windows'
|
|
os.Setenv(k, tmp)
|
|
}
|
|
code := testenv.Run(m)
|
|
forgetProbes()
|
|
if left := leftDownloads(tmp); len(left) > 0 && code == 0 {
|
|
fmt.Fprintf(os.Stderr, "library: the tests left %d skill downloads in the temp dir: %s\n", len(left), strings.Join(left, ", "))
|
|
code = 1
|
|
}
|
|
os.RemoveAll(tmp)
|
|
os.Exit(code)
|
|
}
|
|
|
|
// forgetProbes ends every cached probe, as its expiry would.
|
|
func forgetProbes() {
|
|
probes.Lock()
|
|
m := probes.m
|
|
probes.m = map[string]*probeEntry{}
|
|
probes.Unlock()
|
|
for _, e := range m {
|
|
e.timer.Stop()
|
|
os.RemoveAll(e.tmp)
|
|
}
|
|
}
|
|
|
|
func leftDownloads(dir string) []string {
|
|
got, _ := filepath.Glob(filepath.Join(dir, skillTemp+"*"))
|
|
for i, p := range got {
|
|
got[i] = filepath.Base(p)
|
|
}
|
|
return got
|
|
}
|