Skip to content

Add TRYEXPAND_DEBUG_LOG CLI option #43

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Nov 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ Please make sure to add your changes to the appropriate categories:

### Added

- n/a
- Added `TRYEXPAND_DEBUG_LOG` CLI option for turning on debug logging (use with `-- --nocapture` for optimal results).

### Changed

Expand Down
20 changes: 18 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -223,12 +223,28 @@ cargo +1.76.0 test <OPTIONS>

For each `expand()`-like method call within your tests a temporary and uniquely named Rust project will get generated within `$CARGO_TARGET_DIR/target/tests/`.
By default these projects will get deleted upon test completion (regardless of the outcome).
If you wish to take a look at the actual code/projects being expanded you can provide `TRYEXPAND_KEEP_ARTIFACTS=1` (e.g. `$ TRYEXPAND_KEEP_ARTIFACTS=1 cargo test`) and `tryexpand` will skip the cleanup.
If you wish to take a look at the actual code/projects being expanded you can provide `TRYEXPAND_KEEP_ARTIFACTS=1` and `tryexpand` will skip the cleanup:

```terminal
TRYEXPAND_KEEP_ARTIFACTS=1 cargo test
```

#### `TRYEXPAND_TRUNCATE_OUTPUT`

By default `tryexpand` truncates console output that's longer than 100 lines.
If you wish to temporarily turn this behavior you can provide `TRYEXPAND_TRUNCATE_OUTPUT=0` (e.g. `$ TRYEXPAND_TRUNCATE_OUTPUT=0 cargo test`) and `tryexpand` will produce the full console output.
If you wish to temporarily turn this behavior you can provide `TRYEXPAND_TRUNCATE_OUTPUT=0` and `tryexpand` will produce the full console output:

```terminal
TRYEXPAND_TRUNCATE_OUTPUT=0 cargo test
```

#### `TRYEXPAND_DEBUG_LOG`

If you wish to see the exact commands that `tryexpand` is executing you can provide `TRYEXPAND_DEBUG_LOG=1` (together with `--nocapture`, if you also wish to see logs of passing tests) and `tryexpand` will print additional debug logs:

```terminal
TRYEXPAND_DEBUG_LOG=0 cargo test -- --nocapture
```

## Contributing

Expand Down
15 changes: 14 additions & 1 deletion src/cargo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use crate::{
options::Options,
project::Project,
test::{Test, TestStatus},
utils::should_debug_log,
};

const RUSTFLAGS_ENV_KEY: &str = "RUSTFLAGS";
Expand All @@ -19,7 +20,6 @@ fn raw_cargo() -> Command {
fn cargo(project: &Project) -> Command {
let mut cmd = raw_cargo();
cmd.current_dir(&project.dir);
cmd.env("CARGO_TARGET_DIR", &project.target_dir);
cmd.env(RUSTFLAGS_ENV_KEY, make_rustflags_env());
cmd
}
Expand Down Expand Up @@ -232,13 +232,26 @@ fn run_cargo_command(mut cargo: Command, options: &Options) -> Result<CargoOutpu
cargo.env(key, value);
}

if should_debug_log().unwrap_or(false) {
println!("Command: {:?}", cargo);
println!("Environment: {:?}", options.envs);
println!();
}

let output = cargo
.output()
.map_err(|err| Error::CargoExpandExecution(err.to_string()))?;

let stdout = Some(String::from_utf8_lossy(&output.stdout).into_owned());
let stderr = Some(String::from_utf8_lossy(&output.stderr).into_owned());

if should_debug_log().unwrap_or(false) {
println!("Stdout:\n{}", stdout.as_deref().unwrap_or("None"));
println!();
println!("Stderr:\n{}", stderr.as_deref().unwrap_or("None"));
println!();
}

let evaluation = if output.status.success() {
TestStatus::Success
} else {
Expand Down
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ pub(crate) const TRYEXPAND_ENV_VAL_EXPECT: &str = "expect";

pub(crate) const TRYEXPAND_KEEP_ARTIFACTS_ENV_KEY: &str = "TRYEXPAND_KEEP_ARTIFACTS";
pub(crate) const TRYEXPAND_TRUNCATE_OUTPUT_ENV_KEY: &str = "TRYEXPAND_TRUNCATE_OUTPUT";
pub(crate) const TRYEXPAND_DEBUG_LOG_ENV_KEY: &str = "TRYEXPAND_DEBUG_LOG";

pub(crate) const OUT_RS_FILE_SUFFIX: &str = "out.rs";
pub(crate) const OUT_TXT_FILE_SUFFIX: &str = "out.txt";
Expand Down
16 changes: 16 additions & 0 deletions src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,19 @@ where
source,
})
}

pub(crate) fn should_debug_log() -> Result<bool> {
let key = crate::TRYEXPAND_DEBUG_LOG_ENV_KEY;
let Some(var) = std::env::var_os(key) else {
return Ok(false);
};
let value = var.to_string_lossy().to_lowercase().to_owned();
match value.as_str() {
"1" | "yes" | "true" => Ok(true),
"0" | "no" | "false" => Ok(false),
_ => Err(Error::UnrecognizedEnv {
key: key.to_owned(),
value,
}),
}
}