Esempio n. 1
0
        public static List <string> GetOutputWindowsLines(this VisualStudioHost host)
        {
            if (host == null)
            {
                throw new ArgumentNullException(nameof(host));
            }

            var lines = new List <string>();

            try
            {
                CommonUtility.UIInvoke(() =>
                {
                    var outputPane = host.ObjectModel.Shell.ToolWindows.OutputWindow.GetOutputPane(_nugetOutputWindowGuid);
                    lines.AddRange(outputPane.Text.Split('\n').Select(e => e.Trim()).Where(e => !string.IsNullOrEmpty(e)));
                });
            }
            catch (ArgumentException)
            {
                // If no output has been printed into the nuget output window then the output pane will not be initialized
                // If no output has been printed we know there is no error in the output window.
            }

            return(lines);
        }
Esempio n. 2
0
        private void UseService <T>(FileInfo solutionFile, Action <T> action)
            where T : class
        {
            VisualStudioHost visualStudio = null;

            try
            {
                VisualStudioHostConfiguration configuration = CreateVisualStudioHostConfiguration();

                configuration.AddCompositionAssembly(Assembly.GetExecutingAssembly().Location);

                visualStudio = LaunchVisualStudio(configuration);

                LoadSolution(visualStudio, solutionFile);
                WaitForAutoRestoreToComplete(visualStudio, solutionFile);

                using (Scope.Enter("Get service."))
                {
                    var service = visualStudio.Get <T>();

                    using (Scope.Enter("Verify service."))
                    {
                        action(service);
                    }
                }

                ShutDownVisualStudio(visualStudio);
            }
            catch (Exception ex)
            {
                HandleException(visualStudio, ex);
            }
        }
        public void Dispose()
        {
            if (_visualStudioHost != null && _visualStudioHost.IsRunning)
            {
                try
                {
                    if (_messageFilterSingleton != null)
                    {
                        _messageFilterSingleton.Dispose();
                    }

                    _visualStudioHost.Stop();
                }
                catch (COMException)
                {
                    // VSO 178569: Access to DTE during shutdown may throw a variety of COM exceptions
                    // if inaccessible.
                }
                catch (Exception)
                {
                    //this.Logger.WriteException(EntryType.Warning, filterException, "Could not to tear down the message filter.");
                }
                _visualStudioHost = null;
            }
        }
Esempio n. 4
0
        public ApexTestContext(VisualStudioHost visualStudio, ProjectTemplate projectTemplate, ILogger logger, bool noAutoRestore = false, bool addNetStandardFeeds = false)
        {
            logger.LogInformation("Creating test context");
            _pathContext = new SimpleTestPathContext();

            if (noAutoRestore)
            {
                _pathContext.Settings.DisableAutoRestore();
            }

            if (addNetStandardFeeds)
            {
                _pathContext.Settings.AddNetStandardFeeds();
            }

            _visualStudio        = visualStudio;
            _logger              = logger;
            SolutionService      = _visualStudio.Get <SolutionService>();
            NuGetApexTestService = _visualStudio.Get <NuGetApexTestService>();

            VisualStudioHostExtension.ClearWindows(_visualStudio);

            Project = CommonUtility.CreateAndInitProject(projectTemplate, _pathContext, SolutionService, logger);

            NuGetApexTestService.WaitForAutoRestore();
        }
Esempio n. 5
0
 public void EnsureHost()
 {
     if (_visualStudioHost == null || !_visualStudioHost.IsRunning)
     {
         _messageFilterSingleton = new RetryMessageFilter();
         _visualStudioHost       = Operations.CreateAndStartHost <VisualStudioHost>(VisualStudioHostConfiguration);
     }
 }
Esempio n. 6
0
 public static void SelectProjectInSolutionExplorer(this VisualStudioHost host, string project)
 {
     CommonUtility.UIInvoke(() =>
     {
         var item = host.ObjectModel.Shell.ToolWindows.SolutionExplorer.FindItemRecursive(project);
         item.Select();
     });
 }
