Skip to content

Commit

Permalink
Pwsh -> PowerShell in messages
Browse files Browse the repository at this point in the history
Fix stale comment
Use noun instead of verb in arg name; rename related code accordingly
Make test check key name and initial symbols
  • Loading branch information
LittleBoxOfSunshine committed Jan 10, 2025
1 parent 641e451 commit 72ce2fd
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 26 deletions.
2 changes: 2 additions & 0 deletions .github/.cspell/project-dictionary.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,10 @@ microkernel
MSYSTEM
nextest
notcovered
PowerShell
profdata
profraw
pwsh
rmeta
rustfilt
rustix
Expand Down
6 changes: 2 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -477,13 +477,11 @@ Note: cargo-llvm-cov subcommands other than `report` and `clean` may not work co

Note: To include coverage for doctests you also need to pass `--doctests` to both `cargo llvm-cov show-env` and `cargo llvm-cov report`.

> The same thing can be achieved in pwsh by substituting the source command with:
> The same thing can be achieved in PowerShell 6+ by substituting the source command with:
>
> ```powershell
> Invoke-Expression (cargo llvm-cov show-env --export-pwsh-prefix | Out-String)
> Invoke-Expression (cargo llvm-cov show-env --with-pwsh-env-prefix | Out-String)
> ```
>
> (verified for PowerShell 7 only, results may vary on older versions)
### Exclude file from coverage
Expand Down
4 changes: 2 additions & 2 deletions docs/cargo-llvm-cov-show-env.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ OPTIONS:
--export-prefix
Prepend "export " to each line, so that the output is suitable to be sourced by bash

--export-pwsh-prefix
--with-pwsh-env-prefix
Unicode escape and double quote values + prepend "$env:", so that the output is suitable
to be used with Invoke-Expression in pwsh.
to be used with Invoke-Expression in PowerShell 6+.

--doctests
Including doc tests (unstable)
Expand Down
26 changes: 13 additions & 13 deletions src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -375,20 +375,20 @@ pub(crate) enum ShowEnvFormat {
EscapedKeyValuePair,
/// Prepend "export " to each line, so that the output is suitable to be sourced by bash.
UnixExport,
/// Each value: "$env:{key}=@'\n{}\n", escaped using [`shell_escape::escape`].
PwshExport,
/// Each value: "$env:{key}={value}", where {value} is PowerShell Unicode escaped e.g. "`u{72}".
Pwsh,
}

impl ShowEnvFormat {
pub(crate) fn new(export_prefix: bool, export_pwsh_prefix: bool) -> Result<Self> {
if export_prefix && export_pwsh_prefix {
conflicts("--export-prefix", "--export-pwsh-prefix")?;
pub(crate) fn new(export_prefix: bool, with_pwsh_env_prefix: bool) -> Result<Self> {
if export_prefix && with_pwsh_env_prefix {
conflicts("--export-prefix", "--with-pwsh-env-prefix")?;
}

Ok(if export_prefix {
ShowEnvFormat::UnixExport
} else if export_pwsh_prefix {
ShowEnvFormat::PwshExport
} else if with_pwsh_env_prefix {
ShowEnvFormat::Pwsh
} else {
ShowEnvFormat::EscapedKeyValuePair
})
Expand All @@ -402,7 +402,7 @@ impl ShowEnvFormat {
ShowEnvFormat::UnixExport => {
format!("export {key}={}", shell_escape::escape(value.into()))
}
ShowEnvFormat::PwshExport => {
ShowEnvFormat::Pwsh => {
// PowerShell 6+ expects encoded UTF-8 text. Some env vars like CARGO_ENCODED_RUSTFLAGS
// have non-printable binary characters. We can work around this and any other escape
// related considerations by just escaping all characters. Rust's Unicode escape is
Expand Down Expand Up @@ -583,7 +583,7 @@ impl Args {

// show-env options
let mut export_prefix = false;
let mut export_pwsh_prefix = false;
let mut with_pwsh_env_prefix = false;

// options ambiguous between nextest-related and others
let mut profile = None;
Expand Down Expand Up @@ -763,7 +763,7 @@ impl Args {

// show-env options
Long("export-prefix") => parse_flag!(export_prefix),
Long("export-pwsh-prefix") => parse_flag!(export_pwsh_prefix),
Long("with-pwsh-env-prefix") => parse_flag!(with_pwsh_env_prefix),

// ambiguous between nextest-related and others will be handled later
Long("archive-file") => parse_opt_passthrough!(archive_file),
Expand Down Expand Up @@ -868,13 +868,13 @@ impl Args {

// unexpected options
let show_env_format = match subcommand {
Subcommand::ShowEnv => ShowEnvFormat::new(export_prefix, export_pwsh_prefix)?,
Subcommand::ShowEnv => ShowEnvFormat::new(export_prefix, with_pwsh_env_prefix)?,
_ => {
if export_prefix {
unexpected("--export-prefix", subcommand)?;
}
if export_pwsh_prefix {
unexpected("--export-pwsh-prefix", subcommand)?;
if with_pwsh_env_prefix {
unexpected("--with-pwsh-env-prefix", subcommand)?;
}
ShowEnvFormat::default()
}
Expand Down
13 changes: 6 additions & 7 deletions tests/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -295,16 +295,15 @@ fn show_env() {

cargo_llvm_cov("show-env")
.env("CARGO_ENCODED_RUSTFLAGS", flags)
.arg("--export-pwsh-prefix")
.arg("--with-pwsh-env-prefix")
.assert_success()
// Verify the 2 prefix related elements
.stdout_contains("$env:")
.stdout_contains("\"`u{")
// Verify the prefix related content + the encoding of "--"
.stdout_contains("$env:CARGO_ENCODED_RUSTFLAGS=\"`u{2d}`u{2d}")
// Verify binary character didn't lead to incompatible output for pwsh
.stdout_contains("`u{1f}");
cargo_llvm_cov("show-env")
.arg("--export-prefix")
.arg("--export-pwsh-prefix")
.arg("--with-pwsh-env-prefix")
.assert_failure()
.stderr_contains("may not be used together with");
}
Expand All @@ -320,9 +319,9 @@ fn invalid_arg() {
.assert_failure()
.stderr_contains("invalid option '--export-prefix'");
cargo_llvm_cov(subcommand)
.arg("--export-pwsh-prefix")
.arg("--with-pwsh-env-prefix")
.assert_failure()
.stderr_contains("invalid option '--export-pwsh-prefix'");
.stderr_contains("invalid option '--with-pwsh-env-prefix'");
}
if !matches!(subcommand, "" | "test") {
if matches!(subcommand, "nextest" | "nextest-archive") {
Expand Down

0 comments on commit 72ce2fd

Please sign in to comment.