File tree Expand file tree Collapse file tree 2 files changed +21
-5
lines changed Expand file tree Collapse file tree 2 files changed +21
-5
lines changed Original file line number Diff line number Diff line change @@ -26,14 +26,19 @@ export class ParsedEnvironmentFile {
26
26
}
27
27
28
28
let parseErrors : string [ ] = [ ] ;
29
- let env = initialEnv ?? { } ;
29
+ let safeInitialEnv = initialEnv ?? { } ;
30
+ let env = { ...safeInitialEnv } ;
30
31
31
32
content . split ( "\n" ) . forEach ( line => {
32
33
// Split the line between key and value
33
34
const match = line . match ( / ^ \s * ( [ \w \. \- ] + ) \s * = \s * ( .* ) ? \s * $ / ) ;
34
35
35
36
if ( match !== null ) {
36
37
const key = match [ 1 ] ;
38
+ if ( safeInitialEnv [ key ] !== undefined ) {
39
+ return ;
40
+ }
41
+
37
42
let value = match [ 2 ] ?? "" ;
38
43
if ( value . length > 0 && value . charAt ( 0 ) === '"' && value . charAt ( value . length - 1 ) === '"' ) {
39
44
value = value . replace ( / \\ n / gm, "\n" ) ;
Original file line number Diff line number Diff line change @@ -46,24 +46,35 @@ MyName2=Value2
46
46
result . Env [ "MyName2" ] . should . equal ( "Value2" ) ;
47
47
} ) ;
48
48
49
- test ( "Update variable " , ( ) => {
49
+ test ( "Not override variables " , ( ) => {
50
50
const content = `
51
- MyName1=Value1
51
+ CommonKey=NewValue
52
52
MyName2=Value2
53
53
54
54
` ;
55
55
const initialEnv = {
56
- "MyName1 " : "Value7 " ,
56
+ "CommonKey " : "InitialValue " ,
57
57
"ThisShouldNotChange" : "StillHere"
58
58
} ;
59
59
const result = ParsedEnvironmentFile . CreateFromContent ( content , "TestEnvFileName" , initialEnv ) ;
60
60
61
61
expect ( result . Warning ) . to . be . undefined ;
62
- result . Env [ "MyName1 " ] . should . equal ( "Value1 " ) ;
62
+ result . Env [ "CommonKey " ] . should . equal ( "InitialValue " ) ;
63
63
result . Env [ "MyName2" ] . should . equal ( "Value2" ) ;
64
64
result . Env [ "ThisShouldNotChange" ] . should . equal ( "StillHere" ) ;
65
65
} ) ;
66
66
67
+ test ( "Take last value" , ( ) => {
68
+ const content = `
69
+ Key=FirstValue
70
+ Key=SecondValue
71
+ ` ;
72
+ const result = ParsedEnvironmentFile . CreateFromContent ( content , "TestEnvFileName" , undefined ) ;
73
+
74
+ expect ( result . Warning ) . to . be . undefined ;
75
+ result . Env [ "Key" ] . should . equal ( "SecondValue" ) ;
76
+ } ) ;
77
+
67
78
test ( "Handle comments" , ( ) => {
68
79
const content = `# This is an environment file
69
80
MyName1=Value1
You can’t perform that action at this time.
0 commit comments