Skip to content

Commit 4b46a13

Browse files
rumyaxKonstantin Tyukalovlocalconstkirill-ivlev
authored
WI 204820 improvements (#18840)
* Add env expansion for bash * Add powershell env expand * Update merge func name * Add env variables pwsh test suite * Add special powershell error for safety * Add sanitizing err for bash for safety * Update to while * Fix powershell env expand * Update bash tests * Update powershell tests * Add braced syntax support * Add test cases * Add custom err for powershell * Update error telemetry * Update telemetry data * Update powershell helpers * Add tests for powershell implementation * Remove case-insensetive test * Update sanitize regexes * Add % to allowlist * Fix null-ref error when not sanitized * Add more powershell tests * Add bash tests * Remove minimum agent version assert * Add invalid env syntax test * Update logic of bash env expansion * Update powershell env expand logic * Remove unused telemetry * Fix minor powershell sanitizing issues --------- Co-authored-by: Konstantin Tyukalov <[email protected]> Co-authored-by: Konstantin Tyukalov <[email protected]> Co-authored-by: Kirill Ivlev <[email protected]>
1 parent d870cf0 commit 4b46a13

26 files changed

+1243
-356
lines changed
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
import assert = require('assert');
2+
import { expandBashEnvVariables } from '../../helpers';
3+
4+
export const BashEnvProcessingTests = () => {
5+
const testSuites: [string, string, string[], string][] = [
6+
[
7+
'Handles empty line',
8+
'', [], ''
9+
],
10+
[
11+
'Expanding known env variables',
12+
'$VAR1 2', ['VAR1=value1'], 'value1 2'
13+
],
14+
[
15+
'Expanding env variables with brace syntax',
16+
'${VAR1} 2', ['VAR1=value1'], 'value1 2'
17+
],
18+
[
19+
'Expanding multiple env variables',
20+
'1 ${VAR1} $VAR2', ['VAR1=value1', 'VAR2=value2'], '1 value1 value2'
21+
],
22+
[
23+
'Expanding multiple close env variables',
24+
'$VAR1 ${VAR2}$VAR3', ['VAR1=value1', 'VAR2=value2', 'VAR3=value3'], 'value1 value2value3'
25+
],
26+
[
27+
'Expanding multiple close env variables',
28+
'$VAR1 ${VAR2}_$VAR3', ['VAR1=value1', 'VAR2=value2', 'VAR3=value3'], 'value1 value2_value3'
29+
],
30+
[
31+
'Expanding multiple close env variables 3',
32+
'${VAR1}${VAR2}$VAR3', ['VAR1=1', 'VAR2=2', 'VAR3=3'], '123'
33+
],
34+
[
35+
'Expanding multiple env variables 2',
36+
'$VAR1 $VAR2', ['VAR1=1', 'VAR2=2'], '1 2'
37+
],
38+
[
39+
'Not expanding nested env variables',
40+
'$VAR1 $VAR2', ['VAR1=$NESTED', 'VAR2=2', 'NESTED=nested'], '$NESTED 2'
41+
],
42+
[
43+
'Backslash before env var',
44+
'\\$VAR1', ['VAR1=value1'], '$VAR1'
45+
],
46+
[
47+
'Backslash at start of env var',
48+
'$\\VAR1', ['VAR1=value1'], '$\\VAR1'
49+
],
50+
[
51+
'Backslash inside env var - leave as is',
52+
'$V\\AR1', ['VAR1=value1'], '$V\\AR1'
53+
],
54+
[
55+
`If it's inside the single quotes - it should be ignored`,
56+
`$VAR1 '$VAR2'`, ['VAR1=value1', 'VAR2=value2'], `value1 '$VAR2'`
57+
],
58+
[
59+
`If it's inside the single quotes - it should be ignored 2`,
60+
`$VAR1 ' _ $VAR2 _ '`, ['VAR1=value1', 'VAR2=value2'], `value1 ' _ $VAR2 _ '`
61+
],
62+
[
63+
`If it's inside the single quotes - it should be ignored 3`,
64+
`$VAR1 ' _ $VAR2 _ ''$VAR3'`, ['VAR1=value1', 'VAR2=value2', 'VAR3=value3'], `value1 ' _ $VAR2 _ ''$VAR3'`
65+
],
66+
[
67+
`If it's inside the double quotes - it should be expanded`,
68+
`$VAR1 "$VAR2"`, ['VAR1=value1', 'VAR2=value2'], `value1 "value2"`
69+
],
70+
[
71+
`Close quotes`,
72+
`''$VAR1 $VAR2`, ['VAR1=value1', 'VAR2=value2'], `''value1 value2`
73+
],
74+
[
75+
'Skips variables with indirect expansion',
76+
'${VAR1} ${!VAR2} "${!VAR3}"', ['VAR1=value1', 'VAR2=value2', 'VAR2=value3'], 'value1 ${!VAR2} "${!VAR3}"'
77+
],
78+
[
79+
'Skips variables with invalid name',
80+
"${1VAR1} ${a:b} ${a:+b} ${a:?b} ${VAR1:0:2} ${VAR1\\val\\lav}", ['VAR1=value1', 'a=value3', 'a:b=value4'], "${1VAR1} ${a:b} ${a:+b} ${a:?b} ${VAR1:0:2} ${VAR1\\val\\lav}"
81+
],
82+
[
83+
'If variable syntax is incorrect, it should leave it as is',
84+
'$venv:VAR1 ${_env:VAR2}', ['VAR1=val1', 'VAR2=val2'], '$venv:VAR1 ${_env:VAR2}'
85+
],
86+
[
87+
'If closing brace is not present, it should leave it as is',
88+
'$VAR1 ${VAR2', ['VAR1=val1', 'VAR2=val2'], 'val1 ${VAR2',
89+
],
90+
[
91+
'If closing brace is not present, it should leave it as is 2',
92+
'${VAR1 ${VAR2}', ['VAR1=val1', 'VAR2=val2'], '${VAR1 ${VAR2}',
93+
]
94+
]
95+
96+
for (const [testName, input, variables, expected] of testSuites) {
97+
it(testName, () => {
98+
99+
for (const variable of variables) {
100+
const [name, value] = variable.split('=');
101+
process.env[name] = value;
102+
}
103+
104+
const [result] = expandBashEnvVariables(input);
105+
106+
assert.deepStrictEqual(result, expected);
107+
});
108+
}
109+
};
Lines changed: 10 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,37 @@
11
import assert = require('assert');
2-
import { processBashEnvVariables } from '../../bashEnvProcessor';
2+
import { expandBashEnvVariables } from '../../helpers';
33

44
export const EnvProcessingTelemetryTests = () => {
55
// maybe we should treat that differently
66
it('Found prefixes test', () => {
77
const argsLine = '$ ${} $';
88
const expectedTelemetry = { foundPrefixes: 3 };
99

10-
const [_, resultTelemetry] = processBashEnvVariables(argsLine);
10+
const [_, resultTelemetry] = expandBashEnvVariables(argsLine);
1111

1212
assert.deepStrictEqual(resultTelemetry.foundPrefixes, expectedTelemetry.foundPrefixes);
1313
})
1414
it('Not closed brace syntax', () => {
1515
const argsLine = '${';
1616
const expectedTelemetry = { notClosedBraceSyntaxPosition: 1 };
1717

18-
const [_, resultTelemetry] = processBashEnvVariables(argsLine);
18+
const [_, resultTelemetry] = expandBashEnvVariables(argsLine);
1919

2020
assert.deepStrictEqual(resultTelemetry.notClosedBraceSyntaxPosition, expectedTelemetry.notClosedBraceSyntaxPosition);
2121
})
2222
it('Not closed brace syntax 2', () => {
2323
const argsLine = "'${' ${";
2424
const expectedTelemetry = { notClosedBraceSyntaxPosition: 6 };
2525

26-
const [_, resultTelemetry] = processBashEnvVariables(argsLine);
26+
const [_, resultTelemetry] = expandBashEnvVariables(argsLine);
2727

2828
assert.deepStrictEqual(resultTelemetry.notClosedBraceSyntaxPosition, expectedTelemetry.notClosedBraceSyntaxPosition);
2929
})
3030
it('Not closed quotes', () => {
3131
const argsLine = "' $A";
3232
const expectedTelemetry = { unmatchedQuotes: 1 };
3333

34-
const [_, resultTelemetry] = processBashEnvVariables(argsLine);
34+
const [_, resultTelemetry] = expandBashEnvVariables(argsLine);
3535

3636
assert.deepStrictEqual(resultTelemetry.unmatchedQuotes, expectedTelemetry.unmatchedQuotes);
3737
})
@@ -40,32 +40,24 @@ export const EnvProcessingTelemetryTests = () => {
4040
const argsLine = "'$VAR1' '$VAR2' '3'";
4141
const expectedTelemetry = { quottedBlocks: 2 };
4242

43-
const [_, resultTelemetry] = processBashEnvVariables(argsLine);
43+
const [_, resultTelemetry] = expandBashEnvVariables(argsLine);
4444

4545
assert.deepStrictEqual(resultTelemetry.quottedBlocks, expectedTelemetry.quottedBlocks);
4646
})
47-
it('Counts variables started from esc symbol', () => {
48-
const argsLine = "$\\VAR1 '$\\VAR2' $\\VAR3";
49-
const expectedTelemetry = { variablesStartsFromES: 2 };
50-
51-
const [_, resultTelemetry] = processBashEnvVariables(argsLine);
52-
53-
assert.deepStrictEqual(resultTelemetry.variablesStartsFromES, expectedTelemetry.variablesStartsFromES);
54-
})
5547
it('Catches indirect expansion', () => {
5648
const argsLine = "${!VAR1} ${!VAR2}";
5749
const expectedTelemetry = { indirectExpansion: 2 };
5850

59-
const [_, resultTelemetry] = processBashEnvVariables(argsLine);
51+
const [_, resultTelemetry] = expandBashEnvVariables(argsLine);
6052

61-
assert.deepStrictEqual(resultTelemetry.indirectExpansion, expectedTelemetry.indirectExpansion);
53+
assert.deepStrictEqual(resultTelemetry.indirectExpansionTries, expectedTelemetry.indirectExpansion);
6254
})
6355
it('Skip indirect expansion inside quotes', () => {
6456
const argsLine = "'${!VAR1}'";
6557
const expectedTelemetry = { indirectExpansion: 0 };
6658

67-
const [_, resultTelemetry] = processBashEnvVariables(argsLine);
59+
const [_, resultTelemetry] = expandBashEnvVariables(argsLine);
6860

69-
assert.deepStrictEqual(resultTelemetry.indirectExpansion, expectedTelemetry.indirectExpansion);
61+
assert.deepStrictEqual(resultTelemetry.indirectExpansionTries, expectedTelemetry.indirectExpansion);
7062
})
7163
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
export * from './L0EnvExpansion';
2+
export * from './L0Telemetry';

Tasks/BashV3/Tests/EnvProcessing/L0EnvProcessing.ts

Lines changed: 0 additions & 205 deletions
This file was deleted.

0 commit comments

Comments
 (0)