Skip to content

Commit 3624c06

Browse files
committed
tries to fix tests
1 parent aeabe98 commit 3624c06

File tree

1 file changed

+63
-42
lines changed

1 file changed

+63
-42
lines changed

pyrefly/lib/test/lsp/lsp_interaction/diagnostic.rs

Lines changed: 63 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ use pyrefly_config::environment::environment::PythonEnvironment;
1414

1515
use crate::test::lsp::lsp_interaction::object_model::InitializeSettings;
1616
use crate::test::lsp::lsp_interaction::object_model::LspInteraction;
17+
use crate::test::lsp::lsp_interaction::object_model::ValidationResult;
1718
use crate::test::lsp::lsp_interaction::util::get_test_files_root;
1819

1920
#[test]
@@ -663,65 +664,85 @@ fn test_version_support_publish_diagnostics() {
663664
..Default::default()
664665
});
665666

667+
let gen_validator = |expected_version: i64| {
668+
let actual_uri = uri.as_str();
669+
move |msg: &Message| {
670+
let Message::Notification(Notification { method, params }) = msg else {
671+
return ValidationResult::Skip;
672+
};
673+
let Some(uri_val) = params.get("uri") else {
674+
return ValidationResult::Skip;
675+
};
676+
let Some(expected_uri) = uri_val.as_str() else {
677+
return ValidationResult::Skip;
678+
};
679+
if expected_uri == actual_uri && method == "textDocument/publishDiagnostics" {
680+
if let Some(actual_version) = params.get("version") {
681+
if let Some(actual_version) = actual_version.as_i64() {
682+
assert!(
683+
actual_version <= expected_version,
684+
"expected version: {}, actual version: {}",
685+
expected_version,
686+
actual_version
687+
);
688+
return match actual_version.cmp(&expected_version) {
689+
std::cmp::Ordering::Less => ValidationResult::Skip,
690+
std::cmp::Ordering::Equal => ValidationResult::Pass,
691+
std::cmp::Ordering::Greater => ValidationResult::Fail,
692+
};
693+
}
694+
}
695+
}
696+
ValidationResult::Skip
697+
}
698+
};
699+
666700
interaction.server.did_open("text_document.py");
667-
interaction.server.diagnostic("text_document.py");
668701

669-
interaction
670-
.client
671-
.expect_message(lsp_server::Message::Notification(
672-
lsp_server::Notification {
673-
method: "textDocument/publishDiagnostics".to_owned(),
674-
params: serde_json::json! {{
675-
"uri": uri,
676-
"diagnostics": [],
677-
"version": 1
678-
}},
679-
},
680-
));
702+
let version = 1;
703+
interaction.client.expect_message_helper(
704+
gen_validator(version),
705+
&format!(
706+
"publishDiagnostics notification with version {} for file: {}",
707+
version,
708+
uri.as_str()
709+
),
710+
);
681711

682-
interaction.server.did_change("text_document.py", "# test");
683-
interaction.server.diagnostic("text_document.py");
712+
interaction.server.did_change("text_document.py", "a = b");
684713

685-
// I don't understand why this version is still 1
686-
interaction
687-
.client
688-
.expect_message(lsp_server::Message::Notification(
689-
lsp_server::Notification {
690-
method: "textDocument/publishDiagnostics".to_owned(),
691-
params: serde_json::json! {{
692-
"uri": uri,
693-
"diagnostics": [],
694-
"version": 2
695-
}},
696-
},
697-
));
714+
let version = 2;
715+
interaction.client.expect_message_helper(
716+
gen_validator(version),
717+
&format!(
718+
"publishDiagnostics notification with version {} for file: {}",
719+
version,
720+
uri.as_str()
721+
),
722+
);
698723

699724
interaction
700725
.server
701726
.send_message(Message::Notification(Notification {
702727
method: "textDocument/didClose".to_owned(),
703728
params: serde_json::json!({
704729
"textDocument": {
705-
"uri": uri.to_string(),
730+
"uri": uri.as_str(),
706731
"languageId": "python",
707732
"version": 3
708733
},
709734
}),
710735
}));
711-
interaction.server.diagnostic("text_document.py");
712736

713-
interaction
714-
.client
715-
.expect_message(lsp_server::Message::Notification(
716-
lsp_server::Notification {
717-
method: "textDocument/publishDiagnostics".to_owned(),
718-
params: serde_json::json! {{
719-
"uri": uri,
720-
"diagnostics": [],
721-
"version": 3
722-
}},
723-
},
724-
));
737+
let version = 3;
738+
interaction.client.expect_message_helper(
739+
gen_validator(version),
740+
&format!(
741+
"publishDiagnostics notification with version {} for file: {}",
742+
version,
743+
uri.as_str()
744+
),
745+
);
725746

726747
interaction.shutdown();
727748
}

0 commit comments

Comments
 (0)