Exemplo n.º 1
0
        public async Task TestGetTemplateResolutionResult_MatchByTagsAndMismatchByOtherFilter()
        {
            List <ITemplateInfo> templatesToSearch = new List <ITemplateInfo>();

            templatesToSearch.Add(
                new MockTemplateInfo("console", name: "Long name for Console App", identity: "Console.App.T1", groupIdentity: "Console.App.Test")
                .WithTag("language", "L1")
                .WithTag("type", "project")
                .WithClassifications("Common", "Test")
                .WithBaselineInfo("app", "standard"));

            ListTemplateResolver     resolver    = new ListTemplateResolver(templatesToSearch, new MockHostSpecificDataLoader());
            TemplateResolutionResult matchResult = await resolver.ResolveTemplatesAsync(
                GetListCommandArgsFor("new list --language L2 --type item --tag Common"),
                defaultLanguage : null,
                default).ConfigureAwait(false);

            Assert.False(matchResult.HasTemplateGroupWithTemplateInfoMatches);
            Assert.Equal(0, matchResult.TemplateGroupsWithMatchingTemplateInfo.Count());
            Assert.True(matchResult.HasTemplateGroupMatches);
            Assert.Equal(1, matchResult.TemplateGroups.Count());
            Assert.Equal(1, matchResult.TemplateGroups.Single().Templates.Count);
            Assert.True(matchResult.HasLanguageMismatch);
            Assert.True(matchResult.HasTypeMismatch);
            Assert.False(matchResult.HasBaselineMismatch);
        }
Exemplo n.º 2
0
        public async Task TestGetTemplateResolutionResult_InputLanguageIsPreferredOverDefault()
        {
            List <ITemplateInfo> templatesToSearch = new List <ITemplateInfo>();

            templatesToSearch.Add(new MockTemplateInfo("console", name: "Long name for Console App", identity: "Console.App.L1", groupIdentity: "Console.App.Test").WithTag("language", "L1"));
            templatesToSearch.Add(new MockTemplateInfo("console", name: "Long name for Console App", identity: "Console.App.L2", groupIdentity: "Console.App.Test").WithTag("language", "L2"));

            ListTemplateResolver     resolver    = new ListTemplateResolver(templatesToSearch, new MockHostSpecificDataLoader());
            TemplateResolutionResult matchResult = await resolver.ResolveTemplatesAsync(
                GetListCommandArgsFor("new list console --language L2"),
                defaultLanguage : null,
                default).ConfigureAwait(false);

            Assert.True(matchResult.HasTemplateGroupWithTemplateInfoMatches);
            Assert.NotNull(matchResult.UnambiguousTemplateGroup);
            Assert.Equal(2, matchResult.TemplateGroupsWithMatchingTemplateInfo?.Single().Templates.Count);
            Assert.Equal(2, matchResult.UnambiguousTemplateGroup?.Templates.Count);
        }
Exemplo n.º 3
0
        public async Task TestGetTemplateResolutionResult_TagsMatch(string templateTags, string commandTag, bool matchExpected)
        {
            const string separator = "||";

            string[] templateTagsArray = templateTags.Split(separator);

            List <ITemplateInfo> templatesToSearch = new List <ITemplateInfo>();

            templatesToSearch.Add(
                new MockTemplateInfo("console", name: "Long name for Console App", identity: "Console.App.T1", groupIdentity: "Console.App.Test", author: "TemplateAuthor")
                .WithTag("language", "L1")
                .WithTag("type", "project")
                .WithClassifications(templateTagsArray)
                .WithBaselineInfo("app", "standard"));

            ListTemplateResolver     resolver    = new ListTemplateResolver(templatesToSearch, new MockHostSpecificDataLoader());
            TemplateResolutionResult matchResult = await resolver.ResolveTemplatesAsync(
                GetListCommandArgsFor($"new list console --tag {commandTag}"),
                defaultLanguage : null,
                default).ConfigureAwait(false);

            if (matchExpected)
            {
                Assert.True(matchResult.HasTemplateGroupWithTemplateInfoMatches);
                Assert.Equal(1, matchResult.TemplateGroupsWithMatchingTemplateInfo.Count());
                Assert.Equal(1, matchResult.TemplateGroupsWithMatchingTemplateInfo.Single().Templates.Count);
                Assert.False(matchResult.HasClassificationMismatch);
            }
            else
            {
                Assert.False(matchResult.HasTemplateGroupWithTemplateInfoMatches);
                Assert.Equal(0, matchResult.TemplateGroupsWithMatchingTemplateInfo.Count());
                Assert.True(matchResult.HasTemplateGroupMatches);
                Assert.Equal(1, matchResult.TemplateGroups.Count());
                Assert.Equal(1, matchResult.TemplateGroups.Single().Templates.Count);
                Assert.True(matchResult.HasClassificationMismatch);
            }

            Assert.False(matchResult.HasLanguageMismatch);
            Assert.False(matchResult.HasTypeMismatch);
            Assert.False(matchResult.HasBaselineMismatch);
            Assert.False(matchResult.HasAuthorMismatch);
        }
