private object GetErrorsViewModel(ISolutionProjectModel project, BuildResult buildResult) { var res = new BuildErrorsViewModel(Output.FindErrorTasks(project), this, project, serviceProvider); if (buildResult.Exception != null) { res.Message = buildResult.Exception.Message; } return(res); }
public async Task RunAllTestsAsync(ISolutionProjectModel projectViewModel, IServiceSettings settings, CancellationToken cancellationToken = default(CancellationToken)) { var unitTestServiceSettings = settings.GetSettingsFromProvider <UnitTestServiceSettings>(projectViewModel); await cancellationToken.WaitWhenPaused(); try { if (!IsCancelled(projectViewModel, cancellationToken) && CanRunAnyTests(projectViewModel)) { if (NeedToBeCompiled(projectViewModel)) { await serviceProvider.Get <LocalBuildService>().BuildSolutionsAsync(new[] { projectViewModel }, settings, cancellationToken); var buildErrors = Output.FindErrorTasks(projectViewModel); if (buildErrors.Any()) { statusService.IncrementStep(); return; } } await cancellationToken.WaitWhenPaused(); var externalActionService = serviceProvider.Get <ExternalActionService>(); await externalActionService.RunExternalPreActions(projectViewModel, this, cancellationToken : cancellationToken); void OnError(MsTestCommand command, string s) { if (!cancellationToken.IsCancellationRequested) { OnTestError(projectViewModel, command, s, unitTestServiceSettings.CancelOnFailures); } } void TestData(MsTestCommand command, string s) { if (!cancellationToken.IsCancellationRequested) { OnTestData(projectViewModel, command, s, unitTestServiceSettings.CancelOnFailures); } } await cancellationToken.WaitWhenPaused(); int totalTestCount = 0; using (new PauseCheckedActionScope(() => projectViewModel.CurrentOperation = GetOperationInfo(unitTestServiceSettings.TrackLiveOutput), () => projectViewModel.CurrentOperation = Operations.None, cancellationToken)) { var watch = Stopwatch.StartNew(); bool executed = false; //var unitTestInfos = projectViewModel.GetUnitTestProjects().SelectMany(project => project.GetTestMethods()).ToArray(); var result = new Dictionary <TestRun, IList <MsTestError> >(); foreach (Project project in projectViewModel.GetUnitTestProjects()) { if (!cancellationToken.IsCancellationRequested) { var unitTestInfos = project.GetTestMethods().ToArray(); int progress = 0; int total = unitTestInfos.Length; totalTestCount = totalTestCount + total; if (unitTestServiceSettings.TrackLiveOutput) { projectViewModel.CurrentOperation.SetProgress(total, progress); } var command = GetTestCommand(projectViewModel, settings, unitTestInfos); await command.ExecuteAsync((testCommand, s) => { if (unitTestServiceSettings.TrackLiveOutput && !string.IsNullOrEmpty(s) && s.Contains("[testname]")) { projectViewModel.CurrentOperation?.SetProgress(total, (++progress)); } TestData(testCommand, s); }, OnError, cancellationToken); executed = true; var results = OpenTestResultFileAndGetResults(project, command.ResultFile); if (results.Key != null) { result.Add(results.Key, results.Value); } } } watch.Stop(); List <MsTestError> failedTests = result.SelectMany(pair => pair.Value).ToList(); BuildErrorsViewModel errorModel = GetErrorsViewModel(projectViewModel); int passed = totalTestCount - failedTests.Count; if (errorModel != null && errorModel.Errors.Any()) { projectViewModel.SetResult(new ValidationResult(false, errorModel)); } else { projectViewModel.SetResult(ValidationResult.ValidResult); } await externalActionService.RunExternalPostActions(projectViewModel, this, passed == totalTestCount, cancellationToken : cancellationToken); if (!cancellationToken.IsCancellationRequested) { Output.WriteLine(executed ? string.Format("{2}/{3} Tests passed in testrun for {0} (completed in {1})", projectViewModel.SolutionFileName, watch.Elapsed, passed, totalTestCount) : $"There are no Tests for {projectViewModel.SolutionFileName}"); } } } else { Output.WriteLine($"There are no Tests for {projectViewModel.SolutionFileName}"); } } finally { statusService.IncrementStep(); } }