Skip to content

Commit de947df

Browse files
committed
Refactor out resource test base
1 parent d998430 commit de947df

File tree

3 files changed

+189
-111
lines changed

3 files changed

+189
-111
lines changed

src/AppInstallerCLIE2ETests/ConfigureCommand.cs

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,7 @@ namespace AppInstallerCLIE2ETests
88
{
99
using System;
1010
using System.IO;
11-
using System.Linq;
1211
using AppInstallerCLIE2ETests.Helpers;
13-
using Microsoft.Win32;
1412
using NUnit.Framework;
1513

1614
/// <summary>
@@ -25,14 +23,8 @@ public class ConfigureCommand
2523
/// </summary>
2624
public static void EnsureTestResourcePresence()
2725
{
28-
string outputDirectory = Path.Join(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "Microsoft\\WindowsApps");
29-
Assert.IsNotEmpty(outputDirectory);
30-
31-
var result = TestCommon.RunAICLICommand("dscv3 test-file", $"--manifest -o {outputDirectory}\\test-file.dsc.resource.json");
32-
Assert.AreEqual(0, result.ExitCode);
33-
34-
result = TestCommon.RunAICLICommand("dscv3 test-json", $"--manifest -o {outputDirectory}\\test-json.dsc.resource.json");
35-
Assert.AreEqual(0, result.ExitCode);
26+
DSCv3ResourceTestBase.EnsureTestResourcePresence("test-file");
27+
DSCv3ResourceTestBase.EnsureTestResourcePresence("test-json");
3628
}
3729

3830
/// <summary>

src/AppInstallerCLIE2ETests/DSCv3ResourceCommands.cs

Lines changed: 3 additions & 101 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,7 @@
66

77
namespace AppInstallerCLIE2ETests
88
{
9-
using System;
109
using System.Collections.Generic;
11-
using System.IO;
12-
using System.Text.Json;
1310
using System.Text.Json.Serialization;
1411
using AppInstallerCLIE2ETests.Helpers;
1512
using NUnit.Framework;
@@ -19,33 +16,16 @@ namespace AppInstallerCLIE2ETests
1916
/// </summary>
2017
[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")]
2118
[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")]
22-
public class DSCv3ResourceCommands
19+
public class DSCv3ResourceCommands : DSCv3ResourceTestBase
2320
{
2421
private const string DefaultPackageIdentifier = Constants.ExeInstallerPackageId;
2522
private const string DefaultPackageLowVersion = "1.0.0.0";
2623
private const string DefaultPackageMidVersion = "1.1.0.0";
2724
private const string DefaultPackageHighVersion = "2.0.0.0";
2825
private const string PackageResource = "package";
29-
private const string GetFunction = "get";
30-
private const string TestFunction = "test";
31-
private const string SetFunction = "set";
32-
private const string ExportFunction = "export";
33-
private const string ExistPropertyName = "_exist";
3426
private const string VersionPropertyName = "version";
3527
private const string UseLatestPropertyName = "useLatest";
3628

37-
/// <summary>
38-
/// Ensures that the test resources manifests are present.
39-
/// </summary>
40-
public static void EnsureTestResourcePresence()
41-
{
42-
string outputDirectory = Path.Join(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "Microsoft\\WindowsApps");
43-
Assert.IsNotEmpty(outputDirectory);
44-
45-
var result = TestCommon.RunAICLICommand("dscv3 package", $"--manifest -o {outputDirectory}\\microsoft.winget.package.dsc.resource.json");
46-
Assert.AreEqual(0, result.ExitCode);
47-
}
48-
4929
/// <summary>
5030
/// Setup done once before all the tests here.
5131
/// </summary>
@@ -55,7 +35,7 @@ public void OneTimeSetup()
5535
TestCommon.SetupTestSource();
5636
WinGetSettingsHelper.ConfigureFeature("dsc3", true);
5737
WinGetSettingsHelper.ConfigureLoggingLevel("verbose");
58-
EnsureTestResourcePresence();
38+
EnsureTestResourcePresence(PackageResource);
5939
}
6040

6141
/// <summary>
@@ -552,73 +532,6 @@ private static void RemoveTestPackage()
552532
AssertSuccessfulResourceRun(ref result);
553533
}
554534

555-
private static TestCommon.RunCommandResult RunDSCv3Command(string resource, string function, object input, int timeOut = 60000, bool throwOnTimeout = true)
556-
{
557-
return TestCommon.RunAICLICommand($"dscv3 {resource}", $"--{function}", ConvertToJSON(input), timeOut, throwOnTimeout);
558-
}
559-
560-
private static void AssertSuccessfulResourceRun(ref TestCommon.RunCommandResult result)
561-
{
562-
Assert.AreEqual(0, result.ExitCode);
563-
Assert.IsNotEmpty(result.StdOut);
564-
}
565-
566-
private static JsonSerializerOptions GetDefaultJsonOptions()
567-
{
568-
return new JsonSerializerOptions()
569-
{
570-
DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull,
571-
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
572-
Converters =
573-
{
574-
new JsonStringEnumConverter(),
575-
},
576-
};
577-
}
578-
579-
private static string ConvertToJSON(object value) => value switch
580-
{
581-
string s => s,
582-
null => null,
583-
_ => JsonSerializer.Serialize(value, GetDefaultJsonOptions()),
584-
};
585-
586-
private static string[] GetOutputLines(string output)
587-
{
588-
return output.TrimEnd().Split(Environment.NewLine);
589-
}
590-
591-
private static T GetSingleOutputLineAs<T>(string output)
592-
{
593-
string[] lines = GetOutputLines(output);
594-
Assert.AreEqual(1, lines.Length);
595-
596-
return JsonSerializer.Deserialize<T>(lines[0], GetDefaultJsonOptions());
597-
}
598-
599-
private static (T, List<string>) GetSingleOutputLineAndDiffAs<T>(string output)
600-
{
601-
string[] lines = GetOutputLines(output);
602-
Assert.AreEqual(2, lines.Length);
603-
604-
var options = GetDefaultJsonOptions();
605-
return (JsonSerializer.Deserialize<T>(lines[0], options), JsonSerializer.Deserialize<List<string>>(lines[1], options));
606-
}
607-
608-
private static List<T> GetOutputLinesAs<T>(string output)
609-
{
610-
List<T> result = new List<T>();
611-
string[] lines = GetOutputLines(output);
612-
var options = GetDefaultJsonOptions();
613-
614-
foreach (string line in lines)
615-
{
616-
result.Add(JsonSerializer.Deserialize<T>(line, options));
617-
}
618-
619-
return result;
620-
}
621-
622535
private static void AssertExistingPackageResourceData(PackageResourceData output, string version, bool ignoreLatest = false)
623536
{
624537
Assert.IsNotNull(output);
@@ -639,23 +552,12 @@ private static void AssertExistingPackageResourceData(PackageResourceData output
639552
}
640553
}
641554

642-
private static void AssertDiffState(List<string> diff, IList<string> expected)
643-
{
644-
Assert.IsNotNull(diff);
645-
Assert.AreEqual(expected.Count, diff.Count);
646-
647-
foreach (string item in expected)
648-
{
649-
Assert.Contains(item, diff);
650-
}
651-
}
652-
653555
private class PackageResourceData
654556
{
655557
[JsonPropertyName(ExistPropertyName)]
656558
public bool? Exist { get; set; }
657559

658-
[JsonPropertyName("_inDesiredState")]
560+
[JsonPropertyName(InDesiredStatePropertyName)]
659561
public bool? InDesiredState { get; set; }
660562

661563
[JsonPropertyName("id")]
Lines changed: 184 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,184 @@
1+
// -----------------------------------------------------------------------------
2+
// <copyright file="DSCv3ResourceTestBase.cs" company="Microsoft Corporation">
3+
// Copyright (c) Microsoft Corporation. Licensed under the MIT License.
4+
// </copyright>
5+
// -----------------------------------------------------------------------------
6+
7+
namespace AppInstallerCLIE2ETests
8+
{
9+
using System;
10+
using System.Collections.Generic;
11+
using System.IO;
12+
using System.Text.Json;
13+
using System.Text.Json.Serialization;
14+
using AppInstallerCLIE2ETests.Helpers;
15+
using NUnit.Framework;
16+
17+
/// <summary>
18+
/// Provides common functionality for DSC v3 resource tests.
19+
/// </summary>
20+
public class DSCv3ResourceTestBase
21+
{
22+
/// <summary>
23+
/// The string for the `get` function.
24+
/// </summary>
25+
public const string GetFunction = "get";
26+
27+
/// <summary>
28+
/// The string for the `test` function.
29+
/// </summary>
30+
public const string TestFunction = "test";
31+
32+
/// <summary>
33+
/// The string for the `set` function.
34+
/// </summary>
35+
public const string SetFunction = "set";
36+
37+
/// <summary>
38+
/// The string for the `export` function.
39+
/// </summary>
40+
public const string ExportFunction = "export";
41+
42+
/// <summary>
43+
/// The string for the `_exist` property name.
44+
/// </summary>
45+
public const string ExistPropertyName = "_exist";
46+
47+
/// <summary>
48+
/// The string for the `_inDesiredState` property name.
49+
/// </summary>
50+
public const string InDesiredStatePropertyName = "_inDesiredState";
51+
52+
/// <summary>
53+
/// Write the resource manifest out to the WindowsApps alias directory.
54+
/// </summary>
55+
/// <param name="resource">The resource manifest to write.</param>
56+
public static void EnsureTestResourcePresence(string resource)
57+
{
58+
string outputDirectory = Path.Join(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "Microsoft\\WindowsApps");
59+
Assert.IsNotEmpty(outputDirectory);
60+
61+
var result = TestCommon.RunAICLICommand($"dscv3 {resource}", $"--manifest -o {outputDirectory}\\microsoft.winget.{resource}.dsc.resource.json");
62+
Assert.AreEqual(0, result.ExitCode);
63+
}
64+
65+
/// <summary>
66+
/// Runs a DSC v3 resource command.
67+
/// </summary>
68+
/// <param name="resource">The resource to target.</param>
69+
/// <param name="function">The resource function to run.</param>
70+
/// <param name="input">Input for the function; supports null, direct string, or JSON serialization of complex objects.</param>
71+
/// <param name="timeOut">The maximum time to wait in milliseconds.</param>
72+
/// <param name="throwOnTimeout">Whether to throw on a timeout or simply return the incomplete result.</param>
73+
/// <returns>A RunCommandResult containing the process exit code and output and error streams.</returns>
74+
protected static TestCommon.RunCommandResult RunDSCv3Command(string resource, string function, object input, int timeOut = 60000, bool throwOnTimeout = true)
75+
{
76+
return TestCommon.RunAICLICommand($"dscv3 {resource}", $"--{function}", ConvertToJSON(input), timeOut, throwOnTimeout);
77+
}
78+
79+
/// <summary>
80+
/// Asserts that a RunCommandResult contains a success for a DSC v3 resource command run.
81+
/// </summary>
82+
/// <param name="result">The result of a DSC v3 resource command run.</param>
83+
protected static void AssertSuccessfulResourceRun(ref TestCommon.RunCommandResult result)
84+
{
85+
Assert.AreEqual(0, result.ExitCode);
86+
Assert.IsNotEmpty(result.StdOut);
87+
}
88+
89+
/// <summary>
90+
/// Gets the output as lines.
91+
/// </summary>
92+
/// <param name="output">The output stream from a DSC v3 resource command.</param>
93+
/// <returns>The lines of the output.</returns>
94+
protected static string[] GetOutputLines(string output)
95+
{
96+
return output.TrimEnd().Split(Environment.NewLine);
97+
}
98+
99+
/// <summary>
100+
/// Asserts the the output is a single line and deserializes that line as JSON.
101+
/// </summary>
102+
/// <typeparam name="T">The type to deserialize from JSON.</typeparam>
103+
/// <param name="output">The output stream from a DSC v3 resource command.</param>
104+
/// <returns>The object as deserialized.</returns>
105+
protected static T GetSingleOutputLineAs<T>(string output)
106+
{
107+
string[] lines = GetOutputLines(output);
108+
Assert.AreEqual(1, lines.Length);
109+
110+
return JsonSerializer.Deserialize<T>(lines[0], GetDefaultJsonOptions());
111+
}
112+
113+
/// <summary>
114+
/// Asserts the the output is two lines and deserializes them as a JSON object and JSON string array.
115+
/// </summary>
116+
/// <typeparam name="T">The type to deserialize from JSON.</typeparam>
117+
/// <param name="output">The output stream from a DSC v3 resource command.</param>
118+
/// <returns>The object as deserialized and the contents of the string array.</returns>
119+
protected static (T, List<string>) GetSingleOutputLineAndDiffAs<T>(string output)
120+
{
121+
string[] lines = GetOutputLines(output);
122+
Assert.AreEqual(2, lines.Length);
123+
124+
var options = GetDefaultJsonOptions();
125+
return (JsonSerializer.Deserialize<T>(lines[0], options), JsonSerializer.Deserialize<List<string>>(lines[1], options));
126+
}
127+
128+
/// <summary>
129+
/// Deserializes all lines as JSON objects.
130+
/// </summary>
131+
/// <typeparam name="T">The type to deserialize from JSON.</typeparam>
132+
/// <param name="output">The output stream from a DSC v3 resource command.</param>
133+
/// <returns>A List of objects as deserialized.</returns>
134+
protected static List<T> GetOutputLinesAs<T>(string output)
135+
{
136+
List<T> result = new List<T>();
137+
string[] lines = GetOutputLines(output);
138+
var options = GetDefaultJsonOptions();
139+
140+
foreach (string line in lines)
141+
{
142+
result.Add(JsonSerializer.Deserialize<T>(line, options));
143+
}
144+
145+
return result;
146+
}
147+
148+
/// <summary>
149+
/// Requires that the diff from a resource command contain the same set of strings as expected.
150+
/// </summary>
151+
/// <param name="diff">The diff from a resource command.</param>
152+
/// <param name="expected">The expected strings.</param>
153+
protected static void AssertDiffState(List<string> diff, IList<string> expected)
154+
{
155+
Assert.IsNotNull(diff);
156+
Assert.AreEqual(expected.Count, diff.Count);
157+
158+
foreach (string item in expected)
159+
{
160+
Assert.Contains(item, diff);
161+
}
162+
}
163+
164+
private static JsonSerializerOptions GetDefaultJsonOptions()
165+
{
166+
return new JsonSerializerOptions()
167+
{
168+
DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull,
169+
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
170+
Converters =
171+
{
172+
new JsonStringEnumConverter(),
173+
},
174+
};
175+
}
176+
177+
private static string ConvertToJSON(object value) => value switch
178+
{
179+
string s => s,
180+
null => null,
181+
_ => JsonSerializer.Serialize(value, GetDefaultJsonOptions()),
182+
};
183+
}
184+
}

0 commit comments

Comments
 (0)