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

Replace MkdirTemp calls in tests with t.TempDir() #234

Merged
merged 3 commits into from
Aug 1, 2024
Merged
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
9 changes: 2 additions & 7 deletions cli/app/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package app
import (
"context"
"fmt"
"log"
"os"
"testing"
"time"
Expand All @@ -18,19 +17,15 @@ func TestConfig(t *testing.T) {
ctx, ctxC := context.WithTimeout(context.Background(), time.Second*15)
defer ctxC()

root, err := os.MkdirTemp("/tmp", "tau-test")
if err != nil {
log.Fatal(err)
}
defer os.RemoveAll(root)
root := t.TempDir()

fmt.Println("ROOT ", root)
os.Mkdir(root+"/storage", 0750)
os.Mkdir(root+"/storage/test", 0750)
os.Mkdir(root+"/config", 0750)
os.Mkdir(root+"/config/keys", 0750)

err = newApp().RunContext(ctx, []string{os.Args[0], "--root", root, "cnf", "gen", "-s", "test", "--services", "auth,seer,monkey", "--swarm-key", "--dv-keys"})
err := newApp().RunContext(ctx, []string{os.Args[0], "--root", root, "cnf", "gen", "-s", "test", "--services", "auth,seer,monkey", "--swarm-key", "--dv-keys"})
assert.NilError(t, err)

err = newApp().RunContext(ctx, []string{os.Args[0], "--root", root, "cnf", "ok?", "-s", "test"})
Expand Down
7 changes: 2 additions & 5 deletions cli/app/export_config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,8 @@ import (
)

func setupTestEnvironmentWithKeys(t *testing.T) (string, func()) {
root, err := os.MkdirTemp("", "tau-test")
if err != nil {
t.Fatalf("Failed to create temp directory: %v", err)
}
cleanup := func() { os.RemoveAll(root) }
root := t.TempDir()
cleanup := func() {}

// Creating necessary directories
os.MkdirAll(root+"/config/keys", 0750)
Expand Down
9 changes: 2 additions & 7 deletions cli/app/start_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package app

import (
"context"
"log"
"os"
"testing"
"time"
Expand All @@ -29,11 +28,7 @@ func TestStart(t *testing.T) {
ctx, ctxC := context.WithTimeout(context.Background(), time.Second*15)
defer ctxC()

root, err := os.MkdirTemp("/tmp", "tau-test")
if err != nil {
log.Fatal(err)
}
defer os.RemoveAll(root)
root := t.TempDir()

os.Mkdir(root+"/storage", 0750)
os.Mkdir(root+"/storage/test", 0750)
Expand All @@ -44,6 +39,6 @@ func TestStart(t *testing.T) {
os.WriteFile(root+"/config/keys/test_swarm.key", testSwarmKey, 0640)
os.WriteFile(root+"/config/keys/test.key", testKey, 0640)

err = app.RunContext(ctx, []string{os.Args[0], "start", "-s", "test", "--root", root, "--dev"})
err := app.RunContext(ctx, []string{os.Args[0], "start", "-s", "test", "--root", root, "--dev"})
assert.NilError(t, err)
}
7 changes: 2 additions & 5 deletions clients/p2p/auth/inject_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package auth_test
import (
"bytes"
"encoding/pem"
"os"
"testing"

"github.com/taubyte/http/helpers"
Expand Down Expand Up @@ -38,14 +37,12 @@ func injectCert(t *testing.T, client authIface.Client) []byte {
}

func TestInject(t *testing.T) {
testDir, err := os.MkdirTemp("", "testdir")
assert.NilError(t, err)
defer os.Remove(testDir)
testDir := t.TempDir()

u := dream.New(dream.UniverseConfig{Name: t.Name()})
defer u.Stop()

err = u.StartWithConfig(&dream.Config{
err := u.StartWithConfig(&dream.Config{
Services: map[string]commonIface.ServiceConfig{
"auth": {},
"tns": {},
Expand Down
7 changes: 2 additions & 5 deletions clients/p2p/auth/stats_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package auth_test

import (
"os"
"testing"

commonIface "github.com/taubyte/tau/core/common"
Expand All @@ -11,14 +10,12 @@ import (
)

func TestStats(t *testing.T) {
testDir, err := os.MkdirTemp("", "testdir")
assert.NilError(t, err)
defer os.Remove(testDir)
t.TempDir()

u := dream.New(dream.UniverseConfig{Name: t.Name()})
defer u.Stop()

err = u.StartWithConfig(&dream.Config{
err := u.StartWithConfig(&dream.Config{
Services: map[string]commonIface.ServiceConfig{
"auth": {},
},
Expand Down
8 changes: 1 addition & 7 deletions clients/p2p/hoarder/tests/p2p_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ package tests
import (
"context"
"fmt"
"os"
"testing"
"time"

Expand All @@ -23,12 +22,7 @@ import (
func TestHoarderClient(t *testing.T) {
ctx := context.Background()

srvRoot, err := os.MkdirTemp("/tmp", "clientSrvRoot")
if err != nil {
t.Error(err)
return
}
defer os.RemoveAll(srvRoot)
srvRoot := t.TempDir()

srv, err := service.New(ctx, &config.Node{
Root: srvRoot,
Expand Down
8 changes: 1 addition & 7 deletions p2p/peer/pebble_peer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,14 @@ package peer
import (
"context"
"fmt"
"os"
"testing"

keypair "github.com/taubyte/tau/p2p/keypair"
)

func TestNewPebblePeer(t *testing.T) {
ctx := context.Background()
dir, err := os.MkdirTemp("", "peerRoot")
if err != nil {
t.Error(err)
return
}
defer os.RemoveAll(dir)
dir := t.TempDir()

p1, _ := New(
ctx,
Expand Down
15 changes: 2 additions & 13 deletions p2p/peer/ping_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package peer
import (
"context"
"fmt"
"os"
"testing"
"time"

Expand All @@ -13,19 +12,9 @@ import (
func TestPingPeer(t *testing.T) {
ctx := context.Background()

dir1, err := os.MkdirTemp("", "peerRoot1")
if err != nil {
t.Error(err)
return
}
defer os.RemoveAll(dir1)
dir1 := t.TempDir()

dir2, err := os.MkdirTemp("", "peerRoot2")
if err != nil {
t.Error(err)
return
}
defer os.RemoveAll(dir2)
dir2 := t.TempDir()

p1, err := New(
ctx,
Expand Down
7 changes: 1 addition & 6 deletions pkg/config-compiler/decompile_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,7 @@ func TestDecompileBasic(t *testing.T) {
var gitRoot string

if runInTemp {
gitRoot, err = os.MkdirTemp("", "gitTestRoot")
if err != nil {
t.Error(err)
return
}
defer os.RemoveAll(gitRoot)
gitRoot = t.TempDir()
} else {
gitRoot = "./testdata"
os.RemoveAll(gitRoot)
Expand Down
5 changes: 2 additions & 3 deletions pkg/git/all_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -179,10 +179,9 @@ func TestClone(t *testing.T) {
}

func TestCloneFail(t *testing.T) {
dir, err := os.MkdirTemp("/tmp", t.Name())
assert.NilError(t, err)
dir := t.TempDir()

_, err = New(
_, err := New(
context.Background(),
URL(testRepoHTTPUrl),
Token("wrongauth"),
Expand Down
7 changes: 1 addition & 6 deletions pkg/mycelium/host/all_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package host

import (
"context"
"os"
"path"
"strings"
"testing"
Expand Down Expand Up @@ -226,11 +225,7 @@ func TestHostFs(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), timeout)
defer cancel()

tempDir, err := os.MkdirTemp("/tmp", "tau-test")
if err != nil {
assert.NilError(t, err)
}
defer os.RemoveAll(tempDir)
tempDir := t.TempDir()

fs, err := h.Fs(ctx)
assert.NilError(t, err, "Filesystem creation failed")
Expand Down
8 changes: 1 addition & 7 deletions services/tns/push_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package tns
import (
"context"
"fmt"
"os"
"reflect"
"testing"
"time"
Expand All @@ -24,12 +23,7 @@ func TestPush(t *testing.T) {
time.Sleep(s)
}()

srvRoot, err := os.MkdirTemp("", "srvRoot")
if err != nil {
t.Error(err)
return
}
defer os.RemoveAll(srvRoot)
srvRoot := t.TempDir()

srv, err := New(testCtx, &config.Node{
Root: srvRoot,
Expand Down
Loading