Open-Source Wikis

/

fd

/

How to contribute

/

Testing

sharkdp/fd

Testing

fd has two complementary layers of tests:

  1. Unit tests in #[cfg(test)] mod tests blocks inside production modules. These cover pure logic (size parsing, time parsing, owner parsing, format-template parsing, exit-code merging, regex helpers, hyperlink encoding, walk::search_str_for_entry, …).
  2. Integration tests in tests/tests.rs (~2,800 lines) that spawn the real binary, run it against a synthetic temporary directory tree, and assert on its stdout.

Unit tests

Located inline. Examples:

Module What it tests
src/regex_helper.rs pattern_has_uppercase_char, pattern_matches_strings_with_leading_dot.
src/filter/size.rs Parsing every supported size suffix (b, k/kb/ki/kib, …, t/tb/ti/tib) and the is_within predicate.
src/filter/time.rs TimeFilter::before/after over Span, Timestamp, DateTime, and @<unix-seconds> formats with a thread-local TESTTIME override.
src/filter/owner.rs OwnerFilter::from_string for every documented input.
src/exit_codes.rs merge_exitcodes truth table.
src/exec/mod.rs CommandSet/CommandTemplate parsing and placeholder expansion.
src/fmt/mod.rs and src/fmt/input.rs FormatTemplate::parse/generate and the basename/dirname/remove_extension helpers.
src/walk.rs search_str_for_entry for relative, absolute, and dot-prefixed paths.
src/filesystem.rs strip_current_dir.
src/hyperlink.rs Percent-encoding of non-ASCII path bytes.

Run only unit tests:

cargo test --lib

Integration tests and TestEnv

Integration tests live in tests/tests.rs and use the harness in tests/testenv/mod.rs. The harness:

  • Locates the just-built binary via the CARGO_BIN_EXE_fd env var that Cargo provides to integration tests (tests/testenv/mod.rs).
  • Creates a temporary directory under a fd-tests prefix (tempfile::Builder).
  • Pre-populates a fixed tree of directories, files, and one symlink (symlinkone/two). On Windows, the symlink only works if the test process has SeCreateSymbolicLinkPrivilege.
  • Drops a .fdignore containing fdignored.foo and a .gitignore containing gitignored.foo.
  • Pretends to be a git repository by mkdir-ing a .git/ directory at the temp root, so VCS-ignore handling kicks in.
  • Optionally creates a separate "config" temp dir holding fd/ignore for the global ignore file (create_config_directory_with_global_ignore).

The TestEnv::assert_output API runs the binary with given args and asserts on a normalised stdout string (the harness replaces \0 with NULL\n, optionally sorts whitespace-separated tokens per line, and produces a unified diff on failure via the diff crate).

Typical usage in tests/tests.rs:

let te = TestEnv::new();
te.assert_output(
    &["a.foo"],
    "a.foo\n\
     one/a.foo\n\
     one/two/a.foo\n\
     one/two/three/a.foo\n",
);

How to add a test

  1. Pick the right layer. Pure logic (parsers, predicates) belongs in a unit-test module next to the code. Anything that depends on the actual binary, the filesystem walker, or the global ignore file belongs in tests/tests.rs.
  2. For integration tests, copy an existing #[test] fn … and tailor the args. If you need a new file or directory in the fixture, add it to the lists at the top of create_working_directory in tests/testenv/mod.rs. Be aware that this affects every test — they all share the same tree shape.
  3. For platform-specific tests, use the standard #[cfg(unix)] / #[cfg(windows)] attributes. Several existing tests do this, especially around symlinks, owners, and path separators.

Running tests

# Everything
cargo test --all-features

# A single test
cargo test test_simple_search

# Integration tests only (the file in tests/)
cargo test --test tests

Test data outside the harness

A handful of tests rely on additional crates:

  • test-case for parameterised tests.
  • tempfile for ad-hoc temp directories beyond the harness.
  • filetime for setting precise mtimes when testing --changed-within / --changed-before.
  • diff for the unified-diff output on failure.

These are listed under [dev-dependencies] in Cargo.toml.

CI matrix

.github/workflows/CICD.yml runs cargo test across multiple targets (Linux, macOS, Windows, plus several musl and ARM variants) and additionally runs an MSRV job pinned to the version declared in Cargo.toml.

Built by Factory AutoWiki from public repository content. It is a generated preview for codebase exploration, not source-maintained documentation.

Testing – fd wiki | Factory