h3.
h3.

Reputation: 11068

Testing a python script which use a subprocess to spawn a bash subshell .. is it possible?

I have a quite complex project which I'd like to test .. but I can't find how to test it entirely.

The project itself is a development environment for django. It consist of a Python script which can be used to create a new project or activate a development environment.

To activate a development environment, I use a technique similar Virtualenv. My python script invoke a shell script which itself source a rcfile.

It sounds more complicated than it really is. The work flow looks like this:

$ cd my-project/
$ duke dev
(my-project)$

duke is my Python script which call a bash script named dev. The dev script contains only a line like this:

#!/bin/bash . /path/to/bin/env

So it basically just starts a subshell in which I source a rcfile named env.

The env itself take care to create all the necessary commands and environment variables needed while developing.

My problem is that I cannot test anything that is specific to this env because my script spawns a subprocess which then activate the env in a subshell. The subprocess closes and the subshell is left open until I exit it.

While it works, I can't find a way to reliably test it.

Is it even possible to test this ?

Here's the code:

Upvotes: 1

Views: 761

Answers (1)

groovyspaceman
groovyspaceman

Reputation: 2814

Any script you run will always spawn a subshell.

You should call . %(base_path)s/.duke/bin/env before you start your python code, either on the command-line or as part of the same script which spawns the python.

Don't forget to export variables and functions which you may use in sub-processes.

Upvotes: 1

Related Questions