Skip to content

Commit

Permalink
Fix path trimming and support CARGO_WORKSPACE_DIR env var
Browse files Browse the repository at this point in the history
  • Loading branch information
tyranron committed Sep 12, 2022
1 parent 5edcf9f commit ad0bb22
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 5 deletions.
18 changes: 18 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,24 @@ All user visible changes to `cucumber` crate will be documented in this file. Th



## [0.14.1] · 2022-09-??
[0.14.1]: /../../tree/v0.14.1

[Diff](/../../compare/v0.14.0...v0.14.1) | [Milestone](/../../milestone/14)

### Changed

- Considered stripping `CARGO_WORKSPACE_DIR` from output paths whenever is defined. ([todo])

### Fixed

- `CARGO_MANIFEST_DIR` being detected in compile time. ([todo])

[todo]: /../../commit/todo




## [0.14.0] · 2022-09-08
[0.14.0]: /../../tree/v0.14.0

Expand Down
11 changes: 10 additions & 1 deletion book/src/output/intellij.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,23 @@ World::cucumber()
> 1. Because of [output interpretation issue][3], current timing reports for individual tests are accurate only for serial tests (or for all in case `--concurrency=1` CLI option is used);
> 2. Although debugger works, test window may select `Step` that didn't trigger the breakpoint. To fix this, use `--concurrency=1` CLI option.
> __TIP__: In the multi-crate [Cargo workspace], to support jump-to-definition in the reported paths ([step] or its matcher definition) correctly, consider to define [`CARGO_WORKSPACE_DIR` environment variable in the `.cargo/config.toml` file][4]:
> ```toml
> [env]
> CARGO_WORKSPACE_DIR = { value = "", relative = true }
> ```
[`cucumber`]: https://docs.rs/cucumber
[`writer::Basic`]: https://docs.rs/cucumber/*/cucumber/writer/struct.Basic.html
[`writer::Libtest`]: https://docs.rs/cucumber/*/cucumber/writer/struct.Libtest.html
[IntelliJ Rust]: https://www.jetbrains.com/rust/
[Cargo workspace]: https://doc.rust-lang.org/cargo/reference/workspaces.html
[IntelliJ Rust]: https://www.jetbrains.com/rust
[step]: https://cucumber.io/docs/gherkin/reference#steps
[1]: https://plugins.jetbrains.com/plugin/8182-rust/docs/rust-testing.html
[2]: https://plugins.jetbrains.com/plugin/8182-rust/docs/cargo-command-configuration.html
[3]: https://github.com/intellij-rust/intellij-rust/issues/9041
[4]: https://github.com/rust-lang/cargo/issues/3946#issuecomment-973132993
19 changes: 15 additions & 4 deletions src/writer/basic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

use std::{
borrow::Cow,
cmp,
cmp, env,
fmt::{Debug, Display},
io,
str::FromStr,
Expand All @@ -21,6 +21,7 @@ use std::{
use async_trait::async_trait;
use derive_more::{Deref, DerefMut};
use itertools::Itertools as _;
use once_cell::sync::Lazy;
use regex::CaptureLocations;

use crate::{
Expand Down Expand Up @@ -1090,10 +1091,20 @@ where
formatted
}

/// Trims start of the path if it matches with `CARGO_MANIFEST_DIR` environment
/// variable.
/// Trims start of the path if it matches the current project directory.
pub(crate) fn trim_path(path: &str) -> &str {
path.trim_start_matches(env!("CARGO_MANIFEST_DIR"))
/// Path of the current project directory.
static CURRENT_DIR: Lazy<String> = Lazy::new(|| {
env::var("CARGO_WORKSPACE_DIR")
.or_else(|_| env::var("CARGO_MANIFEST_DIR"))
.unwrap_or_else(|_| {
env::current_dir()
.map(|path| path.display().to_string())
.unwrap_or_default()
})
});

path.trim_start_matches(&**CURRENT_DIR)
.trim_start_matches('/')
.trim_start_matches('\\')
}

0 comments on commit ad0bb22

Please sign in to comment.