-
Notifications
You must be signed in to change notification settings - Fork 359
/
Copy pathXmlPackageManagerRegistriesTest.cs
231 lines (213 loc) · 10.5 KB
/
XmlPackageManagerRegistriesTest.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
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
// <copyright file="PackageManagerRegistryTest.cs" company="Google LLC">
// Copyright (C) 2020 Google LLC All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// </copyright>
namespace Google.PackageManagerResolver.Tests {
using NUnit.Framework;
using System;
using System.Collections.Generic;
using System.IO;
using Google;
/// <summary>
/// Tests the PackageManagerRegistry class.
/// </summary>
[TestFixture]
public class XmlPackageManagerRegistriesTest {
/// <summary>
/// Name of the test configuration file.
/// </summary>
const string TEST_CONFIGURATION_FILENAME = "TestRegistries.xml";
/// <summary>
/// Object under test.
/// </summary>
private XmlPackageManagerRegistries registries;
/// <summary>
/// Logger for this test.
/// </summary>
private Logger logger = new Logger() {
Target = LogTarget.Console,
Level = LogLevel.Debug
};
/// <summary>
/// Write to the test registries file.
/// </summary>
/// <param name="configuration">String to write to the file.</param>
private void WriteRegistries(string configuration) {
if (File.Exists(TEST_CONFIGURATION_FILENAME)) File.Delete(TEST_CONFIGURATION_FILENAME);
File.WriteAllText(TEST_CONFIGURATION_FILENAME, configuration);
}
/// <summary>
/// Setup for the test
/// </summary>
[SetUp]
public void Setup() {
registries = new XmlPackageManagerRegistries();
}
/// <summary>
/// Make sure that a constructed object is empty.
/// </summary>
[Test]
public void TestRegistriesEmpty() {
Assert.That(registries.Registries.Count, Is.EqualTo(0));
}
/// <summary>
/// Add some items to the registries, clear and validate it's empty.
/// </summary>
[Test]
public void TestClear() {
registries.Registries["http://foo.bar.com"] = new PackageManagerRegistry() {
Name = "foobar",
Url = "http://foo.bar.com",
Scopes = new List<string>() { "com.bar" }
};
Assert.That(registries.Registries.Count, Is.EqualTo(1));
registries.Clear();
Assert.That(registries.Registries.Count, Is.EqualTo(0));
}
/// <summary>
/// Determine whether a filename is a container of UPM registries.
/// </summary>
[Test]
public void TestIsRegistriesFile() {
Assert.That(XmlPackageManagerRegistries.IsRegistriesFile(
"Assets/SomeRegistries.xml"),
Is.EqualTo(false));
Assert.That(XmlPackageManagerRegistries.IsRegistriesFile(
"Assets/MyPlugin/SomeRegistries.xml"),
Is.EqualTo(false));
Assert.That(XmlPackageManagerRegistries.IsRegistriesFile(
"Assets/Editor/SomeRegistries.txt"),
Is.EqualTo(false));
Assert.That(XmlPackageManagerRegistries.IsRegistriesFile(
"Assets/Editor/SomeRegistries.xml"),
Is.EqualTo(true));
Assert.That(XmlPackageManagerRegistries.IsRegistriesFile(
"Assets\\Editor\\SomeRegistries.xml"),
Is.EqualTo(true));
Assert.That(XmlPackageManagerRegistries.IsRegistriesFile(
"Assets/MyPlugin/Editor/SomeRegistries.xml"),
Is.EqualTo(true));
Assert.That(XmlPackageManagerRegistries.IsRegistriesFile(
"Assets\\MyPlugin\\Editor\\SomeRegistries.xml"),
Is.EqualTo(true));
}
/// <summary>
/// Test Read() with a valid XML file.
/// </summary>
[Test]
public void TestRead() {
WriteRegistries("<registries>\n" +
" <registry name=\"Reg1\"\n" +
" url=\"https://reg1.com\"\n" +
" termsOfService=\"https://reg1.com/terms\"\n" +
" privacyPolicy=\"https://reg1.com/privacy\">\n" +
" <scopes>\n" +
" <scope>com.reg1</scope>\n" +
" </scopes>\n" +
" </registry>\n" +
" <registry name=\"Reg2\"\n" +
" url=\"https://reg2.com\">\n" +
" <scopes>\n" +
" <scope>com.reg2.foo</scope>\n" +
" <scope>com.reg2.bar</scope>\n" +
" </scopes>\n" +
" </registry>\n" +
"</registries>\n");
Assert.That(registries.Read(TEST_CONFIGURATION_FILENAME, logger), Is.EqualTo(true));
Assert.That(registries.Registries.Count, Is.EqualTo(2));
CollectionAssert.AreEquivalent(registries.Registries.Keys,
new [] { "https://reg1.com", "https://reg2.com"});
var reg1 = registries.Registries["https://reg1.com"];
Assert.That(reg1.Name, Is.EqualTo("Reg1"));
Assert.That(reg1.Url, Is.EqualTo("https://reg1.com"));
Assert.That(reg1.TermsOfService, Is.EqualTo("https://reg1.com/terms"));
Assert.That(reg1.PrivacyPolicy, Is.EqualTo("https://reg1.com/privacy"));
CollectionAssert.AreEquivalent(reg1.Scopes, new [] { "com.reg1" } );
var reg2 = registries.Registries["https://reg2.com"];
Assert.That(reg2.Name, Is.EqualTo("Reg2"));
Assert.That(reg2.Url, Is.EqualTo("https://reg2.com"));
Assert.That(reg2.TermsOfService, Is.EqualTo(""));
CollectionAssert.AreEquivalent(reg2.Scopes, new [] { "com.reg2.foo", "com.reg2.bar" } );
}
/// <summary>
/// Test Read() with a configuration that uses the same registry URL multiple times.
/// </summary>
[Test]
public void TestReadDuplicateUrl() {
WriteRegistries("<registries>\n" +
" <registry name=\"Reg1\"\n" +
" url=\"https://reg1.com\"\n" +
" termsOfService=\"https://reg1.com/terms\"\n" +
" privacyPolicy=\"https://reg1.com/privacy\">\n" +
" <scopes>\n" +
" <scope>com.reg1</scope>\n" +
" </scopes>\n" +
" </registry>\n" +
" <registry name=\"Reg1\"\n" +
" url=\"https://reg1.com\"\n" +
" termsOfService=\"https://reg1.com/terms\"\n" +
" privacyPolicy=\"https://reg1.com/privacy\">\n" +
" <scopes>\n" +
" <scope>com.reg1</scope>\n" +
" </scopes>\n" +
" </registry>\n" +
" <registry name=\"Reg1 Other\"\n" +
" url=\"https://reg1.com\">\n" +
" <scopes>\n" +
" <scope>com.reg1.foobar</scope>\n" +
" </scopes>\n" +
" </registry>\n" +
"</registries>\n");
Assert.That(registries.Read(TEST_CONFIGURATION_FILENAME, logger), Is.EqualTo(true));
Assert.That(registries.Registries.Count, Is.EqualTo(1));
CollectionAssert.AreEquivalent(registries.Registries.Keys,
new [] { "https://reg1.com" });
var reg1 = registries.Registries["https://reg1.com"];
Assert.That(reg1.Name, Is.EqualTo("Reg1"));
Assert.That(reg1.Url, Is.EqualTo("https://reg1.com"));
Assert.That(reg1.TermsOfService, Is.EqualTo("https://reg1.com/terms"));
CollectionAssert.AreEquivalent(reg1.Scopes, new [] { "com.reg1" } );
}
/// <summary>
/// Try reading a malformed configuration files.
/// </summary>
[Test]
public void TestReadBrokenConfigs() {
// Not XML.
WriteRegistries("this is not xml");
Assert.That(registries.Read(TEST_CONFIGURATION_FILENAME, logger), Is.EqualTo(false));
// Invalid tag.
WriteRegistries("<registries><nada></nada></registries>");
Assert.That(registries.Read(TEST_CONFIGURATION_FILENAME, logger), Is.EqualTo(false));
// Missing url attribute.
WriteRegistries("<registries><registry name=\"foo\"></registry></registries>");
Assert.That(registries.Read(TEST_CONFIGURATION_FILENAME, logger), Is.EqualTo(false));
// Missing scopes block and scope entries.
WriteRegistries("<registries>\n" +
" <registry name=\"foo\"\n" +
" url=\"http://foo.bar.com\">\n" +
" </registry>\n" +
"</registries>");
Assert.That(registries.Read(TEST_CONFIGURATION_FILENAME, logger), Is.EqualTo(false));
// Missing scope entries.
WriteRegistries("<registries>\n" +
" <registry name=\"foo\"\n" +
" url=\"http://foo.bar.com\">\n" +
" <scopes></scopes>\n" +
" </registry>\n" +
"</registries>");
Assert.That(registries.Read(TEST_CONFIGURATION_FILENAME, logger), Is.EqualTo(false));
}
}
}