Skip to content

Commit f1186e4

Browse files
committed
feat(e2e): output logs in expandable group when running in github actions
1 parent 81b9bf8 commit f1186e4

File tree

3 files changed

+52
-17
lines changed

3 files changed

+52
-17
lines changed
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
use crate::utils::is_running_in_github_actions;
2+
3+
/// Handle printing a log group title and a separator.
4+
///
5+
/// If running in GitHub actions, the logs produced between the creation of the group and its
6+
/// drop will be wrapped within a `::group::` and `::endgroup::` github action command.
7+
pub struct LogGroup;
8+
9+
impl LogGroup {
10+
pub fn new(name: &str, title: &str) -> Self {
11+
let group_title = Self::group_title(name, title);
12+
if is_running_in_github_actions() {
13+
println!("::group::{group_title}");
14+
}
15+
Self::print_header(&group_title);
16+
17+
Self
18+
}
19+
20+
fn group_title(name: &str, title: &str) -> String {
21+
format!("{} LOGS - {}:", name.to_uppercase(), title)
22+
}
23+
24+
fn print_header(title: &str) {
25+
println!("{:-^100}", "");
26+
println!("{title:^30}",);
27+
println!("{:-^100}", "");
28+
}
29+
}
30+
31+
impl Drop for LogGroup {
32+
fn drop(&mut self) {
33+
if is_running_in_github_actions() {
34+
println!("::endgroup::");
35+
}
36+
}
37+
}

mithril-test-lab/mithril-end-to-end/src/utils/mithril_command.rs

Lines changed: 9 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::utils::file_utils;
1+
use crate::utils::{LogGroup, file_utils};
22
use anyhow::{Context, anyhow};
33
use mithril_common::StdResult;
44
use slog_scope::info;
@@ -146,7 +146,10 @@ impl MithrilCommand {
146146
));
147147
}
148148

149-
self.print_header(name, &format!("LAST {number_of_line} LINES"));
149+
let _log_group_guard = LogGroup::new(
150+
name.unwrap_or(&self.name),
151+
&format!("LAST {number_of_line} LINES"),
152+
);
150153

151154
println!(
152155
"{}",
@@ -172,7 +175,10 @@ impl MithrilCommand {
172175
));
173176
}
174177

175-
self.print_header(name, &format!("LAST {number_of_error} ERROR(S)"));
178+
let _log_group_guard = LogGroup::new(
179+
name.unwrap_or(&self.name),
180+
&format!("LAST {number_of_error} ERROR(S)"),
181+
);
176182

177183
println!(
178184
"{}",
@@ -183,18 +189,4 @@ impl MithrilCommand {
183189

184190
Ok(())
185191
}
186-
187-
fn print_header(&self, name: Option<&str>, title: &str) {
188-
let name = match name {
189-
Some(n) => n,
190-
None => &self.name,
191-
};
192-
193-
println!("{:-^100}", "");
194-
println!(
195-
"{:^30}",
196-
format!("{} LOGS - {}:", name.to_uppercase(), title)
197-
);
198-
println!("{:-^100}", "");
199-
}
200192
}

mithril-test-lab/mithril-end-to-end/src/utils/mod.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,12 @@ mod mithril_command;
22
#[macro_use]
33
mod spec_utils;
44
mod file_utils;
5+
mod formatting;
56

7+
pub use formatting::*;
68
pub use mithril_command::MithrilCommand;
79
pub use spec_utils::AttemptResult;
10+
11+
pub fn is_running_in_github_actions() -> bool {
12+
std::env::var("GITHUB_ACTIONS").is_ok()
13+
}

0 commit comments

Comments
 (0)