-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Expand file tree
/
Copy pathDSCv3UserSettingsFileResourceCommand.cs
More file actions
365 lines (315 loc) · 14.6 KB
/
DSCv3UserSettingsFileResourceCommand.cs
File metadata and controls
365 lines (315 loc) · 14.6 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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
// -----------------------------------------------------------------------------
// <copyright file="DSCv3UserSettingsFileResourceCommand.cs" company="Microsoft Corporation">
// Copyright (c) Microsoft Corporation. Licensed under the MIT License.
// </copyright>
// -----------------------------------------------------------------------------
namespace AppInstallerCLIE2ETests;
using AppInstallerCLIE2ETests.Helpers;
using NUnit.Framework;
using System.Collections.Generic;
using System.IO;
using System.Text.Json.Nodes;
using System.Text.Json.Serialization;
/// <summary>
/// `Configure` command tests.
/// </summary>
[System.Diagnostics.CodeAnalysis.SuppressMessage("StyleCop.CSharp.SpacingRules", "SA1010:Opening square brackets should be spaced correctly", Justification = "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/3687 pending SC 1.2 release")]
[System.Diagnostics.CodeAnalysis.SuppressMessage("StyleCop.CSharp.SpacingRules", "SA1011:Closing square brackets should be spaced correctly", Justification = "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/3687 pending SC 1.2 release")]
public class DSCv3UserSettingsFileResourceCommand : DSCv3ResourceTestBase
{
private const string UserSettingsFileResource = "user-settings-file";
private const string SettingsPropertyName = "settings";
private const string ActionPropertyValueFull = "Full";
private const string ActionPropertyValuePartial = "Partial";
private const string SettingsMock = "mock";
private const string SettingsMockObject = "mockObject";
private const string SettingsMockNested = "mockNested";
/// <summary>
/// Setup done once before all the tests here.
/// </summary>
[OneTimeSetUp]
public void OneTimeSetup()
{
TestCommon.SetupTestSource();
WinGetSettingsHelper.ConfigureFeature("dsc3", true);
EnsureTestResourcePresence();
}
/// <summary>
/// Teardown done once after all the tests here.
/// </summary>
[OneTimeTearDown]
public void OneTimeTeardown()
{
WinGetSettingsHelper.InitializeWingetSettings();
WinGetSettingsHelper.ConfigureFeature("dsc3", false);
}
/// <summary>
/// Set up.
/// </summary>
[SetUp]
public void Setup()
{
// Reset the settings to default before each test.
WinGetSettingsHelper.InitializeWingetSettings();
WinGetSettingsHelper.ConfigureFeature("dsc3", true);
}
/// <summary>
/// Calls `get` on the `user-settings-file` resource.
/// </summary>
[Test]
public void UserSettingsFile_Get()
{
var expected = GetCurrentUserSettings();
var getOutput = Get(new ());
Assert.IsNotNull(getOutput);
Assert.IsNull(getOutput.Action);
AssertSettingsAreEqual(expected, getOutput.Settings);
}
/// <summary>
/// Calls `set` on the `user-settings-file` resource with no diff.
/// </summary>
/// <param name="action">The action value.</param>
[Test]
[TestCase(ActionPropertyValueFull)]
[TestCase(ActionPropertyValuePartial)]
public void UserSettingsFile_Set_NoDiff(string action)
{
var setSettings = GetSettingsArg(action);
(var setOutput, var setDiff) = Set(new () { Action = action, Settings = setSettings });
var expected = GetCurrentUserSettings();
Assert.IsNotNull(setOutput);
Assert.AreEqual(action, setOutput.Action);
AssertSettingsAreEqual(expected, setOutput.Settings);
AssertDiffState(setDiff, []);
}
/// <summary>
/// Calls `set` on the `user-settings-file` resource to add fields.
/// </summary>
/// <param name="action">The action value.</param>
[Test]
[TestCase(ActionPropertyValueFull)]
[TestCase(ActionPropertyValuePartial)]
public void UserSettingsFile_Set_AddFields(string action)
{
// Call `set` to add mock properties to the settings
var setSettings = GetSettingsArg(action);
AddOrModifyMockProperties(setSettings, "mock");
(var setOutput, var setDiff) = Set(new () { Action = action, Settings = setSettings });
var expected = GetCurrentUserSettings();
// Assert that the settings are added
Assert.IsNotNull(setOutput);
Assert.AreEqual(action, setOutput.Action);
AssertMockProperties(setOutput.Settings, "mock");
AssertSettingsAreEqual(expected, setOutput.Settings);
AssertDiffState(setDiff, [ SettingsPropertyName ]);
}
/// <summary>
/// Calls `set` on the `user-settings-file` resource to ensure action is partial by default.
/// </summary>
[Test]
public void UserSettingsFile_Set_ActionIsPartialByDefault()
{
// Call `set` to add mock properties to the settings
var setSettings = GetSettingsArg(ActionPropertyValuePartial);
AddOrModifyMockProperties(setSettings, "mock");
var expected = GetCurrentUserSettings();
AddOrModifyMockProperties(expected, "mock");
(var setOutput, var setDiff) = Set(new () { Settings = setSettings });
// Assert that the settings are added
Assert.IsNotNull(setOutput);
Assert.IsNull(setOutput.Action);
AssertMockProperties(setOutput.Settings, "mock");
AssertSettingsAreEqual(expected, setOutput.Settings);
AssertDiffState(setDiff, [ SettingsPropertyName ]);
}
/// <summary>
/// Calls `set` on the `user-settings-file` resource to update fields.
/// </summary>
/// <param name="action">The action value.</param>
[Test]
[TestCase(ActionPropertyValueFull)]
[TestCase(ActionPropertyValuePartial)]
public void UserSettingsFile_Set_UpdateFields(string action)
{
// Add mock properties to the settings
var set1Settings = new JsonObject();
AddOrModifyMockProperties(set1Settings, "mock_old");
Set(new () { Action = ActionPropertyValuePartial, Settings = set1Settings });
// Call `set` to update the settings
var set2Settings = GetSettingsArg(action);
AddOrModifyMockProperties(set2Settings, "mock_new");
(var setOutput, var setDiff) = Set(new () { Action = action, Settings = set2Settings });
var expected = GetCurrentUserSettings();
// Assert that the settings are updated
Assert.IsNotNull(setOutput);
Assert.AreEqual(action, setOutput.Action);
AssertMockProperties(setOutput.Settings, "mock_new");
AssertSettingsAreEqual(expected, setOutput.Settings);
AssertDiffState(setDiff, [ SettingsPropertyName ]);
}
/// <summary>
/// Calls `test` on the `user-settings-file` resource to check if the settings are in desired state.
/// </summary>
/// <param name="action">The action value.</param>
[Test]
[TestCase(ActionPropertyValueFull)]
[TestCase(ActionPropertyValuePartial)]
public void UserSettingsFile_Test_InDesiredState(string action)
{
// Add mock properties to the settings
var setSettings = new JsonObject();
AddOrModifyMockProperties(setSettings, "mock");
Set(new () { Action = ActionPropertyValuePartial, Settings = setSettings });
// Call `test` to check the settings
var testSettings = GetSettingsArg(action);
AddOrModifyMockProperties(testSettings, "mock");
(var testOutput, var testDiff) = Test(new () { Action = action, Settings = testSettings });
var expected = GetCurrentUserSettings();
// Assert that the settings are in desired state
Assert.IsNotNull(testOutput);
Assert.AreEqual(action, testOutput.Action);
AssertMockProperties(testOutput.Settings, "mock");
AssertSettingsAreEqual(expected, testOutput.Settings);
Assert.IsTrue(testOutput.InDesiredState);
AssertDiffState(testDiff, []);
}
/// <summary>
/// Calls `test` on the `user-settings-file` resource to check if the settings are not in desired state.
/// </summary>
/// <param name="action">The action value.</param>
[Test]
[TestCase(ActionPropertyValueFull)]
[TestCase(ActionPropertyValuePartial)]
public void UserSettingsFile_Test_NotInDesiredState(string action)
{
// Add mock properties to the settings
var setSettings = new JsonObject();
AddOrModifyMockProperties(setSettings, "mock_set");
Set(new () { Action = ActionPropertyValuePartial, Settings = setSettings });
// Call `test` to check the settings
var testSettings = GetSettingsArg(action);
AddOrModifyMockProperties(testSettings, "mock_test");
(var testOutput, var testDiff) = Test(new () { Action = action, Settings = testSettings });
var expected = GetCurrentUserSettings();
// Assert that the settings are not in desired state
Assert.IsNotNull(testOutput);
Assert.AreEqual(action, testOutput.Action);
AssertMockProperties(testOutput.Settings, "mock_set");
AssertSettingsAreEqual(expected, testOutput.Settings);
Assert.IsFalse(testOutput.InDesiredState);
AssertDiffState(testDiff, [ SettingsPropertyName ]);
}
/// <summary>
/// Calls `export` on the `user-settings-file` resource to export the settings.
/// </summary>
[Test]
public void UserSettingsFile_Export()
{
var expected = GetCurrentUserSettings();
var exportOutput = Export(new ());
Assert.IsNotNull(exportOutput);
Assert.IsNull(exportOutput.Action);
AssertSettingsAreEqual(expected, exportOutput.Settings);
}
/// <summary>
/// Calls `get` on the `user-settings-file` resource.
/// </summary>
/// <param name="resourceData">The input resource data.</param>
/// <returns>The output resource data.</returns>
private static UserSettingsFileResourceData Get(UserSettingsFileResourceData resourceData)
{
var result = RunDSCv3Command(UserSettingsFileResource, GetFunction, resourceData);
AssertSuccessfulResourceRun(ref result);
return GetSingleOutputLineAs<UserSettingsFileResourceData>(result.StdOut);
}
/// <summary>
/// Calls `set` on the `user-settings-file` resource.
/// </summary>
/// <param name="resourceData">The input resource data.</param>
/// <returns>The output resource data and the diff.</returns>
private static (UserSettingsFileResourceData, List<string>) Set(UserSettingsFileResourceData resourceData)
{
var result = RunDSCv3Command(UserSettingsFileResource, SetFunction, resourceData);
AssertSuccessfulResourceRun(ref result);
return GetSingleOutputLineAndDiffAs<UserSettingsFileResourceData>(result.StdOut);
}
/// <summary>
/// Calls `test` on the `user-settings-file` resource.
/// </summary>
/// <param name="resourceData">The input resource data.</param>
/// <returns>The output resource data and the diff.</returns>
private static (UserSettingsFileResourceData, List<string>) Test(UserSettingsFileResourceData resourceData)
{
var result = RunDSCv3Command(UserSettingsFileResource, TestFunction, resourceData);
AssertSuccessfulResourceRun(ref result);
return GetSingleOutputLineAndDiffAs<UserSettingsFileResourceData>(result.StdOut);
}
/// <summary>
/// Calls `export` on the `user-settings-file` resource.
/// </summary>
/// <param name="resourceData">The input resource data.</param>
/// <returns>The output resource data.</returns>
private static UserSettingsFileResourceData Export(UserSettingsFileResourceData resourceData)
{
var result = RunDSCv3Command(UserSettingsFileResource, ExportFunction, resourceData);
AssertSuccessfulResourceRun(ref result);
return GetSingleOutputLineAs<UserSettingsFileResourceData>(result.StdOut);
}
/// <summary>
/// Gets the current user settings from the settings file.
/// </summary>
/// <returns>The current user settings as a JsonObject.</returns>
private static JsonObject GetCurrentUserSettings()
{
var settingsContent = File.ReadAllText(WinGetSettingsHelper.GetUserSettingsPath());
return JsonNode.Parse(settingsContent).AsObject();
}
/// <summary>
/// Adds or modifies mock properties in the settings.
/// </summary>
/// <param name="settings">Target settings.</param>
/// <param name="value">The mock value.</param>
private static void AddOrModifyMockProperties(JsonObject settings, string value)
{
settings[SettingsMock] = value;
settings[SettingsMockObject] ??= new JsonObject();
settings[SettingsMockObject][SettingsMockNested] = value;
}
/// <summary>
/// Asserts that the settings contain the expected mock properties.
/// </summary>
/// <param name="settings">Target settings.</param>
/// <param name="value">The expected mock value.</param>
private static void AssertMockProperties(JsonObject settings, string value)
{
Assert.IsNotNull(settings);
Assert.IsTrue(settings.ContainsKey(SettingsMock));
Assert.AreEqual(settings[SettingsMock].ToString(), value);
Assert.IsTrue(settings.ContainsKey(SettingsMockObject));
Assert.IsTrue(settings[SettingsMockObject].AsObject().ContainsKey(SettingsMockNested));
Assert.AreEqual(settings[SettingsMockObject][SettingsMockNested].ToString(), value);
}
/// <summary>
/// Asserts that the diff state is as expected.
/// </summary>
/// <param name="expected">The expected settings.</param>
/// <param name="actual">The actual settings.</param>
private static void AssertSettingsAreEqual(JsonObject expected, JsonObject actual)
{
Assert.IsTrue(JsonNode.DeepEquals(expected, actual));
}
/// <summary>
/// Gets the settings argument based on the action.
/// </summary>
/// <param name="action">The action value.</param>
/// <returns>The settings argument as a JsonObject.</returns>
private static JsonObject GetSettingsArg(string action) => action == ActionPropertyValueFull ? GetCurrentUserSettings() : new ();
private class UserSettingsFileResourceData
{
[JsonPropertyName(InDesiredStatePropertyName)]
public bool? InDesiredState { get; set; }
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingDefault)]
public string Action { get; set; }
public JsonObject Settings { get; set; }
}
}