Skip to content

Commit

Permalink
Fixed embedded resource path and null reference exception in statisti…
Browse files Browse the repository at this point in the history
…c report (#62)
  • Loading branch information
MilleBo authored Apr 13, 2020
1 parent d474c53 commit ca2785d
Show file tree
Hide file tree
Showing 13 changed files with 95 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ public async Task<MutationRunResult> Handle(ExecuteMutationsCommand command, Can
var mutationDocuments = new Queue<MutationDocument>(command.MutationDocuments);
var currentRunningDocuments = new List<Task>();

var start = DateTime.Now;
var numberOfMutationsLeft = command.MutationDocuments.Count;

_mutationRunLoggerManager.Initialize(command.Config.MutationRunLoggers);
Expand Down Expand Up @@ -97,7 +98,7 @@ await Task.Run(() =>
// Wait for the final ones
await Task.WhenAll(currentRunningDocuments);

var mutationRunResult = new MutationRunResult(results, cancellationToken.IsCancellationRequested);
var mutationRunResult = new MutationRunResult(results, cancellationToken.IsCancellationRequested, DateTime.Now - start);

if (cancellationToken.IsCancellationRequested)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,11 @@ public class CreateReportCommandHandler : IRequestHandler<CreateReportCommand, b
{
public Task<bool> Handle(CreateReportCommand command, CancellationToken cancellationToken)
{
Parallel.ForEach(command.ReportCreators, reportCreator => reportCreator.SaveReport(command.Mutations, command.ExecutionTime));
foreach (var commandReportCreator in command.ReportCreators)
{
commandReportCreator.SaveReport(command.Mutations, command.ExecutionTime);
}

return Task.FromResult(true);
}
}
Expand Down
3 changes: 1 addition & 2 deletions src/Testura.Mutation.Console/ConsoleMutationExecutor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ public async Task<bool> ExecuteMutationRunner(string configPath, string savePath
{
var config = await _mediator.Send(new OpenProjectCommand(configPath));

var start = DateTime.Now;
var mutationDocuments = await _mediator.Send(new CreateMutationsCommand(config));
var results = await _mediator.Send(new ExecuteMutationsCommand(config, mutationDocuments, null));

Expand All @@ -44,7 +43,7 @@ public async Task<bool> ExecuteMutationRunner(string configPath, string savePath
new TesturaMutationStatisticReportCreator(Path.Combine(savePath, "mutationStatistics.json"))
};

await _mediator.Send(new CreateReportCommand(results.MutationDocumentResults, reports, DateTime.Now - start));
await _mediator.Send(new CreateReportCommand(results.MutationDocumentResults, reports, results.ExecutionTime));

return results.Success;
}
Expand Down
4 changes: 2 additions & 2 deletions src/Testura.Mutation.Console/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,6 @@
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.8.2.0")]
[assembly: AssemblyFileVersion("1.8.2.0")]
[assembly: AssemblyVersion("1.8.3.0")]
[assembly: AssemblyFileVersion("1.8.3.0")]
[assembly: log4net.Config.XmlConfigurator(ConfigFile = "log4Net.config", Watch = true)]
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ private Stream ProvideResourceData(string resourceFullFilename)
resourceWriter.TypeNameConverter = TypeNameConverter;
using (var resourceReader = new ResXResourceReader(resourceFullFilename))
{
resourceReader.BasePath = Path.GetDirectoryName(resourceFullFilename);
var dictionaryEnumerator = resourceReader.GetEnumerator();
while (dictionaryEnumerator.MoveNext())
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ private List<TesturaMutationReportItem> CreateMutationReportItems(IList<Mutation
var mutationOperatorTypes = Enum.GetNames(typeof(MutationOperators));
foreach (var mutationOperatorType in mutationOperatorTypes)
{
var mutationByOperator = mutations.Where(m => m.Category.HeadCategory == mutationOperatorType);
var mutationByOperator = mutations.Where(m => m.Category != null && m.Category.HeadCategory == mutationOperatorType).ToList();

if (!mutationByOperator.Any())
{
Expand All @@ -65,7 +65,7 @@ private List<TesturaMutationReportItem> CreateMutationReportItems(IList<Mutation

if (locationKind != null)
{
mutationByOperator = mutations.Where(m => m.Location.Kind == locationKind);
mutationByOperator = mutationByOperator.Where(m => m.Location != null && m.Location.Kind == locationKind).ToList();
}

var mutationOperatorsBySubCategories = mutationByOperator.GroupBy(m => m.Category.Subcategory);
Expand All @@ -86,14 +86,21 @@ private TesturaMutationReportItem CreateTesturaMutationReportItem(
var survived = mutationDocumentResults.Count(m => m.Survived);
var killed = mutationDocumentResults.Count(m => !m.Survived && m.CompilationResult != null && m.CompilationResult.IsSuccess);

double mutationScore = 0.0;

if (survived + killed != 0)
{
mutationScore = (double)killed / (survived + killed);
}

return new TesturaMutationReportItem
{
HeadCategory = headCategory.ToString(),
SubCategory = subCategory,
FailedOnCompilation = mutationDocumentResults.Count(m => !m.CompilationResult.IsSuccess),
Killed = killed,
Survived = survived,
MutationScore = (double)killed / (survived + killed),
MutationScore = mutationScore,
};
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@ namespace Testura.Mutation.Core.Execution.Result
{
public class MutationRunResult
{
public MutationRunResult(IList<MutationDocumentResult> mutationDocumentResults, bool wasCancelled)
public MutationRunResult(IList<MutationDocumentResult> mutationDocumentResults, bool wasCancelled, TimeSpan executionTime)
{
MutationDocumentResults = mutationDocumentResults;
WasCancelled = wasCancelled;
ExecutionTime = executionTime;
}

public IList<MutationDocumentResult> MutationDocumentResults { get; }
Expand All @@ -18,6 +19,8 @@ public MutationRunResult(IList<MutationDocumentResult> mutationDocumentResults,

public bool Success => MutationDocumentResults.All(r => !r.Survived);

public TimeSpan ExecutionTime { get; }

public double GetMutationScore()
{
var validMutations = MutationDocumentResults.Where(r => r.CompilationResult != null && r.CompilationResult.IsSuccess && r.UnexpectedError == null).ToList();
Expand Down
4 changes: 2 additions & 2 deletions src/Testura.Mutation.VsExtension/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,5 @@
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.10.1.0")]
[assembly: AssemblyFileVersion("1.10.1.0")]
[assembly: AssemblyVersion("1.11.0.0")]
[assembly: AssemblyFileVersion("1.11.0.0")]
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,12 @@
Command="{Binding RemoveKilledMutationsCommand}">
<Image Source="../../Resources/iconRemoveKilledMutations.png" />
</Button>
<Button
ToolTip="Save result"
IsEnabled="{Binding IsSaveButtonEnabled}"
Command="{Binding SaveReportCommand}">
<Image Source="../../Resources/iconSave.png" Width="32" />
</Button>
</ToolBar>
</Grid>

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using System.Linq;
using System.Threading;
using System.Windows.Forms;
using DiffPlex;
using DiffPlex.DiffBuilder;
using DiffPlex.DiffBuilder.Model;
Expand All @@ -14,8 +16,12 @@
using Testura.Mutation.Application.Commands.Mutation.CreateMutations;
using Testura.Mutation.Application.Commands.Mutation.ExecuteMutations;
using Testura.Mutation.Application.Commands.Project.OpenProject;
using Testura.Mutation.Application.Commands.Report.Creator;
using Testura.Mutation.Core.Config;
using Testura.Mutation.Core.Creator.Filter;
using Testura.Mutation.Core.Execution.Report;
using Testura.Mutation.Core.Execution.Report.Testura;
using Testura.Mutation.Core.Execution.Result;
using Testura.Mutation.VsExtension.Models;
using Testura.Mutation.VsExtension.MutationHighlight;
using Testura.Mutation.VsExtension.MutationHighlight.Glyph.Dialog;
Expand All @@ -40,14 +46,16 @@ public class MutationExplorerWindowViewModel : BindableBase
private bool _isRunButtonEnabled;
private bool _isStopButtonEnabled;
private bool _isLoadingMutationsVisible;
private bool _shouldRefresh;
private bool _isSaveButtonEnabled;

private MutationRunItem _selectedMutation;
private IEnumerable<MutationRunItem> _selectedMutations;
private IList<MutationDocumentFilterItem> _filterItems;
private MutationConfig _config;
private CancellationTokenSource _tokenSource;
private IEnumerable<MutationDocumentFilterItem> _originalFilterItems;
private bool _shouldRefresh;
private MutationRunResult _latestResult;

public MutationExplorerWindowViewModel(
EnvironmentService environmentService,
Expand Down Expand Up @@ -78,6 +86,7 @@ public MutationExplorerWindowViewModel(
RunOnlySurvivingMutationsCommand = new DelegateCommand(RunOnlySurvivingMutations);
RefreshMutationsCommand = new DelegateCommand(() => Initialize(_originalFilterItems));
RunSelectedMutationsCommand = new DelegateCommand(RunSelectedMutations);
SaveReportCommand = new DelegateCommand(SaveReport);

StopCommand = new DelegateCommand(() =>
{
Expand Down Expand Up @@ -118,6 +127,8 @@ public MutationExplorerWindowViewModel(

public DelegateCommand RunSelectedMutationsCommand { get; set; }

public DelegateCommand SaveReportCommand { get; set; }

public DelegateCommand<System.Collections.IList> MutationsSelectedCommand { get; set; }

public ObservableCollection<MutationRunItem> Mutations { get; set; }
Expand Down Expand Up @@ -164,6 +175,12 @@ public bool IsStopButtonEnabled
set => SetProperty(ref _isStopButtonEnabled, value);
}

public bool IsSaveButtonEnabled
{
get => _isSaveButtonEnabled;
set => SetProperty(ref _isSaveButtonEnabled, value);
}

public MutationRunItem SelectedMutation
{
get => _selectedMutation;
Expand Down Expand Up @@ -234,6 +251,9 @@ public void Initialize(IEnumerable<MutationDocumentFilterItem> filterItems = nul
}

_shouldRefresh = false;
_latestResult = null;

IsSaveButtonEnabled = false;
IsRunButtonEnabled = false;
IsStopButtonEnabled = true;

Expand Down Expand Up @@ -297,16 +317,19 @@ private void RunMutations(IEnumerable<MutationRunItem> mutations)
{
try
{
IsSaveButtonEnabled = false;
IsStopButtonEnabled = true;
IsRunButtonEnabled = false;

await _mediator.Send(
_latestResult = await _mediator.Send(
new ExecuteMutationsCommand(
_config,
mutations.Select(r => r.Document).ToList(),
_mutationRunDocumentService.MutationDocumentStarted,
_mutationRunDocumentService.MutationDocumentCompleted),
_tokenSource.Token);

IsSaveButtonEnabled = true;
}
catch (Exception)
{
Expand Down Expand Up @@ -390,10 +413,43 @@ private void ResetWindow()
Mutations.Clear();
MutationCodeHighlightHandler.ClearHighlights();

IsSaveButtonEnabled = false;
IsRunButtonEnabled = false;
IsLoadingMutationsVisible = false;
IsStopButtonEnabled = false;

_latestResult = null;
_shouldRefresh = false;
}

private void SaveReport()
{
if (_latestResult == null)
{
return;
}

using (var folderBrowserDialog = new FolderBrowserDialog())
{
var result = folderBrowserDialog.ShowDialog();
if (result == DialogResult.OK && !string.IsNullOrWhiteSpace(folderBrowserDialog.SelectedPath))
{
var directory = folderBrowserDialog.SelectedPath;
var savePath = Path.Combine(directory, "result.json");

var reports = new List<ReportCreator>
{
new TesturaMutationReportCreator(Path.ChangeExtension(savePath, ".json")),
new TesturaMutationStatisticReportCreator(Path.Combine(directory, "mutationStatistics.json"))
};

_environmentService.JoinableTaskFactory.RunAsync(async () =>
{
await _mediator.Send(new CreateReportCommand(_latestResult.MutationDocumentResults, reports, _latestResult.ExecutionTime));
_environmentService.UserNotificationService.ShowMessage("Report saved.");
});
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,9 @@
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
<IncludeInVSIX>true</IncludeInVSIX>
</Content>
<Resource Include="Resources\iconSave.png">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Resource>
<Resource Include="Resources\iconRemoveKilledMutations.png">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Resource>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0" ?>
<PackageManifest Version="2.0.0" xmlns="http://schemas.microsoft.com/developer/vsx-schema/2011" xmlns:d="http://schemas.microsoft.com/developer/vsx-schema-design/2011">
<Metadata>
<Identity Id="Testura.MutationVsExtension.71e8249a-b76c-4035-af2b-0304cbf33623" Version="1.10.1" Language="en-US" Publisher="Mille Boström" />
<Identity Id="Testura.MutationVsExtension.71e8249a-b76c-4035-af2b-0304cbf33623" Version="1.11.0" Language="en-US" Publisher="Mille Boström" />
<DisplayName>Testura.Mutation</DisplayName>
<Description xml:space="preserve">Testura is a mutation testing extension for visual studio that verifies the quality of your unit tests by injecting different mutations in your production code and then checks whether your unit tests catch them.</Description>
<MoreInfo>https://github.com/Testura/Testura.Mutation</MoreInfo>
Expand Down

0 comments on commit ca2785d

Please sign in to comment.