2021-03-11 09:17:54 +00:00
|
|
|
// +build windows
|
|
|
|
|
|
|
|
package aghtest
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2021-03-11 18:30:52 +00:00
|
|
|
maxRetryDur = 1000 * time.Millisecond
|
2021-03-11 09:17:54 +00:00
|
|
|
retryDur = 5 * time.Millisecond
|
|
|
|
)
|
|
|
|
|
|
|
|
func prepareTestDir(t *testing.T) (dir string) {
|
|
|
|
// Windows, including the version of Windows Server that Github Actions
|
|
|
|
// uses, apparently likes to overly eagerly inspect new directories with
|
|
|
|
// its Windows Defender. Disabling it might require additional
|
|
|
|
// workarounds, and until we've figured it out, just retry the deletion
|
|
|
|
// until the error goes away.
|
|
|
|
//
|
|
|
|
// The code is largely inspired by the one that has been introduced into
|
|
|
|
// the go command itself. We should probably make a proposal to use the
|
|
|
|
// same mechanism in t.TempDir.
|
|
|
|
//
|
|
|
|
// See https://go-review.googlesource.com/c/go/+/172337.
|
|
|
|
//
|
|
|
|
// See https://github.com/golang/go/issues/44919.
|
|
|
|
|
|
|
|
t.Helper()
|
|
|
|
|
|
|
|
wd, err := os.Getwd()
|
2021-03-25 15:03:29 +00:00
|
|
|
require.NoError(t, err)
|
2021-03-11 09:17:54 +00:00
|
|
|
|
|
|
|
dir, err = ioutil.TempDir(wd, "agh-test")
|
2021-03-25 15:03:29 +00:00
|
|
|
require.NoError(t, err)
|
2021-03-11 09:17:54 +00:00
|
|
|
require.NotEmpty(t, dir)
|
|
|
|
|
|
|
|
t.Cleanup(func() {
|
|
|
|
start := time.Now()
|
|
|
|
for {
|
2021-03-11 12:38:39 +00:00
|
|
|
err = os.RemoveAll(dir)
|
2021-03-11 09:17:54 +00:00
|
|
|
if err == nil {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
if time.Since(start) >= maxRetryDur {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
time.Sleep(retryDur)
|
|
|
|
}
|
|
|
|
|
2021-03-25 15:03:29 +00:00
|
|
|
assert.NoError(t, err, "after %s", time.Since(start))
|
2021-03-11 09:17:54 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
return dir
|
|
|
|
}
|