Esempio n. 7
0
 private void ShutDownVisualStudio(VisualStudioHost visualStudio)
 {
     using (Scope.Enter("Close solution and shut down Visual Studio."))
     {
         visualStudio.ObjectModel.Solution.Close();
         visualStudio.Stop();
     }
 }
Esempio n. 8
0
 private void BuildSolution(VisualStudioHost visualStudio)
 {
     using (Scope.Enter("Build solution."))
     {
         // Rebuild to ensure a clean build.
         visualStudio.ObjectModel.Solution.BuildManager.Rebuild();
         visualStudio.ObjectModel.Solution.BuildManager.Verify.Succeeded();
     }
 }
Esempio n. 9
0
 private void LoadSolution(VisualStudioHost visualStudio, FileInfo solutionFile)
 {
     using (Scope.Enter("Load solution."))
     {
         visualStudio.ObjectModel.Solution.WaitForFullyLoadedOnOpen = true;
         visualStudio.ObjectModel.Solution.Open(solutionFile.FullName);
         visualStudio.ObjectModel.Solution.Verify.HasProject();
     }
 }
Esempio n. 10
0
        private VisualStudioHost LaunchVisualStudio(VisualStudioHostConfiguration configuration)
        {
            VisualStudioHost visualStudio = Operations.CreateHost <VisualStudioHost>(configuration);

            using (Scope.Enter("Launch Visual Studio."))
            {
                visualStudio.Start();
            }

            return(visualStudio);
        }
Esempio n. 11
0
        public static string AppendErrors(string s, VisualStudioHost visualStudio)
        {
            var errors = visualStudio.GetErrorsInOutputWindows();

            if (errors.Any())
            {
                s += Environment.NewLine + string.Join("\n\t error: ", errors);
            }

            return(s);
        }
Esempio n. 12
0
        /// <summary>
        /// Assert no errors in the error list
        /// </summary>
        internal static List <string> GetErrorListErrors(this VisualStudioHost host)
        {
            var errors = new List <string>();

            CommonUtility.UIInvoke(() =>
            {
                errors.AddRange(host.ObjectModel.Shell.ToolWindows.ErrorList.Messages.Select(e => e.Description));
            });

            return(errors);
        }
Esempio n. 13
0
 public static void ClearErrorWindow(this VisualStudioHost host)
 {
     try
     {
         CommonUtility.UIInvoke(() => host.ObjectModel.Shell.ToolWindows.ErrorList.HideAllItems());
     }
     catch (ArgumentException)
     {
         // ignore errors
     }
 }
Esempio n. 14
0
        public static void AssertPackageReferenceDoesNotExist(VisualStudioHost visualStudio, ProjectTestExtension project, string packageName, ILogger logger)
        {
            logger.LogInformation($"Checking for PackageReference {packageName}");

            var matches = GetPackageReferences(project)
                          .Where(e => e.Name.Equals(packageName, StringComparison.OrdinalIgnoreCase))
                          .ToList();

            logger.LogInformation($"Matches: {matches.Count}");

            matches.Any().Should().BeFalse($"A PackageReference for {packageName} was found in {project.FullPath}");
        }
Esempio n. 15
0
        public static void AssertPackageNotInPackagesConfig(VisualStudioHost visualStudio, ProjectTestExtension project, string packageName, ILogger logger)
        {
            logger.LogInformation($"Checking project for {packageName}");
            var testService = visualStudio.Get <NuGetApexTestService>();

            // Check using the IVs APIs
            var exists = testService.IsPackageInstalled(project.UniqueName, packageName);

            logger.LogInformation($"Exists: {exists}");

            exists.Should().BeFalse(AppendErrors($"{packageName} should NOT be in {project.Name}", visualStudio));
        }
Esempio n. 16
0
 public static void ClearOutputWindow(this VisualStudioHost host)
 {
     try
     {
         var outputPane = host.ObjectModel.Shell.ToolWindows.OutputWindow.GetOutputPane(_nugetOutputWindowGuid);
         outputPane.Clear();
     }
     catch (ArgumentException)
     {
         //if outputPane doesn't exist, ignore it
     }
 }
