From: <nan...@na...> - 2013-03-28 22:20:14
|
From: Giacomo Tesio <gi...@te...> --- src/NAnt.Core/Tasks/SysInfo.cs | 2 +- tests/NAnt.Core/Tasks/SysInfoTaskTest.cs | 34 +++++++++++++++++++++++------ 2 files changed, 28 insertions(+), 8 deletions(-) diff --git a/src/NAnt.Core/Tasks/SysInfo.cs b/src/NAnt.Core/Tasks/SysInfo.cs index de719f9..dfacfcc 100644 --- a/src/NAnt.Core/Tasks/SysInfo.cs +++ b/src/NAnt.Core/Tasks/SysInfo.cs @@ -190,7 +190,7 @@ namespace NAnt.Core.Tasks { IDictionary variables = Environment.GetEnvironmentVariables(); foreach (string name in variables.Keys) { try { - string safeName = name.Replace("(x86)", ".x86"); // since on 64bit Windows provide such variable names, let's make them nice + string safeName = name.EndsWith("(x86)") ? name.Replace("(x86)", ".x86") : name; // since on 64bit Windows provide such variable names, let's make them nice safeName = Regex.Replace(safeName, "[^_A-Za-z0-9\\-.]", "_"); // see PropertyDictionary.ValidatePropertyName Properties[Prefix + "env." + safeName] = (string)variables[name]; } catch (Exception ex) { diff --git a/tests/NAnt.Core/Tasks/SysInfoTaskTest.cs b/tests/NAnt.Core/Tasks/SysInfoTaskTest.cs index 16a85d5..989a16d 100644 --- a/tests/NAnt.Core/Tasks/SysInfoTaskTest.cs +++ b/tests/NAnt.Core/Tasks/SysInfoTaskTest.cs @@ -86,23 +86,43 @@ namespace Tests.NAnt.Core.Tasks { Assert.IsTrue(match.Success, "SysInfo property should've been modified!" + Environment.NewLine + result); } - [Test] - [ExpectedException(typeof(TestBuildException))] - public void Test_FailOnIllegalPropertyNames() { + [TestCase("AnIllegalPropertyWithWrong^Chars>", "Value")] + [TestCase("AnIllegalPropertyWithWrong(Chars)", "Value")] + [TestCase("My(x86)Property", "Value")] + public void Test_FixEnvironmentVariablesThatWouldBeMappedToIllegalPropertyNames(string name, string value) + { // - // ensure properties with parentheses will fail like ProgramFiles(x86) + // ensure properties with names that would be illegal property names are fixed (with an "_" replacing invalid chars) // SetEnvironmentVariable is first available on .Net 2.0 // string xml = @"<?xml version='1.0' ?> <project> - <sysinfo /> + <sysinfo verbose='true' /> </project>"; - if (AssignEnvironmentVariable("AnIllegalPropertyWithPa(rens)", "AnIllegalPropertyWithParens")) { - RunBuild(xml); + if (AssignEnvironmentVariable(name, value)) + { + string result = RunBuild(xml); + string expression = string.Format("{0} = {1}", Regex.Replace(name, "[^_A-Za-z0-9\\-.]", "_"), value); + Assert.IsTrue(result.Contains(expression), "SysInfo should have fixed an environment variable name that is not a valid property name!" + Environment.NewLine + result); } else { throw new TestBuildException(); } + } + [TestCase("sys.env.CommonProgramFiles.x86")] + [TestCase("sys.env.ProgramFiles.x86")] + public void Test_FixEnvironmentVariablesEndingWith_x86(string expectedVariableName) + { + // + // ensure properties with "(x86)" will fail like ProgramFiles(x86) + // SetEnvironmentVariable is first available on .Net 2.0 + // + string xml = @"<?xml version='1.0' ?> + <project> + <sysinfo verbose='true' /> + </project>"; + string result = RunBuild(xml); + Assert.IsTrue(result.Contains(expectedVariableName + " = "), "SysInfo should have fixed an environment variable name that is not a valid property name!" + Environment.NewLine + result); } #if (ONLY_1_0 || ONLY_1_1) -- 1.7.7 |