<commit_before>﻿using System;
using System.IO;
using System.Linq;
using Xunit.Abstractions;

namespace Templates.Test.Helpers
{
    internal static class TemplatePackageInstaller
    {
        private static object _templatePackagesReinstallationLock = new object();
        private static bool _haveReinstalledTemplatePackages;

        private static readonly string[] _templatePackages = new[]
        {
            "Microsoft.DotNet.Web.Client.ItemTemplates",
            "Microsoft.DotNet.Web.ItemTemplates",
            "Microsoft.DotNet.Web.ProjectTemplates.2.0",
            "Microsoft.DotNet.Web.Spa.ProjectTemplates",
            "Microsoft.AspNetCore.SpaTemplates",
        };

        public static void EnsureTemplatePackagesWereReinstalled(ITestOutputHelper output)
        {
            lock (_templatePackagesReinstallationLock)
            {
                if (!_haveReinstalledTemplatePackages)
                {
                    ReinstallTemplatePackages(output);
                    _haveReinstalledTemplatePackages = true;
                }
            }
        }

        private static void ReinstallTemplatePackages(ITestOutputHelper output)
        {
            // Remove any previous or prebundled version of the template packages
            foreach (var packageName in _templatePackages)
            {
                var proc = ProcessEx.Run(
                    output,
                    Directory.GetCurrentDirectory(),
                    "dotnet",
                    $"new --uninstall {packageName}");

                // We don't need this command to succeed, because we'll verify next that
                // uninstallation had the desired effect. This command is expected to fail
                // in the case where the package wasn't previously installed.<commit_msg>Fix for CI agents having extra template package already installed<commit_after>﻿using System;
using System.IO;
using System.Linq;
using Xunit.Abstractions;

namespace Templates.Test.Helpers
{
    internal static class TemplatePackageInstaller
    {
        private static object _templatePackagesReinstallationLock = new object();
        private static bool _haveReinstalledTemplatePackages;

        private static readonly string[] _templatePackages = new[]
        {
            "Microsoft.DotNet.Web.Client.ItemTemplates",
            "Microsoft.DotNet.Web.ItemTemplates",
            "Microsoft.DotNet.Web.ProjectTemplates.2.0",
            "Microsoft.DotNet.Web.Spa.ProjectTemplates",
            "Microsoft.AspNetCore.SpaTemplates",
        };

        private static readonly string[] _extraTemplatePackagesToUninstall = new[]
        {
            "Microsoft.DotNet.Web.ProjectTemplates.2.1",
        };

        public static void EnsureTemplatePackagesWereReinstalled(ITestOutputHelper output)
        {
            lock (_templatePackagesReinstallationLock)
            {
                if (!_haveReinstalledTemplatePackages)
                {
                    ReinstallTemplatePackages(output);
                    _haveReinstalledTemplatePackages = true;
                }
            }
        }

        private static void ReinstallTemplatePackages(ITestOutputHelper output)
        {
            // Remove any previous or prebundled version of the template packages
            foreach (var packageName in _templatePackages.Concat(_extraTemplatePackagesToUninstall))
            {
                var proc = ProcessEx.Run(
                    output,
                    Directory.GetCurrentDirectory(),
                    "dotnet",
                    $"new --uninstall {packageName}");

                // We don't need this command to succeed, because we'll verify next that
                // uninstallation had the desired effect. This command is expected to fail
                // in the case where the package wasn't previously installed.
{'fraction_non_alphanumeric': 0.06616889804325438, 'fraction_numerical': 0.001544799176107106, 'mean_word_length': 1.9770114942528736, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4551699', 'n_tokens_mistral': 930, 'n_tokens_neox': 885, 'n_words': 231}