-
Notifications
You must be signed in to change notification settings - Fork 677
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Warning processing fix and tests for env files
This commit builds on the work of @SebastianPfliegel to finish up support for env files in launch.json. Two changes: 1. Fixes a build break in the way warnings were hooked up. I also decided to add a button to open up the env file that has the warning. 2. Moves the parser into a seperate class and adds a unit test.
- Loading branch information
1 parent
12984b3
commit 45c2f6d
Showing
3 changed files
with
194 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
/*--------------------------------------------------------------------------------------------- | ||
* Copyright (c) Microsoft Corporation. All rights reserved. | ||
* Licensed under the MIT License. See License.txt in the project root for license information. | ||
*--------------------------------------------------------------------------------------------*/ | ||
|
||
import * as fs from 'fs-extra'; | ||
|
||
export class ParsedEnvironmentFile | ||
{ | ||
public Env: { [key: string]: any }; | ||
public Warning: string | null; | ||
|
||
private constructor(env: { [key: string]: any }, warning: string | null) | ||
{ | ||
this.Env = env; | ||
this.Warning = warning; | ||
} | ||
|
||
public static CreateFromFile(envFile: string, initialEnv: { [key: string]: any } | undefined): ParsedEnvironmentFile { | ||
let content: string = fs.readFileSync(envFile, "utf8"); | ||
return this.CreateFromContent(content, envFile, initialEnv); | ||
} | ||
|
||
public static CreateFromContent(content: string, envFile: string, initialEnv: { [key: string]: any } | undefined): ParsedEnvironmentFile { | ||
|
||
// Remove UTF-8 BOM if present | ||
if(content.charAt(0) === '\uFEFF') { | ||
content = content.substr(1); | ||
} | ||
|
||
let parseErrors: string[] = []; | ||
let env: { [key: string]: any } = initialEnv; | ||
if (!env) { | ||
env = {}; | ||
} | ||
|
||
content.split("\n").forEach(line => { | ||
// Split the line between key and value | ||
const r: RegExpMatchArray = line.match(/^\s*([\w\.\-]+)\s*=\s*(.*)?\s*$/); | ||
|
||
if (r !== null) { | ||
const key: string = r[1]; | ||
let value: string = r[2] || ""; | ||
if ((value.length > 0) && (value.charAt(0) === '"') && (value.charAt(value.length - 1) === '"')) { | ||
value = value.replace(/\\n/gm, "\n"); | ||
} | ||
|
||
value = value.replace(/(^['"]|['"]$)/g, ""); | ||
|
||
env[key] = value; | ||
} | ||
else { | ||
// Blank lines and lines starting with # are no parse errors | ||
const comments: RegExp = new RegExp(/^\s*(#|$)/); | ||
if (!comments.test(line)) { | ||
parseErrors.push(line); | ||
} | ||
} | ||
}); | ||
|
||
// show error message if single lines cannot get parsed | ||
let warning: string = null; | ||
if(parseErrors.length !== 0) { | ||
warning = "Ignoring non-parseable lines in envFile " + envFile + ": "; | ||
parseErrors.forEach(function (value, idx, array) { | ||
warning += "\"" + value + "\"" + ((idx !== array.length - 1) ? ", " : "."); | ||
}); | ||
} | ||
|
||
return new ParsedEnvironmentFile(env, warning); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
/*--------------------------------------------------------------------------------------------- | ||
* Copyright (c) Microsoft Corporation. All rights reserved. | ||
* Licensed under the MIT License. See License.txt in the project root for license information. | ||
*--------------------------------------------------------------------------------------------*/ | ||
|
||
import { ParsedEnvironmentFile } from '../../src/coreclr-debug/ParsedEnvironmentFile'; | ||
import { should, expect } from 'chai'; | ||
|
||
suite("ParsedEnvironmentFile", () => { | ||
suiteSetup(() => should()); | ||
|
||
test("Add single variable", () => { | ||
const content = `MyName=VALUE`; | ||
const fakeConfig : { [key: string]: any } = {}; | ||
const result = ParsedEnvironmentFile.CreateFromContent(content, "TestEnvFileName", fakeConfig["env"]); | ||
|
||
expect(result.Warning).to.be.null; | ||
result.Env["MyName"].should.equal("VALUE"); | ||
}); | ||
|
||
test("Handle quoted values", () => { | ||
const content = `MyName="VALUE"`; | ||
const fakeConfig : { [key: string]: any } = {}; | ||
const result = ParsedEnvironmentFile.CreateFromContent(content, "TestEnvFileName", fakeConfig["env"]); | ||
|
||
expect(result.Warning).to.be.null; | ||
result.Env["MyName"].should.equal("VALUE"); | ||
}); | ||
|
||
test("Handle BOM", () => { | ||
const content = "\uFEFFMyName=VALUE"; | ||
const fakeConfig : { [key: string]: any } = {}; | ||
const result = ParsedEnvironmentFile.CreateFromContent(content, "TestEnvFileName", fakeConfig["env"]); | ||
|
||
expect(result.Warning).to.be.null; | ||
result.Env["MyName"].should.equal("VALUE"); | ||
}); | ||
|
||
test("Add multiple variables", () => { | ||
const content = ` | ||
MyName1=Value1 | ||
MyName2=Value2 | ||
`; | ||
const fakeConfig : { [key: string]: any } = {}; | ||
const result = ParsedEnvironmentFile.CreateFromContent(content, "TestEnvFileName", fakeConfig["env"]); | ||
|
||
expect(result.Warning).to.be.null; | ||
result.Env["MyName1"].should.equal("Value1"); | ||
result.Env["MyName2"].should.equal("Value2"); | ||
}); | ||
|
||
test("Update variable", () => { | ||
const content = ` | ||
MyName1=Value1 | ||
MyName2=Value2 | ||
`; | ||
const initialEnv : { [key: string]: any } = { | ||
"MyName1": "Value7", | ||
"ThisShouldNotChange": "StillHere" | ||
}; | ||
const result = ParsedEnvironmentFile.CreateFromContent(content, "TestEnvFileName", initialEnv); | ||
|
||
expect(result.Warning).to.be.null; | ||
result.Env["MyName1"].should.equal("Value1"); | ||
result.Env["MyName2"].should.equal("Value2"); | ||
result.Env["ThisShouldNotChange"].should.equal("StillHere"); | ||
}); | ||
|
||
test("Handle comments", () => { | ||
const content = `# This is an environment file | ||
MyName1=Value1 | ||
# This is a comment in the middle of the file | ||
MyName2=Value2 | ||
`; | ||
const fakeConfig : { [key: string]: any } = {}; | ||
const result = ParsedEnvironmentFile.CreateFromContent(content, "TestEnvFileName", fakeConfig["env"]); | ||
|
||
expect(result.Warning).to.be.null; | ||
result.Env["MyName1"].should.equal("Value1"); | ||
result.Env["MyName2"].should.equal("Value2"); | ||
}); | ||
|
||
test("Handle invalid lines", () => { | ||
const content = ` | ||
This_Line_Is_Wrong | ||
MyName1=Value1 | ||
MyName2=Value2 | ||
`; | ||
const fakeConfig : { [key: string]: any } = {}; | ||
const result = ParsedEnvironmentFile.CreateFromContent(content, "TestEnvFileName", fakeConfig["env"]); | ||
|
||
result.Warning.should.startWith("Ignoring non-parseable lines in envFile TestEnvFileName"); | ||
result.Env["MyName1"].should.equal("Value1"); | ||
result.Env["MyName2"].should.equal("Value2"); | ||
}); | ||
}); |