Skip to content

Commit c9158e5

Browse files
committed
fix: Don't write to tmpdir in CI
Skip writing to temporary file in CI which is then typically inaccessible from an already failed CI job. In those cases, it's better to dump directly to stderr, since that'll typically be captured by console logging. Inspired by zizmor
1 parent 539d6e0 commit c9158e5

2 files changed

Lines changed: 20 additions & 23 deletions

File tree

src/panic.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ pub fn setup_panic(meta: impl Fn() -> Metadata) {
1919

2020
panic::set_hook(Box::new(move |info: &PanicHookInfo<'_>| {
2121
let report = Report::with_panic(&meta, info);
22-
let file_path = report.persist().ok();
22+
let file_path = if is_ci() { None } else { report.persist().ok() };
2323
if file_path.is_none() {
2424
use std::io::Write as _;
2525
let stderr = std::io::stderr();
@@ -40,6 +40,11 @@ pub fn setup_panic(meta: impl Fn() -> Metadata) {
4040
}
4141
}
4242

43+
/// Returns whether we are running in a CI environment.
44+
fn is_ci() -> bool {
45+
std::env::var_os("CI").is_some()
46+
}
47+
4348
/// Style of panic to be used
4449
#[non_exhaustive]
4550
#[derive(Copy, Clone, PartialEq, Eq)]

tests/single-panic/tests/integration.rs

Lines changed: 14 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -127,11 +127,22 @@ fn release_with_ci() {
127127
.env("CI", "1")
128128
.assert()
129129
.stderr_eq(snapbox::str![[r#"
130+
name = "single-panic-test"
131+
operating_system = "[..]"
132+
crate_version = "0.1.0"
133+
explanation = """
134+
Panic occurred in file 'tests/single-panic/src/main.rs' at line 27
135+
"""
136+
cause = "OMG EVERYTHING IS ON FIRE!!!"
137+
method = "Panic"
138+
backtrace = [..]
139+
...
140+
130141
Well, this is embarrassing.
131142
132143
single-panic-test had a problem and crashed. To help us diagnose the problem you can send us a crash report.
133144
134-
We have generated a report file at "[..].toml". Submit an issue or email with the subject of "single-panic-test Crash Report" and include the report as an attachment.
145+
We have generated a report file at "<Failed to store file to disk>". Submit an issue or email with the subject of "single-panic-test Crash Report" and include the report as an attachment.
135146
136147
- Authors: Human Panic Authors <human-panic-crate@example.com>
137148
@@ -144,7 +155,7 @@ Thank you kindly!
144155

145156
#[cfg(unix)]
146157
{
147-
let mut files = root_path
158+
let files = root_path
148159
.read_dir()
149160
.unwrap()
150161
.map(|e| {
@@ -154,26 +165,7 @@ Thank you kindly!
154165
(path, content)
155166
})
156167
.collect::<Vec<_>>();
157-
assert_eq!(files.len(), 1, "{files:?}");
158-
let (_, report) = files.pop().unwrap();
159-
let report = report.unwrap();
160-
snapbox::assert_data_eq!(
161-
report,
162-
snapbox::str![[r#"
163-
name = "single-panic-test"
164-
operating_system = "[..]"
165-
crate_version = "0.1.0"
166-
explanation = """
167-
Panic occurred in file 'tests/single-panic/src/main.rs' at line [..]
168-
"""
169-
cause = "OMG EVERYTHING IS ON FIRE!!!"
170-
method = "Panic"
171-
backtrace = """
172-
...
173-
"""
174-
175-
"#]]
176-
);
168+
assert_eq!(files.len(), 0, "{files:?}");
177169
}
178170

179171
root.close().unwrap();

0 commit comments

Comments
 (0)