move env vars for snapshot tests to UpdateTest

This commit is contained in:
roife 2024-12-26 20:08:12 +08:00
parent 1dcce45f48
commit bba8d2dc02
3 changed files with 33 additions and 15 deletions

View file

@ -1,4 +1,4 @@
use std::{fmt, ops::Not}; use std::fmt;
use ast::HasName; use ast::HasName;
use cfg::{CfgAtom, CfgExpr}; use cfg::{CfgAtom, CfgExpr};
@ -20,7 +20,7 @@ use span::{Edition, TextSize};
use stdx::format_to; use stdx::format_to;
use syntax::{ use syntax::{
ast::{self, AstNode}, ast::{self, AstNode},
SmolStr, SyntaxNode, ToSmolStr, format_smolstr, SmolStr, SyntaxNode, ToSmolStr,
}; };
use crate::{references, FileId, NavigationTarget, ToNav, TryToNav}; use crate::{references, FileId, NavigationTarget, ToNav, TryToNav};
@ -639,7 +639,25 @@ impl UpdateTest {
} }
let res: SmolStr = builder.join(" + ").into(); let res: SmolStr = builder.join(" + ").into();
res.is_empty().not().then_some(res) if res.is_empty() {
None
} else {
Some(format_smolstr!("\u{fe0e} Update Tests ({res})"))
}
}
pub fn env(&self) -> SmallVec<[(&str, &str); 3]> {
let mut env = SmallVec::new();
if self.expect_test {
env.push(("UPDATE_EXPECT", "1"));
}
if self.insta {
env.push(("INSTA_UPDATE", "always"));
}
if self.snapbox {
env.push(("SNAPSHOTS", "overwrite"));
}
env
} }
} }

View file

@ -943,7 +943,8 @@ pub(crate) fn handle_runnables(
let update_test = runnable.update_test; let update_test = runnable.update_test;
if let Some(mut runnable) = to_proto::runnable(&snap, runnable)? { if let Some(mut runnable) = to_proto::runnable(&snap, runnable)? {
if let Some(runnable) = to_proto::make_update_runnable(&runnable, &update_test.label()) if let Some(runnable) =
to_proto::make_update_runnable(&runnable, &update_test.label(), &update_test.env())
{ {
res.push(runnable); res.push(runnable);
} }
@ -2135,10 +2136,7 @@ fn runnable_action_links(
} }
let client_commands_config = snap.config.client_commands(); let client_commands_config = snap.config.client_commands();
if !(client_commands_config.run_single if !(client_commands_config.run_single || client_commands_config.debug_single) {
|| client_commands_config.debug_single
|| client_commands_config.update_single)
{
return None; return None;
} }
@ -2158,8 +2156,10 @@ fn runnable_action_links(
group.commands.push(to_command_link(dbg_command, r.label.clone())); group.commands.push(to_command_link(dbg_command, r.label.clone()));
} }
if hover_actions_config.update_test && client_commands_config.update_single { if hover_actions_config.update_test && client_commands_config.run_single {
if let Some(update_command) = to_proto::command::update_single(&r, &update_test.label()) { let label = update_test.label();
if let Some(r) = to_proto::make_update_runnable(&r, &label, &update_test.env()) {
let update_command = to_proto::command::run_single(&r, label.unwrap().as_str());
group.commands.push(to_command_link(update_command, r.label.clone())); group.commands.push(to_command_link(update_command, r.label.clone()));
} }
} }

View file

@ -1606,7 +1606,8 @@ pub(crate) fn code_lens(
} }
if lens_config.update_test && client_commands_config.run_single { if lens_config.update_test && client_commands_config.run_single {
let label = update_test.label(); let label = update_test.label();
if let Some(r) = make_update_runnable(&r, &label) { let env = update_test.env();
if let Some(r) = make_update_runnable(&r, &label, &env) {
let command = command::run_single(&r, label.unwrap().as_str()); let command = command::run_single(&r, label.unwrap().as_str());
acc.push(lsp_types::CodeLens { acc.push(lsp_types::CodeLens {
range: annotation_range, range: annotation_range,
@ -1851,9 +1852,10 @@ pub(crate) mod command {
} }
} }
fn make_update_runnable( pub(crate) fn make_update_runnable(
runnable: &lsp_ext::Runnable, runnable: &lsp_ext::Runnable,
label: &Option<SmolStr>, label: &Option<SmolStr>,
env: &[(&str, &str)],
) -> Option<lsp_ext::Runnable> { ) -> Option<lsp_ext::Runnable> {
if !matches!(runnable.args, lsp_ext::RunnableArgs::Cargo(_)) { if !matches!(runnable.args, lsp_ext::RunnableArgs::Cargo(_)) {
return None; return None;
@ -1867,9 +1869,7 @@ fn make_update_runnable(
unreachable!(); unreachable!();
}; };
let environment_vars = r.environment.extend(env.iter().map(|(k, v)| (k.to_string(), v.to_string())));
[("UPDATE_EXPECT", "1"), ("INSTA_UPDATE", "always"), ("SNAPSHOTS", "overwrite")];
r.environment.extend(environment_vars.into_iter().map(|(k, v)| (k.to_owned(), v.to_owned())));
Some(runnable) Some(runnable)
} }