Exemplo n.º 4
0
        public async Task TestGetTemplateResolutionResult_ExactMatchOnShortNameMatchesCorrectly()
        {
            List <ITemplateInfo> templatesToSearch = new List <ITemplateInfo>();

            templatesToSearch.Add(new MockTemplateInfo("console", name: "Long name for Console App", identity: "Console.App"));
            templatesToSearch.Add(new MockTemplateInfo("console2", name: "Long name for Console App #2", identity: "Console.App2"));

            ListTemplateResolver     resolver    = new ListTemplateResolver(templatesToSearch, new MockHostSpecificDataLoader());
            TemplateResolutionResult matchResult = await resolver.ResolveTemplatesAsync(
                GetListCommandArgsFor("new list console"),
                defaultLanguage : null,
                default).ConfigureAwait(false);

            Assert.True(matchResult.HasTemplateGroupWithTemplateInfoMatches);
            Assert.Null(matchResult.UnambiguousTemplateGroup);
            Assert.Equal(2, matchResult.TemplateGroupsWithMatchingTemplateInfo.Count());
            Assert.NotNull(matchResult.TemplateGroupsWithMatchingTemplateInfo.SelectMany(group => group.Templates).Single(t => t.Identity == "Console.App"));
            Assert.NotNull(matchResult.TemplateGroupsWithMatchingTemplateInfo.SelectMany(group => group.Templates).Single(t => t.Identity == "Console.App2"));
        }
Exemplo n.º 5
0
        public async Task TestGetTemplateResolutionResult_UniqueNameMatchesCorrectly()
        {
            IReadOnlyList <ITemplateInfo> templatesToSearch = new List <ITemplateInfo>()
            {
                new MockTemplateInfo("console1", name: "Long name for Console App", identity: "Console.App"),
                new MockTemplateInfo("console2", name: "Long name for Console App #2", identity: "Console.App2")
            };

            ListTemplateResolver     resolver    = new ListTemplateResolver(templatesToSearch, new MockHostSpecificDataLoader());
            TemplateResolutionResult matchResult = await resolver.ResolveTemplatesAsync(
                GetListCommandArgsFor("new list console2"),
                defaultLanguage : null,
                default).ConfigureAwait(false);

            Assert.True(matchResult.HasTemplateGroupWithTemplateInfoMatches);
            Assert.NotNull(matchResult.UnambiguousTemplateGroup);
            Assert.Equal("console2", matchResult.UnambiguousTemplateGroup?.Templates.Single().ShortNameList.Single());
            Assert.Equal("Console.App2", matchResult.UnambiguousTemplateGroup?.Templates.Single().Identity);
            Assert.Equal(1, matchResult.UnambiguousTemplateGroup?.Templates.Count);
        }
Exemplo n.º 6
0
        public async Task TestGetTemplateResolutionResult_MultipleGroupsAreFound()
        {
            List <ITemplateInfo> templatesToSearch = new List <ITemplateInfo>();

            templatesToSearch.Add(new MockTemplateInfo("console", name: "Long name for Console App", identity: "Console.App.L1", groupIdentity: "Console.App.Test").WithTag("language", "L1"));
            templatesToSearch.Add(new MockTemplateInfo("console", name: "Long name for Console App", identity: "Console.App.L2", groupIdentity: "Console.App.Test").WithTag("language", "L2"));
            templatesToSearch.Add(new MockTemplateInfo("console", name: "Long name for Console App", identity: "Console.App.L3", groupIdentity: "Console.App.Test").WithTag("language", "L3"));
            templatesToSearch.Add(new MockTemplateInfo("classlib", name: "Long name for Class Library App", identity: "Class.Library.L1", groupIdentity: "Class.Library.Test").WithTag("language", "L1"));
            templatesToSearch.Add(new MockTemplateInfo("classlib", name: "Long name for Class Library App", identity: "Class.Library.L2", groupIdentity: "Class.Library.Test").WithTag("language", "L2"));

            ListTemplateResolver     resolver    = new ListTemplateResolver(templatesToSearch, new MockHostSpecificDataLoader());
            TemplateResolutionResult matchResult = await resolver.ResolveTemplatesAsync(
                GetListCommandArgsFor("new list c"),
                defaultLanguage : null,
                default).ConfigureAwait(false);

            Assert.True(matchResult.HasTemplateGroupWithTemplateInfoMatches);
            Assert.Equal(2, matchResult.TemplateGroupsWithMatchingTemplateInfo.Count());
            Assert.Equal(5, matchResult.TemplateGroupsWithMatchingTemplateInfo.SelectMany(group => group.Templates).Count());
            Assert.Null(matchResult.UnambiguousTemplateGroup);
        }