Esempio n. 17
0
        public static void AssertPackageReferenceExists(VisualStudioHost visualStudio, ProjectTestExtension project, string packageName, string packageVersion, ILogger logger)
        {
            logger.LogInformation($"Checking for PackageReference {packageName} {packageVersion}");

            var matches = GetPackageReferences(project)
                          .Where(e => e.Name.Equals(packageName, StringComparison.OrdinalIgnoreCase) &&
                                 e.LibraryRange.VersionRange.MinVersion.Equals(NuGetVersion.Parse(packageVersion)))
                          .ToList();

            logger.LogInformation($"Matches: {matches.Count}");

            matches.Any().Should().BeTrue($"A PackageReference with {packageName}/{packageVersion} was not found in {project.FullPath}");
        }
Esempio n. 18
0
        public ApexTestContext(VisualStudioHost visualStudio, ProjectTemplate projectTemplate, bool noAutoRestore = false)
        {
            _pathContext = new SimpleTestPathContext();

            if (noAutoRestore)
            {
                _pathContext.Settings.DisableAutoRestore();
            }

            _visualStudio   = visualStudio;
            SolutionService = _visualStudio.Get <SolutionService>();

            Project = Utils.CreateAndInitProject(projectTemplate, _pathContext, SolutionService);
        }
Esempio n. 19
0
        public static bool HasNoErrorsInOutputWindows(this VisualStudioHost host)
        {
            try
            {
                var outputPane = host.ObjectModel.Shell.ToolWindows.OutputWindow.GetOutputPane(_nugetOutputWindowGuid);

                return(!outputPane.Text.ToLowerInvariant().Contains("failed"));
            }
            catch (ArgumentException)
            {
                // If no output has been printed into the nuget output window then the output pane will not be initialized
                // If no output has been printed we know there is no error in the output window.
                return(true);
            }
        }
Esempio n. 20
0
        protected override void DoHostTestInitialize()
        {
            base.DoHostTestInitialize();

            VsHost  = VisualStudio;
            Helpers = new HelperWrapper(VisualStudio);

            Solution.Open(SolutionPath);
            Solution.WaitForFullyLoaded(); // This will get modified after bug 627108 get fixed

            _webProject               = Solution[ProjectName];
            _libmanConfig             = _webProject[LibManManifestFile];
            _pathToLibmanFile         = Path.Combine(SolutionRootPath, ProjectName, LibManManifestFile);
            _initialLibmanFileContent = File.ReadAllText(_pathToLibmanFile);
        }
Esempio n. 21
0
        private void HandleException(VisualStudioHost visualStudio, Exception ex)
        {
            using (Scope.Enter("Handle exception."))
            {
                if (visualStudio != null && visualStudio.IsRunning)
                {
                    visualStudio.CaptureHostProcessDumpIfRunning(MiniDumpType.WithFullMemory);
                    visualStudio.HostProcess.Kill();
                }

                Logger.WriteException(EntryType.Error, ex);
                Verify.Fail("Exception encountered during test execution");
                Assert.Fail(ex.Message);
            }
        }
Esempio n. 22
0
        public static void AssertPackageNotInAssetsFile(VisualStudioHost visualStudio, ProjectTestExtension project, string packageName, string packageVersion, ILogger logger)
        {
            logger.LogInformation($"Checking assets file for {packageName}");
            var testService = visualStudio.Get <NuGetApexTestService>();

            testService.WaitForAutoRestore();

            var assetsFilePath = GetAssetsFilePath(project.FullPath);

            // Project has an assets file, let's look there to assert
            var inAssetsFile = IsPackageInstalledInAssetsFile(assetsFilePath, packageName, packageVersion, false);

            logger.LogInformation($"Exists: {inAssetsFile}");

            inAssetsFile.Should().BeFalse(AppendErrors($"{packageName}/{packageVersion} should not be installed in {project.Name}", visualStudio));
        }
