1
1
package cli_test
2
2
3
3
import (
4
+ "fmt"
4
5
"os"
5
6
"os/exec"
6
7
"path/filepath"
@@ -9,6 +10,8 @@ import (
9
10
"github.com/stretchr/testify/assert"
10
11
11
12
"github.com/coder/coder/cli/clitest"
13
+ "github.com/coder/coder/cli/config"
14
+ "github.com/coder/coder/cryptorand"
12
15
)
13
16
14
17
// nolint:paralleltest
@@ -20,31 +23,13 @@ func TestDotfiles(t *testing.T) {
20
23
})
21
24
t .Run ("NoInstallScript" , func (t * testing.T ) {
22
25
_ , 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 )
32
27
33
28
// nolint:gosec
34
- err = os .WriteFile (filepath .Join (testRepo , ".bashrc" ), []byte ("wow" ), 0750 )
29
+ err : = os .WriteFile (filepath .Join (testRepo , ".bashrc" ), []byte ("wow" ), 0750 )
35
30
assert .NoError (t , err )
36
31
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" )
48
33
c .Dir = testRepo
49
34
err = c .Run ()
50
35
assert .NoError (t , err )
@@ -64,30 +49,13 @@ func TestDotfiles(t *testing.T) {
64
49
})
65
50
t .Run ("InstallScript" , func (t * testing.T ) {
66
51
_ , 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 )
75
53
76
54
// nolint:gosec
77
- err = os .WriteFile (filepath .Join (testRepo , "install.sh" ), []byte ("#!/bin/bash\n echo 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\n echo wow > " + filepath .Join (string (root ), ".bashrc" )), 0750 )
83
56
assert .NoError (t , err )
84
57
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" )
91
59
c .Dir = testRepo
92
60
err = c .Run ()
93
61
assert .NoError (t , err )
@@ -106,3 +74,28 @@ func TestDotfiles(t *testing.T) {
106
74
assert .Equal (t , string (b ), "wow\n " )
107
75
})
108
76
}
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