forked from dotnet/sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTabCompletionTests.cs
605 lines (465 loc) · 34.1 KB
/
TabCompletionTests.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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System.CommandLine;
using System.CommandLine.Completions;
using FakeItEasy;
using Microsoft.TemplateEngine.Abstractions;
using Microsoft.TemplateEngine.Abstractions.Constraints;
using Microsoft.TemplateEngine.Cli.Commands;
using Microsoft.TemplateEngine.Edge;
using Microsoft.TemplateEngine.Edge.Settings;
using Microsoft.TemplateEngine.Mocks;
using Microsoft.TemplateEngine.TestHelper;
namespace Microsoft.TemplateEngine.Cli.UnitTests.ParserTests
{
public partial class TabCompletionTests
{
[Fact]
public void Instantiate_CanSuggestTemplateOption_StartsWith()
{
ICliTemplateEngineHost host = CliTestHostFactory.GetVirtualHost(additionalComponents: BuiltInTemplatePackagesProviderFactory.GetComponents(RepoTemplatePackages));
CliCommand myCommand = NewCommandFactory.Create("new", _ => host);
ParseResult parseResult = myCommand.Parse($"new console --framework {ToolsetInfo.CurrentTargetFramework} --l");
string[] suggestions = parseResult.GetCompletions().Select(l => l.Label).ToArray();
Assert.Equal(2, suggestions.Length);
Assert.Contains("--langVersion", suggestions);
Assert.Contains("--language", suggestions);
}
#pragma warning disable xUnit1004 // Test methods should not be skipped
[Fact(Skip = "doesn't work at the moment; it matches with legacy --language option which cannot be completed; to discuss how to avoid that")]
#pragma warning restore xUnit1004 // Test methods should not be skipped
public void Instantiate_CanSuggestLanguages()
{
ICliTemplateEngineHost host = CliTestHostFactory.GetVirtualHost(additionalComponents: BuiltInTemplatePackagesProviderFactory.GetComponents(RepoTemplatePackages));
CliCommand myCommand = NewCommandFactory.Create("new", _ => host);
ParseResult parseResult = myCommand.Parse("new console --language ");
string[] suggestions = parseResult.GetCompletions().Select(l => l.Label).ToArray();
Assert.Equal(3, suggestions.Length);
Assert.Contains("C#", suggestions);
Assert.Contains("F#", suggestions);
Assert.Contains("VB", suggestions);
}
#pragma warning disable xUnit1004 // Test methods should not be skipped
[Fact(Skip = "not valid behavior for parser, should suggest --nuget-source")]
#pragma warning restore xUnit1004 // Test methods should not be skipped
public void Install_GetSuggestionsAfterInteractive()
{
ICliTemplateEngineHost host = CliTestHostFactory.GetVirtualHost(additionalComponents: BuiltInTemplatePackagesProviderFactory.GetComponents(RepoTemplatePackages));
CliCommand myCommand = NewCommandFactory.Create("new", _ => host);
ParseResult parseResult = myCommand.Parse("new install --interactive ");
string[] result = parseResult.GetCompletions().Select(l => l.Label).ToArray();
Assert.Equal(2, result.Length);
Assert.Contains("--nuget-source", result);
}
[Fact]
public void Install_GetSuggestionsAfterOptionWithoutArg()
{
ICliTemplateEngineHost host = CliTestHostFactory.GetVirtualHost(additionalComponents: BuiltInTemplatePackagesProviderFactory.GetComponents(RepoTemplatePackages));
CliCommand myCommand = NewCommandFactory.Create("new", _ => host);
ParseResult parseResult = myCommand.Parse("new install --nuget-source ");
CompletionItem[] result = parseResult.GetCompletions().ToArray();
Assert.Empty(result);
}
#pragma warning disable xUnit1004 // Test methods should not be skipped
[Fact(Skip = "not valid behavior for parser, should suggest --interactive")]
#pragma warning restore xUnit1004 // Test methods should not be skipped
public void Install_GetSuggestionsAfterOptionWithArg()
{
ICliTemplateEngineHost host = CliTestHostFactory.GetVirtualHost(additionalComponents: BuiltInTemplatePackagesProviderFactory.GetComponents(RepoTemplatePackages));
CliCommand myCommand = NewCommandFactory.Create("new", _ => host);
ParseResult parseResult = myCommand.Parse("new install --nuget-source me");
string[] result = parseResult.GetCompletions().Select(l => l.Label).ToArray();
Assert.Single(result);
Assert.Contains("--interactive", result);
}
[Fact]
public void Instantiate_CanSuggestTemplate_StartsWith()
{
ICliTemplateEngineHost host = CliTestHostFactory.GetVirtualHost(additionalComponents: BuiltInTemplatePackagesProviderFactory.GetComponents(RepoTemplatePackages));
CliCommand myCommand = NewCommandFactory.Create("new", _ => host);
ParseResult parseResult = myCommand.Parse("new co");
string[] suggestions = parseResult.GetCompletions().Select(l => l.Label).ToArray();
Assert.Single(suggestions);
Assert.Equal("console", suggestions.Single());
}
[Fact]
public void CanCompleteChoice_FromSingleTemplate()
{
MockTemplateInfo template = new MockTemplateInfo("foo", identity: "foo.1", groupIdentity: "foo.group")
.WithChoiceParameter("testChoice", "val1", "val2", "val3");
IEnumerable<TemplateGroup> templateGroups = TemplateGroup.FromTemplateList(
CliTemplateInfo.FromTemplateInfo(new[] { template }, A.Fake<IHostSpecificDataLoader>()));
ICliTemplateEngineHost host = CliTestHostFactory.GetVirtualHost();
IEngineEnvironmentSettings settings = new EngineEnvironmentSettings(host, virtualizeSettings: true);
TemplatePackageManager packageManager = A.Fake<TemplatePackageManager>();
NewCommand myCommand = (NewCommand)NewCommandFactory.Create("new", _ => host);
ParseResult parseResult = myCommand.Parse($" new foo --testChoice ");
var completionContext = parseResult.GetCompletionContext() as TextCompletionContext;
Assert.NotNull(completionContext);
InstantiateCommandArgs args = InstantiateCommandArgs.FromNewCommandArgs(new NewCommandArgs(myCommand, parseResult));
IEnumerable<string> result = InstantiateCommand.GetTemplateCompletions(args, templateGroups, settings, packageManager, completionContext!).Select(l => l.Label);
Assert.Equal(new[] { "val1", "val2", "val3" }, result);
}
[Theory]
[InlineData(" new foo --testChoice val2 --testChoice va", new[] { "val1", "val2", "val3" })]
[InlineData(" new foo --testC", new[] { "--testChoice" })]
// [InlineData(" new foo --testChoice val2 --testC", new[] { "--testChoice" },
// Skip = "Multiple arity option completion does not work. https://github.com/dotnet/command-line-api/issues/1727")]
public void CanCompleteChoice_MultichoiceTabCompletion(string command, string[] suggestions)
{
MockTemplateInfo template = new MockTemplateInfo("foo", identity: "foo.1", groupIdentity: "foo.group")
.WithMultiChoiceParameter("testChoice", "val1", "val2", "val3");
IEnumerable<TemplateGroup> templateGroups = TemplateGroup.FromTemplateList(
CliTemplateInfo.FromTemplateInfo(new[] { template }, A.Fake<IHostSpecificDataLoader>()));
ICliTemplateEngineHost host = CliTestHostFactory.GetVirtualHost();
IEngineEnvironmentSettings settings = new EngineEnvironmentSettings(host, virtualizeSettings: true);
TemplatePackageManager packageManager = A.Fake<TemplatePackageManager>();
NewCommand myCommand = (NewCommand)NewCommandFactory.Create("new", _ => host);
ParseResult parseResult = myCommand.Parse(command);
var completionContext = parseResult.GetCompletionContext() as TextCompletionContext;
Assert.NotNull(completionContext);
InstantiateCommandArgs args = InstantiateCommandArgs.FromNewCommandArgs(new NewCommandArgs(myCommand, parseResult));
IEnumerable<string> result = InstantiateCommand.GetTemplateCompletions(args, templateGroups, settings, packageManager, completionContext!).Select(l => l.Label);
Assert.Equal(suggestions, result);
}
[Fact]
public void CanCompleteChoice_FromSingleTemplate_StartsWith()
{
MockTemplateInfo template = new MockTemplateInfo("foo", identity: "foo.1", groupIdentity: "foo.group")
.WithChoiceParameter("testChoice", "val1", "val2", "boo");
IEnumerable<TemplateGroup> templateGroups = TemplateGroup.FromTemplateList(
CliTemplateInfo.FromTemplateInfo(new[] { template }, A.Fake<IHostSpecificDataLoader>()));
ICliTemplateEngineHost host = CliTestHostFactory.GetVirtualHost();
IEngineEnvironmentSettings settings = new EngineEnvironmentSettings(host, virtualizeSettings: true);
TemplatePackageManager packageManager = A.Fake<TemplatePackageManager>();
NewCommand myCommand = (NewCommand)NewCommandFactory.Create("new", _ => host);
ParseResult parseResult = myCommand.Parse($" new foo --testChoice v");
var completionContext = parseResult.GetCompletionContext() as TextCompletionContext;
Assert.NotNull(completionContext);
InstantiateCommandArgs args = InstantiateCommandArgs.FromNewCommandArgs(new NewCommandArgs(myCommand, parseResult));
IEnumerable<string> result = InstantiateCommand.GetTemplateCompletions(args, templateGroups, settings, packageManager, completionContext!).Select(l => l.Label);
Assert.Equal(new[] { "val1", "val2" }, result);
}
[Fact]
public void CanCompleteChoice_FromSingleTemplate_InTheMiddle()
{
MockTemplateInfo template = new MockTemplateInfo("foo", identity: "foo.1", groupIdentity: "foo.group")
.WithChoiceParameter("testChoice", "val1", "val2", "boo");
IEnumerable<TemplateGroup> templateGroups = TemplateGroup.FromTemplateList(
CliTemplateInfo.FromTemplateInfo(new[] { template }, A.Fake<IHostSpecificDataLoader>()));
ICliTemplateEngineHost host = CliTestHostFactory.GetVirtualHost();
IEngineEnvironmentSettings settings = new EngineEnvironmentSettings(host, virtualizeSettings: true);
TemplatePackageManager packageManager = A.Fake<TemplatePackageManager>();
NewCommand myCommand = (NewCommand)NewCommandFactory.Create("new", _ => host);
ParseResult parseResult = myCommand.Parse($" new foo --testChoice v --name test");
InstantiateCommandArgs args = InstantiateCommandArgs.FromNewCommandArgs(new NewCommandArgs(myCommand, parseResult));
var completionContext = parseResult.GetCompletionContext() as TextCompletionContext;
Assert.NotNull(completionContext);
completionContext = completionContext!.AtCursorPosition(23);
IEnumerable<string> result = InstantiateCommand.GetTemplateCompletions(args, templateGroups, settings, packageManager, completionContext!).Select(l => l.Label);
Assert.Equal(new[] { "val1", "val2" }, result);
}
[Fact]
public void CanCompleteChoice_FromMultipleTemplates()
{
MockTemplateInfo template1 = new MockTemplateInfo("foo", identity: "foo.1", groupIdentity: "foo.group")
.WithChoiceParameter("testChoice", "val1");
MockTemplateInfo template2 = new MockTemplateInfo("foo", identity: "foo.2", groupIdentity: "foo.group")
.WithChoiceParameter("testChoice", "val2", "val3");
IEnumerable<TemplateGroup> templateGroups = TemplateGroup.FromTemplateList(
CliTemplateInfo.FromTemplateInfo(new[] { template1, template2 }, A.Fake<IHostSpecificDataLoader>()));
ICliTemplateEngineHost host = CliTestHostFactory.GetVirtualHost();
IEngineEnvironmentSettings settings = new EngineEnvironmentSettings(host, virtualizeSettings: true);
TemplatePackageManager packageManager = A.Fake<TemplatePackageManager>();
NewCommand myCommand = (NewCommand)NewCommandFactory.Create("new", _ => host);
ParseResult parseResult = myCommand.Parse($" new foo --testChoice ");
InstantiateCommandArgs args = InstantiateCommandArgs.FromNewCommandArgs(new NewCommandArgs(myCommand, parseResult));
var completionContext = parseResult.GetCompletionContext() as TextCompletionContext;
Assert.NotNull(completionContext);
IEnumerable<string> result = InstantiateCommand.GetTemplateCompletions(args, templateGroups, settings, packageManager, completionContext!).Select(l => l.Label);
Assert.Equal(new[] { "val1", "val2", "val3" }, result);
}
[Fact]
public void CanCompleteChoice_FromMultipleTemplates_StartsWith()
{
MockTemplateInfo template1 = new MockTemplateInfo("foo", identity: "foo.1", groupIdentity: "foo.group")
.WithChoiceParameter("testChoice", "val1");
MockTemplateInfo template2 = new MockTemplateInfo("foo", identity: "foo.2", groupIdentity: "foo.group")
.WithChoiceParameter("testChoice", "val2", "boo");
IEnumerable<TemplateGroup> templateGroups = TemplateGroup.FromTemplateList(
CliTemplateInfo.FromTemplateInfo(new[] { template1, template2 }, A.Fake<IHostSpecificDataLoader>()));
ICliTemplateEngineHost host = CliTestHostFactory.GetVirtualHost();
IEngineEnvironmentSettings settings = new EngineEnvironmentSettings(host, virtualizeSettings: true);
TemplatePackageManager packageManager = A.Fake<TemplatePackageManager>();
NewCommand myCommand = (NewCommand)NewCommandFactory.Create("new", _ => host);
ParseResult parseResult = myCommand.Parse($" new foo --testChoice v");
InstantiateCommandArgs args = InstantiateCommandArgs.FromNewCommandArgs(new NewCommandArgs(myCommand, parseResult));
var completionContext = parseResult.GetCompletionContext() as TextCompletionContext;
Assert.NotNull(completionContext);
IEnumerable<string> result = InstantiateCommand.GetTemplateCompletions(args, templateGroups, settings, packageManager, completionContext!).Select(l => l.Label);
Assert.Equal(new[] { "val1", "val2" }, result);
}
[Fact]
public void CanCompleteParameters_FromMultipleTemplates()
{
MockTemplateInfo template1 = new MockTemplateInfo("foo", identity: "foo.1", groupIdentity: "foo.group")
.WithChoiceParameter("testChoice", "val1")
.WithParameters("foo", "bar");
MockTemplateInfo template2 = new MockTemplateInfo("foo", identity: "foo.2", groupIdentity: "foo.group")
.WithChoiceParameter("testChoice", "val2", "val3")
.WithParameters("param");
IEnumerable<TemplateGroup> templateGroups = TemplateGroup.FromTemplateList(
CliTemplateInfo.FromTemplateInfo(new[] { template1, template2 }, A.Fake<IHostSpecificDataLoader>()));
ICliTemplateEngineHost host = CliTestHostFactory.GetVirtualHost();
IEngineEnvironmentSettings settings = new EngineEnvironmentSettings(host, virtualizeSettings: true);
TemplatePackageManager packageManager = A.Fake<TemplatePackageManager>();
NewCommand myCommand = (NewCommand)NewCommandFactory.Create("new", _ => host);
ParseResult parseResult = myCommand.Parse($" new foo ");
InstantiateCommandArgs args = InstantiateCommandArgs.FromNewCommandArgs(new NewCommandArgs(myCommand, parseResult));
var completionContext = parseResult.GetCompletionContext() as TextCompletionContext;
Assert.NotNull(completionContext);
IEnumerable<string> result = InstantiateCommand.GetTemplateCompletions(args, templateGroups, settings, packageManager, completionContext!).Select(l => l.Label);
Assert.Contains("--param", result);
Assert.Contains("--testChoice", result);
Assert.Contains("--foo", result);
Assert.Contains("--bar", result);
Assert.Contains("-p", result);
Assert.Contains("-t", result);
Assert.Contains("-f", result);
Assert.Contains("-b", result);
Assert.DoesNotContain("--language", result);
Assert.DoesNotContain("--type", result);
Assert.DoesNotContain("--baseline", result);
}
[Fact]
public void CanCompleteParameters_StartsWith_FromMultipleTemplates()
{
MockTemplateInfo template1 = new MockTemplateInfo("foo", identity: "foo.1", groupIdentity: "foo.group")
.WithChoiceParameter("testChoice", "val1")
.WithParameters("foo", "bar");
MockTemplateInfo template2 = new MockTemplateInfo("foo", identity: "foo.2", groupIdentity: "foo.group")
.WithChoiceParameter("testChoice", "val2", "val3")
.WithParameters("test");
IEnumerable<TemplateGroup> templateGroups = TemplateGroup.FromTemplateList(
CliTemplateInfo.FromTemplateInfo(new[] { template1, template2 }, A.Fake<IHostSpecificDataLoader>()));
ICliTemplateEngineHost host = CliTestHostFactory.GetVirtualHost();
IEngineEnvironmentSettings settings = new EngineEnvironmentSettings(host, virtualizeSettings: true);
TemplatePackageManager packageManager = A.Fake<TemplatePackageManager>();
NewCommand myCommand = (NewCommand)NewCommandFactory.Create("new", _ => host);
ParseResult parseResult = myCommand.Parse($" new foo --t");
InstantiateCommandArgs args = InstantiateCommandArgs.FromNewCommandArgs(new NewCommandArgs(myCommand, parseResult));
var completionContext = parseResult.GetCompletionContext() as TextCompletionContext;
Assert.NotNull(completionContext);
IEnumerable<string> result = InstantiateCommand.GetTemplateCompletions(args, templateGroups, settings, packageManager, completionContext!).Select(l => l.Label);
Assert.Contains("--test", result);
Assert.Contains("--testChoice", result);
Assert.DoesNotContain("--foo", result);
Assert.DoesNotContain("--bar", result);
Assert.DoesNotContain("-p", result);
Assert.DoesNotContain("-t", result);
Assert.DoesNotContain("-f", result);
Assert.DoesNotContain("-b", result);
Assert.DoesNotContain("--language", result);
Assert.DoesNotContain("--type", result);
Assert.DoesNotContain("--baseline", result);
}
#pragma warning disable xUnit1004 // Test methods should not be skipped
[Fact(Skip = "https://github.com/dotnet/templating/issues/4387")]
#pragma warning restore xUnit1004 // Test methods should not be skipped
public void CanCompleteParameters_StartsWith_AfterOption()
{
MockTemplateInfo template1 = new MockTemplateInfo("foo", identity: "foo.1", groupIdentity: "foo.group")
.WithChoiceParameter("testChoice", "val1")
.WithParameters("foo", "bar");
MockTemplateInfo template2 = new MockTemplateInfo("foo", identity: "foo.2", groupIdentity: "foo.group")
.WithChoiceParameter("testChoice", "val2", "val3")
.WithParameters("test");
IEnumerable<TemplateGroup> templateGroups = TemplateGroup.FromTemplateList(
CliTemplateInfo.FromTemplateInfo(new[] { template1, template2 }, A.Fake<IHostSpecificDataLoader>()));
ICliTemplateEngineHost host = CliTestHostFactory.GetVirtualHost();
IEngineEnvironmentSettings settings = new EngineEnvironmentSettings(host, virtualizeSettings: true);
TemplatePackageManager packageManager = A.Fake<TemplatePackageManager>();
NewCommand myCommand = (NewCommand)NewCommandFactory.Create("new", _ => host);
ParseResult parseResult = myCommand.Parse($" new foo --foo val1 --bar val2 --t");
InstantiateCommandArgs args = InstantiateCommandArgs.FromNewCommandArgs(new NewCommandArgs(myCommand, parseResult));
var completionContext = parseResult.GetCompletionContext() as TextCompletionContext;
Assert.NotNull(completionContext);
IEnumerable<string> result = InstantiateCommand.GetTemplateCompletions(args, templateGroups, settings, packageManager, completionContext!).Select(l => l.Label);
Assert.DoesNotContain("--test", result);
Assert.Contains("--testChoice", result);
Assert.DoesNotContain("--foo", result);
Assert.DoesNotContain("--bar", result);
Assert.DoesNotContain("-p", result);
Assert.DoesNotContain("-t", result);
Assert.DoesNotContain("-f", result);
Assert.DoesNotContain("-b", result);
Assert.DoesNotContain("--language", result);
Assert.DoesNotContain("--type", result);
Assert.DoesNotContain("--baseline", result);
}
[Theory]
[InlineData("-lang")]
[InlineData("--language")]
public void CanCompleteLanguages(string optionName)
{
MockTemplateInfo template1 = new MockTemplateInfo("foo", identity: "foo.1", groupIdentity: "foo.group")
.WithTag("language", "C#");
MockTemplateInfo template2 = new MockTemplateInfo("foo", identity: "foo.2", groupIdentity: "foo.group")
.WithTag("language", "F#");
IEnumerable<TemplateGroup> templateGroups = TemplateGroup.FromTemplateList(
CliTemplateInfo.FromTemplateInfo(new[] { template1, template2 }, A.Fake<IHostSpecificDataLoader>()));
ICliTemplateEngineHost host = CliTestHostFactory.GetVirtualHost();
IEngineEnvironmentSettings settings = new EngineEnvironmentSettings(host, virtualizeSettings: true);
TemplatePackageManager packageManager = A.Fake<TemplatePackageManager>();
NewCommand myCommand = (NewCommand)NewCommandFactory.Create("new", _ => host);
ParseResult parseResult = myCommand.Parse($" new foo {optionName} ");
InstantiateCommandArgs args = InstantiateCommandArgs.FromNewCommandArgs(new NewCommandArgs(myCommand, parseResult));
var completionContext = parseResult.GetCompletionContext() as TextCompletionContext;
Assert.NotNull(completionContext);
IEnumerable<string> result = InstantiateCommand.GetTemplateCompletions(args, templateGroups, settings, packageManager, completionContext!).Select(l => l.Label);
Assert.Equal(new[] { "C#", "F#" }, result);
}
[Fact]
public void CanCompleteTypes()
{
MockTemplateInfo template1 = new MockTemplateInfo("foo", identity: "foo.1", groupIdentity: "foo.group")
.WithTag("type", "project");
MockTemplateInfo template2 = new MockTemplateInfo("foo", identity: "foo.2", groupIdentity: "foo.group")
.WithTag("type", "solution");
IEnumerable<TemplateGroup> templateGroups = TemplateGroup.FromTemplateList(
CliTemplateInfo.FromTemplateInfo(new[] { template1, template2 }, A.Fake<IHostSpecificDataLoader>()));
ICliTemplateEngineHost host = CliTestHostFactory.GetVirtualHost();
IEngineEnvironmentSettings settings = new EngineEnvironmentSettings(host, virtualizeSettings: true);
TemplatePackageManager packageManager = A.Fake<TemplatePackageManager>();
NewCommand myCommand = (NewCommand)NewCommandFactory.Create("new", _ => host);
ParseResult parseResult = myCommand.Parse($" new foo --type ");
InstantiateCommandArgs args = InstantiateCommandArgs.FromNewCommandArgs(new NewCommandArgs(myCommand, parseResult));
var completionContext = parseResult.GetCompletionContext() as TextCompletionContext;
Assert.NotNull(completionContext);
IEnumerable<string> result = InstantiateCommand.GetTemplateCompletions(args, templateGroups, settings, packageManager, completionContext!).Select(l => l.Label);
Assert.Equal(new[] { "project", "solution" }, result);
}
[Fact]
public void CanIgnoreTemplateGroupsWithConstraints()
{
MockTemplateInfo template1 = new MockTemplateInfo("foo1", identity: "foo.1")
.WithConstraints(new TemplateConstraintInfo("test", "yes"));
MockTemplateInfo template2 = new MockTemplateInfo("foo2", identity: "foo.2")
.WithConstraints(new TemplateConstraintInfo("test", "no"));
MockTemplateInfo template3 = new MockTemplateInfo("foo3", identity: "foo.3")
.WithConstraints(new TemplateConstraintInfo("test", "bad-params"));
IEnumerable<TemplateGroup> templateGroups = TemplateGroup.FromTemplateList(
CliTemplateInfo.FromTemplateInfo(new[] { template1, template2, template3 }, A.Fake<IHostSpecificDataLoader>()));
ICliTemplateEngineHost host = CliTestHostFactory.GetVirtualHost(additionalComponents: new[] { (typeof(ITemplateConstraintFactory), (IIdentifiedComponent)new TestConstraintFactory("test")) });
IEngineEnvironmentSettings settings = new EngineEnvironmentSettings(host, virtualizeSettings: true);
TemplatePackageManager packageManager = A.Fake<TemplatePackageManager>();
NewCommand myCommand = (NewCommand)NewCommandFactory.Create("new", _ => host);
ParseResult parseResult = myCommand.Parse($" new fo");
InstantiateCommandArgs args = InstantiateCommandArgs.FromNewCommandArgs(new NewCommandArgs(myCommand, parseResult));
var completionContext = parseResult.GetCompletionContext() as TextCompletionContext;
Assert.NotNull(completionContext);
IEnumerable<string> result = InstantiateCommand.GetTemplateNameCompletions(args.ShortName, templateGroups, settings).Select(l => l.Label);
Assert.Equal(new[] { "foo1" }, result);
}
[Fact]
public void CanIgnoreTemplateGroupsWithConstraints_IgnoresLongEvaluationTemplateGroups()
{
MockTemplateInfo template1 = new MockTemplateInfo("foo1", identity: "foo.1")
.WithConstraints(new TemplateConstraintInfo("test", "yes"));
MockTemplateInfo template2 = new MockTemplateInfo("foo2", identity: "foo.2")
.WithConstraints(new TemplateConstraintInfo("test", "no"));
MockTemplateInfo template3 = new MockTemplateInfo("foo3", identity: "foo.3")
.WithConstraints(new TemplateConstraintInfo("test", "bad-params"));
IEnumerable<TemplateGroup> templateGroups = TemplateGroup.FromTemplateList(
CliTemplateInfo.FromTemplateInfo(new[] { template1, template2, template3 }, A.Fake<IHostSpecificDataLoader>()));
ICliTemplateEngineHost host = CliTestHostFactory.GetVirtualHost(additionalComponents: new[] { (typeof(ITemplateConstraintFactory), (IIdentifiedComponent)new LongRunningConstraintFactory("test", 1500)) });
IEngineEnvironmentSettings settings = new EngineEnvironmentSettings(host, virtualizeSettings: true);
TemplatePackageManager packageManager = A.Fake<TemplatePackageManager>();
NewCommand myCommand = (NewCommand)NewCommandFactory.Create("new", _ => host);
ParseResult parseResult = myCommand.Parse($" new fo");
InstantiateCommandArgs args = InstantiateCommandArgs.FromNewCommandArgs(new NewCommandArgs(myCommand, parseResult));
var completionContext = parseResult.GetCompletionContext() as TextCompletionContext;
Assert.NotNull(completionContext);
IEnumerable<string> result = InstantiateCommand.GetTemplateNameCompletions(args.ShortName, templateGroups, settings).Select(l => l.Label);
Assert.Empty(result);
}
[Fact]
public void CanIgnoreTemplatesInGroupWithConstraints()
{
MockTemplateInfo template1 = new MockTemplateInfo("foo", identity: "foo.1", groupIdentity: "group")
.WithConstraints(new TemplateConstraintInfo("test", "yes"))
.WithParameter("a");
MockTemplateInfo template2 = new MockTemplateInfo("foo", identity: "foo.2", groupIdentity: "group")
.WithConstraints(new TemplateConstraintInfo("test", "no"))
.WithParameter("b");
MockTemplateInfo template3 = new MockTemplateInfo("foo", identity: "foo.3", groupIdentity: "group")
.WithConstraints(new TemplateConstraintInfo("test", "bad-params"))
.WithParameter("c");
IEnumerable<TemplateGroup> templateGroups = TemplateGroup.FromTemplateList(
CliTemplateInfo.FromTemplateInfo(new[] { template1, template2, template3 }, A.Fake<IHostSpecificDataLoader>()));
ICliTemplateEngineHost host = CliTestHostFactory.GetVirtualHost(additionalComponents: new[] { (typeof(ITemplateConstraintFactory), (IIdentifiedComponent)new TestConstraintFactory("test")) });
IEngineEnvironmentSettings settings = new EngineEnvironmentSettings(host, virtualizeSettings: true);
TemplatePackageManager packageManager = A.Fake<TemplatePackageManager>();
NewCommand myCommand = (NewCommand)NewCommandFactory.Create("new", _ => host);
ParseResult parseResult = myCommand.Parse($" new foo ");
InstantiateCommandArgs args = InstantiateCommandArgs.FromNewCommandArgs(new NewCommandArgs(myCommand, parseResult));
var completionContext = parseResult.GetCompletionContext() as TextCompletionContext;
Assert.NotNull(completionContext);
IEnumerable<string> result = InstantiateCommand.GetTemplateCompletions(args, templateGroups, settings, packageManager, completionContext!).Select(l => l.Label);
Assert.Contains("--a", result);
Assert.DoesNotContain("--b", result);
Assert.DoesNotContain("--c", result);
}
[Fact]
public void IncludesTemplatesInGroupWithLongEvaluatedConstraints()
{
MockTemplateInfo template1 = new MockTemplateInfo("foo", identity: "foo.1", groupIdentity: "group")
.WithConstraints(new TemplateConstraintInfo("test", "yes"))
.WithParameter("a");
MockTemplateInfo template2 = new MockTemplateInfo("foo", identity: "foo.2", groupIdentity: "group")
.WithConstraints(new TemplateConstraintInfo("test", "no"))
.WithParameter("b");
MockTemplateInfo template3 = new MockTemplateInfo("foo", identity: "foo.3", groupIdentity: "group")
.WithConstraints(new TemplateConstraintInfo("test", "bad-params"))
.WithParameter("c");
IEnumerable<TemplateGroup> templateGroups = TemplateGroup.FromTemplateList(
CliTemplateInfo.FromTemplateInfo(new[] { template1, template2, template3 }, A.Fake<IHostSpecificDataLoader>()));
ICliTemplateEngineHost host = CliTestHostFactory.GetVirtualHost(additionalComponents: new[] { (typeof(ITemplateConstraintFactory), (IIdentifiedComponent)new LongRunningConstraintFactory("test", 1500)) });
IEngineEnvironmentSettings settings = new EngineEnvironmentSettings(host, virtualizeSettings: true);
TemplatePackageManager packageManager = A.Fake<TemplatePackageManager>();
NewCommand myCommand = (NewCommand)NewCommandFactory.Create("new", _ => host);
ParseResult parseResult = myCommand.Parse($" new foo ");
InstantiateCommandArgs args = InstantiateCommandArgs.FromNewCommandArgs(new NewCommandArgs(myCommand, parseResult));
var completionContext = parseResult.GetCompletionContext() as TextCompletionContext;
Assert.NotNull(completionContext);
IEnumerable<string> result = InstantiateCommand.GetTemplateCompletions(args, templateGroups, settings, packageManager, completionContext!).Select(l => l.Label);
Assert.Contains("--a", result);
Assert.Contains("--b", result);
Assert.Contains("--c", result);
}
[Fact]
public void WillNotEvaluateConstraints_WhenAtLeastOneTemplateInGroupDoesNotHaveConstraints()
{
MockTemplateInfo template1 = new MockTemplateInfo("foo", identity: "foo.1", groupIdentity: "group")
.WithParameter("a");
MockTemplateInfo template2 = new MockTemplateInfo("foo", identity: "foo.2", groupIdentity: "group")
.WithConstraints(new TemplateConstraintInfo("test", "no"))
.WithParameter("b");
MockTemplateInfo template3 = new MockTemplateInfo("foo", identity: "foo.3", groupIdentity: "group")
.WithConstraints(new TemplateConstraintInfo("test", "bad-params"))
.WithParameter("c");
IEnumerable<TemplateGroup> templateGroups = TemplateGroup.FromTemplateList(
CliTemplateInfo.FromTemplateInfo(new[] { template1, template2, template3 }, A.Fake<IHostSpecificDataLoader>()));
ICliTemplateEngineHost host = CliTestHostFactory.GetVirtualHost(additionalComponents: new[] { (typeof(ITemplateConstraintFactory), (IIdentifiedComponent)new LongRunningConstraintFactory("test", 3000)) });
IEngineEnvironmentSettings settings = new EngineEnvironmentSettings(host, virtualizeSettings: true);
TemplatePackageManager packageManager = A.Fake<TemplatePackageManager>();
NewCommand myCommand = (NewCommand)NewCommandFactory.Create("new", _ => host);
ParseResult parseResult = myCommand.Parse($" new fo");
InstantiateCommandArgs args = InstantiateCommandArgs.FromNewCommandArgs(new NewCommandArgs(myCommand, parseResult));
var completionContext = parseResult.GetCompletionContext() as TextCompletionContext;
Assert.NotNull(completionContext);
IEnumerable<string> result = InstantiateCommand.GetTemplateNameCompletions(args.ShortName, templateGroups, settings).Select(l => l.Label);
Assert.Equal(new[] { "foo" }, result);
}
}
}