forked from microsoft/winget-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathImportCommand.cs
More file actions
166 lines (148 loc) · 6.46 KB
/
Copy pathImportCommand.cs
File metadata and controls
166 lines (148 loc) · 6.46 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
// -----------------------------------------------------------------------------
// <copyright file="ImportCommand.cs" company="Microsoft Corporation">
// Copyright (c) Microsoft Corporation. Licensed under the MIT License.
// </copyright>
// -----------------------------------------------------------------------------
namespace AppInstallerCLIE2ETests
{
using System.IO;
using AppInstallerCLIE2ETests.Helpers;
using NUnit.Framework;
/// <summary>
/// Import command tests.
/// </summary>
public class ImportCommand : BaseCommand
{
/// <summary>
/// Set up.
/// </summary>
[SetUp]
public void Setup()
{
this.CleanupTestExe();
}
/// <summary>
/// Test import v1.
/// </summary>
[Test]
public void ImportSuccessful_1_0()
{
var result = TestCommon.RunAICLICommand("import", this.GetTestImportFile("ImportFile-Good.1.0.json"));
Assert.AreEqual(Constants.ErrorCode.S_OK, result.ExitCode);
Assert.True(this.VerifyTestExeInstalled());
this.UninstallTestExe();
}
/// <summary>
/// Test import v2.
/// </summary>
[Test]
public void ImportSuccessful_2_0()
{
var result = TestCommon.RunAICLICommand("import", this.GetTestImportFile("ImportFile-Good.2.0.json"));
Assert.AreEqual(Constants.ErrorCode.S_OK, result.ExitCode);
Assert.True(this.VerifyTestExeInstalled());
this.UninstallTestExe();
}
/// <summary>
/// Test import invalid file.
/// </summary>
[Test]
public void ImportInvalidFile()
{
// Verify failure when trying to import with an invalid file
var result = TestCommon.RunAICLICommand("import", this.GetTestImportFile("ImportFile-Bad-Invalid.json"));
Assert.AreEqual(Constants.ErrorCode.ERROR_JSON_INVALID_FILE, result.ExitCode);
Assert.True(result.StdOut.Contains("JSON file is not valid"));
}
/// <summary>
/// Test import from an unknown source.
/// </summary>
[Test]
public void ImportUnknownSource()
{
// Verify failure when trying to import from an unknown source
var result = TestCommon.RunAICLICommand("import", this.GetTestImportFile("ImportFile-Bad-UnknownSource.json"));
Assert.AreEqual(Constants.ErrorCode.ERROR_SOURCE_NAME_DOES_NOT_EXIST, result.ExitCode);
Assert.True(result.StdOut.Contains("Source required for import is not installed"));
}
/// <summary>
/// Test import for an unknown package.
/// </summary>
[Test]
public void ImportUnavailablePackage()
{
// Verify failure when trying to import an unavailable package
var result = TestCommon.RunAICLICommand("import", this.GetTestImportFile("ImportFile-Bad-UnknownPackage.json"));
Assert.AreEqual(Constants.ErrorCode.ERROR_NOT_ALL_QUERIES_FOUND_SINGLE, result.ExitCode);
Assert.True(result.StdOut.Contains("Package not found: MissingPackage"));
}
/// <summary>
/// Test import when the package version is not present.
/// </summary>
[Test]
public void ImportUnavailableVersion()
{
// Verify failure when trying to import an unavailable package
var result = TestCommon.RunAICLICommand("import", this.GetTestImportFile("ImportFile-Bad-UnknownPackageVersion.json"));
Assert.AreEqual(Constants.ErrorCode.ERROR_NOT_ALL_QUERIES_FOUND_SINGLE, result.ExitCode);
Assert.True(result.StdOut.Contains("Search failed for: AppInstallerTest.TestExeInstaller"));
}
/// <summary>
/// Test import when the package is already installed.
/// </summary>
[Test]
public void ImportAlreadyInstalled()
{
// Verify success with message when trying to import a package that is already installed
var installDir = TestCommon.GetRandomTestDir();
TestCommon.RunAICLICommand("install", $"AppInstallerTest.TestExeInstaller -l {installDir}");
var result = TestCommon.RunAICLICommand("import", $"{this.GetTestImportFile("ImportFile-Good.1.0.json")}");
Assert.AreEqual(Constants.ErrorCode.S_OK, result.ExitCode);
Assert.True(result.StdOut.Contains("Package is already installed"));
Assert.False(this.VerifyTestExeInstalled());
this.UninstallTestExe();
}
/// <summary>
/// Test Import with an exported file.
/// </summary>
[Test]
public void ImportExportedFile()
{
// Verify success when importing an exported list of packages.
// First install the test package to ensure it is exported.
TestCommon.RunAICLICommand("install", Constants.ExeInstallerPackageId);
var jsonFile = TestCommon.GetRandomTestFile(".json");
TestCommon.RunAICLICommand("export", $"{jsonFile} -s TestSource");
// Uninstall the package to ensure we can install it again
this.UninstallTestExe();
// Import the file
var result = TestCommon.RunAICLICommand("import", jsonFile);
Assert.AreEqual(Constants.ErrorCode.S_OK, result.ExitCode);
Assert.True(this.VerifyTestExeInstalled());
this.UninstallTestExe();
}
private string GetTestImportFile(string importFileName)
{
return TestCommon.GetTestDataFile(Path.Combine("ImportFiles", importFileName));
}
private bool VerifyTestExeInstalled(string installDir = null)
{
if (string.IsNullOrEmpty(installDir))
{
// Default location used by installer
installDir = Path.GetTempPath();
}
return File.Exists(Path.Combine(installDir, Constants.TestExeInstalledFileName));
}
private void UninstallTestExe()
{
TestCommon.RunAICLICommand("uninstall", Constants.ExeInstallerPackageId);
}
private void CleanupTestExe()
{
this.UninstallTestExe();
File.Delete(Path.Combine(Path.GetTempPath(), Constants.TestExeInstalledFileName));
File.Delete(Path.Combine(Path.GetTempPath(), Constants.TestExeUninstallerFileName));
}
}
}