Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 26 additions & 1 deletion src/tutorial/testing.md
Original file line number Diff line number Diff line change
Expand Up @@ -472,7 +472,7 @@ at the end of the function,
the actual temporary file will automatically get deleted.

```rust,ignore
{{#include testing/tests/cli.rs:17:32}}
{{#include testing/tests/cli.rs:17:30}}
Comment thread
Greenheart marked this conversation as resolved.
Outdated
```

<aside class="exercise">
Expand All @@ -483,6 +483,31 @@ Adjust the program as needed.

</aside>

## Executing tests in a temporary directory
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is all focusing on teaching an API and is not fitting it within the narrative of the book

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will rewrite the section intro to fit better with the style of the section before and after.

For example, by connecting the newly introduced APIs to what the book has explored so far.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It also needs to be tied into the application we are testing

Copy link
Copy Markdown
Contributor Author

@Greenheart Greenheart Jan 19, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi again! Please review the combined diff for this PR to see the latest version :)

1: I rewrote the section and created a new test case to describe the new concepts in a way that fits better with the rest of the book. I opted to make the test and description slightly verbose and more explicit to give a better understanding. This way, we clearly show what happens when you set the current working directory, and explain why it works in one case but not the other.

This could technically be rewritten in a shorter format, skipping the nested directory. However, then we wouldn't have such a clear visual example with a directory structure when we should explain how the current working directory works.

I think it's worth a longer description and example, because the CWD is a very important concept for CLI tools, especially when accepting paths as CLI arguments.


2: I also removed the changes that were moved to a separate PR.


To fully control the behavior of your CLI app, you can use
`assert_fs::TempDir::new()` to create a temporary directory.
This is useful when you need to read and write
multiple directories or files. You can also use it as the
current working directory (CWD) when executing your CLI app,
ensuring test results are consistent and don't interfere with
other, unrelated files.

Temporary directories allow you to create multiple child
directories and files within them, enabling more advanced
test cases. Just like for single temporary files,
all nested files and directories within temporary directories
are also cleaned up after the test is completed.

```rust,ignore
{{#include testing/tests/cli.rs:32:56}}
```

You now know the core features of the crates `assert_cmd`,
`assert_fs` and `predicates` and are ready to start testing
your own CLI apps. Feel free to check out their documentation
to find more useful features.

## What to test?

While it can certainly be fun to write integration tests,
Expand Down
26 changes: 26 additions & 0 deletions src/tutorial/testing/tests/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,29 @@ fn find_content_in_file() -> Result<(), Box<dyn std::error::Error>> {

Ok(())
}

#[test]
fn find_content_in_file_of_tmp_dir() -> Result<(), Box<dyn std::error::Error>> {
let cwd = assert_fs::TempDir::new()?;

let child_dir = cwd.child("nested/child_dir");
let child_file = child_dir.child("sample.txt");

child_file.write_str("The first\ntest file.\nLast line of first file.")?;

// Files can be nested several levels within the temporary directory
assert!(child_file.path().ends_with("nested/child_dir/sample.txt"));

cargo_bin_cmd!("grrs")
// Execute in the temporary directory
.current_dir(cwd.path())
.arg("first")
.arg(child_file.path())
.assert()
.success()
.stdout(predicate::str::contains(
"The first\nLast line of first file.",
));

Ok(())
}