@@ -3,6 +3,8 @@ package executor
3
3
import (
4
4
"github.com/cirruslabs/cirrus-ci-agent/api"
5
5
"github.com/stretchr/testify/require"
6
+ "os"
7
+ "runtime"
6
8
"testing"
7
9
)
8
10
@@ -54,3 +56,160 @@ func TestLimitCommands(t *testing.T) {
54
56
})
55
57
}
56
58
}
59
+
60
+ func TestPopulateCloneAndWorkingDirEnvironmentVariables (t * testing.T ) {
61
+ if runtime .GOOS == "windows" {
62
+ t .Skip ()
63
+ return
64
+ }
65
+ tmpDirToRestore := os .Getenv ("TMPDIR" )
66
+ _ = os .Setenv ("TMPDIR" , "/tmp" )
67
+ e := & Executor {}
68
+ ePreCreate := & Executor {}
69
+ ePreCreate .SetPreCreatedWorkingDir ("/tmp/precreated-build" )
70
+ examples := []struct {
71
+ Executor * Executor
72
+ Description string
73
+ Given , Expected map [string ]string
74
+ }{
75
+ {
76
+ e ,
77
+ "empty" ,
78
+ map [string ]string {},
79
+ map [string ]string {
80
+ "CIRRUS_CLONE_DIR" : "/tmp/cirrus-ci-build" ,
81
+ "CIRRUS_WORKING_DIR" : "$CIRRUS_CLONE_DIR" ,
82
+ },
83
+ },
84
+ {
85
+ ePreCreate ,
86
+ "empty (precreated)" ,
87
+ map [string ]string {},
88
+ map [string ]string {
89
+ "CIRRUS_CLONE_DIR" : "/tmp/precreated-build" ,
90
+ "CIRRUS_WORKING_DIR" : "$CIRRUS_CLONE_DIR" ,
91
+ },
92
+ },
93
+ {
94
+ e ,
95
+ "only working" ,
96
+ map [string ]string {
97
+ "CIRRUS_WORKING_DIR" : "/tmp/foo" ,
98
+ },
99
+ map [string ]string {
100
+ "CIRRUS_CLONE_DIR" : "$CIRRUS_WORKING_DIR" ,
101
+ "CIRRUS_WORKING_DIR" : "/tmp/foo" ,
102
+ },
103
+ },
104
+ {
105
+ ePreCreate ,
106
+ "only working (precreated)" ,
107
+ map [string ]string {
108
+ "CIRRUS_WORKING_DIR" : "/tmp/foo" ,
109
+ },
110
+ map [string ]string {
111
+ "CIRRUS_CLONE_DIR" : "/tmp/precreated-build" ,
112
+ "CIRRUS_WORKING_DIR" : "$CIRRUS_CLONE_DIR" ,
113
+ },
114
+ },
115
+ {
116
+ e ,
117
+ "only working (monorepo)" ,
118
+ map [string ]string {
119
+ "CIRRUS_WORKING_DIR" : "$CIRRUS_CLONE_DIR/foo" ,
120
+ },
121
+ map [string ]string {
122
+ "CIRRUS_CLONE_DIR" : "/tmp/cirrus-ci-build" ,
123
+ "CIRRUS_WORKING_DIR" : "$CIRRUS_CLONE_DIR/foo" ,
124
+ },
125
+ },
126
+ {
127
+ ePreCreate ,
128
+ "only working (monorepo + precreated)" ,
129
+ map [string ]string {
130
+ "CIRRUS_WORKING_DIR" : "$CIRRUS_CLONE_DIR/foo" ,
131
+ },
132
+ map [string ]string {
133
+ "CIRRUS_CLONE_DIR" : "/tmp/precreated-build" ,
134
+ "CIRRUS_WORKING_DIR" : "$CIRRUS_CLONE_DIR/foo" ,
135
+ },
136
+ },
137
+ {
138
+ e ,
139
+ "only clone" ,
140
+ map [string ]string {
141
+ "CIRRUS_CLONE_DIR" : "/tmp/foo" ,
142
+ },
143
+ map [string ]string {
144
+ "CIRRUS_CLONE_DIR" : "/tmp/foo" ,
145
+ "CIRRUS_WORKING_DIR" : "$CIRRUS_CLONE_DIR" ,
146
+ },
147
+ },
148
+ {
149
+ ePreCreate ,
150
+ "only clone (precreated)" ,
151
+ map [string ]string {
152
+ "CIRRUS_CLONE_DIR" : "/tmp/foo" ,
153
+ },
154
+ map [string ]string {
155
+ "CIRRUS_CLONE_DIR" : "/tmp/precreated-build" ,
156
+ "CIRRUS_WORKING_DIR" : "$CIRRUS_CLONE_DIR" ,
157
+ },
158
+ },
159
+ {
160
+ e ,
161
+ "both" ,
162
+ map [string ]string {
163
+ "CIRRUS_CLONE_DIR" : "/tmp/foo" ,
164
+ "CIRRUS_WORKING_DIR" : "/tmp/foo" ,
165
+ },
166
+ map [string ]string {
167
+ "CIRRUS_CLONE_DIR" : "/tmp/foo" ,
168
+ "CIRRUS_WORKING_DIR" : "/tmp/foo" ,
169
+ },
170
+ },
171
+ {
172
+ ePreCreate ,
173
+ "both (precreated)" ,
174
+ map [string ]string {
175
+ "CIRRUS_CLONE_DIR" : "/tmp/foo" ,
176
+ "CIRRUS_WORKING_DIR" : "/tmp/foo" ,
177
+ },
178
+ map [string ]string {
179
+ "CIRRUS_CLONE_DIR" : "/tmp/precreated-build" ,
180
+ "CIRRUS_WORKING_DIR" : "$CIRRUS_CLONE_DIR" ,
181
+ },
182
+ },
183
+ {
184
+ e ,
185
+ "both (monorepo)" ,
186
+ map [string ]string {
187
+ "CIRRUS_CLONE_DIR" : "/tmp/foo" ,
188
+ "CIRRUS_WORKING_DIR" : "$CIRRUS_CLONE_DIR/bar" ,
189
+ },
190
+ map [string ]string {
191
+ "CIRRUS_CLONE_DIR" : "/tmp/foo" ,
192
+ "CIRRUS_WORKING_DIR" : "$CIRRUS_CLONE_DIR/bar" ,
193
+ },
194
+ },
195
+ {
196
+ ePreCreate ,
197
+ "both (monorepo + precreated)" ,
198
+ map [string ]string {
199
+ "CIRRUS_CLONE_DIR" : "/tmp/foo" ,
200
+ "CIRRUS_WORKING_DIR" : "$CIRRUS_CLONE_DIR/bar" ,
201
+ },
202
+ map [string ]string {
203
+ "CIRRUS_CLONE_DIR" : "/tmp/precreated-build" ,
204
+ "CIRRUS_WORKING_DIR" : "$CIRRUS_CLONE_DIR/bar" ,
205
+ },
206
+ },
207
+ }
208
+
209
+ for _ , example := range examples {
210
+ t .Run (example .Description , func (t * testing.T ) {
211
+ require .Equal (t , example .Expected , example .Executor .PopulateCloneAndWorkingDirEnvironmentVariables (example .Given ))
212
+ })
213
+ }
214
+ _ = os .Setenv ("TMPDIR" , tmpDirToRestore )
215
+ }
0 commit comments