Skip to content

Commit b059fdf

Browse files
committed
organize:
1 parent 44efa67 commit b059fdf

File tree

1 file changed

+34
-41
lines changed

1 file changed

+34
-41
lines changed

cli/dotfiles_test.go

Lines changed: 34 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package cli_test
22

33
import (
4+
"fmt"
45
"os"
56
"os/exec"
67
"path/filepath"
@@ -9,6 +10,8 @@ import (
910
"github.com/stretchr/testify/assert"
1011

1112
"github.com/coder/coder/cli/clitest"
13+
"github.com/coder/coder/cli/config"
14+
"github.com/coder/coder/cryptorand"
1215
)
1316

1417
// nolint:paralleltest
@@ -20,31 +23,13 @@ func TestDotfiles(t *testing.T) {
2023
})
2124
t.Run("NoInstallScript", func(t *testing.T) {
2225
_, root := clitest.New(t)
23-
testRepo := filepath.Join(string(root), "test-repo")
24-
25-
err := os.MkdirAll(testRepo, 0750)
26-
assert.NoError(t, err)
27-
28-
c := exec.Command("git", "init")
29-
c.Dir = testRepo
30-
err = c.Run()
31-
assert.NoError(t, err)
26+
testRepo := testGitRepo(t, root)
3227

3328
// nolint:gosec
34-
err = os.WriteFile(filepath.Join(testRepo, ".bashrc"), []byte("wow"), 0750)
29+
err := os.WriteFile(filepath.Join(testRepo, ".bashrc"), []byte("wow"), 0750)
3530
assert.NoError(t, err)
3631

37-
c = exec.Command("git", "add", ".bashrc")
38-
c.Dir = testRepo
39-
err = c.Run()
40-
assert.NoError(t, err)
41-
42-
c = exec.Command("git", "config", "user.email", "ci@coder.com")
43-
c.Dir = testRepo
44-
err = c.Run()
45-
assert.NoError(t, err)
46-
47-
c = exec.Command("git", "config", "user.name", "C I")
32+
c := exec.Command("git", "add", ".bashrc")
4833
c.Dir = testRepo
4934
err = c.Run()
5035
assert.NoError(t, err)
@@ -64,30 +49,13 @@ func TestDotfiles(t *testing.T) {
6449
})
6550
t.Run("InstallScript", func(t *testing.T) {
6651
_, root := clitest.New(t)
67-
testRepo := filepath.Join(string(root), "test-repo")
68-
err := os.MkdirAll(testRepo, 0750)
69-
assert.NoError(t, err)
70-
71-
c := exec.Command("git", "init")
72-
c.Dir = testRepo
73-
err = c.Run()
74-
assert.NoError(t, err)
52+
testRepo := testGitRepo(t, root)
7553

7654
// nolint:gosec
77-
err = os.WriteFile(filepath.Join(testRepo, "install.sh"), []byte("#!/bin/bash\necho wow > "+filepath.Join(string(root), ".bashrc")), 0750)
78-
assert.NoError(t, err)
79-
80-
c = exec.Command("git", "add", "install.sh")
81-
c.Dir = testRepo
82-
err = c.Run()
55+
err := os.WriteFile(filepath.Join(testRepo, "install.sh"), []byte("#!/bin/bash\necho wow > "+filepath.Join(string(root), ".bashrc")), 0750)
8356
assert.NoError(t, err)
8457

85-
c = exec.Command("git", "config", "user.email", "ci@coder.com")
86-
c.Dir = testRepo
87-
err = c.Run()
88-
assert.NoError(t, err)
89-
90-
c = exec.Command("git", "config", "user.name", "C I")
58+
c := exec.Command("git", "add", "install.sh")
9159
c.Dir = testRepo
9260
err = c.Run()
9361
assert.NoError(t, err)
@@ -106,3 +74,28 @@ func TestDotfiles(t *testing.T) {
10674
assert.Equal(t, string(b), "wow\n")
10775
})
10876
}
77+
78+
func testGitRepo(t *testing.T, root config.Root) string {
79+
r, err := cryptorand.String(8)
80+
assert.NoError(t, err)
81+
dir := filepath.Join(string(root), fmt.Sprintf("test-repo-%s", r))
82+
err = os.MkdirAll(dir, 0750)
83+
assert.NoError(t, err)
84+
85+
c := exec.Command("git", "init")
86+
c.Dir = dir
87+
err = c.Run()
88+
assert.NoError(t, err)
89+
90+
c = exec.Command("git", "config", "user.email", "ci@coder.com")
91+
c.Dir = dir
92+
err = c.Run()
93+
assert.NoError(t, err)
94+
95+
c = exec.Command("git", "config", "user.name", "C I")
96+
c.Dir = dir
97+
err = c.Run()
98+
assert.NoError(t, err)
99+
100+
return dir
101+
}

0 commit comments

Comments
 (0)