Skip to content

Commit bcb21f4

Browse files
committed
Describe how to create temporary directories, and how to execute the CLI
test with a specific current working directory (CWD). This expands on the basic examples and ensures the common needs for CLI app testing are covered. Hopefully, this will make testing more accessible to people reading the book.
1 parent 332f60a commit bcb21f4

File tree

2 files changed

+54
-1
lines changed

2 files changed

+54
-1
lines changed

src/tutorial/testing.md

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -472,7 +472,7 @@ at the end of the function,
472472
the actual temporary file will automatically get deleted.
473473

474474
```rust,ignore
475-
{{#include testing/tests/cli.rs:17:32}}
475+
{{#include testing/tests/cli.rs:17:30}}
476476
```
477477

478478
<aside class="exercise">
@@ -483,6 +483,31 @@ 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.
501+
502+
```rust,ignore
503+
{{#include testing/tests/cli.rs:31:56}}
504+
```
505+
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.
510+
486511
## What to test?
487512

488513
While it can certainly be fun to write integration tests,

src/tutorial/testing/tests/cli.rs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
use std::path::Path;
2+
13
use assert_cmd::cargo::*; // Import cargo_bin_cmd! macro and methods
24
use predicates::prelude::*; // Used for writing assertions
35

@@ -28,3 +30,29 @@ fn find_content_in_file() -> Result<(), Box<dyn std::error::Error>> {
2830

2931
Ok(())
3032
}
33+
34+
#[test]
35+
fn find_content_in_file_of_tmp_dir() -> Result<(), Box<dyn std::error::Error>> {
36+
let cwd = assert_fs::TempDir::new()?;
37+
38+
let child_dir = cwd.child("nested/child_dir");
39+
let child_file = child_dir.child("sample.txt");
40+
41+
child_file.write_str("The first\ntest file.\nLast line of first file.")?;
42+
43+
// Files can be nested several levels within the temporary directory
44+
assert!(child_file.path().ends_with("nested/child_dir/sample.txt"));
45+
46+
cargo_bin_cmd!("grrs")
47+
// Execute in the temporary directory
48+
.current_dir(cwd.path())
49+
.arg("first")
50+
.arg(child_file.path())
51+
.assert()
52+
.success()
53+
.stdout(predicate::str::contains(
54+
"The first\nLast line of first file.",
55+
));
56+
57+
Ok(())
58+
}

0 commit comments

Comments
 (0)