Our cargo binary is named
garden and it calls out to get user input multiple times, including calling
EDITOR and asking for user responses on stdout.
First we'll create a fake editor script to use instead of vim, etc.
tests/fake_editor.sh is a one-liner.
Then we'll write our test using
assert_cmd to build up a
Command that points to the
garden binary in out
We grab the fake editor path script using
std::env::current_dir(), make sure the file exists (which means we've grabbed the right path), and spawn a new
Command with the
EDITOR variable set to the fake editor script. The equivalent call to our bin is:
And the rust code to execute.
Note that we're asserting that the command exited sucessfully, and that we're inputing
N\n to the bin's stdin, which responds to a request for user input after the editor closes.
Note that this doesn't actually test for the resulting file existence, and also that we're writing our files out directly into our default digital garden! We can solve this with
We'll bring in both preludes and create a new temporary directory.
a prelude is a bunch of stuff the library author thinks will be useful to have around all the time
We'll use the
GARDEN_PATH environment variable to pass the temporary directory in to the
garden bin and that's it, we've added a temporary directory to our test.
No more polluting the default garden!
Now that's enough, but we can also check to make sure the file we expect to exist actually exists.
temp_dir.child gives us the possible file, and we can assert on that file with a test from
predicates that tests to make sure the file exists.