-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdiagnostic_json_tests.rs
More file actions
201 lines (187 loc) · 6.84 KB
/
Copy pathdiagnostic_json_tests.rs
File metadata and controls
201 lines (187 loc) · 6.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
//! Tests for Netsuke's JSON diagnostics schema.
use super::{render_diagnostic_json, render_error_json};
use crate::localization::{self, keys};
use crate::manifest;
use crate::runner::RunnerError;
use anyhow::{Context, Result, ensure};
use insta::{Settings, assert_snapshot};
use rstest::rstest;
use serde_json::{Map, Value};
use std::path::PathBuf;
use test_support::{localizer_test_lock, set_en_localizer};
fn parse_json_value(document: &str) -> Result<Value> {
serde_json::from_str(document).context("parse diagnostics JSON")
}
fn snapshot_settings() -> Settings {
let mut settings = Settings::new();
settings.set_snapshot_path(concat!(
env!("CARGO_MANIFEST_DIR"),
"/src/snapshots/diagnostic_json"
));
settings
}
fn first_diagnostic(value: &Value) -> Result<&Map<String, Value>> {
value
.get("diagnostics")
.and_then(Value::as_array)
.and_then(|diagnostics| diagnostics.first())
.and_then(Value::as_object)
.context("diagnostic entry should be an object")
}
fn manifest_not_found_error() -> RunnerError {
RunnerError::ManifestNotFound {
manifest_name: String::from("Netsukefile"),
directory: String::from("the current directory"),
path: PathBuf::from("/workspace/Netsukefile"),
message: localization::message(keys::RUNNER_MANIFEST_NOT_FOUND)
.with_arg("manifest_name", "Netsukefile")
.with_arg("directory", "the current directory"),
help: localization::message(keys::RUNNER_MANIFEST_NOT_FOUND_HELP),
}
}
#[rstest]
fn render_plain_error_json_records_cause_chain() -> Result<()> {
let error = anyhow::anyhow!("top level failure").context("outer context");
let document = render_error_json(error.as_ref())?;
let value = parse_json_value(&document)?;
let diagnostic = first_diagnostic(&value)?;
let schema_version = value
.get("schema_version")
.and_then(Value::as_i64)
.context("schema version should be present")?;
let generator_name = value
.get("generator")
.and_then(Value::as_object)
.and_then(|generator| generator.get("name"))
.and_then(Value::as_str)
.context("generator name should be present")?;
let message = diagnostic
.get("message")
.and_then(Value::as_str)
.context("message should be present")?;
let causes = diagnostic
.get("causes")
.context("causes should be present")?;
let labels = diagnostic
.get("labels")
.context("labels should be present")?;
ensure!(schema_version == 1, "schema version should be stable",);
ensure!(
generator_name == "netsuke",
"generator name should be present",
);
ensure!(
message == "outer context",
"plain errors should use the top-level message"
);
ensure!(
causes == &Value::from(vec![String::from("top level failure")]),
"plain errors should record the error cause chain",
);
ensure!(
labels == &Value::Array(Vec::new()),
"plain errors should not fabricate labels",
);
Ok(())
}
#[rstest]
fn render_runner_diagnostic_json_records_help_without_spans() -> Result<()> {
let _lock = localizer_test_lock().expect("localizer test lock poisoned");
let _guard = set_en_localizer();
let document = render_diagnostic_json(&manifest_not_found_error())?;
let value = parse_json_value(&document)?;
let diagnostic = first_diagnostic(&value)?;
let code = diagnostic
.get("code")
.and_then(Value::as_str)
.context("diagnostic code should be present")?;
let help = diagnostic
.get("help")
.and_then(Value::as_str)
.context("diagnostic help should be present")?;
let source = diagnostic
.get("source")
.context("source should be present")?;
let labels = diagnostic
.get("labels")
.context("labels should be present")?;
ensure!(
code == "netsuke::runner::manifest_not_found",
"runner diagnostic code should be stable",
);
ensure!(
help == "Ensure the manifest exists or pass `--file` with the correct path.",
"runner diagnostics should include help text",
);
ensure!(
source.is_null(),
"manifest-not-found should not claim a source file span"
);
ensure!(
labels == &Value::Array(Vec::new()),
"manifest-not-found should not include labels",
);
Ok(())
}
#[rstest]
fn render_manifest_parse_diagnostic_matches_snapshot() -> Result<()> {
let _lock = localizer_test_lock().expect("localizer test lock poisoned");
let _guard = set_en_localizer();
let err = manifest::from_str("targets:\n\t- name: test\n")
.expect_err("invalid YAML should fail to parse");
let manifest_err = err
.downcast_ref::<manifest::ManifestError>()
.context("expected ManifestError")?;
let document = render_diagnostic_json(manifest_err)?;
let value = parse_json_value(&document)?;
let rendered =
serde_json::to_string_pretty(&value).context("render diagnostic JSON snapshot value")?;
snapshot_settings().bind(|| {
assert_snapshot!("manifest_parse_error", rendered);
});
Ok(())
}
/// Build a real `IrGenError::CircularDependency` through the IR pipeline.
///
/// Two targets that consume each other's outputs guarantee a cycle, and the
/// detector's canonicalisation keeps the reported sequence deterministic.
fn circular_dependency_error() -> Result<crate::ir::IrGenError> {
let manifest = manifest::from_str(concat!(
"netsuke_version: \"1.0.0\"\n",
"targets:\n",
" - name: a\n",
" sources: b\n",
" command: echo a\n",
" - name: b\n",
" sources: a\n",
" command: echo b\n",
))?;
match crate::ir::BuildGraph::from_manifest(&manifest) {
Err(err) => Ok(err),
Ok(graph) => anyhow::bail!("expected circular dependency error, got graph {graph:?}"),
}
}
#[rstest]
fn circular_dependency_display_matches_snapshot() -> Result<()> {
let _lock = localizer_test_lock().expect("localizer test lock poisoned");
let _guard = set_en_localizer();
let err = circular_dependency_error()?;
snapshot_settings().bind(|| {
assert_snapshot!("circular_dependency_display", err.to_string());
});
Ok(())
}
#[rstest]
fn circular_dependency_error_json_matches_snapshot() -> Result<()> {
let _lock = localizer_test_lock().expect("localizer test lock poisoned");
let _guard = set_en_localizer();
let err = circular_dependency_error()?;
let document = render_error_json(&err)?;
let value = parse_json_value(&document)?;
let rendered =
serde_json::to_string_pretty(&value).context("render circular dependency JSON")?;
snapshot_settings().bind(|| {
assert_snapshot!("circular_dependency_error_json", rendered);
});
Ok(())
}