public void TestEnvironmentVariables_Environment_DataRoundTrips()
{
ProcessStartInfo psi = new ProcessStartInfo();
// Creating a detached ProcessStartInfo will pre-populate the environment
// with current environmental variables.
psi.Environment.Clear();
psi.EnvironmentVariables.Add("NewKey", "NewValue");
psi.Environment.Add("NewKey2", "NewValue2");
Assert.Equal(psi.Environment["NewKey"], psi.EnvironmentVariables["NewKey"]);
Assert.Equal(psi.Environment["NewKey2"], psi.EnvironmentVariables["NewKey2"]);
Assert.Equal(2, psi.EnvironmentVariables.Count);
Assert.Equal(psi.Environment.Count, psi.EnvironmentVariables.Count);
Assert.Throws<ArgumentException>(() => { psi.EnvironmentVariables.Add("NewKey2", "NewValue2"); });
psi.EnvironmentVariables.Add("NewKey3", "NewValue3");
Assert.Throws<ArgumentException>(() => { psi.Environment.Add("NewKey3", "NewValue3"); });
psi.EnvironmentVariables.Clear();
Assert.Equal(0, psi.Environment.Count);
psi.EnvironmentVariables.Add("NewKey", "NewValue");
psi.EnvironmentVariables.Add("NewKey2", "NewValue2");
string environmentResultKey = "";
string environmentResultValue = "";
foreach(var entry in psi.Environment)
{
environmentResultKey += entry.Key;
environmentResultValue += entry.Value;
}
Assert.Equal("NewKeyNewKey2", environmentResultKey);
Assert.Equal("NewValueNewValue2", environmentResultValue);
string envVarResultKey = "";
string envVarResultValue = "";
foreach(KeyValuePair<string, string> entry in psi.EnvironmentVariables)
{
envVarResultKey += entry.Key;
envVarResultValue += entry.Value;
}
Assert.Equal(environmentResultKey, envVarResultKey);
Assert.Equal(environmentResultValue, envVarResultValue);
psi.EnvironmentVariables.Add("NewKey3", "NewValue3");
KeyValuePair<string, string>[] kvpa = new KeyValuePair<string, string>[5];
psi.Environment.CopyTo(kvpa, 0);
Assert.Equal("NewKey3", kvpa[2].Key);
Assert.Equal("NewValue3", kvpa[2].Value);
psi.EnvironmentVariables.Remove("NewKey3");
Assert.False(psi.Environment.Contains(new KeyValuePair<string,string>("NewKey3", "NewValue3")));
}