File tree Expand file tree Collapse file tree 2 files changed +55
-5
lines changed
web-integration/tests/unit-test/yaml Expand file tree Collapse file tree 2 files changed +55
-5
lines changed Original file line number Diff line number Diff line change @@ -11,13 +11,29 @@ import yaml from 'js-yaml';
1111const debugUtils = getDebug ( 'yaml:utils' ) ;
1212
1313export function interpolateEnvVars ( content : string ) : string {
14- return content . replace ( / \$ \{ ( [ ^ } ] + ) \} / g, ( _ , envVar ) => {
15- const value = process . env [ envVar . trim ( ) ] ;
16- if ( value === undefined ) {
17- throw new Error ( `Environment variable "${ envVar . trim ( ) } " is not defined` ) ;
14+ // Process line by line to skip commented lines
15+ const lines = content . split ( '\n' ) ;
16+ const processedLines = lines . map ( ( line ) => {
17+ // Check if the line is a YAML comment (starts with # after optional whitespace)
18+ const trimmedLine = line . trimStart ( ) ;
19+ if ( trimmedLine . startsWith ( '#' ) ) {
20+ // Skip interpolation for comment lines
21+ return line ;
1822 }
19- return value ;
23+
24+ // Process environment variables for non-comment lines
25+ return line . replace ( / \$ \{ ( [ ^ } ] + ) \} / g, ( _ , envVar ) => {
26+ const value = process . env [ envVar . trim ( ) ] ;
27+ if ( value === undefined ) {
28+ throw new Error (
29+ `Environment variable "${ envVar . trim ( ) } " is not defined` ,
30+ ) ;
31+ }
32+ return value ;
33+ } ) ;
2034 } ) ;
35+
36+ return processedLines . join ( '\n' ) ;
2137}
2238
2339export function parseYamlScript (
Original file line number Diff line number Diff line change @@ -53,6 +53,40 @@ tasks:
5353 ) ;
5454 } ) ;
5555
56+ test ( 'should not throw error for undefined env vars in commented lines' , ( ) => {
57+ const yamlContent = `
58+ # DEV_USERNAME="\${UNDEFINED_ENV_VAR}"
59+ target:
60+ url: "https://example.com"
61+ tasks:
62+ - sleep: 1000
63+ # - aiInput: "\${ANOTHER_UNDEFINED_VAR}"
64+ # locate: "input field"
65+ ` ;
66+
67+ expect ( ( ) => parseYamlScript ( yamlContent ) ) . not . toThrow ( ) ;
68+ const result = parseYamlScript ( yamlContent ) ;
69+ expect ( result . target ?. url ) . toBe ( 'https://example.com' ) ;
70+ } ) ;
71+
72+ test ( 'should handle mixed commented and uncommented env vars' , ( ) => {
73+ process . env . DEFINED_VAR = 'defined_value' ;
74+
75+ const yamlContent = `
76+ # This is a comment with \${UNDEFINED_VAR_IN_COMMENT}
77+ target:
78+ url: "https://example.com/\${DEFINED_VAR}"
79+ tasks:
80+ - sleep: 1000
81+ # - aiAction: "click \${UNDEFINED_IN_COMMENT}"
82+ ` ;
83+
84+ const result = parseYamlScript ( yamlContent ) ;
85+ expect ( result . target ?. url ) . toBe ( 'https://example.com/defined_value' ) ;
86+
87+ process . env . DEFINED_VAR = undefined ;
88+ } ) ;
89+
5690 test ( 'android number-style deviceId' , ( ) => {
5791 const yamlContent = `
5892android:
You can’t perform that action at this time.
0 commit comments