Skip to content

Commit 688a9d0

Browse files
committed
Add unit tests for reserved aliases and host specific template data
1 parent 317b48e commit 688a9d0

File tree

1 file changed

+274
-0
lines changed

1 file changed

+274
-0
lines changed

src/Tests/Microsoft.TemplateEngine.Cli.UnitTests/AliasAssignmentTests.cs

Lines changed: 274 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,14 @@
22
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
33
//
44

5+
using System.CommandLine;
6+
using FakeItEasy;
7+
using Microsoft.TemplateEngine.Abstractions;
58
using Microsoft.TemplateEngine.Cli.Commands;
9+
using Microsoft.TemplateEngine.Edge;
10+
using Microsoft.TemplateEngine.Edge.Settings;
11+
using Microsoft.TemplateEngine.Mocks;
12+
using Newtonsoft.Json.Linq;
613

714
namespace Microsoft.TemplateEngine.Cli.UnitTests
815
{
@@ -200,5 +207,272 @@ public void CheckAliasAssignmentsMvc20()
200207
Assert.Contains("--no-restore", result["skipRestore"].Aliases);
201208
Assert.DoesNotContain(result, r => r.Value.Errors.Any());
202209
}
210+
211+
[Theory]
212+
[InlineData("package", "--param:package")]
213+
[InlineData("u", "-p:u")]
214+
[InlineData("notreserved", "--notreserved")]
215+
public void CanAssignAliasForParameterWithReservedAlias(string parameterName, string expectedContainedAlias)
216+
{
217+
string command = "foo";
218+
MockTemplateInfo[] templates = new MockTemplateInfo[]
219+
{
220+
new MockTemplateInfo($"{command}", identity: "foo.1", groupIdentity: "foo.group").WithParameters(parameterName)
221+
};
222+
ICliTemplateEngineHost host = CliTestHostFactory.GetVirtualHost();
223+
IEngineEnvironmentSettings settings = new EngineEnvironmentSettings(host, virtualizeSettings: true);
224+
TemplatePackageManager templatePackageManager = A.Fake<TemplatePackageManager>();
225+
226+
NewCommand myCommand = (NewCommand)NewCommandFactory.Create("new", _ => host);
227+
ParseResult parseResult = myCommand.Parse($" new {command}");
228+
var args = InstantiateCommandArgs.FromNewCommandArgs(new NewCommandArgs(myCommand, parseResult));
229+
TemplateGroup templateGroup = TemplateGroup
230+
.FromTemplateList(CliTemplateInfo.FromTemplateInfo(templates, A.Fake<IHostSpecificDataLoader>()))
231+
.Single();
232+
var templateCommands = InstantiateCommand.GetTemplateCommand(args, settings, A.Fake<TemplatePackageManager>(), templateGroup);
233+
Assert.Single(templateCommands);
234+
var templateOption = templateCommands.Single().TemplateOptions[parameterName];
235+
Assert.Contains(expectedContainedAlias, templateOption.Aliases);
236+
}
237+
238+
[Theory]
239+
#pragma warning disable CA1825 // Avoid zero-length array allocations. https://github.com/dotnet/sdk/issues/28672
240+
[MemberData(nameof(GetTemplateData))]
241+
#pragma warning restore CA1825 // Avoid zero-length array allocations.
242+
public void CanOverrideAliasesForParameterWithHostData(string hostJsonData, string expectedJsonResult)
243+
{
244+
var hostData = new HostSpecificTemplateData(string.IsNullOrEmpty(hostJsonData) ? null : JObject.Parse(hostJsonData));
245+
var expectedResults = JObject.Parse(expectedJsonResult);
246+
var template = new MockTemplateInfo("foo", identity: "foo.1", groupIdentity: "foo.group");
247+
foreach (var expectedResult in expectedResults)
248+
{
249+
template.WithParameter(expectedResult.Key);
250+
}
251+
var hostDataLoader = A.Fake<IHostSpecificDataLoader>();
252+
A.CallTo(() => hostDataLoader.ReadHostSpecificTemplateData(template)).Returns(hostData);
253+
TemplateGroup templateGroup = TemplateGroup.FromTemplateList(
254+
CliTemplateInfo.FromTemplateInfo(new[] { template }, hostDataLoader))
255+
.Single();
256+
ICliTemplateEngineHost host = CliTestHostFactory.GetVirtualHost();
257+
IEngineEnvironmentSettings settings = new EngineEnvironmentSettings(host, virtualizeSettings: true);
258+
TemplatePackageManager templatePackageManager = A.Fake<TemplatePackageManager>();
259+
NewCommand myCommand = (NewCommand)NewCommandFactory.Create("new", _ => host);
260+
ParseResult parseResult = myCommand.Parse(" new foo");
261+
InstantiateCommandArgs args = InstantiateCommandArgs.FromNewCommandArgs(new NewCommandArgs(myCommand, parseResult));
262+
var templateCommands = InstantiateCommand.GetTemplateCommand(args, settings, templatePackageManager, templateGroup);
263+
Assert.Single(templateCommands);
264+
foreach (var expectedResult in expectedResults)
265+
{
266+
var expectedValues = expectedResult.Value!.Select(s => ((JValue)s).Value).ToArray();
267+
var expectedLongAlias = expectedValues[0];
268+
var expectedShortAlias = expectedValues[1];
269+
var expectedIsHidden = expectedValues[2];
270+
var templateOptions = templateCommands.Single().TemplateOptions;
271+
Assert.NotNull(templateOptions);
272+
Assert.Contains(expectedResult.Key, templateOptions.Keys);
273+
var templateOption = templateOptions[expectedResult.Key];
274+
Assert.NotNull(templateOption);
275+
Assert.True(templateOption.Aliases.Count > 0);
276+
//var longAlias = templateOption.Aliases[0];
277+
//var shortAlias = templateOption.Aliases.Count > 1 ? templateOption.Aliases[1] : null;
278+
//var isHidden = templateOption.Option.IsHidden;
279+
//Assert.Equal(expectedLongAlias, longAlias);
280+
//Assert.Equal(expectedShortAlias, shortAlias);
281+
//Assert.Equal(expectedIsHidden, isHidden);
282+
}
283+
}
284+
285+
public static IEnumerable<object[]> GetTemplateData()
286+
{
287+
// host data and expected option with long alias, short alias and if it is hidden:
288+
// [0] host data
289+
// [1] expected option : 0 - long alias, 1 - short alias, 2 - isHidden
290+
yield return new object[]
291+
{
292+
string.Empty,
293+
@"{ ""Framework"": [""--Framework"", ""-F"", false] }"
294+
};
295+
296+
yield return new object[]
297+
{
298+
@"{
299+
""symbolInfo"": {
300+
""Framework"": {
301+
}
302+
}
303+
}",
304+
@"{ ""Framework"": [""--Framework"", ""-F"", false] }"
305+
};
306+
307+
yield return new object[]
308+
{
309+
@"{
310+
""symbolInfo"": {
311+
""Framework"": {
312+
""longName"": ""targetframework""
313+
}
314+
}
315+
}",
316+
@"{ ""Framework"": [""--targetframework"", ""-t"", false] }"
317+
};
318+
319+
yield return new object[]
320+
{
321+
@"{
322+
""symbolInfo"": {
323+
""Framework"": {
324+
""shortName"": ""fr""
325+
}
326+
}
327+
}",
328+
@"{ ""Framework"": [""--Framework"", ""-fr"", false] }"
329+
};
330+
331+
yield return new object[]
332+
{
333+
@"{
334+
""symbolInfo"": {
335+
""Framework"": {
336+
""longName"": ""targetframework"",
337+
""shortName"": ""fr""
338+
}
339+
}
340+
}",
341+
@"{ ""Framework"": [""--targetframework"", ""-fr"", false] }"
342+
};
343+
344+
yield return new object[]
345+
{
346+
@"{
347+
""symbolInfo"": {
348+
""Framework"": {
349+
""longName"": ""targetframework"",
350+
""shortName"": """"
351+
}
352+
}
353+
}",
354+
@"{ ""Framework"": [""--targetframework"", null, false] }"
355+
};
356+
357+
yield return new object[]
358+
{
359+
@"{
360+
""symbolInfo"": {
361+
""Framework"": {
362+
""isHidden"": ""true"",
363+
""longName"": ""targetframework"",
364+
""shortName"": ""fr""
365+
}
366+
}
367+
}",
368+
@"{ ""Framework"": [""--targetframework"", ""-fr"", true] }"
369+
};
370+
371+
yield return new object[]
372+
{
373+
@"{
374+
""symbolInfo"": {
375+
""Framework"": {
376+
""isHidden"": ""false"",
377+
""longName"": ""targetframework"",
378+
""shortName"": ""fr""
379+
}
380+
}
381+
}",
382+
@"{ ""Framework"": [""--targetframework"", ""-fr"", false] }"
383+
};
384+
385+
yield return new object[]
386+
{
387+
@"{
388+
""symbolInfo"": {
389+
""install"": {
390+
""longName"": ""set""
391+
}
392+
}
393+
}",
394+
@"{ ""install"": [""--set"", ""-s"", false] }"
395+
};
396+
397+
yield return new object[]
398+
{
399+
@"{
400+
""symbolInfo"": {
401+
""install"": {
402+
""longName"": ""setup"",
403+
""shortName"": ""set""
404+
}
405+
}
406+
}",
407+
@"{ ""install"": [""--setup"", ""-set"", false] }"
408+
};
409+
410+
yield return new object[]
411+
{
412+
@"{
413+
""symbolInfo"": {
414+
""install"": {
415+
""longName"": ""set"",
416+
""shortName"": """"
417+
}
418+
}
419+
}",
420+
@"{ ""install"": [""--set"", null, false] }"
421+
};
422+
423+
yield return new object[]
424+
{
425+
@"{
426+
""symbolInfo"": {
427+
""pack"": {
428+
""longName"": ""package""
429+
}
430+
}
431+
}",
432+
@"{ ""pack"": [""--param:package"", ""-p"", false] }"
433+
};
434+
435+
yield return new object[]
436+
{
437+
@"{
438+
""symbolInfo"": {
439+
""add"": {
440+
""shortName"": ""i""
441+
}
442+
}
443+
}",
444+
@"{ ""add"": [""--add"", ""-p:i"", false] }"
445+
};
446+
447+
yield return new object[]
448+
{
449+
@"{
450+
""symbolInfo"": {
451+
""delete"": {
452+
""longName"": ""remove""
453+
}
454+
}
455+
}",
456+
@"{
457+
""delete"": [""--remove"", ""-r"", false],
458+
""remove"": [""--param:remove"", ""-re"", false]
459+
}"
460+
};
461+
462+
yield return new object[]
463+
{
464+
@"{
465+
""symbolInfo"": {
466+
""delete"": {
467+
""longName"": ""remove""
468+
}
469+
}
470+
}",
471+
@"{
472+
""remove"": [""--param:remove"", ""-r"", false],
473+
""delete"": [""--remove"", ""-re"", false]
474+
}"
475+
};
476+
}
203477
}
204478
}

0 commit comments

Comments
 (0)