forked from dotnet/runtime
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAppSettingsTests.cs
More file actions
164 lines (143 loc) · 6.72 KB
/
AppSettingsTests.cs
File metadata and controls
164 lines (143 loc) · 6.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System.Collections.Generic;
using System.Configuration;
using System.IO;
using Xunit;
namespace System.ConfigurationTests
{
public class AppSettingsTests
{
[Fact]
public void EmptyRuntimeAppSettings()
{
var appSettings = ConfigurationManager.AppSettings;
Assert.NotNull(appSettings);
Assert.Empty(appSettings);
}
[Fact]
public void EmptyDesignTimeAppSettings()
{
using (var temp = new TempConfig(TestData.EmptyConfig))
{
var config = ConfigurationManager.OpenExeConfiguration(temp.ExePath);
Assert.NotNull(config.AppSettings);
Assert.Empty(config.AppSettings.Settings);
}
}
[Fact]
public void SimpleAppSettings()
{
using (var temp = new TempConfig(TestData.SimpleConfig))
{
var config = ConfigurationManager.OpenExeConfiguration(temp.ExePath);
Assert.NotNull(config.AppSettings);
Assert.Equal(2, config.AppSettings.Settings.Count);
Assert.Equal("FooValue", config.AppSettings.Settings["FooKey"].Value);
Assert.Equal("BarValue", config.AppSettings.Settings["BarKey"].Value);
}
}
[Fact]
public void AddToAppSettings_NoSave()
{
using (var temp = new TempConfig(TestData.EmptyConfig))
{
var config = ConfigurationManager.OpenExeConfiguration(temp.ExePath);
Assert.NotNull(config.AppSettings);
Assert.Empty(config.AppSettings.Settings);
config.AppSettings.Settings.Add("NewKey", "NewValue");
Assert.NotEmpty(config.AppSettings.Settings);
Assert.Equal("NewValue", config.AppSettings.Settings["NewKey"].Value);
config = ConfigurationManager.OpenExeConfiguration(temp.ExePath);
Assert.Empty(config.AppSettings.Settings);
}
}
[Fact]
[ActiveIssue("https://github.com/dotnet/runtime/issues/21510", TargetFrameworkMonikers.NetFramework)]
public void AddToAppSettings_Save()
{
using (var temp = new TempConfig(TestData.EmptyConfig))
{
var config = ConfigurationManager.OpenExeConfiguration(temp.ExePath);
Assert.NotNull(config.AppSettings);
Assert.Empty(config.AppSettings.Settings);
config.AppSettings.Settings.Add("NewKey", "NewValue");
Assert.NotEmpty(config.AppSettings.Settings);
Assert.Equal("NewValue", config.AppSettings.Settings["NewKey"].Value);
config.Save();
// Make sure we didn't serialize the implicit machine.config
Assert.False(File.Exists(new ConfigurationFileMap().MachineConfigFilename));
config = ConfigurationManager.OpenExeConfiguration(temp.ExePath);
Assert.NotEmpty(config.AppSettings.Settings);
Assert.Equal("NewValue", config.AppSettings.Settings["NewKey"].Value);
}
}
[Fact]
public void AppSettingsCannotLoadFromUser()
{
// By default you can't load a section from a user config file- validating that appSettings falls in this bucket
using (var machine = new TempConfig(TestData.ImplicitMachineConfig))
using (var exe = new TempConfig(TestData.EmptyConfig))
using (var user = new TempConfig(TestData.SimpleConfig))
{
ExeConfigurationFileMap map = new ExeConfigurationFileMap
{
MachineConfigFilename = machine.ConfigPath,
ExeConfigFilename = exe.ConfigPath,
RoamingUserConfigFilename = user.ConfigPath
};
var config = ConfigurationManager.OpenMappedExeConfiguration(map, ConfigurationUserLevel.PerUserRoaming);
Assert.Throws<ConfigurationErrorsException>(() => config.AppSettings);
}
}
public static string AppSettingsConfig =
@"<?xml version='1.0' encoding='utf-8' ?>
<appSettings>
<add key='AppSettingsKey' value='AppSettingsValue'/>
</appSettings>";
[Fact]
public void AppSettingsCannotLoadFromConfigSource()
{
using (var tempConfig = new TempConfig(TestData.EmptyConfig))
{
const string SubDirectory = "Config";
const string AppConfigFileName = "tempAppConfig.config";
string tempConfigDirectory = Path.Combine(Path.GetDirectoryName(tempConfig.ConfigPath), SubDirectory);
using (var tempDirectory = new TempDirectory(tempConfigDirectory))
{
// set configSource and save the config
var config = ConfigurationManager.OpenExeConfiguration(tempConfig.ExePath);
config.AppSettings.SectionInformation.ConfigSource = Path.Combine(SubDirectory, AppConfigFileName);
config.Save();
// write temporary appConfig
var tempAppConfigPath = Path.Combine(tempConfigDirectory, AppConfigFileName);
File.WriteAllText(tempAppConfigPath, AppSettingsConfig);
// load config and test the appSettings
config = ConfigurationManager.OpenExeConfiguration(tempConfig.ExePath);
Assert.NotEmpty(config.AppSettings.Settings);
Assert.Equal("AppSettingsValue", config.AppSettings.Settings["AppSettingsKey"].Value);
}
}
}
public static IEnumerable<object[]> ConfigPaths = new List<object[]>()
{
new object[] { @"../Config/foo.config" },
new object[] { @"..\Config\foo.config" },
new object[] { @"\Config\foo.config" },
new object[] { @"/Config/foo.config" },
new object[] { @"\..\Config\foo.config" },
new object[] { @"/../Config/foo.config" },
};
[Theory]
[MemberData(nameof(ConfigPaths))]
public void AppSettingsInvalidConfigSourcePath_Throws(string configPath)
{
using (var tempConfig = new TempConfig(TestData.EmptyConfig))
{
var config = ConfigurationManager.OpenExeConfiguration(tempConfig.ExePath);
Assert.ThrowsAny<Exception>(() => config.AppSettings.SectionInformation.ConfigSource = configPath);
}
}
}
}