Esempio n. 23
0
        public ApexTestContext(VisualStudioHost visualStudio, ProjectTemplate projectTemplate, ILogger logger, bool noAutoRestore = false)
        {
            logger.LogInformation("Creating test context");
            _pathContext = new SimpleTestPathContext();

            if (noAutoRestore)
            {
                _pathContext.Settings.DisableAutoRestore();
            }

            _visualStudio        = visualStudio;
            _logger              = logger;
            SolutionService      = _visualStudio.Get <SolutionService>();
            NuGetApexTestService = _visualStudio.Get <NuGetApexTestService>();

            Project = Utils.CreateAndInitProject(projectTemplate, _pathContext, SolutionService, logger);

            NuGetApexTestService.WaitForAutoRestore();
        }
Esempio n. 24
0
        private void WaitForAutoRestoreToComplete(VisualStudioHost visualStudio, FileInfo solutionFile)
        {
            using (Scope.Enter("Wait for auto restore completion."))
            {
                var assetsFile = new FileInfo(Path.Combine(solutionFile.DirectoryName, solutionFile.Directory.Name, "obj", "project.assets.json"));
                var timeout    = TimeSpan.FromMinutes(1);
                var interval   = TimeSpan.FromSeconds(5);

                const string RestoreOutputCompletionMarker = "========== Finished ==========";

                visualStudio.ObjectModel.Solution.WaitForIntellisenseStage(TimeSpan.FromMinutes(5));
                visualStudio.ObjectModel.Shell.ToolWindows.OutputWindow.ToolWindow.Show();

                // Wait for the assets file to be created.
                Omni.Common.WaitFor.IsTrue(
                    () => File.Exists(assetsFile.FullName),
                    timeout,
                    interval,
                    $"An assets file was not created at '{assetsFile.FullName}'.");

                // Wait for the solution restore to complete.
                Omni.Common.WaitFor.IsTrue(
                    () =>
                {
                    if (TryGetPackageManagerOutputWindowPane(visualStudio, out OutputWindowPaneTestExtension packageManagerOutputWindowPane))
                    {
                        var output = packageManagerOutputWindowPane.Text;

                        if (!string.IsNullOrEmpty(output))
                        {
                            output = output.TrimEnd('\r', '\n');

                            return(output.Contains(RestoreOutputCompletionMarker));
                        }
                    }

                    return(false);
                },
                    timeout,
                    interval,
                    "Solution restore did not complete according to the Package Manager output window pane.");
            }
        }
Esempio n. 25
0
        private void OpenSolutionAndBuild(FileInfo solutionFile)
        {
            VisualStudioHost visualStudio = null;

            try
            {
                VisualStudioHostConfiguration configuration = CreateVisualStudioHostConfiguration();

                visualStudio = LaunchVisualStudio(configuration);

                LoadSolution(visualStudio, solutionFile);
                WaitForAutoRestoreToComplete(visualStudio, solutionFile);
                BuildSolution(visualStudio);
                ShutDownVisualStudio(visualStudio);
            }
            catch (Exception ex)
            {
                HandleException(visualStudio, ex);
            }
        }
Esempio n. 26
0
 /// <summary>
 /// Assert no errors in the error list or output window
 /// </summary>
 internal static void AssertNoErrors(this VisualStudioHost host)
 {
     host.AssertNuGetOutputDoesNotHaveErrors();
     host.GetErrorListErrors().Should().BeEmpty("Empty errors in error list");
 }
Esempio n. 27
0
 /// <summary>
 /// Clear the error list and output window
 /// </summary>
 public static void ClearWindows(this VisualStudioHost host)
 {
     host.ClearOutputWindow();
     host.ClearErrorWindow();
 }
Esempio n. 28
0
 /// <summary>
 /// Assert no errors in nuget output window
 /// </summary>
 internal static void AssertNuGetOutputDoesNotHaveErrors(this VisualStudioHost host)
 {
     host.GetErrorsInOutputWindows().Should().BeEmpty();
 }
Esempio n. 29
0
 public static bool HasNoErrorsInOutputWindows(this VisualStudioHost host)
 {
     return(host.GetErrorsInOutputWindows().Count == 0);
 }
Esempio n. 30
0
 public static List <string> GetErrorsInOutputWindows(this VisualStudioHost host)
 {
     return(host.GetOutputWindowsLines().Where(e => e.IndexOf("failed", StringComparison.OrdinalIgnoreCase) > -1).ToList());
 }