2018-09-01 12:21:11 -05:00
|
|
|
use std::{
|
2018-09-02 04:34:06 -05:00
|
|
|
cell::{Cell, RefCell},
|
2018-10-15 16:44:23 -05:00
|
|
|
fs,
|
2019-04-13 12:45:21 -05:00
|
|
|
path::{Path, PathBuf},
|
2018-09-02 06:46:15 -05:00
|
|
|
sync::Once,
|
2018-10-15 16:44:23 -05:00
|
|
|
time::Duration,
|
2018-09-01 12:21:11 -05:00
|
|
|
};
|
|
|
|
|
2018-12-06 12:03:39 -06:00
|
|
|
use crossbeam_channel::{after, select, Receiver};
|
2019-08-30 12:18:57 -05:00
|
|
|
use lsp_server::{Connection, Message, Notification, Request};
|
2021-01-28 10:10:48 -06:00
|
|
|
use lsp_types::{notification::Exit, request::Shutdown, TextDocumentIdentifier, Url};
|
|
|
|
use rust_analyzer::{config::Config, lsp_ext, main_loop};
|
2020-07-08 11:22:57 -05:00
|
|
|
use serde::Serialize;
|
2021-01-28 10:10:48 -06:00
|
|
|
use serde_json::{json, to_string_pretty, Value};
|
2023-04-10 10:15:39 -05:00
|
|
|
use test_utils::FixtureWithProjectMeta;
|
2023-06-19 08:11:20 -05:00
|
|
|
use tracing_subscriber::{prelude::*, Layer};
|
2020-07-08 11:22:57 -05:00
|
|
|
use vfs::AbsPathBuf;
|
2018-09-01 12:21:11 -05:00
|
|
|
|
2020-07-23 15:26:25 -05:00
|
|
|
use crate::testdir::TestDir;
|
|
|
|
|
2020-11-02 09:31:38 -06:00
|
|
|
pub(crate) struct Project<'a> {
|
2019-04-13 13:33:49 -05:00
|
|
|
fixture: &'a str,
|
2020-07-23 15:26:25 -05:00
|
|
|
tmp_dir: Option<TestDir>,
|
2019-04-13 13:33:49 -05:00
|
|
|
roots: Vec<PathBuf>,
|
2021-01-06 04:54:28 -06:00
|
|
|
config: serde_json::Value,
|
2019-03-05 15:29:23 -06:00
|
|
|
}
|
|
|
|
|
2023-06-29 09:27:28 -05:00
|
|
|
impl Project<'_> {
|
2022-07-20 08:02:08 -05:00
|
|
|
pub(crate) fn with_fixture(fixture: &str) -> Project<'_> {
|
2021-03-01 03:14:16 -06:00
|
|
|
Project {
|
|
|
|
fixture,
|
|
|
|
tmp_dir: None,
|
|
|
|
roots: vec![],
|
|
|
|
config: serde_json::json!({
|
2021-04-12 03:04:36 -05:00
|
|
|
"cargo": {
|
|
|
|
// Loading standard library is costly, let's ignore it by default
|
2022-10-01 13:47:31 -05:00
|
|
|
"sysroot": null,
|
2021-04-12 03:04:36 -05:00
|
|
|
// Can't use test binary as rustc wrapper.
|
2022-04-27 11:25:12 -05:00
|
|
|
"buildScripts": {
|
2023-04-04 12:02:10 -05:00
|
|
|
"useRustcWrapper": false,
|
|
|
|
"enable": false,
|
2022-04-27 11:25:12 -05:00
|
|
|
},
|
2023-04-04 12:02:10 -05:00
|
|
|
},
|
|
|
|
"procMacro": {
|
|
|
|
"enable": false,
|
2021-04-12 03:04:36 -05:00
|
|
|
}
|
2021-03-01 03:14:16 -06:00
|
|
|
}),
|
|
|
|
}
|
2019-04-13 13:33:49 -05:00
|
|
|
}
|
|
|
|
|
2023-06-29 09:27:28 -05:00
|
|
|
pub(crate) fn tmp_dir(mut self, tmp_dir: TestDir) -> Self {
|
2019-04-13 13:33:49 -05:00
|
|
|
self.tmp_dir = Some(tmp_dir);
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2023-06-29 09:27:28 -05:00
|
|
|
pub(crate) fn root(mut self, path: &str) -> Self {
|
2019-04-13 13:33:49 -05:00
|
|
|
self.roots.push(path.into());
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2023-06-29 09:27:28 -05:00
|
|
|
pub(crate) fn with_config(mut self, config: serde_json::Value) -> Self {
|
2021-04-12 03:04:36 -05:00
|
|
|
fn merge(dst: &mut serde_json::Value, src: serde_json::Value) {
|
|
|
|
match (dst, src) {
|
|
|
|
(Value::Object(dst), Value::Object(src)) => {
|
|
|
|
for (k, v) in src {
|
|
|
|
merge(dst.entry(k).or_insert(v.clone()), v)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
(dst, src) => *dst = src,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
merge(&mut self.config, config);
|
2020-03-31 12:16:25 -05:00
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2020-11-02 09:31:38 -06:00
|
|
|
pub(crate) fn server(self) -> Server {
|
2021-03-21 09:33:18 -05:00
|
|
|
let tmp_dir = self.tmp_dir.unwrap_or_else(TestDir::new);
|
2019-04-13 13:33:49 -05:00
|
|
|
static INIT: Once = Once::new();
|
|
|
|
INIT.call_once(|| {
|
2023-06-19 08:11:20 -05:00
|
|
|
let filter: tracing_subscriber::filter::Targets =
|
|
|
|
std::env::var("RA_LOG").ok().and_then(|it| it.parse().ok()).unwrap_or_default();
|
|
|
|
let layer =
|
|
|
|
tracing_subscriber::fmt::Layer::new().with_test_writer().with_filter(filter);
|
|
|
|
tracing_subscriber::Registry::default().with(layer).init();
|
2020-08-12 09:32:36 -05:00
|
|
|
profile::init_from(crate::PROFILE);
|
2019-04-13 13:33:49 -05:00
|
|
|
});
|
2018-09-01 12:21:11 -05:00
|
|
|
|
2023-04-10 10:15:39 -05:00
|
|
|
let FixtureWithProjectMeta { fixture, mini_core, proc_macro_names, toolchain } =
|
|
|
|
FixtureWithProjectMeta::parse(self.fixture);
|
|
|
|
assert!(proc_macro_names.is_empty());
|
2021-06-15 13:02:40 -05:00
|
|
|
assert!(mini_core.is_none());
|
2023-04-10 10:15:39 -05:00
|
|
|
assert!(toolchain.is_none());
|
|
|
|
for entry in fixture {
|
2020-06-23 11:34:50 -05:00
|
|
|
let path = tmp_dir.path().join(&entry.path['/'.len_utf8()..]);
|
2019-04-13 13:33:49 -05:00
|
|
|
fs::create_dir_all(path.parent().unwrap()).unwrap();
|
|
|
|
fs::write(path.as_path(), entry.text.as_bytes()).unwrap();
|
|
|
|
}
|
|
|
|
|
2020-06-24 06:34:24 -05:00
|
|
|
let tmp_dir_path = AbsPathBuf::assert(tmp_dir.path().to_path_buf());
|
2020-06-03 05:22:01 -05:00
|
|
|
let mut roots =
|
2020-06-24 06:34:24 -05:00
|
|
|
self.roots.into_iter().map(|root| tmp_dir_path.join(root)).collect::<Vec<_>>();
|
2020-06-03 05:22:01 -05:00
|
|
|
if roots.is_empty() {
|
2020-06-24 06:34:24 -05:00
|
|
|
roots.push(tmp_dir_path.clone());
|
2020-06-03 05:22:01 -05:00
|
|
|
}
|
2019-04-13 13:33:49 -05:00
|
|
|
|
2021-01-06 04:54:28 -06:00
|
|
|
let mut config = Config::new(
|
|
|
|
tmp_dir_path,
|
|
|
|
lsp_types::ClientCapabilities {
|
2022-01-01 10:10:34 -06:00
|
|
|
workspace: Some(lsp_types::WorkspaceClientCapabilities {
|
|
|
|
did_change_watched_files: Some(
|
|
|
|
lsp_types::DidChangeWatchedFilesClientCapabilities {
|
|
|
|
dynamic_registration: Some(true),
|
2023-02-14 07:59:50 -06:00
|
|
|
relative_pattern_support: None,
|
2022-01-01 10:10:34 -06:00
|
|
|
},
|
|
|
|
),
|
2023-06-22 13:29:11 -05:00
|
|
|
workspace_edit: Some(lsp_types::WorkspaceEditClientCapabilities {
|
|
|
|
resource_operations: Some(vec![
|
|
|
|
lsp_types::ResourceOperationKind::Create,
|
|
|
|
lsp_types::ResourceOperationKind::Delete,
|
|
|
|
lsp_types::ResourceOperationKind::Rename,
|
|
|
|
]),
|
|
|
|
..Default::default()
|
|
|
|
}),
|
2022-01-01 10:10:34 -06:00
|
|
|
..Default::default()
|
|
|
|
}),
|
2021-01-05 07:57:05 -06:00
|
|
|
text_document: Some(lsp_types::TextDocumentClientCapabilities {
|
|
|
|
definition: Some(lsp_types::GotoCapability {
|
|
|
|
link_support: Some(true),
|
|
|
|
..Default::default()
|
|
|
|
}),
|
|
|
|
code_action: Some(lsp_types::CodeActionClientCapabilities {
|
|
|
|
code_action_literal_support: Some(
|
|
|
|
lsp_types::CodeActionLiteralSupport::default(),
|
|
|
|
),
|
|
|
|
..Default::default()
|
|
|
|
}),
|
2021-01-06 04:54:28 -06:00
|
|
|
hover: Some(lsp_types::HoverClientCapabilities {
|
|
|
|
content_format: Some(vec![lsp_types::MarkupKind::Markdown]),
|
|
|
|
..Default::default()
|
|
|
|
}),
|
2021-01-05 07:57:05 -06:00
|
|
|
..Default::default()
|
|
|
|
}),
|
|
|
|
window: Some(lsp_types::WindowClientCapabilities {
|
2021-01-28 10:10:48 -06:00
|
|
|
work_done_progress: Some(false),
|
2021-01-05 07:57:05 -06:00
|
|
|
..Default::default()
|
|
|
|
}),
|
2021-01-28 10:10:48 -06:00
|
|
|
experimental: Some(json!({
|
2021-04-06 06:16:35 -05:00
|
|
|
"serverStatusNotification": true,
|
2021-01-28 10:10:48 -06:00
|
|
|
})),
|
2020-04-26 17:54:05 -05:00
|
|
|
..Default::default()
|
|
|
|
},
|
2023-04-18 06:25:54 -05:00
|
|
|
roots,
|
2021-01-06 04:54:28 -06:00
|
|
|
);
|
2022-04-27 11:25:12 -05:00
|
|
|
config.update(self.config).expect("invalid config");
|
2023-04-18 06:25:54 -05:00
|
|
|
config.rediscover_workspaces();
|
2020-03-31 12:16:25 -05:00
|
|
|
|
2020-06-23 06:20:53 -05:00
|
|
|
Server::new(tmp_dir, config)
|
2018-09-01 12:21:11 -05:00
|
|
|
}
|
2019-04-13 13:33:49 -05:00
|
|
|
}
|
|
|
|
|
2020-11-02 09:31:38 -06:00
|
|
|
pub(crate) fn project(fixture: &str) -> Server {
|
2019-04-13 13:33:49 -05:00
|
|
|
Project::with_fixture(fixture).server()
|
2018-09-01 12:21:11 -05:00
|
|
|
}
|
|
|
|
|
2020-11-02 09:31:38 -06:00
|
|
|
pub(crate) struct Server {
|
2020-11-16 14:10:13 -06:00
|
|
|
req_id: Cell<i32>,
|
2019-08-30 09:24:11 -05:00
|
|
|
messages: RefCell<Vec<Message>>,
|
2023-05-20 07:29:32 -05:00
|
|
|
_thread: stdx::thread::JoinHandle,
|
2019-08-30 12:18:57 -05:00
|
|
|
client: Connection,
|
2020-03-28 07:19:05 -05:00
|
|
|
/// XXX: remove the tempdir last
|
2020-07-23 15:26:25 -05:00
|
|
|
dir: TestDir,
|
2018-09-01 12:21:11 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Server {
|
2020-07-23 15:26:25 -05:00
|
|
|
fn new(dir: TestDir, config: Config) -> Server {
|
2019-08-30 12:18:57 -05:00
|
|
|
let (connection, client) = Connection::memory();
|
2019-04-13 13:33:49 -05:00
|
|
|
|
2023-05-26 12:18:17 -05:00
|
|
|
let _thread = stdx::thread::Builder::new(stdx::thread::ThreadIntent::Worker)
|
2019-08-30 12:18:57 -05:00
|
|
|
.name("test server".to_string())
|
2020-06-03 05:22:01 -05:00
|
|
|
.spawn(move || main_loop(config, connection).unwrap())
|
2019-08-30 12:18:57 -05:00
|
|
|
.expect("failed to spawn a thread");
|
|
|
|
|
2020-06-23 06:20:53 -05:00
|
|
|
Server { req_id: Cell::new(1), dir, messages: Default::default(), client, _thread }
|
2018-09-01 12:21:11 -05:00
|
|
|
}
|
|
|
|
|
2020-11-02 09:31:38 -06:00
|
|
|
pub(crate) fn doc_id(&self, rel_path: &str) -> TextDocumentIdentifier {
|
2018-09-01 12:21:11 -05:00
|
|
|
let path = self.dir.path().join(rel_path);
|
2019-02-08 05:49:43 -06:00
|
|
|
TextDocumentIdentifier { uri: Url::from_file_path(path).unwrap() }
|
2018-09-01 12:21:11 -05:00
|
|
|
}
|
|
|
|
|
2020-11-02 09:31:38 -06:00
|
|
|
pub(crate) fn notification<N>(&self, params: N::Params)
|
2019-05-27 06:20:11 -05:00
|
|
|
where
|
2019-08-30 09:24:11 -05:00
|
|
|
N: lsp_types::notification::Notification,
|
2019-05-27 06:20:11 -05:00
|
|
|
N::Params: Serialize,
|
|
|
|
{
|
2019-08-30 09:24:11 -05:00
|
|
|
let r = Notification::new(N::METHOD.to_string(), params);
|
2019-05-27 06:20:11 -05:00
|
|
|
self.send_notification(r)
|
|
|
|
}
|
|
|
|
|
2021-04-13 03:48:12 -05:00
|
|
|
#[track_caller]
|
2020-11-02 09:31:38 -06:00
|
|
|
pub(crate) fn request<R>(&self, params: R::Params, expected_resp: Value)
|
2018-09-01 12:21:11 -05:00
|
|
|
where
|
2019-08-30 09:24:11 -05:00
|
|
|
R: lsp_types::request::Request,
|
2018-09-01 12:21:11 -05:00
|
|
|
R::Params: Serialize,
|
|
|
|
{
|
2019-01-10 15:37:10 -06:00
|
|
|
let actual = self.send_request::<R>(params);
|
2019-06-03 09:01:10 -05:00
|
|
|
if let Some((expected_part, actual_part)) = find_mismatch(&expected_resp, &actual) {
|
|
|
|
panic!(
|
2018-12-06 14:07:31 -06:00
|
|
|
"JSON mismatch\nExpected:\n{}\nWas:\n{}\nExpected part:\n{}\nActual part:\n{}\n",
|
|
|
|
to_string_pretty(&expected_resp).unwrap(),
|
|
|
|
to_string_pretty(&actual).unwrap(),
|
|
|
|
to_string_pretty(expected_part).unwrap(),
|
|
|
|
to_string_pretty(actual_part).unwrap(),
|
2019-06-03 09:01:10 -05:00
|
|
|
);
|
2018-12-06 14:07:31 -06:00
|
|
|
}
|
2018-09-01 12:21:11 -05:00
|
|
|
}
|
|
|
|
|
2020-11-02 09:31:38 -06:00
|
|
|
pub(crate) fn send_request<R>(&self, params: R::Params) -> Value
|
2018-09-01 12:21:11 -05:00
|
|
|
where
|
2019-08-30 09:24:11 -05:00
|
|
|
R: lsp_types::request::Request,
|
2018-09-01 12:21:11 -05:00
|
|
|
R::Params: Serialize,
|
|
|
|
{
|
2019-01-10 15:37:10 -06:00
|
|
|
let id = self.req_id.get();
|
2020-11-16 14:10:13 -06:00
|
|
|
self.req_id.set(id.wrapping_add(1));
|
2019-01-10 15:37:10 -06:00
|
|
|
|
2019-08-30 09:24:11 -05:00
|
|
|
let r = Request::new(id.into(), R::METHOD.to_string(), params);
|
2018-09-02 08:36:03 -05:00
|
|
|
self.send_request_(r)
|
|
|
|
}
|
2019-08-30 09:24:11 -05:00
|
|
|
fn send_request_(&self, r: Request) -> Value {
|
|
|
|
let id = r.id.clone();
|
2021-01-28 07:00:33 -06:00
|
|
|
self.client.sender.send(r.clone().into()).unwrap();
|
2022-12-23 12:42:58 -06:00
|
|
|
while let Some(msg) = self.recv().unwrap_or_else(|Timeout| panic!("timeout: {r:?}")) {
|
2018-09-01 12:21:11 -05:00
|
|
|
match msg {
|
2020-06-24 05:27:13 -05:00
|
|
|
Message::Request(req) => {
|
2020-07-02 08:32:56 -05:00
|
|
|
if req.method == "client/registerCapability" {
|
|
|
|
let params = req.params.to_string();
|
|
|
|
if ["workspace/didChangeWatchedFiles", "textDocument/didSave"]
|
2021-11-03 07:21:46 -05:00
|
|
|
.into_iter()
|
|
|
|
.any(|it| params.contains(it))
|
2020-07-02 08:32:56 -05:00
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
2022-12-23 12:42:58 -06:00
|
|
|
panic!("unexpected request: {req:?}")
|
2020-06-24 05:27:13 -05:00
|
|
|
}
|
2019-08-30 09:24:11 -05:00
|
|
|
Message::Notification(_) => (),
|
|
|
|
Message::Response(res) => {
|
2018-09-01 12:21:11 -05:00
|
|
|
assert_eq!(res.id, id);
|
|
|
|
if let Some(err) = res.error {
|
2022-12-23 12:42:58 -06:00
|
|
|
panic!("error response: {err:#?}");
|
2018-09-01 12:21:11 -05:00
|
|
|
}
|
|
|
|
return res.result.unwrap();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-12-23 12:42:58 -06:00
|
|
|
panic!("no response for {r:?}");
|
2018-09-01 12:21:11 -05:00
|
|
|
}
|
2020-11-02 09:31:38 -06:00
|
|
|
pub(crate) fn wait_until_workspace_is_loaded(self) -> Server {
|
2019-08-30 09:24:11 -05:00
|
|
|
self.wait_for_message_cond(1, &|msg: &Message| match msg {
|
2021-04-06 06:16:35 -05:00
|
|
|
Message::Notification(n) if n.method == "experimental/serverStatus" => {
|
2021-01-28 10:10:48 -06:00
|
|
|
let status = n
|
|
|
|
.clone()
|
2021-04-06 06:16:35 -05:00
|
|
|
.extract::<lsp_ext::ServerStatusParams>("experimental/serverStatus")
|
|
|
|
.unwrap();
|
2023-04-04 12:25:57 -05:00
|
|
|
if status.health != lsp_ext::Health::Ok {
|
|
|
|
panic!("server errored/warned while loading workspace: {:?}", status.message);
|
2023-04-04 12:12:10 -05:00
|
|
|
}
|
2021-04-06 06:16:35 -05:00
|
|
|
status.quiescent
|
2018-10-15 16:44:23 -05:00
|
|
|
}
|
|
|
|
_ => false,
|
2021-01-28 07:00:33 -06:00
|
|
|
})
|
|
|
|
.unwrap_or_else(|Timeout| panic!("timeout while waiting for ws to load"));
|
2020-08-25 12:02:28 -05:00
|
|
|
self
|
2019-03-07 08:46:17 -06:00
|
|
|
}
|
2021-01-28 07:00:33 -06:00
|
|
|
fn wait_for_message_cond(
|
|
|
|
&self,
|
|
|
|
n: usize,
|
|
|
|
cond: &dyn Fn(&Message) -> bool,
|
|
|
|
) -> Result<(), Timeout> {
|
2018-09-03 16:49:21 -05:00
|
|
|
let mut total = 0;
|
2018-09-02 06:46:15 -05:00
|
|
|
for msg in self.messages.borrow().iter() {
|
2019-03-07 08:46:17 -06:00
|
|
|
if cond(msg) {
|
2018-09-03 16:49:21 -05:00
|
|
|
total += 1
|
2018-09-02 06:46:15 -05:00
|
|
|
}
|
|
|
|
}
|
2018-09-03 16:49:21 -05:00
|
|
|
while total < n {
|
2021-01-28 07:00:33 -06:00
|
|
|
let msg = self.recv()?.expect("no response");
|
2019-03-07 08:46:17 -06:00
|
|
|
if cond(&msg) {
|
2018-09-03 16:49:21 -05:00
|
|
|
total += 1;
|
2018-09-02 06:46:15 -05:00
|
|
|
}
|
|
|
|
}
|
2021-01-28 07:00:33 -06:00
|
|
|
Ok(())
|
2018-09-02 06:46:15 -05:00
|
|
|
}
|
2021-01-28 07:00:33 -06:00
|
|
|
fn recv(&self) -> Result<Option<Message>, Timeout> {
|
|
|
|
let msg = recv_timeout(&self.client.receiver)?;
|
|
|
|
let msg = msg.map(|msg| {
|
2018-10-15 16:44:23 -05:00
|
|
|
self.messages.borrow_mut().push(msg.clone());
|
|
|
|
msg
|
2021-01-28 07:00:33 -06:00
|
|
|
});
|
|
|
|
Ok(msg)
|
2018-09-02 04:34:06 -05:00
|
|
|
}
|
2019-08-30 09:24:11 -05:00
|
|
|
fn send_notification(&self, not: Notification) {
|
2019-08-30 12:18:57 -05:00
|
|
|
self.client.sender.send(Message::Notification(not)).unwrap();
|
2018-09-01 12:21:11 -05:00
|
|
|
}
|
2019-04-13 12:45:21 -05:00
|
|
|
|
2020-11-02 09:31:38 -06:00
|
|
|
pub(crate) fn path(&self) -> &Path {
|
2019-04-13 12:45:21 -05:00
|
|
|
self.dir.path()
|
|
|
|
}
|
2018-09-01 12:21:11 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Drop for Server {
|
|
|
|
fn drop(&mut self) {
|
2019-08-30 12:18:57 -05:00
|
|
|
self.request::<Shutdown>((), Value::Null);
|
|
|
|
self.notification::<Exit>(());
|
2018-09-01 12:21:11 -05:00
|
|
|
}
|
|
|
|
}
|
2018-09-08 04:08:46 -05:00
|
|
|
|
2021-01-28 07:00:33 -06:00
|
|
|
struct Timeout;
|
|
|
|
|
|
|
|
fn recv_timeout(receiver: &Receiver<Message>) -> Result<Option<Message>, Timeout> {
|
2020-07-28 03:24:33 -05:00
|
|
|
let timeout =
|
|
|
|
if cfg!(target_os = "macos") { Duration::from_secs(300) } else { Duration::from_secs(120) };
|
2018-09-08 04:08:46 -05:00
|
|
|
select! {
|
2021-01-28 07:00:33 -06:00
|
|
|
recv(receiver) -> msg => Ok(msg.ok()),
|
|
|
|
recv(after(timeout)) -> _ => Err(Timeout),
|
2018-09-08 04:08:46 -05:00
|
|
|
}
|
|
|
|
}
|
2021-03-08 11:27:08 -06:00
|
|
|
|
|
|
|
// Comparison functionality borrowed from cargo:
|
|
|
|
|
|
|
|
/// Compares JSON object for approximate equality.
|
|
|
|
/// You can use `[..]` wildcard in strings (useful for OS dependent things such
|
|
|
|
/// as paths). You can use a `"{...}"` string literal as a wildcard for
|
|
|
|
/// arbitrary nested JSON. Arrays are sorted before comparison.
|
|
|
|
fn find_mismatch<'a>(expected: &'a Value, actual: &'a Value) -> Option<(&'a Value, &'a Value)> {
|
|
|
|
match (expected, actual) {
|
|
|
|
(Value::Number(l), Value::Number(r)) if l == r => None,
|
|
|
|
(Value::Bool(l), Value::Bool(r)) if l == r => None,
|
|
|
|
(Value::String(l), Value::String(r)) if lines_match(l, r) => None,
|
|
|
|
(Value::Array(l), Value::Array(r)) => {
|
|
|
|
if l.len() != r.len() {
|
|
|
|
return Some((expected, actual));
|
|
|
|
}
|
|
|
|
|
|
|
|
let mut l = l.iter().collect::<Vec<_>>();
|
|
|
|
let mut r = r.iter().collect::<Vec<_>>();
|
|
|
|
|
|
|
|
l.retain(|l| match r.iter().position(|r| find_mismatch(l, r).is_none()) {
|
|
|
|
Some(i) => {
|
|
|
|
r.remove(i);
|
|
|
|
false
|
|
|
|
}
|
|
|
|
None => true,
|
|
|
|
});
|
|
|
|
|
|
|
|
if !l.is_empty() {
|
|
|
|
assert!(!r.is_empty());
|
2021-06-12 22:54:16 -05:00
|
|
|
Some((l[0], r[0]))
|
2021-03-08 11:27:08 -06:00
|
|
|
} else {
|
|
|
|
assert_eq!(r.len(), 0);
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
(Value::Object(l), Value::Object(r)) => {
|
|
|
|
fn sorted_values(obj: &serde_json::Map<String, Value>) -> Vec<&Value> {
|
|
|
|
let mut entries = obj.iter().collect::<Vec<_>>();
|
|
|
|
entries.sort_by_key(|it| it.0);
|
|
|
|
entries.into_iter().map(|(_k, v)| v).collect::<Vec<_>>()
|
|
|
|
}
|
|
|
|
|
|
|
|
let same_keys = l.len() == r.len() && l.keys().all(|k| r.contains_key(k));
|
|
|
|
if !same_keys {
|
|
|
|
return Some((expected, actual));
|
|
|
|
}
|
|
|
|
|
|
|
|
let l = sorted_values(l);
|
|
|
|
let r = sorted_values(r);
|
|
|
|
|
2022-06-08 09:35:11 -05:00
|
|
|
l.into_iter().zip(r).find_map(|(l, r)| find_mismatch(l, r))
|
2021-03-08 11:27:08 -06:00
|
|
|
}
|
|
|
|
(Value::Null, Value::Null) => None,
|
|
|
|
// magic string literal "{...}" acts as wildcard for any sub-JSON
|
|
|
|
(Value::String(l), _) if l == "{...}" => None,
|
|
|
|
_ => Some((expected, actual)),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Compare a line with an expected pattern.
|
|
|
|
/// - Use `[..]` as a wildcard to match 0 or more characters on the same line
|
|
|
|
/// (similar to `.*` in a regex).
|
|
|
|
fn lines_match(expected: &str, actual: &str) -> bool {
|
|
|
|
// Let's not deal with / vs \ (windows...)
|
|
|
|
// First replace backslash-escaped backslashes with forward slashes
|
|
|
|
// which can occur in, for example, JSON output
|
2022-03-12 06:22:12 -06:00
|
|
|
let expected = expected.replace(r"\\", "/").replace('\\', "/");
|
|
|
|
let mut actual: &str = &actual.replace(r"\\", "/").replace('\\', "/");
|
2021-03-08 11:27:08 -06:00
|
|
|
for (i, part) in expected.split("[..]").enumerate() {
|
|
|
|
match actual.find(part) {
|
|
|
|
Some(j) => {
|
|
|
|
if i == 0 && j != 0 {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
actual = &actual[j + part.len()..];
|
|
|
|
}
|
|
|
|
None => return false,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
actual.is_empty() || expected.ends_with("[..]")
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn lines_match_works() {
|
|
|
|
assert!(lines_match("a b", "a b"));
|
|
|
|
assert!(lines_match("a[..]b", "a b"));
|
|
|
|
assert!(lines_match("a[..]", "a b"));
|
|
|
|
assert!(lines_match("[..]", "a b"));
|
|
|
|
assert!(lines_match("[..]b", "a b"));
|
|
|
|
|
|
|
|
assert!(!lines_match("[..]b", "c"));
|
|
|
|
assert!(!lines_match("b", "c"));
|
|
|
|
assert!(!lines_match("b", "cb"));
|
|
|
|
}
|