-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
Copy pathSetUpFixture.cs
147 lines (129 loc) · 5.16 KB
/
SetUpFixture.cs
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
// -----------------------------------------------------------------------------
// <copyright file="SetUpFixture.cs" company="Microsoft Corporation">
// Copyright (c) Microsoft Corporation. Licensed under the MIT License.
// </copyright>
// -----------------------------------------------------------------------------
namespace AppInstallerCLIE2ETests
{
using AppInstallerCLIE2ETests.Helpers;
using Microsoft.Win32;
using NUnit.Framework;
/// <summary>
/// Set up fixture.
/// </summary>
[SetUpFixture]
public class SetUpFixture
{
private static bool shouldDisableDevModeOnExit = true;
private static bool shouldRevertDefaultFileTypeRiskOnExit = true;
private static bool shouldDoAnyTeardown = true;
private static string defaultFileTypes = string.Empty;
/// <summary>
/// Set up.
/// </summary>
[OneTimeSetUp]
public void Setup()
{
var testParams = TestSetup.Parameters;
if (testParams.IsDefault)
{
// If no parameters are provided, use defaults that work locally.
// This allows the user to assume responsibility for setup.
shouldDoAnyTeardown = false;
}
else
{
shouldDisableDevModeOnExit = this.EnableDevMode(true);
shouldRevertDefaultFileTypeRiskOnExit = this.DecreaseFileTypeRisk(".exe;.msi", false);
if (testParams.PackagedContext)
{
if (testParams.LooseFileRegistration)
{
Assert.True(TestCommon.InstallMsixRegister(testParams.AICLIPackagePath), $"InstallMsixRegister : {testParams.AICLIPackagePath}");
}
else
{
Assert.True(TestCommon.InstallMsix(testParams.AICLIPackagePath), $"InstallMsix : {testParams.AICLIPackagePath}");
}
}
}
if (!testParams.SkipTestSource)
{
TestIndex.GenerateE2ESource();
}
WinGetSettingsHelper.ForcedExperimentalFeatures = testParams.ForcedExperimentalFeatures;
WinGetSettingsHelper.InitializeWingetSettings();
}
/// <summary>
/// Tear down.
/// </summary>
[OneTimeTearDown]
public void TearDown()
{
if (shouldDoAnyTeardown)
{
if (shouldDisableDevModeOnExit)
{
this.EnableDevMode(false);
}
if (shouldRevertDefaultFileTypeRiskOnExit)
{
this.DecreaseFileTypeRisk(defaultFileTypes, true);
}
TestCommon.PublishE2ETestLogs();
if (TestSetup.Parameters.PackagedContext)
{
TestCommon.RemoveMsix(Constants.AICLIPackageName);
}
}
}
// Returns whether there's a change to the dev mode state after execution
private bool EnableDevMode(bool enable)
{
var appModelUnlockKey = Registry.LocalMachine.CreateSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\AppModelUnlock");
if (enable)
{
var value = appModelUnlockKey.GetValue("AllowDevelopmentWithoutDevLicense");
if (value == null || (int)value == 0)
{
appModelUnlockKey.SetValue("AllowDevelopmentWithoutDevLicense", 1, RegistryValueKind.DWord);
return true;
}
}
else
{
var value = appModelUnlockKey.GetValue("AllowDevelopmentWithoutDevLicense");
if (value != null && ((int)value) != 0)
{
appModelUnlockKey.SetValue("AllowDevelopmentWithoutDevLicense", 0, RegistryValueKind.DWord);
return true;
}
}
return false;
}
private bool DecreaseFileTypeRisk(string fileTypes, bool revert)
{
var defaultFileTypeRiskKey = Registry.CurrentUser.CreateSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Policies\Associations");
string value = (string)defaultFileTypeRiskKey.GetValue("DefaultFileTypeRisk");
if (revert)
{
defaultFileTypeRiskKey.SetValue("LowRiskFileTypes", fileTypes);
return false;
}
else
{
if (string.IsNullOrEmpty(value))
{
defaultFileTypes = string.Empty;
defaultFileTypeRiskKey.SetValue("LowRiskFileTypes", fileTypes);
}
else
{
defaultFileTypes = value;
defaultFileTypeRiskKey.SetValue("LowRiskFileTypes", string.Concat(value, fileTypes));
}
return true;
}
}
}
}