Matthew Maurer | f4d8f81 | 2020-03-27 13:14:30 -0700 | [diff] [blame] | 1 | //! Tests for --message-format flag. |
| 2 | |
Chris Wailes | 32f7835 | 2021-07-20 14:04:55 -0700 | [diff] [blame^] | 3 | use cargo_test_support::{basic_lib_manifest, basic_manifest, project}; |
Chih-Hung Hsieh | 43f0694 | 2019-12-19 15:01:08 -0800 | [diff] [blame] | 4 | |
| 5 | #[cargo_test] |
| 6 | fn cannot_specify_two() { |
Chih-Hung Hsieh | 43f0694 | 2019-12-19 15:01:08 -0800 | [diff] [blame] | 7 | 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] |
| 26 | fn double_json_works() { |
Chih-Hung Hsieh | 43f0694 | 2019-12-19 15:01:08 -0800 | [diff] [blame] | 27 | 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] |
| 47 | fn cargo_renders() { |
Chih-Hung Hsieh | 43f0694 | 2019-12-19 15:01:08 -0800 | [diff] [blame] | 48 | 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 Stoep | ad6790c | 2020-06-24 15:34:31 +0200 | [diff] [blame] | 67 | .with_stdout( |
| 68 | "{\"reason\":\"compiler-artifact\",[..]\n\ |
| 69 | {\"reason\":\"build-finished\",\"success\":false}", |
| 70 | ) |
Chih-Hung Hsieh | 43f0694 | 2019-12-19 15:01:08 -0800 | [diff] [blame] | 71 | .with_stderr_contains( |
| 72 | "\ |
| 73 | [COMPILING] bar [..] |
| 74 | [COMPILING] foo [..] |
| 75 | error[..]`main`[..] |
| 76 | ", |
| 77 | ) |
| 78 | .run(); |
| 79 | } |
| 80 | |
| 81 | #[cargo_test] |
| 82 | fn cargo_renders_short() { |
Chih-Hung Hsieh | 43f0694 | 2019-12-19 15:01:08 -0800 | [diff] [blame] | 83 | 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 [..] |
| 93 | error[..]`main`[..] |
| 94 | ", |
| 95 | ) |
| 96 | .with_stderr_does_not_contain("note:") |
| 97 | .run(); |
| 98 | } |
| 99 | |
| 100 | #[cargo_test] |
| 101 | fn cargo_renders_ansi() { |
Chih-Hung Hsieh | 43f0694 | 2019-12-19 15:01:08 -0800 | [diff] [blame] | 102 | 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 Wailes | e3116c4 | 2021-07-13 14:40:48 -0700 | [diff] [blame] | 112 | |
| 113 | #[cargo_test] |
| 114 | fn cargo_renders_doctests() { |
Chris Wailes | e3116c4 | 2021-07-13 14:40:48 -0700 | [diff] [blame] | 115 | 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 | } |