Pull request: all: fix windows tempdir

Merge in DNS/adguard-home from try-fixing-windows-tests to master

Squashed commit of the following:

commit 25a43db5d53f24b98921efa21d8d2f231992e761
Author: Ainar Garipov <A.Garipov@AdGuard.COM>
Date:   Fri Mar 26 21:19:00 2021 +0300

    all: fix windows tempdir
This commit is contained in:
Ainar Garipov
2021-03-29 11:40:04 +03:00
parent 179b76da77
commit ffb503976b
7 changed files with 48 additions and 135 deletions

View File

@@ -1,13 +0,0 @@
package aghtest
import (
"testing"
)
// PrepareTestDir returns the full path to temporary created directory and
// registers the appropriate cleanup for *t.
func PrepareTestDir(t *testing.T) (dir string) {
t.Helper()
return prepareTestDir(t)
}

View File

@@ -1,13 +0,0 @@
// +build !windows
package aghtest
import (
"testing"
)
func prepareTestDir(t *testing.T) (dir string) {
t.Helper()
return t.TempDir()
}

View File

@@ -1,63 +0,0 @@
// +build windows
package aghtest
import (
"io/ioutil"
"os"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
const (
maxRetryDur = 1000 * time.Millisecond
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()
require.NoError(t, err)
dir, err = ioutil.TempDir(wd, "agh-test")
require.NoError(t, err)
require.NotEmpty(t, dir)
t.Cleanup(func() {
start := time.Now()
for {
err = os.RemoveAll(dir)
if err == nil {
break
}
if time.Since(start) >= maxRetryDur {
break
}
time.Sleep(retryDur)
}
assert.NoError(t, err, "after %s", time.Since(start))
})
return dir
}