11package trellis
22
33import (
4+ "bytes"
45 "fmt"
56 "io/ioutil"
67 "os"
8+ "os/exec"
79 "path/filepath"
810 "strings"
911 "testing"
12+
13+ "github.com/roots/trellis-cli/command"
1014)
1115
1216func TestNewVirtualenv (t * testing.T ) {
@@ -103,30 +107,6 @@ func TestDeactive(t *testing.T) {
103107 }
104108}
105109
106- func TestLocalPath (t * testing.T ) {
107- venv := NewVirtualenv ("trellis" )
108- originalConfigHome := os .Getenv ("XDG_CONFIG_HOME" )
109- os .Unsetenv ("XDG_CONFIG_HOME" )
110- defer os .Setenv ("XDG_CONFIG_HOME" , originalConfigHome )
111-
112- homeDir , _ := os .UserHomeDir ()
113-
114- localPath := venv .LocalPath ()
115-
116- if localPath != filepath .Join (homeDir , ".local/share/trellis/virtualenv" ) {
117- t .Error ("Expected LocalPath to default to $USER/.local/share" )
118- }
119-
120- os .Setenv ("XDG_CONFIG_HOME" , "mydir" )
121- defer os .Setenv ("XDG_CONFIG_HOME" , originalConfigHome )
122-
123- localPath = venv .LocalPath ()
124-
125- if localPath != filepath .Join ("mydir" , "trellis/virtualenv" ) {
126- t .Error ("Expected LocalPath to use XDG_CONFIG_HOME when set" )
127- }
128- }
129-
130110func TestInitialized (t * testing.T ) {
131111 tempDir , err := ioutil .TempDir ("" , "trellis" )
132112 defer os .RemoveAll (tempDir )
@@ -163,7 +143,7 @@ func TestInstalled(t *testing.T) {
163143 }
164144}
165145
166- func TestInstalledPython3 (t * testing.T ) {
146+ func TestInstalledPython3WithEnsurepip (t * testing.T ) {
167147 tempDir , err := ioutil .TempDir ("" , "trellis" )
168148 defer os .RemoveAll (tempDir )
169149
@@ -178,18 +158,36 @@ func TestInstalledPython3(t *testing.T) {
178158
179159 venv := NewVirtualenv (tempDir )
180160
161+ var output bytes.Buffer
162+
163+ mockExecCommand := func (command string , args []string ) * exec.Cmd {
164+ cs := []string {"-test.run=TestEnsurePipSuccessHelperProcess" , "--" , command }
165+ cs = append (cs , args ... )
166+ cmd := exec .Command (os .Args [0 ], cs ... )
167+ cmd .Stderr = & output
168+ cmd .Stdout = & output
169+ cmd .Env = []string {"GO_WANT_HELPER_PROCESS=1" }
170+ return cmd
171+ }
172+
173+ command .Mock (mockExecCommand )
174+ defer command .Restore ()
175+
181176 ok , cmd := venv .Installed ()
182177
183178 if ! ok {
184179 t .Error ("Expected to be installed" )
185180 }
186181
187- if strings .Join (cmd .Args , " " ) != fmt .Sprintf ("%s -m venv" , pythonPath ) {
188- t .Error ("Expected args incorrect" )
182+ expected := "python3 -m venv"
183+ actual := cmd .String ()
184+
185+ if ! strings .Contains (actual , expected ) {
186+ t .Errorf ("Expected command incorrect.\n expected: %s\n got: %s" , expected , actual )
189187 }
190188}
191189
192- func TestInstalledVirtualenv (t * testing.T ) {
190+ func TestInstalledPython3WithoutEnsurepip (t * testing.T ) {
193191 tempDir , err := ioutil .TempDir ("" , "trellis" )
194192 defer os .RemoveAll (tempDir )
195193
@@ -199,45 +197,55 @@ func TestInstalledVirtualenv(t *testing.T) {
199197
200198 defer testSetEnv ("PATH" , tempDir )()
201199
202- venvPath := filepath .Join (tempDir , "virtualenv" )
203- os .OpenFile (venvPath , os .O_CREATE , 0555 )
204-
205- venv := NewVirtualenv (tempDir )
200+ pythonPath := filepath .Join (tempDir , "python3" )
201+ os .OpenFile (pythonPath , os .O_CREATE , 0555 )
206202
207- ok , cmd := venv . Installed ()
203+ var output bytes. Buffer
208204
209- if ! ok {
210- t .Error ("Expected to be installed" )
205+ mockExecCommand := func (command string , args []string ) * exec.Cmd {
206+ cs := []string {"-test.run=TestEnsurePipFailureHelperProcess" , "--" , command }
207+ cs = append (cs , args ... )
208+ cmd := exec .Command (os .Args [0 ], cs ... )
209+ cmd .Stderr = & output
210+ cmd .Stdout = & output
211+ cmd .Env = []string {"GO_WANT_HELPER_PROCESS=1" }
212+ return cmd
211213 }
212214
213- if strings .Join (cmd .Args , " " ) != venvPath {
214- t .Error ("Expected args incorrect" )
215+ command .Mock (mockExecCommand )
216+ defer command .Restore ()
217+
218+ venv := NewVirtualenv (tempDir )
219+
220+ ok , _ := venv .Installed ()
221+
222+ if ok {
223+ t .Error ("Expected not to be installed" )
215224 }
216225}
217226
218- func TestInstalledLocalVirtualenv (t * testing.T ) {
227+ func TestInstalledVirtualenv (t * testing.T ) {
219228 tempDir , err := ioutil .TempDir ("" , "trellis" )
220229 defer os .RemoveAll (tempDir )
221230
222231 if err != nil {
223232 t .Fatalf ("err: %s" , err )
224233 }
225234
226- defer testSetEnv ("PATH" , "" )()
227- defer testSetEnv ("XDG_CONFIG_HOME" , tempDir )()
235+ defer testSetEnv ("PATH" , tempDir )()
236+
237+ venvPath := filepath .Join (tempDir , "virtualenv" )
238+ os .OpenFile (venvPath , os .O_CREATE , 0555 )
228239
229240 venv := NewVirtualenv (tempDir )
230- localVenvPath := filepath .Join (venv .LocalPath (), "virtualenv.py" )
231- os .MkdirAll (venv .LocalPath (), os .ModePerm )
232- testCreateFile (t , localVenvPath )()
233241
234242 ok , cmd := venv .Installed ()
235243
236244 if ! ok {
237245 t .Error ("Expected to be installed" )
238246 }
239247
240- if strings .Join (cmd .Args , " " ) != fmt . Sprintf ( "python %s" , localVenvPath ) {
248+ if strings .Join (cmd .Args , " " ) != venvPath {
241249 t .Error ("Expected args incorrect" )
242250 }
243251}
@@ -257,3 +265,21 @@ func testSetEnv(env string, value string) func() {
257265 os .Setenv (env , value )
258266 return func () { os .Setenv (env , old ) }
259267}
268+
269+ func TestEnsurePipSuccessHelperProcess (t * testing.T ) {
270+ if os .Getenv ("GO_WANT_HELPER_PROCESS" ) != "1" {
271+ return
272+ }
273+
274+ fmt .Fprintf (os .Stdout , strings .Join (os .Args [3 :], " " ))
275+ os .Exit (0 )
276+ }
277+
278+ func TestEnsurePipFailureHelperProcess (t * testing.T ) {
279+ if os .Getenv ("GO_WANT_HELPER_PROCESS" ) != "1" {
280+ return
281+ }
282+
283+ fmt .Fprintf (os .Stderr , strings .Join (os .Args [3 :], " " ))
284+ os .Exit (1 )
285+ }
0 commit comments