Skip to content

feat(rust): relax project root path validation - #154

Open
nizos wants to merge 8 commits into
mainfrom
feat/139-rust-relax-project-root
Open

feat(rust): relax project root path validation#154
nizos wants to merge 8 commits into
mainfrom
feat/139-rust-relax-project-root

Conversation

@nizos

@nizos nizos commented Apr 12, 2026

Copy link
Copy Markdown
Owner

Summary

The Rust reporter no longer requires an absolute path for --project-root, and the flag itself is now optional. Relative paths and paths containing .. are resolved against the current working directory. The reporter now reads TDD_GUARD_PROJECT_ROOT as a fallback and errors when no project root is configured.

Details

  • The project root is now resolved rather than validated. std::fs::canonicalize handles absolute, relative, and .. paths uniformly, replacing the previous is_absolute and exists checks in main().
  • TDD_GUARD_PROJECT_ROOT environment variable is read when no --project-root flag is provided. When neither is configured, the reporter errors with a descriptive message.
  • main() logic is extracted into a testable run(args, base_dir) function. base_dir is injected so tests use isolated temp dirs without global cwd mutation, allowing parallel execution.
  • A TestContext struct consolidates repeated test setup — temp dir creation, path construction, file reading, and JSON parsing — into a builder with helpers at the bottom of the test file.
  • Pre-existing clippy warnings and formatting issues across the reporter are resolved in dedicated commits.

Notes

  • The rationale for the relaxation is recorded in ADR-009 and ADR-010.

@nizos

nizos commented Apr 12, 2026

Copy link
Copy Markdown
Owner Author

@104hp6u Can I get a review on this? :)

nizos and others added 8 commits April 12, 2026 11:59
Introduces resolve_project_root using std::fs::canonicalize to handle
absolute, relative, and `..` paths uniformly. Defaults to cwd when
no project root is provided. Not yet wired into main — existing
validation in main() is unchanged.

Note: tests that mutate cwd require --test-threads=1 to avoid races.

Per ADR-009. Refs #139.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Adds a base_dir parameter so relative paths resolve against an
explicit directory instead of the process-global cwd. This makes
the function pure and allows tests to run in parallel without
cwd mutation races.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
…t optional

Extracts main() logic into a testable run(args, base_dir) function
that uses resolve_project_root for path handling. The --project-root
flag is now optional — omitting it defaults to the current working
directory. base_dir is injected for testability so tests use isolated
temp dirs with no global cwd mutation.

Per ADR-009. Refs #139.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Introduces TestContext struct with setup(), with_sub_dir(),
test_json_path(), read_test_output(), process_and_read(), and
make_args(). Consolidates repeated temp dir creation, path
construction, file reading, and JSON parsing. Helpers placed at
bottom of the test module, tests at the top.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Remove unnecessary borrow in detect_runner args, derive Default
instead of manual impl for TddGuardOutput, simplify identical
if/else branches in reason logic, use .first() instead of .get(0),
and remove unused has_test_pass variable.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Update examples, flag description, and project root documentation
to reflect that --project-root is now optional and accepts relative
paths.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Reads TDD_GUARD_PROJECT_ROOT as a fallback when no --project-root flag
is provided. Errors when neither is configured instead of silently
defaulting to cwd, which caused test results to land in the wrong
directory when tests ran from a subdirectory.

Per ADR-010. Refs #139.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@nizos
nizos force-pushed the feat/139-rust-relax-project-root branch from 21f66ff to 7fc7479 Compare April 12, 2026 10:32
@nizos

nizos commented Apr 22, 2026

Copy link
Copy Markdown
Owner Author

Can I get a review on this @vnsamy-code @Hiro-Chiba? :)

@vnsamy-code

Copy link
Copy Markdown
Contributor

Looks great to me!

I was wondering if all the reporter tests should include a test for this behaviour: "TDD_GUARD_PROJECT_ROOT environment variable is read when no --project-root flag is provided. "

--project-root has higher precedence over TDD_GUARD_PROJECT_ROOT env var.

nizos pushed a commit that referenced this pull request May 10, 2026
Apply ADR-009 and ADR-010 to the Ruby reporters, mirroring PR #151
(PHPUnit), #153 (Go), and #154 (Rust):

- Accept relative paths and paths containing "..", resolved with
  File.expand_path against the working directory.
- Raise ArgumentError when TDD_GUARD_PROJECT_ROOT is not configured
  instead of silently defaulting to cwd. Per ADR-010, this is an
  intentional breaking change so misconfiguration surfaces upfront.
- Preserve the cwd-within-root sanity check on the resolved path,
  using File.realpath to canonicalize symlinks (macOS /var ->
  /private/var) so comparisons are stable.

The Minitest reporter's `.handle_load_error` and
`.append_unhandled_errors` class methods rescue the configuration
ArgumentError so the autorun at_exit hooks do not double-raise when
the env var is missing.

Closes #173, #174.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants