Skip to content

Commit ac1dc0b

Browse files
committed
test: verify runner doesn't inherit jobserver
1 parent a62a3c6 commit ac1dc0b

File tree

1 file changed

+146
-3
lines changed

1 file changed

+146
-3
lines changed

tests/testsuite/jobserver.rs

Lines changed: 146 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,23 @@
11
//! Tests for the jobserver protocol.
22
33
use cargo_util::is_ci;
4+
use std::env;
45
use std::net::TcpListener;
56
use std::process::Command;
67
use std::thread;
78

8-
use cargo_test_support::install::{assert_has_installed_exe, cargo_home};
9-
use cargo_test_support::{cargo_exe, project};
9+
use cargo_test_support::basic_bin_manifest;
10+
use cargo_test_support::cargo_exe;
11+
use cargo_test_support::install::assert_has_installed_exe;
12+
use cargo_test_support::install::cargo_home;
13+
use cargo_test_support::project;
14+
use cargo_test_support::rustc_host;
1015

1116
const EXE_CONTENT: &str = r#"
1217
use std::env;
1318
1419
fn main() {
15-
let var = env::var("CARGO_MAKEFLAGS").unwrap();
20+
let var = env::var("CARGO_MAKEFLAGS").expect("no jobserver from env");
1621
let arg = var.split(' ')
1722
.find(|p| p.starts_with("--jobserver"))
1823
.unwrap();
@@ -105,6 +110,144 @@ all:
105110
p.process(make).env("CARGO", cargo_exe()).arg("-j2").run();
106111
}
107112

113+
#[cargo_test]
114+
fn runner_inherits_jobserver() {
115+
let make = make_exe();
116+
if Command::new(make).arg("--version").output().is_err() {
117+
return;
118+
}
119+
120+
let runner = "runner";
121+
project()
122+
.at(runner)
123+
.file("Cargo.toml", &basic_bin_manifest(runner))
124+
.file(
125+
"src/main.rs",
126+
r#"
127+
pub fn main() {
128+
eprintln!("this is a runner");
129+
let args: Vec<String> = std::env::args().collect();
130+
let status = std::process::Command::new(&args[1]).status().unwrap();
131+
assert!(status.success());
132+
}
133+
"#,
134+
)
135+
.build()
136+
.cargo("install --path .")
137+
.run();
138+
139+
// Add .cargo/bin to PATH
140+
let mut path: Vec<_> = env::split_paths(&env::var_os("PATH").unwrap_or_default()).collect();
141+
path.push(cargo_home().join("bin"));
142+
let path = &env::join_paths(path).unwrap();
143+
assert_has_installed_exe(cargo_home(), runner);
144+
145+
let host = rustc_host();
146+
let config_value = &format!("target.{host}.runner = \"{runner}\"");
147+
148+
let name = "cargo-jobserver-check";
149+
let p = project()
150+
.file(
151+
"Cargo.toml",
152+
&format!(
153+
r#"
154+
[package]
155+
name = "{name}"
156+
version = "0.0.1"
157+
"#
158+
),
159+
)
160+
.file(
161+
"src/lib.rs",
162+
r#"
163+
#[test]
164+
fn test() {
165+
_ = std::env::var("CARGO_MAKEFLAGS").expect("no jobserver from env");
166+
}
167+
"#,
168+
)
169+
.file("src/main.rs", EXE_CONTENT)
170+
.file(
171+
"Makefile",
172+
&format!(
173+
"\
174+
run:
175+
\t+$(CARGO) run
176+
177+
run-runner:
178+
\t+$(CARGO) run --config '{config_value}'
179+
180+
test:
181+
\t+$(CARGO) test --lib
182+
183+
test-runner:
184+
\t+$(CARGO) test --lib --config '{config_value}'
185+
",
186+
),
187+
)
188+
.build();
189+
190+
// jobserver can be inherited from env
191+
p.process(make)
192+
.env("CARGO", cargo_exe())
193+
.arg("run")
194+
.arg("-j2")
195+
.with_status(2)
196+
.with_stderr_contains("[..]no jobserver from env[..]")
197+
.run();
198+
p.process(make)
199+
.env("PATH", path)
200+
.env("CARGO", cargo_exe())
201+
.arg("run-runner")
202+
.arg("-j2")
203+
.with_status(2)
204+
.with_stderr_contains("[..]this is a runner[..]")
205+
.with_stderr_contains("[..]no jobserver from env[..]")
206+
.run();
207+
p.process(make)
208+
.env("CARGO", cargo_exe())
209+
.arg("test")
210+
.arg("-j2")
211+
.with_status(2)
212+
.with_stdout_contains("[..]no jobserver from env[..]")
213+
.run();
214+
p.process(make)
215+
.env("PATH", path)
216+
.env("CARGO", cargo_exe())
217+
.arg("test-runner")
218+
.arg("-j2")
219+
.with_status(2)
220+
.with_stderr_contains("[..]this is a runner[..]")
221+
.with_stdout_contains("[..]no jobserver from env[..]")
222+
.run();
223+
224+
// but not from `-j` flag
225+
p.cargo("run -j2")
226+
.with_status(101)
227+
.with_stderr_contains("[..]no jobserver from env[..]")
228+
.run();
229+
p.cargo("run -j2")
230+
.env("PATH", path)
231+
.arg("--config")
232+
.arg(config_value)
233+
.with_status(101)
234+
.with_stderr_contains("[..]this is a runner[..]")
235+
.with_stderr_contains("[..]no jobserver from env[..]")
236+
.run();
237+
p.cargo("test -j2")
238+
.with_status(101)
239+
.with_stdout_contains("[..]no jobserver from env[..]")
240+
.run();
241+
p.cargo("test -j2")
242+
.env("PATH", path)
243+
.arg("--config")
244+
.arg(config_value)
245+
.with_status(101)
246+
.with_stderr_contains("[..]this is a runner[..]")
247+
.with_stdout_contains("[..]no jobserver from env[..]")
248+
.run();
249+
}
250+
108251
#[cargo_test]
109252
fn makes_jobserver_used() {
110253
let make = make_exe();

0 commit comments

Comments
 (0)