Use t.Chdir in tests

This commit is contained in:
William Martin 2025-06-17 11:13:45 +02:00
parent b4cd2273d8
commit 49ed6c4681
4 changed files with 15 additions and 45 deletions

View file

@ -23,15 +23,14 @@ import (
"github.com/cli/cli/v2/pkg/iostreams"
"github.com/spf13/cobra"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestNewCmdExtension(t *testing.T) {
tempDir := t.TempDir()
oldWd, _ := os.Getwd()
localExtensionTempDir := filepath.Join(tempDir, "gh-hello")
assert.NoError(t, os.MkdirAll(localExtensionTempDir, 0755))
assert.NoError(t, os.Chdir(localExtensionTempDir))
t.Cleanup(func() { _ = os.Chdir(oldWd) })
require.NoError(t, os.MkdirAll(localExtensionTempDir, 0755))
t.Chdir(localExtensionTempDir)
tests := []struct {
name string

View file

@ -1251,9 +1251,10 @@ func TestManager_repo_not_found(t *testing.T) {
}
func TestManager_Create(t *testing.T) {
chdirTemp(t)
tempDir := t.TempDir()
t.Chdir(tempDir)
err := os.MkdirAll("gh-test", 0755)
assert.NoError(t, err)
require.NoError(t, err)
ios, _, stdout, stderr := iostreams.Test()
@ -1279,9 +1280,10 @@ func TestManager_Create(t *testing.T) {
}
func TestManager_Create_go_binary(t *testing.T) {
chdirTemp(t)
tempDir := t.TempDir()
t.Chdir(tempDir)
err := os.MkdirAll("gh-test", 0755)
assert.NoError(t, err)
require.NoError(t, err)
reg := httpmock.Registry{}
defer reg.Verify(t)
@ -1329,9 +1331,10 @@ func TestManager_Create_go_binary(t *testing.T) {
}
func TestManager_Create_other_binary(t *testing.T) {
chdirTemp(t)
tempDir := t.TempDir()
t.Chdir(tempDir)
err := os.MkdirAll("gh-test", 0755)
assert.NoError(t, err)
require.NoError(t, err)
ios, _, stdout, stderr := iostreams.Test()
@ -1392,18 +1395,6 @@ func Test_ensurePrefixed(t *testing.T) {
}
}
// chdirTemp changes the current working directory to a temporary directory for the duration of the test.
func chdirTemp(t *testing.T) {
oldWd, _ := os.Getwd()
tempDir := t.TempDir()
if err := os.Chdir(tempDir); err != nil {
t.Fatal(err)
}
t.Cleanup(func() {
_ = os.Chdir(oldWd)
})
}
func fileNames(files []os.DirEntry) []string {
names := make([]string, len(files))
for i, f := range files {

View file

@ -174,11 +174,6 @@ func Test_NewCmdDownload(t *testing.T) {
}
func Test_downloadRun(t *testing.T) {
oldwd, err := os.Getwd()
if err != nil {
t.Fatalf("could not determine working directory: %v", err)
}
tests := []struct {
name string
isTTY bool
@ -526,11 +521,7 @@ func Test_downloadRun(t *testing.T) {
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
tempDir := t.TempDir()
if err := os.Chdir(tempDir); err == nil {
t.Cleanup(func() { _ = os.Chdir(oldwd) })
} else {
t.Fatal(err)
}
t.Chdir(tempDir)
ios, _, stdout, stderr := iostreams.Test()
ios.SetStdoutTTY(tt.isTTY)

View file

@ -210,17 +210,10 @@ func createTestDir(t *testing.T) (cleanupFn func()) {
rootDir := t.TempDir()
// Move workspace to temporary directory
cwd, err := os.Getwd()
if err != nil {
t.Fatal(err)
}
err = os.Chdir(rootDir)
if err != nil {
t.Fatal(err)
}
t.Chdir(rootDir)
// Make subdirectories
err = os.Mkdir(filepath.Join(rootDir, "subDir1"), 0755)
err := os.Mkdir(filepath.Join(rootDir, "subDir1"), 0755)
if err != nil {
t.Fatal(err)
}
@ -253,10 +246,6 @@ func createTestDir(t *testing.T) (cleanupFn func()) {
cleanupFn = func() {
os.RemoveAll(rootDir)
err = os.Chdir(cwd)
if err != nil {
t.Fatal(err)
}
}
return cleanupFn
}