@@ -483,30 +483,90 @@ Adjust the program as needed.
483483
484484</aside >
485485
486- ## Executing tests in a temporary directory
487-
488- To fully control the behavior of your CLI app, you can use
489- ` assert_fs::TempDir::new() ` to create a temporary directory.
490- This is useful when you need to read and write
491- multiple directories or files. You can also use it as the
492- current working directory (CWD) when executing your CLI app,
493- ensuring test results are consistent and don't interfere with
494- other, unrelated files.
495-
496- Temporary directories allow you to create multiple child
497- directories and files within them, enabling more advanced
498- test cases. Just like for single temporary files,
499- all nested files and directories within temporary directories
500- are also cleaned up after the test is completed.
486+ ## Executing tests in temporary directories
487+
488+ In the last test, we created a temporary file and passed it as an
489+ _ absolute file path_ to our program. While this is enough to verify
490+ our core functionality, how could we write a test to ensure our program
491+ also accepts _ relative file paths_ like ` sample.txt ` as input?
492+
493+ The relative file path should be resolved based on the current working
494+ directory (CWD) where we execute our program, and this usually works fine
495+ in regular directories. However, when writing a test with temporary files,
496+ how do we know which directory where our test file was created?
497+
498+ One way to implement this test is by creating a temporary directory, and
499+ then setting it as the current working directory when executing our program.
500+
501+ Just like we created temporary files, we can use ` assert_fs ` to create
502+ temporary directories that are automatically deleted after the test is
503+ finished. To clearly show the effect of setting the current directory,
504+ and that our relative path ` sample.txt ` only can be resolved when we execute
505+ the program in the same directory as the file, we will create two nested
506+ temporary directories and a temporary file with the following structure:
507+
508+ ``` txt
509+ tmp_dir/
510+ child_dir/
511+ sample.txt
512+ ```
513+
514+ We can describe the test for our program with the following assertions:
515+
516+ 1 . It ** fails** to resolve the relative path ` sample.txt ` when executed
517+ with ` tmp_dir ` as the current working directory.
518+ 2 . It ** succeeds** to resolve the relative path ` sample.txt ` when executed
519+ with ` child_dir ` as the current working directory.
520+
521+ To create the temporary directory and file structure we need for our test,
522+ we start by creating a temporary directory ` tmp_dir ` by calling
523+ [ ` assert_fs::TempDir::new() ` ] [ TempDir ] . Then we create a child directory within it
524+ called ` child_dir ` , in which we finally create the ` sample.txt ` file.
525+
526+ [ TempDir ] : https://docs.rs/assert_fs/latest/assert_fs/struct.TempDir.html#method.new
527+
528+ ``` rust,ignore
529+ {{#include testing/tests/cli.rs:32:37}}
530+ {{#include testing/tests/cli.rs:54:56}}
531+ ```
532+
533+ For our first, failing assertion, we set the current working directory to
534+ ` tmp_dir ` by calling [ ` current_dir(&tmp_dir) ` ] [ current_dir ] . This way, ` sample.txt ` resolves
535+ to ` tmp_dir/sample.txt ` which does not exist, making the program exit with an error.
536+
537+ [ current_dir ] : https://docs.rs/assert_cmd/latest/assert_cmd/cmd/struct.Command.html#method.current_dir
538+
539+ ``` rust,ignore
540+ {{#include testing/tests/cli.rs:32:33}}
541+ // ...
542+
543+ {{#include testing/tests/cli.rs:39:45}}
544+ {{#include testing/tests/cli.rs:54:56}}
545+ ```
546+
547+ For our second, successful assertion, we instead call ` current_dir(&child_dir) ` .
548+ This makes the program successfully resolve ` sample.txt ` to
549+ ` tmp_dir/child_dir/sample.txt ` and print the expected result.
550+
551+ ``` rust,ignore
552+ {{#include testing/tests/cli.rs:32:33}}
553+ // ...
554+
555+ {{#include testing/tests/cli.rs:47:53}}
556+ {{#include testing/tests/cli.rs:54:56}}
557+ ```
558+
559+ Putting it all together, the complete test case looks like this:
501560
502561``` rust,ignore
503562{{#include testing/tests/cli.rs:32:56}}
504563```
505564
506- You now know the core features of the crates ` assert_cmd ` ,
507- ` assert_fs ` and ` predicates ` and are ready to start testing
508- your own CLI apps. Feel free to check out their documentation
509- to find more useful features.
565+ You now know the core features of the crates ` assert_cmd ` , ` assert_fs ` and
566+ ` predicates ` and are ready to test your own CLI apps in ways that ensure
567+ consistent test results and without interfering with other, unrelated files.
568+ You can learn about more useful features in these crates by exploring
569+ their documentation.
510570
511571## What to test?
512572
0 commit comments