@@ -296,6 +296,66 @@ func Test_venvExists(t *testing.T) {
296296 }
297297}
298298
299+ func Test_getVenvBinDir (t * testing.T ) {
300+ result := getVenvBinDir ("/path/to/.venv" )
301+ if runtime .GOOS == "windows" {
302+ require .Equal (t , filepath .Join ("/path/to/.venv" , "Scripts" ), result )
303+ } else {
304+ require .Equal (t , filepath .Join ("/path/to/.venv" , "bin" ), result )
305+ }
306+ }
307+
308+ func Test_ActivateVenvIfPresent (t * testing.T ) {
309+ tests := map [string ]struct {
310+ createVenv bool
311+ expectedActivated bool
312+ }{
313+ "activates venv when it exists" : {
314+ createVenv : true ,
315+ expectedActivated : true ,
316+ },
317+ "no-op when venv does not exist" : {
318+ createVenv : false ,
319+ expectedActivated : false ,
320+ },
321+ }
322+ for name , tc := range tests {
323+ t .Run (name , func (t * testing.T ) {
324+ fs := slackdeps .NewFsMock ()
325+ osMock := slackdeps .NewOsMock ()
326+ projectDir := "/path/to/project"
327+ venvPath := filepath .Join (projectDir , ".venv" )
328+
329+ originalPath := "/usr/bin:/bin"
330+ osMock .On ("Getenv" , "PATH" ).Return (originalPath )
331+ osMock .AddDefaultMocks ()
332+
333+ if tc .createVenv {
334+ // Create the pip executable so venvExists returns true
335+ pipPath := getPipExecutable (venvPath )
336+ err := fs .MkdirAll (filepath .Dir (pipPath ), 0755 )
337+ require .NoError (t , err )
338+ err = afero .WriteFile (fs , pipPath , []byte ("" ), 0755 )
339+ require .NoError (t , err )
340+ }
341+
342+ activated , err := ActivateVenvIfPresent (fs , osMock , projectDir )
343+ require .NoError (t , err )
344+ require .Equal (t , tc .expectedActivated , activated )
345+
346+ if tc .expectedActivated {
347+ expectedBinDir := getVenvBinDir (venvPath )
348+ osMock .AssertCalled (t , "Setenv" , "VIRTUAL_ENV" , venvPath )
349+ osMock .AssertCalled (t , "Setenv" , "PATH" , expectedBinDir + string (filepath .ListSeparator )+ originalPath )
350+ osMock .AssertCalled (t , "Unsetenv" , "PYTHONHOME" )
351+ } else {
352+ osMock .AssertNotCalled (t , "Setenv" , mock .Anything , mock .Anything )
353+ osMock .AssertNotCalled (t , "Unsetenv" , mock .Anything )
354+ }
355+ })
356+ }
357+ }
358+
299359func Test_Python_InstallProjectDependencies (t * testing.T ) {
300360 tests := map [string ]struct {
301361 existingFiles map [string ]string
0 commit comments