Skip to content

Commit 81d3c2b

Browse files
Improve PowerShell escaping fixtures, refine regexes
Expand test fixtures for PowerShell escaping based on mutation testing results. This mostly identified gaps in the testing where the use of the "g" flag wasn't covered by tests due to a lack of repeated characters to escape in a string. Additionally, based on mutation testing too, update some of the regular expressions. Expressions anchored to the start or end of the string don't need to have the "g" flag and the `backslashSuffix` regex can't possibly match the start of the string because it requires at least a non-whitespace character at the start and a whitespace character after that (because of the guard in which this expression is used).
1 parent 8ba4d12 commit 81d3c2b

File tree

2 files changed

+58
-2
lines changed

2 files changed

+58
-2
lines changed

src/internal/win/powershell.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,10 @@ export function getEscapeFunction() {
2020
const specials2 = new RegExp("([$&'(),;{|}‘’‚‛“”„])", "g");
2121

2222
const whitespace = new RegExp("([\\s\u0085])", "g");
23-
const whitespacePrefix = new RegExp("^[\\s\u0085]+", "g");
23+
const whitespacePrefix = new RegExp("^[\\s\u0085]+");
2424

2525
const quote = new RegExp('(^|[^\\\\])(\\\\*)"', "g");
26-
const backslashSuffix = new RegExp("(^|[^\\\\])(\\\\+)$", "g");
26+
const backslashSuffix = new RegExp("([^\\\\])(\\\\+)$");
2727

2828
return (arg) => {
2929
arg = arg

test/fixtures/win.js

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3074,6 +3074,10 @@ export const escape = {
30743074
input: "a\u0085@b",
30753075
expected: "a`\u0085`@b",
30763076
},
3077+
{
3078+
input: "a @b @c",
3079+
expected: "a` `@b` `@c",
3080+
},
30773081
],
30783082
"hashtags ('#')": [
30793083
{
@@ -3110,6 +3114,10 @@ export const escape = {
31103114
input: "a\u0085#b",
31113115
expected: "a`\u0085`#b",
31123116
},
3117+
{
3118+
input: "a #b #c",
3119+
expected: "a` `#b` `#c",
3120+
},
31133121
],
31143122
"carets ('^')": [
31153123
{
@@ -3218,6 +3226,10 @@ export const escape = {
32183226
input: "a\u0085-b",
32193227
expected: "a`\u0085`-b",
32203228
},
3229+
{
3230+
input: "a -b -c",
3231+
expected: "a` `-b` `-c",
3232+
},
32213233
],
32223234
"backslashes ('\\')": [
32233235
{
@@ -3236,6 +3248,10 @@ export const escape = {
32363248
input: "a\\",
32373249
expected: "a\\",
32383250
},
3251+
{
3252+
input: "\\",
3253+
expected: "\\",
3254+
},
32393255
],
32403256
"backslashes ('\\') + whitespace": [
32413257
{
@@ -3274,6 +3290,14 @@ export const escape = {
32743290
input: " a b\\",
32753291
expected: "` ` a` b\\\\",
32763292
},
3293+
{
3294+
input: " \\",
3295+
expected: "` \\",
3296+
},
3297+
{
3298+
input: "\\ \\",
3299+
expected: "\\` \\\\",
3300+
},
32773301
],
32783302
"colons (':')": [
32793303
{
@@ -3310,6 +3334,10 @@ export const escape = {
33103334
input: "a\u0085:b",
33113335
expected: "a`\u0085`:b",
33123336
},
3337+
{
3338+
input: "a :b :c",
3339+
expected: "a` `:b` `:c",
3340+
},
33133341
],
33143342
"semicolons (';')": [
33153343
{
@@ -3466,6 +3494,18 @@ export const escape = {
34663494
input: "a\u0085]b",
34673495
expected: "a`\u0085`]b",
34683496
},
3497+
{
3498+
input: "a [b [c",
3499+
expected: "a` [b` [c",
3500+
},
3501+
{
3502+
input: "a ]b ]c",
3503+
expected: "a` `]b` `]c",
3504+
},
3505+
{
3506+
input: "a [b ]c",
3507+
expected: "a` [b` `]c",
3508+
},
34693509
],
34703510
"curly brackets ('{', '}')": [
34713511
{
@@ -3568,6 +3608,14 @@ export const escape = {
35683608
input: "a\u0085>b",
35693609
expected: "a`\u0085`>b",
35703610
},
3611+
{
3612+
input: "a <b <c",
3613+
expected: "a` `<b` `<c",
3614+
},
3615+
{
3616+
input: "a >b >c",
3617+
expected: "a` `>b` `>c",
3618+
},
35713619
],
35723620
"right angle brackets ('>') + digits (/[0-9]/)": [
35733621
{
@@ -3650,6 +3698,10 @@ export const escape = {
36503698
input: "a 0>b",
36513699
expected: "a` 0>b",
36523700
},
3701+
{
3702+
input: "a 0>b 1>c 2>d 3>e 4>f 5>g 6>h 7>i 8>j 9>k",
3703+
expected: "a` 0>b` 1`>c` 2`>d` 3`>e` 4`>f` 5`>g` 6`>h` 7>i` 8>j` 9>k",
3704+
},
36533705
{
36543706
input: "a\t1>b",
36553707
expected: "a`\t1`>b",
@@ -3748,6 +3800,10 @@ export const escape = {
37483800
input: "a\u0085*>b",
37493801
expected: "a`\u0085*`>b",
37503802
},
3803+
{
3804+
input: "a *>b *>c",
3805+
expected: "a` *`>b` *`>c",
3806+
},
37513807
],
37523808
"left double quotation mark ('“')": [
37533809
{

0 commit comments

Comments
 (0)