blob: 5a79056cf1b879f4a2c739cd10bebaefa3aff727 [file] [log] [blame]
Matthew Maurerf4d8f812020-03-27 13:14:30 -07001//! Tests for --message-format flag.
2
Chris Wailes32f78352021-07-20 14:04:55 -07003use cargo_test_support::{basic_lib_manifest, basic_manifest, project};
Chih-Hung Hsieh43f06942019-12-19 15:01:08 -08004
5#[cargo_test]
6fn cannot_specify_two() {
Chih-Hung Hsieh43f06942019-12-19 15:01:08 -08007 let p = project()
8 .file("Cargo.toml", &basic_manifest("foo", "0.1.0"))
9 .file("src/main.rs", "fn main() {}")
10 .build();
11
12 let formats = ["human", "json", "short"];
13
14 let two_kinds = "error: cannot specify two kinds of `message-format` arguments\n";
15 for a in formats.iter() {
16 for b in formats.iter() {
17 p.cargo(&format!("build --message-format {},{}", a, b))
18 .with_status(101)
19 .with_stderr(two_kinds)
20 .run();
21 }
22 }
23}
24
25#[cargo_test]
26fn double_json_works() {
Chih-Hung Hsieh43f06942019-12-19 15:01:08 -080027 let p = project()
28 .file("Cargo.toml", &basic_manifest("foo", "0.1.0"))
29 .file("src/main.rs", "fn main() {}")
30 .build();
31
32 p.cargo("build --message-format json,json-render-diagnostics")
33 .run();
34 p.cargo("build --message-format json,json-diagnostic-short")
35 .run();
36 p.cargo("build --message-format json,json-diagnostic-rendered-ansi")
37 .run();
38 p.cargo("build --message-format json --message-format json-diagnostic-rendered-ansi")
39 .run();
40 p.cargo("build --message-format json-diagnostic-rendered-ansi")
41 .run();
42 p.cargo("build --message-format json-diagnostic-short,json-diagnostic-rendered-ansi")
43 .run();
44}
45
46#[cargo_test]
47fn cargo_renders() {
Chih-Hung Hsieh43f06942019-12-19 15:01:08 -080048 let p = project()
49 .file(
50 "Cargo.toml",
51 r#"
52 [package]
53 name = 'foo'
54 version = '0.1.0'
55
56 [dependencies]
57 bar = { path = 'bar' }
58 "#,
59 )
60 .file("src/main.rs", "")
61 .file("bar/Cargo.toml", &basic_manifest("bar", "0.1.0"))
62 .file("bar/src/lib.rs", "")
63 .build();
64
65 p.cargo("build --message-format json-render-diagnostics")
66 .with_status(101)
Jeff Vander Stoepad6790c2020-06-24 15:34:31 +020067 .with_stdout(
68 "{\"reason\":\"compiler-artifact\",[..]\n\
69 {\"reason\":\"build-finished\",\"success\":false}",
70 )
Chih-Hung Hsieh43f06942019-12-19 15:01:08 -080071 .with_stderr_contains(
72 "\
73[COMPILING] bar [..]
74[COMPILING] foo [..]
75error[..]`main`[..]
76",
77 )
78 .run();
79}
80
81#[cargo_test]
82fn cargo_renders_short() {
Chih-Hung Hsieh43f06942019-12-19 15:01:08 -080083 let p = project()
84 .file("Cargo.toml", &basic_manifest("foo", "0.1.0"))
85 .file("src/main.rs", "")
86 .build();
87
88 p.cargo("build --message-format json-render-diagnostics,json-diagnostic-short")
89 .with_status(101)
90 .with_stderr_contains(
91 "\
92[COMPILING] foo [..]
93error[..]`main`[..]
94",
95 )
96 .with_stderr_does_not_contain("note:")
97 .run();
98}
99
100#[cargo_test]
101fn cargo_renders_ansi() {
Chih-Hung Hsieh43f06942019-12-19 15:01:08 -0800102 let p = project()
103 .file("Cargo.toml", &basic_manifest("foo", "0.1.0"))
104 .file("src/main.rs", "")
105 .build();
106
107 p.cargo("build --message-format json-diagnostic-rendered-ansi")
108 .with_status(101)
109 .with_stdout_contains("[..]\\u001b[38;5;9merror[..]")
110 .run();
111}
Chris Wailese3116c42021-07-13 14:40:48 -0700112
113#[cargo_test]
114fn cargo_renders_doctests() {
Chris Wailese3116c42021-07-13 14:40:48 -0700115 let p = project()
116 .file("Cargo.toml", &basic_lib_manifest("foo"))
117 .file(
118 "src/lib.rs",
119 "\
120 /// ```rust
121 /// bar()
122 /// ```
123 pub fn bar() {}
124 ",
125 )
126 .build();
127
128 p.cargo("test --doc --message-format short")
129 .with_status(101)
130 .with_stdout_contains("src/lib.rs:2:1: error[E0425]:[..]")
131 .with_stdout_contains("[..]src/lib.rs - bar (line 1)[..]")
132 .run();
133}