Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[no-release-notes] Change some tests to use t.TempDir instead of os.TempDir so that their temporary files get cleaned up. #8792

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 10 additions & 7 deletions go/cmd/dolt/commands/signed_commits_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"testing"

"github.com/dolthub/dolt/go/cmd/dolt/cli"
"github.com/dolthub/dolt/go/libraries/doltcore/dbfactory"
"github.com/dolthub/dolt/go/libraries/doltcore/env"
"github.com/dolthub/dolt/go/libraries/utils/argparser"
"github.com/dolthub/dolt/go/libraries/utils/config"
Expand All @@ -45,10 +46,9 @@ func importKey(t *testing.T, ctx context.Context) {
}

func setupTestDB(t *testing.T, ctx context.Context, fs filesys.Filesys) string {
dir, err := os.MkdirTemp(os.TempDir(), "signed_commits")
require.NoError(t, err)
dir := t.TempDir()
dbDir := filepath.Join(dir, "db")
err = filesys.CopyDir("testdata/signed_commits/db/", dbDir, fs)
err := filesys.CopyDir("testdata/signed_commits/db/", dbDir, fs)
require.NoError(t, err)

log.Println(dbDir)
Expand Down Expand Up @@ -79,6 +79,9 @@ func TestSignAndVerifyCommit(t *testing.T) {
ctx := context.Background()
importKey(t, ctx)
dbDir := setupTestDB(t, ctx, filesys.LocalFS)
t.Cleanup(func() {
dbfactory.CloseAllLocalDatabases()
})

global := map[string]string{
"user.name": "First Last",
Expand All @@ -90,7 +93,7 @@ func TestSignAndVerifyCommit(t *testing.T) {
apr, err := cli.CreateCommitArgParser().Parse(test.commitArgs)
require.NoError(t, err)

_, err = execCommand(ctx, dbDir, CommitCmd{}, test.commitArgs, apr, map[string]string{}, global)
_, err = execCommand(ctx, t, dbDir, CommitCmd{}, test.commitArgs, apr, map[string]string{}, global)

if test.expectErr {
require.Error(t, err)
Expand All @@ -103,14 +106,14 @@ func TestSignAndVerifyCommit(t *testing.T) {
apr, err = cli.CreateLogArgParser(false).Parse(args)
require.NoError(t, err)

logOutput, err := execCommand(ctx, dbDir, LogCmd{}, args, apr, map[string]string{}, global)
logOutput, err := execCommand(ctx, t, dbDir, LogCmd{}, args, apr, map[string]string{}, global)
require.NoError(t, err)
require.Contains(t, logOutput, "Good signature from \"Test User <[email protected]>\"")
})
}
}

func execCommand(ctx context.Context, wd string, cmd cli.Command, args []string, apr *argparser.ArgParseResults, local, global map[string]string) (output string, err error) {
func execCommand(ctx context.Context, t *testing.T, wd string, cmd cli.Command, args []string, apr *argparser.ArgParseResults, local, global map[string]string) (output string, err error) {
err = os.Chdir(wd)
if err != nil {
err = fmt.Errorf("error changing directory to %s: %w", wd, err)
Expand Down Expand Up @@ -157,7 +160,7 @@ func execCommand(ctx context.Context, wd string, cmd cli.Command, args []string,

initialOut := os.Stdout
initialErr := os.Stderr
f, err := os.CreateTemp(os.TempDir(), "signed-commit-test-*")
f, err := os.CreateTemp(t.TempDir(), "signed-commit-test-*")
if err != nil {
err = fmt.Errorf("error creating temp file: %w", err)
return
Expand Down
8 changes: 4 additions & 4 deletions go/libraries/doltcore/doltdb/commit_hooks_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func TestPushOnWriteHook(t *testing.T) {
ctx := context.Background()

// destination repo
testDir, err := test.ChangeToTestDir("TestReplicationDest")
testDir, err := test.ChangeToTestDir(t.TempDir(), "TestReplicationDest")

if err != nil {
panic("Couldn't change the working directory to the test directory.")
Expand All @@ -62,7 +62,7 @@ func TestPushOnWriteHook(t *testing.T) {
destDB, _ := LoadDoltDB(context.Background(), types.Format_Default, LocalDirDoltDB, filesys.LocalFS)

// source repo
testDir, err = test.ChangeToTestDir("TestReplicationSource")
testDir, err = test.ChangeToTestDir(t.TempDir(), "TestReplicationSource")

if err != nil {
panic("Couldn't change the working directory to the test directory.")
Expand Down Expand Up @@ -183,7 +183,7 @@ func TestAsyncPushOnWrite(t *testing.T) {
ctx := context.Background()

// destination repo
testDir, err := test.ChangeToTestDir("TestReplicationDest")
testDir, err := test.ChangeToTestDir(t.TempDir(), "TestReplicationDest")

if err != nil {
panic("Couldn't change the working directory to the test directory.")
Expand All @@ -202,7 +202,7 @@ func TestAsyncPushOnWrite(t *testing.T) {
destDB, _ := LoadDoltDB(context.Background(), types.Format_Default, LocalDirDoltDB, filesys.LocalFS)

// source repo
testDir, err = test.ChangeToTestDir("TestReplicationSource")
testDir, err = test.ChangeToTestDir(t.TempDir(), "TestReplicationSource")

if err != nil {
panic("Couldn't change the working directory to the test directory.")
Expand Down
6 changes: 3 additions & 3 deletions go/libraries/doltcore/doltdb/doltdb_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ func TestEmptyInMemoryRepoCreation(t *testing.T) {
}

func TestLoadNonExistentLocalFSRepo(t *testing.T) {
_, err := test.ChangeToTestDir("TestLoadRepo")
_, err := test.ChangeToTestDir(t.TempDir(), "TestLoadRepo")

if err != nil {
panic("Couldn't change the working directory to the test directory.")
Expand All @@ -231,7 +231,7 @@ func TestLoadNonExistentLocalFSRepo(t *testing.T) {
}

func TestLoadBadLocalFSRepo(t *testing.T) {
testDir, err := test.ChangeToTestDir("TestLoadRepo")
testDir, err := test.ChangeToTestDir(t.TempDir(), "TestLoadRepo")

if err != nil {
panic("Couldn't change the working directory to the test directory.")
Expand All @@ -246,7 +246,7 @@ func TestLoadBadLocalFSRepo(t *testing.T) {
}

func TestLDNoms(t *testing.T) {
testDir, err := test.ChangeToTestDir("TestLoadRepo")
testDir, err := test.ChangeToTestDir(t.TempDir(), "TestLoadRepo")

if err != nil {
panic("Couldn't change the working directory to the test directory.")
Expand Down
8 changes: 1 addition & 7 deletions go/libraries/doltcore/dtestutils/environment.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ package dtestutils

import (
"context"
"os"
"path/filepath"

"github.com/dolthub/dolt/go/libraries/doltcore/doltdb"
Expand All @@ -41,12 +40,7 @@ func CreateTestEnv() *env.DoltEnv {
// CreateTestEnvForLocalFilesystem creates a new DoltEnv for testing, using a local FS, instead of an in-memory
// filesystem, for persisting files. This is useful for tests that require a disk-based filesystem and will not
// work correctly with an in-memory filesystem and in-memory blob store (e.g. dolt_undrop() tests).
func CreateTestEnvForLocalFilesystem() *env.DoltEnv {
tempDir, err := os.MkdirTemp(os.TempDir(), "dolt-*")
if err != nil {
panic(err)
}

func CreateTestEnvForLocalFilesystem(tempDir string) *env.DoltEnv {
fs, err := filesys.LocalFilesysWithWorkingDir(tempDir)
if err != nil {
panic(err)
Expand Down
8 changes: 2 additions & 6 deletions go/libraries/doltcore/dtestutils/sql_server_driver/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,13 +76,9 @@ type DoltUser struct {
var _ DoltCmdable = DoltUser{}
var _ DoltDebuggable = DoltUser{}

func NewDoltUser() (DoltUser, error) {
tmpdir, err := os.MkdirTemp("", "go-sql-server-driver-")
if err != nil {
return DoltUser{}, err
}
func NewDoltUser(tmpdir string) (DoltUser, error) {
res := DoltUser{tmpdir}
err = res.DoltExec("config", "--global", "--add", "metrics.disabled", "true")
err := res.DoltExec("config", "--global", "--add", "metrics.disabled", "true")
if err != nil {
return DoltUser{}, err
}
Expand Down
6 changes: 3 additions & 3 deletions go/libraries/doltcore/env/multi_repo_env_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ func initRepoWithRelativePath(t *testing.T, envPath string, hdp HomeDirProvider)
}

func TestMultiEnvForDirectory(t *testing.T) {
rootPath, err := test.ChangeToTestDir("TestDoltEnvAsMultiEnv")
rootPath, err := test.ChangeToTestDir(t.TempDir(), "TestDoltEnvAsMultiEnv")
require.NoError(t, err)

hdp := func() (string, error) { return rootPath, nil }
Expand Down Expand Up @@ -150,7 +150,7 @@ func TestMultiEnvForDirectory(t *testing.T) {
}

func TestMultiEnvForDirectoryWithMultipleRepos(t *testing.T) {
rootPath, err := test.ChangeToTestDir("TestDoltEnvAsMultiEnvWithMultipleRepos")
rootPath, err := test.ChangeToTestDir(t.TempDir(), "TestDoltEnvAsMultiEnvWithMultipleRepos")
require.NoError(t, err)

hdp := func() (string, error) { return rootPath, nil }
Expand All @@ -177,7 +177,7 @@ func TestMultiEnvForDirectoryWithMultipleRepos(t *testing.T) {
}

func initMultiEnv(t *testing.T, testName string, names []string) (string, HomeDirProvider, map[string]*DoltEnv) {
rootPath, err := test.ChangeToTestDir(testName)
rootPath, err := test.ChangeToTestDir(t.TempDir(), testName)
require.NoError(t, err)

hdp := func() (string, error) { return rootPath, nil }
Expand Down
2 changes: 1 addition & 1 deletion go/libraries/doltcore/sqle/enginetest/dolt_harness.go
Original file line number Diff line number Diff line change
Expand Up @@ -515,7 +515,7 @@ func (d *DoltHarness) newProvider() sql.MutableDatabaseProvider {

var dEnv *env.DoltEnv
if d.useLocalFilesystem {
dEnv = dtestutils.CreateTestEnvForLocalFilesystem()
dEnv = dtestutils.CreateTestEnvForLocalFilesystem(d.t.TempDir())
} else {
dEnv = dtestutils.CreateTestEnv()
}
Expand Down
2 changes: 1 addition & 1 deletion go/libraries/events/event_flush_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ func TestEventFlushing(t *testing.T) {
fs := filesys.LocalFS

path := filepath.Join(dPath, evtPath)
dDir := testLib.TestDir(path)
dDir := testLib.TestDir(t.TempDir(), path)

ft = createFlushTester(fs, "", dDir)
}
Expand Down
10 changes: 5 additions & 5 deletions go/libraries/utils/filesys/fs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ var filesysetmsToTest = map[string]Filesys{
}

func TestFilesystems(t *testing.T) {
dir := test.TestDir("filesys_test")
newLocation := test.TestDir("newLocation")
dir := test.TestDir(t.TempDir(), "filesys_test")
newLocation := test.TestDir(t.TempDir(), "newLocation")
subdir := filepath.Join(dir, "subdir")
subdirFile := filepath.Join(subdir, testSubdirFilename)
fp := filepath.Join(dir, testFilename)
Expand Down Expand Up @@ -186,7 +186,7 @@ func TestNewInMemFS(t *testing.T) {
}

func TestRecursiveFSIteration(t *testing.T) {
dir := test.TestDir("TestRecursiveFSIteration")
dir := test.TestDir(t.TempDir(), "TestRecursiveFSIteration")

for fsName, fs := range filesysetmsToTest {
var expectedDirs []string
Expand Down Expand Up @@ -215,7 +215,7 @@ func TestRecursiveFSIteration(t *testing.T) {
}

func TestFSIteration(t *testing.T) {
dir := test.TestDir("TestFSIteration")
dir := test.TestDir(t.TempDir(), "TestFSIteration")

for fsName, fs := range filesysetmsToTest {
var expectedDirs []string
Expand Down Expand Up @@ -249,7 +249,7 @@ func TestFSIteration(t *testing.T) {
}

func TestDeletes(t *testing.T) {
dir := test.TestDir("TestDeletes")
dir := test.TestDir(t.TempDir(), "TestDeletes")

for fsName, fs := range filesysetmsToTest {
var ignored []string
Expand Down
16 changes: 4 additions & 12 deletions go/libraries/utils/test/files.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,27 +22,19 @@ import (
)

// TestDir creates a subdirectory inside the systems temp directory
func TestDir(testName string) string {
id, err := uuid.NewRandom()

if err != nil {
panic(ShouldNeverHappen)
}

return filepath.Join(os.TempDir(), testName, id.String())
func TestDir(dir, testName string) string {
return filepath.Join(dir, testName, uuid.NewString())
}

// ChangeToTestDir creates a new test directory and changes the current directory to be
func ChangeToTestDir(testName string) (string, error) {
dir := TestDir(testName)
func ChangeToTestDir(tempDir, testName string) (string, error) {
dir := TestDir(tempDir, testName)
err := os.MkdirAll(dir, os.ModePerm)

if err != nil {
return "", err
}

err = os.Chdir(dir)

if err != nil {
return "", err
}
Expand Down
2 changes: 1 addition & 1 deletion go/libraries/utils/test/test_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import (
// test your tests so you can test while you test

func TestLDTestUtils(t *testing.T) {
dir, err := ChangeToTestDir("TestLDTestUtils")
dir, err := ChangeToTestDir(t.TempDir(), "TestLDTestUtils")

if err != nil {
t.Fatal("Couldn't change to test dir")
Expand Down
10 changes: 8 additions & 2 deletions go/performance/import_benchmarker/cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,16 @@ func main() {
if err != nil {
log.Fatalln(err)
}
defer os.RemoveAll(tmpdir)

userdir, err := os.MkdirTemp("", "import-benchmarker-")
if err != nil {
log.Fatalln(err)
}
defer os.RemoveAll(userdir)

results := new(ib.ImportResults)
u, err := driver.NewDoltUser()
u, err := driver.NewDoltUser(userdir)
for _, test := range def.Tests {
test.Results = results
test.InitWithTmpDir(tmpdir)
Expand Down Expand Up @@ -73,5 +80,4 @@ func main() {
} else {
fmt.Println(results.SqlDump())
}
os.Exit(0)
}
2 changes: 1 addition & 1 deletion go/performance/import_benchmarker/testdef.go
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ func (test *ImportTest) Run(t *testing.T) {
test.InitWithTmpDir(tmp)
}

u, err := driver.NewDoltUser()
u, err := driver.NewDoltUser(t.TempDir())
for _, r := range test.Repos {
if r.ExternalServer != nil {
err := test.RunExternalServerTests(r.Name, r.ExternalServer)
Expand Down
10 changes: 8 additions & 2 deletions go/performance/sysbench/cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,16 @@ func main() {
if err != nil {
log.Fatalln(err)
}
defer os.RemoveAll(tmpdir)

userdir, err := os.MkdirTemp("", "sysbench-user-dir_")
if err != nil {
log.Fatalln(err)
}
defer os.RemoveAll(userdir)

results := new(sysbench.Results)
u, err := driver.NewDoltUser()
u, err := driver.NewDoltUser(userdir)
for _, test := range defs.Tests {
test.InitWithTmpDir(tmpdir)

Expand Down Expand Up @@ -83,5 +90,4 @@ func main() {
} else {
fmt.Println(results.SqlDump())
}
os.Exit(0)
}
2 changes: 1 addition & 1 deletion go/performance/sysbench/testdef.go
Original file line number Diff line number Diff line change
Expand Up @@ -440,7 +440,7 @@ func (test *Script) Run(t *testing.T) {
}

results := new(Results)
u, err := driver.NewDoltUser()
u, err := driver.NewDoltUser(t.TempDir())
test.Results = results
test.InitWithTmpDir(tmpdir)
for _, r := range test.Repos {
Expand Down
Loading
Loading