AdGuardHome/internal/aghtest/os_windows.go
Ainar Garipov 2c9992e0cc Pull request: all: rm var shadowing, vol. 1
Updates #2803.

Squashed commit of the following:

commit 15fa64ad4fb7561c4b0542245f99869685643bbd
Author: Ainar Garipov <A.Garipov@AdGuard.COM>
Date:   Thu Mar 11 14:55:28 2021 +0300

    util: imp code

commit c3b0563a44ccc98a5901df19174acdca8a350d62
Author: Ainar Garipov <A.Garipov@AdGuard.COM>
Date:   Thu Mar 11 14:38:54 2021 +0300

    all: rm var shadowing, vol. 1
2021-03-11 15:38:39 +03:00

64 lines
1.3 KiB
Go

// +build windows
package aghtest
import (
"io/ioutil"
"os"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
const (
maxRetryDur = 500 * 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.Nil(t, err)
dir, err = ioutil.TempDir(wd, "agh-test")
require.Nil(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.Nil(t, err)
})
return dir
}