1 // Licensed to the .NET Foundation under one or more agreements. 2 // The .NET Foundation licenses this file to you under the MIT license. 3 // See the LICENSE file in the project root for more information. 4 5 using Xunit; 6 7 namespace System.Tests 8 { 9 public class ExpandEnvironmentVariables 10 { 11 [Fact] NullArgumentThrowsArgumentNull()12 public void NullArgumentThrowsArgumentNull() 13 { 14 Assert.Throws<ArgumentNullException>(() => Environment.ExpandEnvironmentVariables(null)); 15 } 16 17 [Fact] EmptyArgumentReturnsEmpty()18 public void EmptyArgumentReturnsEmpty() 19 { 20 Assert.Equal(String.Empty, Environment.ExpandEnvironmentVariables(String.Empty)); 21 } 22 23 [Fact] ExpansionOfVariableSucceeds()24 public void ExpansionOfVariableSucceeds() 25 { 26 // The test is going to test that we can correctly expand variables. 27 // We are going to do: 28 // envvar1=animal; 29 // and we are going to check that the expanded %envvar1% is animal. 30 31 Random r = new Random(); 32 string envVar1 = "TestVariable_ExpansionOfVariableSucceeds_" + r.Next().ToString(); 33 string expectedValue = "animal"; 34 35 try 36 { 37 Environment.SetEnvironmentVariable(envVar1, expectedValue); 38 39 string result = Environment.ExpandEnvironmentVariables("%" + envVar1 + "%"); 40 41 Assert.Equal(expectedValue, result); 42 } 43 finally 44 { 45 // Clear the variables we just set 46 Environment.SetEnvironmentVariable(envVar1, null); 47 } 48 } 49 50 [Fact] VariableThatDoesNotExistGoesThroughUnexpanded()51 public void VariableThatDoesNotExistGoesThroughUnexpanded() 52 { 53 string unexpanded = "%TestVariable_ThatDoesNotExist%"; 54 Assert.Equal(unexpanded, Environment.ExpandEnvironmentVariables(unexpanded)); 55 } 56 57 [Theory] 58 [InlineData("Hello World")] 59 [InlineData("C:\\J\\workspace\\debug_windows---bc3c3f12\\artifacts\\win81-x86\\stage2\\sdk\\2.0.0-preview1-005938\\Extensions")] // longer than 100 chars, a magic number in the impl StringWithNoEnvironmentVariablesGoesThroughUnchanged(string value)60 public void StringWithNoEnvironmentVariablesGoesThroughUnchanged(string value) 61 { 62 for (int i = 0; i < 2; i++) // invoke multiple times to exercise StringBuilder reuse path 63 { 64 Assert.Equal(value, Environment.ExpandEnvironmentVariables(value)); 65 } 66 } 67 68 [Fact] PotentiallyAmbiguousInputIsHandledCorrectly()69 public void PotentiallyAmbiguousInputIsHandledCorrectly() 70 { 71 int count = 6; 72 string prefix = "ExpandTestVar_@"; 73 string[] keys = new string[count]; 74 string[] values = new string[count]; 75 76 for (int i = 0; i < count; i++) 77 { 78 keys[i] = prefix + (i + 1); 79 Assert.Equal(null, Environment.GetEnvironmentVariable(keys[i])); 80 81 if (i < 3) 82 { 83 Environment.SetEnvironmentVariable(keys[i], "value" + (i + 1)); 84 } 85 } 86 87 string set1 = keys[0], set2 = keys[1], set3 = keys[2]; 88 string unset1 = keys[3], unset2 = keys[4], unset3 = keys[5]; 89 string value1, value2, value3; 90 91 value1 = "value1"; 92 value2 = "value2"; 93 value3 = "value3"; 94 95 Test("%", 96 "%"); 97 98 Test("%%", 99 "%%"); 100 101 Test("%%%", 102 "%%%"); 103 104 Test(("%" + set1 + "%") + set2 + ("%" + set3 + "%"), 105 value1 + set2 + value3); 106 107 Test("%" + ("%" + set1 + "%"), 108 "%" + value1); 109 110 Test("%%" + ("%" + set1 + "%") + "%%", 111 "%%" + value1 + "%%"); 112 113 Test("%%%" + ("%" + set1 + "%") + "%", 114 "%%%" + value1 + "%"); 115 116 Test(("%" + set1 + "%") + ("%" + set2 + "%"), 117 value1 + value2); 118 119 Test(("%" + unset1 + "%") + ("%" + set1 + "%"), 120 ("%" + unset1 + "%") + value1); 121 122 Test(("%" + set2 + "%") + "hello" + ("%" + unset2 + "%"), 123 value2 + "hello" + ("%" + unset2 + "%")); 124 125 Test(("%" + unset2 + "%") + ("%" + unset3 + "%"), 126 ("%" + unset2 + "%") + ("%" + unset3 + "%")); 127 128 Test("% " + set1 + "%", 129 "% " + set1 + "%"); 130 131 Test("% " + set1 + " %", 132 "% " + set1 + " %"); 133 134 Test("%\t" + set1 + "%", 135 "%\t" + set1 + "%"); 136 137 Test("%%% " + set1 + "%", 138 "%%% " + set1 + "%"); 139 } 140 Test(string toExpand, string expectedExpansion)141 private void Test(string toExpand, string expectedExpansion) 142 { 143 Assert.Equal(expectedExpansion, Environment.ExpandEnvironmentVariables(toExpand)); 144 Assert.Equal("qq" + expectedExpansion + "rr", "qq" + Environment.ExpandEnvironmentVariables(toExpand) + "rr"); 145 } 146 } 147 } 148