Exemplo n.º 7
0
        /// <summary>
        /// Handles template list display (dotnet new3 --list).
        /// </summary>
        /// <param name="args">user command input.</param>
        /// <param name="cancellationToken">cancellation token.</param>
        /// <returns></returns>
        internal async Task <NewCommandStatus> DisplayTemplateGroupListAsync(
            ListCommandArgs args,
            CancellationToken cancellationToken)
        {
            cancellationToken.ThrowIfCancellationRequested();

            ListTemplateResolver     resolver         = new ListTemplateResolver(_constraintManager, _templatePackageManager, _hostSpecificDataLoader);
            TemplateResolutionResult resolutionResult = await resolver.ResolveTemplatesAsync(args, _defaultLanguage, cancellationToken).ConfigureAwait(false);

            //IReadOnlyDictionary<string, string?>? appliedParameterMatches = resolutionResult.GetAllMatchedParametersList();
            if (resolutionResult.TemplateGroupsWithMatchingTemplateInfoAndParameters.Any())
            {
                Reporter.Output.WriteLine(
                    string.Format(
                        LocalizableStrings.TemplatesFoundMatchingInputParameters,
                        GetInputParametersString(args /*, appliedParameterMatches*/)));
                Reporter.Output.WriteLine();

                TabularOutputSettings settings = new TabularOutputSettings(_engineEnvironmentSettings.Environment, args);

                TemplateGroupDisplay.DisplayTemplateList(
                    _engineEnvironmentSettings,
                    resolutionResult.TemplateGroupsWithMatchingTemplateInfoAndParameters,
                    settings,
                    reporter: Reporter.Output,
                    selectedLanguage: args.Language);
                return(NewCommandStatus.Success);
            }
            else
            {
                //if there is no criteria and filters it means that dotnet new list was run but there is no templates installed.
                if (args.ListNameCriteria == null && !args.AppliedFilters.Any())
                {
                    //No templates installed.
                    Reporter.Output.WriteLine(LocalizableStrings.NoTemplatesFound);
                    Reporter.Output.WriteLine();
                    // To search for the templates on NuGet.org, run:
                    Reporter.Output.WriteLine(LocalizableStrings.SearchTemplatesCommand);
                    Reporter.Output.WriteCommand(
                        Example
                        .For <NewCommand>(args.ParseResult)
                        .WithSubcommand <SearchCommand>()
                        .WithArgument(SearchCommand.NameArgument));
                    Reporter.Output.WriteLine();
                    return(NewCommandStatus.Success);
                }

                // at least one criteria was specified.
                // No templates found matching the following input parameter(s): {0}.
                Reporter.Error.WriteLine(
                    string.Format(
                        LocalizableStrings.NoTemplatesMatchingInputParameters,
                        GetInputParametersString(args /*, appliedParameterMatches*/))
                    .Bold().Red());

                if (resolutionResult.HasTemplateGroupMatches && resolutionResult.ListFilterMismatchGroupCount > 0)
                {
                    // {0} template(s) partially matched, but failed on {1}.
                    Reporter.Error.WriteLine(
                        string.Format(
                            LocalizableStrings.TemplatesNotValidGivenTheSpecifiedFilter,
                            resolutionResult.ListFilterMismatchGroupCount,
                            GetPartialMatchReason(resolutionResult, args /*, appliedParameterMatches*/))
                        .Bold().Red());
                }

                if (resolutionResult.HasTemplateGroupMatches && resolutionResult.ContraintsMismatchGroupCount > 0)
                {
                    // {0} template(s) are not displayed due to their constraints are not satisfied.
                    // To display them add "--ignore-constraints" option.
                    Reporter.Error.WriteLine(
                        string.Format(
                            LocalizableStrings.TemplateListCoordinator_Error_FailedConstraints,
                            resolutionResult.ContraintsMismatchGroupCount,
                            ListCommand.IgnoreConstraintsOption.Aliases.First())
                        .Bold().Red());
                }

                Reporter.Error.WriteLine();
                // To search for the templates on NuGet.org, run:
                Reporter.Error.WriteLine(LocalizableStrings.SearchTemplatesCommand);
                if (string.IsNullOrWhiteSpace(args.ListNameCriteria))
                {
                    Reporter.Error.WriteCommand(
                        Example
                        .For <NewCommand>(args.ParseResult)
                        .WithSubcommand <SearchCommand>()
                        .WithArgument(SearchCommand.NameArgument));
                }
                else
                {
                    Reporter.Error.WriteCommand(
                        Example
                        .For <NewCommand>(args.ParseResult)
                        .WithSubcommand <SearchCommand>()
                        .WithArgument(SearchCommand.NameArgument, args.ListNameCriteria));
                }
                Reporter.Error.WriteLine();
                return(NewCommandStatus.NotFound);
            }
        }