Skip to content

Commit 0a62f82

Browse files
committed
sudo-test: copy code coverage data from container back into host
when the SUDO_TEST_PROFRAW_DIR env var is set
1 parent 7df8c08 commit 0a62f82

File tree

2 files changed

+29
-2
lines changed

2 files changed

+29
-2
lines changed

test-framework/sudo-test/src/docker.rs

+26-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use std::{
33
env,
44
fs::{self, File},
55
io::{Seek, SeekFrom, Write},
6-
path::PathBuf,
6+
path::{Path, PathBuf},
77
process::{self, Command as StdCommand, Stdio},
88
};
99

@@ -92,10 +92,35 @@ impl Container {
9292

9393
Ok(())
9494
}
95+
96+
fn copy_profraw_data(&mut self, profraw_dir: impl AsRef<Path>) -> Result<()> {
97+
let profraw_dir = profraw_dir.as_ref();
98+
fs::create_dir_all(profraw_dir)?;
99+
100+
self.exec(Command::new("sh").args([
101+
"-c",
102+
"mkdir /tmp/profraw; find / -name '*.profraw' -exec cp {} /tmp/profraw/ \\; || true",
103+
]))?
104+
.assert_success()?;
105+
106+
let src_path = format!("{}:/tmp/profraw", self.id);
107+
let dst_path = profraw_dir.join(&self.id).display().to_string();
108+
run(
109+
StdCommand::new("docker").args(["cp", &src_path, &dst_path]),
110+
None,
111+
)?
112+
.assert_success()?;
113+
114+
Ok(())
115+
}
95116
}
96117

97118
impl Drop for Container {
98119
fn drop(&mut self) {
120+
if let Ok(dir) = env::var("SUDO_TEST_PROFRAW_DIR") {
121+
let _ = self.copy_profraw_data(dir);
122+
}
123+
99124
// running this to completion would block the current thread for several seconds so just
100125
// fire and forget
101126
let _ = StdCommand::new("docker")

test-framework/sudo-test/src/ours.Dockerfile

+3-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@ RUN apt-get update && \
55
RUN cargo search sudo
66
WORKDIR /usr/src/sudo
77
COPY . .
8-
RUN --mount=type=cache,target=/usr/src/sudo/target cargo build --locked -p sudo && mkdir -p build && cp target/debug/sudo build/sudo
8+
RUN --mount=type=cache,target=/usr/src/sudo/target RUSTFLAGS="-C instrument-coverage" cargo build --locked -p sudo && mkdir -p build && cp target/debug/sudo build/sudo
9+
# discard code coverage data created during `cargo build`
10+
RUN find / -name '*.profraw' -exec rm {} \;
911
# set setuid on install
1012
RUN install --mode 4755 build/sudo /usr/bin/sudo
1113
# remove build dependencies

0 commit comments

Comments
 (0)