Skip to content

Commit 46060db

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 46060db

2 files changed

Lines changed: 21 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: 15 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -127,11 +127,23 @@ 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+
"""
141+
130142
Well, this is embarrassing.
131143
132144
single-panic-test had a problem and crashed. To help us diagnose the problem you can send us a crash report.
133145
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.
146+
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.
135147
136148
- Authors: Human Panic Authors <human-panic-crate@example.com>
137149
@@ -144,7 +156,7 @@ Thank you kindly!
144156

145157
#[cfg(unix)]
146158
{
147-
let mut files = root_path
159+
let files = root_path
148160
.read_dir()
149161
.unwrap()
150162
.map(|e| {
@@ -154,26 +166,7 @@ Thank you kindly!
154166
(path, content)
155167
})
156168
.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-
);
169+
assert_eq!(files.len(), 0, "{files:?}");
177170
}
178171

179172
root.close().unwrap();

0 commit comments

Comments
 (0)