Skip to content

Commit

Permalink
test: verify runner doesn't inherit jobserver
Browse files Browse the repository at this point in the history
  • Loading branch information
weihanglo committed Jan 19, 2024
1 parent a62a3c6 commit ac1dc0b
Showing 1 changed file with 146 additions and 3 deletions.
149 changes: 146 additions & 3 deletions tests/testsuite/jobserver.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,23 @@
//! Tests for the jobserver protocol.
use cargo_util::is_ci;
use std::env;
use std::net::TcpListener;
use std::process::Command;
use std::thread;

use cargo_test_support::install::{assert_has_installed_exe, cargo_home};
use cargo_test_support::{cargo_exe, project};
use cargo_test_support::basic_bin_manifest;
use cargo_test_support::cargo_exe;
use cargo_test_support::install::assert_has_installed_exe;
use cargo_test_support::install::cargo_home;
use cargo_test_support::project;
use cargo_test_support::rustc_host;

const EXE_CONTENT: &str = r#"
use std::env;
fn main() {
let var = env::var("CARGO_MAKEFLAGS").unwrap();
let var = env::var("CARGO_MAKEFLAGS").expect("no jobserver from env");
let arg = var.split(' ')
.find(|p| p.starts_with("--jobserver"))
.unwrap();
Expand Down Expand Up @@ -105,6 +110,144 @@ all:
p.process(make).env("CARGO", cargo_exe()).arg("-j2").run();
}

#[cargo_test]
fn runner_inherits_jobserver() {
let make = make_exe();
if Command::new(make).arg("--version").output().is_err() {
return;
}

let runner = "runner";
project()
.at(runner)
.file("Cargo.toml", &basic_bin_manifest(runner))
.file(
"src/main.rs",
r#"
pub fn main() {
eprintln!("this is a runner");
let args: Vec<String> = std::env::args().collect();
let status = std::process::Command::new(&args[1]).status().unwrap();
assert!(status.success());
}
"#,
)
.build()
.cargo("install --path .")
.run();

// Add .cargo/bin to PATH
let mut path: Vec<_> = env::split_paths(&env::var_os("PATH").unwrap_or_default()).collect();
path.push(cargo_home().join("bin"));
let path = &env::join_paths(path).unwrap();
assert_has_installed_exe(cargo_home(), runner);

let host = rustc_host();
let config_value = &format!("target.{host}.runner = \"{runner}\"");

let name = "cargo-jobserver-check";
let p = project()
.file(
"Cargo.toml",
&format!(
r#"
[package]
name = "{name}"
version = "0.0.1"
"#
),
)
.file(
"src/lib.rs",
r#"
#[test]
fn test() {
_ = std::env::var("CARGO_MAKEFLAGS").expect("no jobserver from env");
}
"#,
)
.file("src/main.rs", EXE_CONTENT)
.file(
"Makefile",
&format!(
"\
run:
\t+$(CARGO) run
run-runner:
\t+$(CARGO) run --config '{config_value}'
test:
\t+$(CARGO) test --lib
test-runner:
\t+$(CARGO) test --lib --config '{config_value}'
",
),
)
.build();

// jobserver can be inherited from env
p.process(make)
.env("CARGO", cargo_exe())
.arg("run")
.arg("-j2")
.with_status(2)
.with_stderr_contains("[..]no jobserver from env[..]")
.run();
p.process(make)
.env("PATH", path)
.env("CARGO", cargo_exe())
.arg("run-runner")
.arg("-j2")
.with_status(2)
.with_stderr_contains("[..]this is a runner[..]")
.with_stderr_contains("[..]no jobserver from env[..]")
.run();
p.process(make)
.env("CARGO", cargo_exe())
.arg("test")
.arg("-j2")
.with_status(2)
.with_stdout_contains("[..]no jobserver from env[..]")
.run();
p.process(make)
.env("PATH", path)
.env("CARGO", cargo_exe())
.arg("test-runner")
.arg("-j2")
.with_status(2)
.with_stderr_contains("[..]this is a runner[..]")
.with_stdout_contains("[..]no jobserver from env[..]")
.run();

// but not from `-j` flag
p.cargo("run -j2")
.with_status(101)
.with_stderr_contains("[..]no jobserver from env[..]")
.run();
p.cargo("run -j2")
.env("PATH", path)
.arg("--config")
.arg(config_value)
.with_status(101)
.with_stderr_contains("[..]this is a runner[..]")
.with_stderr_contains("[..]no jobserver from env[..]")
.run();
p.cargo("test -j2")
.with_status(101)
.with_stdout_contains("[..]no jobserver from env[..]")
.run();
p.cargo("test -j2")
.env("PATH", path)
.arg("--config")
.arg(config_value)
.with_status(101)
.with_stderr_contains("[..]this is a runner[..]")
.with_stdout_contains("[..]no jobserver from env[..]")
.run();
}

#[cargo_test]
fn makes_jobserver_used() {
let make = make_exe();
Expand Down

0 comments on commit ac1dc0b

Please sign in to comment.