Skip to content

Commit 596f32f

Browse files
kinto0meta-codesync[bot]
authored andcommitted
test diagnostics changes upon config invalidation
Summary: D87952758 will also fix this bug. if an existing error change, it previously would not update I would've liked to do this as a before/after test, but I didn't think it was worth adding an expect_diagnostisc_message_does_not_contain (because that would pass too frequently and likely be useless) Reviewed By: yangdanny97 Differential Revision: D88529135 fbshipit-source-id: 6788b614bb6567c66259b663719741131c3e7da5
1 parent e3ba534 commit 596f32f

File tree

2 files changed

+87
-0
lines changed

2 files changed

+87
-0
lines changed

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

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -357,6 +357,58 @@ fn test_interpreter_change_removes_type_errors() {
357357
interaction.shutdown().unwrap();
358358
}
359359

360+
// Only run this test on unix since windows has no way to mock a .exe without compiling something
361+
// (we call python with python.exe)
362+
#[cfg(unix)]
363+
#[test]
364+
fn test_interpreter_change_changes_existing_type_errors() {
365+
let test_files_root = get_test_files_root();
366+
let interpreter_path = setup_dummy_interpreter(
367+
&test_files_root
368+
.path()
369+
.join("interpreter_with_no_site_packages"),
370+
);
371+
372+
let mut interaction = LspInteraction::new();
373+
interaction.set_root(test_files_root.path().to_path_buf());
374+
interaction
375+
.initialize(InitializeSettings {
376+
configuration: Some(Some(
377+
json!([{"pyrefly": {"displayTypeErrors": "force-on"}}]),
378+
)),
379+
..Default::default()
380+
})
381+
.unwrap();
382+
383+
interaction.client.did_open("custom_interpreter/src/foo.py");
384+
// Without any interpreter configured, there should be 1 import error
385+
interaction
386+
.client
387+
.expect_publish_diagnostics_error_count(
388+
test_files_root.path().join("custom_interpreter/src/foo.py"),
389+
1,
390+
)
391+
.unwrap();
392+
interaction.client.did_change_configuration();
393+
interaction
394+
.client
395+
.expect_request::<WorkspaceConfiguration>(json!({"items":[{"section":"python"}]}))
396+
.unwrap()
397+
.send_configuration_response(json!([
398+
{
399+
"pythonPath": interpreter_path.to_str().unwrap()
400+
}
401+
]));
402+
interaction
403+
.client
404+
.expect_publish_diagnostics_message_contains(
405+
test_files_root.path().join("custom_interpreter/src/foo.py"),
406+
"interpreter_with_no_site_packages",
407+
)
408+
.unwrap();
409+
interaction.shutdown().unwrap();
410+
}
411+
360412
#[test]
361413
fn test_disable_language_services() {
362414
let test_files_root = get_test_files_root();

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

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -824,6 +824,41 @@ impl TestClient {
824824
})
825825
}
826826

827+
/// Wait for a publishDiagnostics notification, then check if it contains the message
828+
pub fn expect_publish_diagnostics_message_contains(
829+
&self,
830+
path: PathBuf,
831+
message: &str,
832+
) -> Result<(), LspMessageError> {
833+
self.expect_message(
834+
&format!(
835+
"publishDiagnostics notification containing message '{message}' for file: {}",
836+
path.display()
837+
),
838+
|msg| {
839+
if let Message::Notification(x) = msg
840+
&& x.method == PublishDiagnostics::METHOD
841+
{
842+
let params: PublishDiagnosticsParams =
843+
serde_json::from_value(x.params).unwrap();
844+
if params.uri.to_file_path().ok().as_ref() == Some(&path)
845+
&& params
846+
.diagnostics
847+
.iter()
848+
.any(|d| d.message.contains(message))
849+
{
850+
Some(())
851+
} else {
852+
None
853+
}
854+
} else {
855+
None
856+
}
857+
},
858+
)?;
859+
Ok(())
860+
}
861+
827862
/// Wait for a publishDiagnostics notification, then check if it has the correct path and count
828863
pub fn expect_publish_diagnostics_error_count(
829864
&self,

0 commit comments

Comments
 (0)