Skip to content

Commit 1c27a6d

Browse files
lsndrosiewicz
andauthored
Do not escape glob pattern in dynamic Jest/Vitest test names (#36999)
Related to #35090 Release Notes: - javascript: Fixed name escaping in dynamic jest/vitest task names --------- Co-authored-by: Piotr Osiewicz <[email protected]>
1 parent 256a910 commit 1c27a6d

File tree

3 files changed

+43
-5
lines changed

3 files changed

+43
-5
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/languages/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ dap.workspace = true
4545
futures.workspace = true
4646
gpui.workspace = true
4747
http_client.workspace = true
48+
itertools.workspace = true
4849
language.workspace = true
4950
log.workspace = true
5051
lsp.workspace = true

crates/languages/src/typescript.rs

Lines changed: 41 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ use collections::HashMap;
55
use futures::future::join_all;
66
use gpui::{App, AppContext, AsyncApp, Task};
77
use http_client::github::{AssetKind, GitHubLspBinaryVersion, build_asset_url};
8+
use itertools::Itertools as _;
89
use language::{
910
ContextLocation, ContextProvider, File, LanguageName, LanguageToolchainStore, LspAdapter,
1011
LspAdapterDelegate, LspInstaller, Toolchain,
@@ -18,7 +19,7 @@ use std::{
1819
borrow::Cow,
1920
ffi::OsString,
2021
path::{Path, PathBuf},
21-
sync::Arc,
22+
sync::{Arc, LazyLock},
2223
};
2324
use task::{TaskTemplate, TaskTemplates, VariableName};
2425
use util::merge_json_value_into;
@@ -511,9 +512,9 @@ fn eslint_server_binary_arguments(server_path: &Path) -> Vec<OsString> {
511512
}
512513

513514
fn replace_test_name_parameters(test_name: &str) -> String {
514-
let pattern = regex::Regex::new(r"(%|\$)[0-9a-zA-Z]+").unwrap();
515-
516-
regex::escape(&pattern.replace_all(test_name, "(.+?)"))
515+
static PATTERN: LazyLock<regex::Regex> =
516+
LazyLock::new(|| regex::Regex::new(r"(\$([A-Za-z0-9_\.]+|[\#])|%[psdifjo#\$%])").unwrap());
517+
PATTERN.split(test_name).map(regex::escape).join("(.+?)")
517518
}
518519

519520
pub struct TypeScriptLspAdapter {
@@ -1015,7 +1016,9 @@ mod tests {
10151016
use unindent::Unindent;
10161017
use util::path;
10171018

1018-
use crate::typescript::{PackageJsonData, TypeScriptContextProvider};
1019+
use crate::typescript::{
1020+
PackageJsonData, TypeScriptContextProvider, replace_test_name_parameters,
1021+
};
10191022

10201023
#[gpui::test]
10211024
async fn test_outline(cx: &mut TestAppContext) {
@@ -1227,4 +1230,37 @@ mod tests {
12271230
]
12281231
);
12291232
}
1233+
#[test]
1234+
fn test_escaping_name() {
1235+
let cases = [
1236+
("plain test name", "plain test name"),
1237+
("test name with $param_name", "test name with (.+?)"),
1238+
("test name with $nested.param.name", "test name with (.+?)"),
1239+
("test name with $#", "test name with (.+?)"),
1240+
("test name with $##", "test name with (.+?)\\#"),
1241+
("test name with %p", "test name with (.+?)"),
1242+
("test name with %s", "test name with (.+?)"),
1243+
("test name with %d", "test name with (.+?)"),
1244+
("test name with %i", "test name with (.+?)"),
1245+
("test name with %f", "test name with (.+?)"),
1246+
("test name with %j", "test name with (.+?)"),
1247+
("test name with %o", "test name with (.+?)"),
1248+
("test name with %#", "test name with (.+?)"),
1249+
("test name with %$", "test name with (.+?)"),
1250+
("test name with %%", "test name with (.+?)"),
1251+
("test name with %q", "test name with %q"),
1252+
(
1253+
"test name with regex chars .*+?^${}()|[]\\",
1254+
"test name with regex chars \\.\\*\\+\\?\\^\\$\\{\\}\\(\\)\\|\\[\\]\\\\",
1255+
),
1256+
(
1257+
"test name with multiple $params and %pretty and %b and (.+?)",
1258+
"test name with multiple (.+?) and (.+?)retty and %b and \\(\\.\\+\\?\\)",
1259+
),
1260+
];
1261+
1262+
for (input, expected) in cases {
1263+
assert_eq!(replace_test_name_parameters(input), expected);
1264+
}
1265+
}
12301266
}

0 commit comments

Comments
 (0)