Skip to content

Commit f57aae1

Browse files
committed
Add --log-group option to opt-out/force-enable log grouping
1 parent dd5481a commit f57aae1

File tree

7 files changed

+115
-17
lines changed

7 files changed

+115
-17
lines changed

README.md

+5
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,11 @@ OPTIONS:
202202
--keep-going
203203
Keep going on failure.
204204

205+
--log-group <KIND>
206+
Log grouping: none, github-actions.
207+
208+
If this option is not used, the environment will be automatically detected.
209+
205210
--print-command-list
206211
Print commands without run (Unstable).
207212

src/cli.rs

+15-2
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use lexopt::{
1111
ValueExt,
1212
};
1313

14-
use crate::{term, version::VersionRange, Feature, Rustup};
14+
use crate::{term, version::VersionRange, Feature, LogGroup, Rustup};
1515

1616
pub(crate) struct Args {
1717
pub(crate) leading_args: Vec<String>,
@@ -51,10 +51,12 @@ pub(crate) struct Args {
5151
pub(crate) keep_going: bool,
5252
/// --print-command-list
5353
pub(crate) print_command_list: bool,
54-
/// --version-range
54+
/// --version-range/--rust-version
5555
pub(crate) version_range: Option<VersionRange>,
5656
/// --version-step
5757
pub(crate) version_step: u16,
58+
/// --log-group
59+
pub(crate) log_group: LogGroup,
5860

5961
// options for --each-feature and --feature-powerset
6062
/// --optional-deps [DEPS]...
@@ -152,6 +154,7 @@ impl Args {
152154
let mut rust_version = false;
153155
let mut version_range = None;
154156
let mut version_step = None;
157+
let mut log_group: Option<String> = None;
155158

156159
let mut optional_deps = None;
157160
let mut include_features = vec![];
@@ -240,6 +243,7 @@ impl Args {
240243
Long("rust-version") => parse_flag!(rust_version),
241244
Long("version-range") => parse_opt!(version_range, false),
242245
Long("version-step") => parse_opt!(version_step, false),
246+
Long("log-group") => parse_opt!(log_group, false),
243247

244248
Short('p') | Long("package") => package.push(parser.value()?.parse()?),
245249
Long("exclude") => exclude.push(parser.value()?.parse()?),
@@ -544,6 +548,11 @@ impl Args {
544548
bail!("--version-step cannot be zero");
545549
}
546550

551+
let log_group = match log_group {
552+
Some(v) => v.parse()?,
553+
None => LogGroup::auto(),
554+
};
555+
547556
if no_dev_deps {
548557
info!(
549558
"--no-dev-deps removes dev-dependencies from real `Cargo.toml` while cargo-hack is running and restores it when finished"
@@ -593,6 +602,7 @@ impl Args {
593602
include_deps_features,
594603
version_range,
595604
version_step,
605+
log_group,
596606

597607
depth,
598608
group_features,
@@ -789,6 +799,9 @@ const HELP: &[HelpText<'_>] = &[
789799
"This flag can only be used together with --version-range flag.",
790800
]),
791801
("", "--keep-going", "", "Keep going on failure", &[]),
802+
("", "--log-group", "<KIND>", "Log grouping: none, github-actions", &[
803+
"If this option is not used, the environment will be automatically detected."
804+
]),
792805
("", "--print-command-list", "", "Print commands without run (Unstable)", &[]),
793806
("", "--no-manifest-path", "", "Do not pass --manifest-path option to cargo (Unstable)", &[]),
794807
("-v", "--verbose", "", "Use verbose output", &[]),

src/context.rs

-3
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ pub(crate) struct Context {
2727
pub(crate) cargo_version: u32,
2828
pub(crate) restore: restore::Manager,
2929
pub(crate) current_dir: PathBuf,
30-
pub(crate) use_github_action_grouping: bool,
3130
}
3231

3332
impl Context {
@@ -74,8 +73,6 @@ impl Context {
7473
cargo_version,
7574
restore,
7675
current_dir: env::current_dir()?,
77-
// TODO: should be optional?
78-
use_github_action_grouping: env::var_os("GITHUB_ACTIONS").is_some(),
7976
};
8077

8178
// TODO: Ideally, we should do this, but for now, we allow it as cargo-hack

src/main.rs

+46-12
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,10 @@ use std::{
2929
collections::BTreeMap,
3030
env,
3131
fmt::{self, Write},
32+
str::FromStr,
3233
};
3334

34-
use anyhow::{bail, format_err, Result};
35+
use anyhow::{bail, format_err, Error, Result};
3536

3637
use crate::{
3738
context::Context,
@@ -510,6 +511,36 @@ impl fmt::Display for KeepGoing {
510511
}
511512
}
512513

514+
#[derive(Clone, Copy, PartialEq)]
515+
enum LogGroup {
516+
None,
517+
GithubActions,
518+
}
519+
520+
impl LogGroup {
521+
fn auto() -> Self {
522+
if env::var_os("GITHUB_ACTIONS").is_some() {
523+
Self::GithubActions
524+
} else {
525+
Self::None
526+
}
527+
}
528+
}
529+
530+
impl FromStr for LogGroup {
531+
type Err = Error;
532+
533+
fn from_str(s: &str) -> std::result::Result<Self, Self::Err> {
534+
match s {
535+
"none" => Ok(Self::None),
536+
"github-actions" => Ok(Self::GithubActions),
537+
other => bail!(
538+
"argument for --log-group must be none or github-actions, but found `{other}`"
539+
),
540+
}
541+
}
542+
}
543+
513544
fn exec_cargo(
514545
cx: &Context,
515546
id: &PackageId,
@@ -540,7 +571,7 @@ fn exec_cargo_inner(
540571
line: &mut ProcessBuilder<'_>,
541572
progress: &mut Progress,
542573
) -> Result<()> {
543-
if progress.count != 0 && !cx.print_command_list && !cx.use_github_action_grouping {
574+
if progress.count != 0 && !cx.print_command_list && cx.log_group == LogGroup::None {
544575
eprintln!();
545576
}
546577
progress.count += 1;
@@ -562,18 +593,21 @@ fn exec_cargo_inner(
562593
write!(msg, "running {line} on {}", cx.packages(id).name).unwrap();
563594
}
564595
write!(msg, " ({}/{})", progress.count, progress.total).unwrap();
565-
let _guard = if cx.use_github_action_grouping {
566-
struct Guard;
567-
impl Drop for Guard {
568-
fn drop(&mut self) {
569-
println!("::endgroup::");
596+
let _guard = match cx.log_group {
597+
LogGroup::GithubActions => {
598+
struct Guard;
599+
impl Drop for Guard {
600+
fn drop(&mut self) {
601+
println!("::endgroup::");
602+
}
570603
}
604+
println!("::group::{msg}");
605+
Some(Guard)
606+
}
607+
LogGroup::None => {
608+
info!("{msg}");
609+
None
571610
}
572-
println!("::group::{msg}");
573-
Some(Guard)
574-
} else {
575-
info!("{msg}");
576-
None
577611
};
578612

579613
line.run()

tests/long-help.txt

+5
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,11 @@ OPTIONS:
173173
--keep-going
174174
Keep going on failure.
175175

176+
--log-group <KIND>
177+
Log grouping: none, github-actions.
178+
179+
If this option is not used, the environment will be automatically detected.
180+
176181
--print-command-list
177182
Print commands without run (Unstable).
178183

tests/short-help.txt

+1
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ OPTIONS:
4343
command
4444
--clean-per-version Remove artifacts per Rust version
4545
--keep-going Keep going on failure
46+
--log-group <KIND> Log grouping: none, github-actions
4647
--print-command-list Print commands without run (Unstable)
4748
--no-manifest-path Do not pass --manifest-path option to cargo (Unstable)
4849
-v, --verbose Use verbose output

tests/test.rs

+43
Original file line numberDiff line numberDiff line change
@@ -299,6 +299,49 @@ fn exclude_failure() {
299299
.stderr_contains("--exclude can only be used together with --workspace");
300300
}
301301

302+
#[test]
303+
fn log_group() {
304+
cargo_hack(["check", "--all", "--log-group", "none"])
305+
.assert_success("virtual")
306+
.stderr_contains(
307+
"
308+
running `cargo check` on member1
309+
running `cargo check` on member2
310+
",
311+
)
312+
.stdout_not_contains(
313+
"
314+
::endgroup::
315+
::group::
316+
",
317+
)
318+
.stderr_not_contains(
319+
"
320+
::endgroup::
321+
::group::
322+
",
323+
);
324+
325+
cargo_hack(["check", "--all", "--log-group", "github-actions"])
326+
.assert_success("virtual")
327+
.stdout_contains(
328+
"
329+
::group::running `cargo check` on member1
330+
::endgroup::
331+
::group::running `cargo check` on member2
332+
::endgroup::
333+
",
334+
)
335+
.stderr_not_contains(
336+
"
337+
::group::running `cargo check` on member1
338+
::endgroup::
339+
::group::running `cargo check` on member2
340+
::endgroup::
341+
",
342+
);
343+
}
344+
302345
#[test]
303346
fn no_dev_deps() {
304347
cargo_hack(["check", "--no-dev-deps"]).assert_success("real").stderr_contains(

0 commit comments

Comments
 (0)