Skip to content

Commit 68613b9

Browse files
committed
Require --message-format=json with CARGO_PRINT_RUN
1 parent e69bd9b commit 68613b9

File tree

3 files changed

+61
-5
lines changed

3 files changed

+61
-5
lines changed

src/cargo/ops/cargo_run.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use std::path::Path;
22

3-
use ops::{self, CompileFilter, Packages};
3+
use ops::{self, CompileFilter, Packages, MessageFormat};
44
use util::{self, human, CargoResult, ProcessError};
55
use util::machine_message::{self, RunProfile};
66
use core::Workspace;
@@ -66,6 +66,9 @@ pub fn run(ws: &Workspace,
6666
process.args(args).cwd(config.cwd());
6767

6868
if config.print_run() {
69+
if options.message_format != MessageFormat::Json {
70+
bail!("CARGO_PRINT_RUN requires --message-format=json")
71+
}
6972
machine_message::emit(RunProfile::new(&process));
7073
Ok(None)
7174
} else {

src/cargo/ops/cargo_test.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use std::ffi::{OsString, OsStr};
22

3-
use ops::{self, Compilation};
3+
use ops::{self, Compilation, MessageFormat};
44
use util::{self, CargoResult, CargoTestError, Test, ProcessError};
55
use util::machine_message::{self, RunProfile};
66
use core::Workspace;
@@ -98,6 +98,9 @@ fn run_unit_tests(options: &TestOptions,
9898
})?;
9999

100100
if config.print_run() {
101+
if options.compile_opts.message_format != MessageFormat::Json {
102+
bail!("CARGO_PRINT_RUN requires --message-format=json")
103+
}
101104
machine_message::emit(RunProfile::new(&cmd));
102105
} else {
103106
config.shell().verbose(|shell| {

tests/print_run.rs

Lines changed: 53 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,22 @@ fn single_bin() {
1818
fn main() { println!("hello"); }
1919
"#);
2020

21-
assert_that(p.cargo_process("run").env("CARGO_PRINT_RUN", "1"),
21+
assert_that(p.cargo_process("run").env("CARGO_PRINT_RUN", "1").arg("--message-format=json"),
2222
execs().with_status(0)
2323
.with_stderr(&"\
2424
[COMPILING] foo v0.0.1 ([..])
2525
[FINISHED] dev [unoptimized + debuginfo] target(s) in [..]")
2626
.with_json(r#"
27+
{
28+
"features": [],
29+
"filenames": ["[..]target[/]debug[/]foo"],
30+
"fresh": false,
31+
"package_id": "foo 0.0.1 ([..])",
32+
"profile": "{...}",
33+
"reason": "compiler-artifact",
34+
"target": "{...}"
35+
}
36+
2737
{
2838
"reason": "run-profile",
2939
"program": "[..][/]foo[/]target[/]debug[/]foo",
@@ -87,9 +97,26 @@ fn several_tests() {
8797
"CARGO_MANIFEST_DIR": "[..]",
8898
"CARGO": "[..]"
8999
}"#;
90-
assert_that(p.cargo_process("test").env("CARGO_PRINT_RUN", "1"),
100+
let artifact = r#"{
101+
"features": [],
102+
"filenames": "{...}",
103+
"fresh": false,
104+
"package_id": "foo 0.0.1 ([..])",
105+
"profile": "{...}",
106+
"reason": "compiler-artifact",
107+
"target": "{...}"
108+
}"#;
109+
assert_that(p.cargo_process("test").env("CARGO_PRINT_RUN", "1").arg("--message-format=json"),
91110
execs().with_status(0)
92111
.with_json(&format!(r#"
112+
{artifact}
113+
114+
{artifact}
115+
116+
{artifact}
117+
118+
{artifact}
119+
93120
{{
94121
"reason": "run-profile",
95122
"program": "[..]bar-[..]",
@@ -113,6 +140,29 @@ fn several_tests() {
113140
"cwd": "[..]",
114141
"args": []
115142
}}
116-
"#, env=env)));
143+
"#, env=env, artifact=artifact)));
117144
}
118145

146+
#[test]
147+
fn print_run_needs_json() {
148+
let p = project("foo")
149+
.file("Cargo.toml", r#"
150+
[project]
151+
name = "foo"
152+
version = "0.0.1"
153+
authors = []
154+
"#)
155+
.file("src/main.rs", r#"
156+
fn main() { println!("hello"); }
157+
"#);
158+
p.build();
159+
160+
assert_that(p.cargo("run").env("CARGO_PRINT_RUN", "1"),
161+
execs().with_status(101).with_stderr_contains("\
162+
[ERROR] CARGO_PRINT_RUN requires --message-format=json"));
163+
164+
assert_that(p.cargo("test").env("CARGO_PRINT_RUN", "1"),
165+
execs().with_status(101).with_stderr_contains("\
166+
[ERROR] CARGO_PRINT_RUN requires --message-format=json"));
167+
168+
}

0 commit comments

Comments
 (0)