Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/report improvement #23

Merged
merged 3 commits into from
Dec 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,11 @@ There's also an option to generate a nice html report for all test scenarios. Ju

![image](https://github.com/cezarypiatek/NScenario/assets/7759991/13c501d6-d26b-4406-93fb-1da29dca9bff)


This report browser supports links to scenario steps definition. To make it work, you need to set the following msbuild properties (or environment variables):
- RepositoryUrl
- SourceRevisionId

## Test scenario title

Test scenario title is generated by removing underscores and splitting camel/pascalcase string from the test method name (`[CallerMemberName]` is used to retrieve that name). This allows for immediate review of the test name (I saw many, extremely long and totally ridiculous test method names. A good test method name should reveal the intention of the test case, not its details). You can always override the default title by setting it explicitly during test scenario creation (especially useful for parametrized test methods):
Expand Down
17 changes: 16 additions & 1 deletion src/NScenario/ReportExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text.Json;

namespace NScenario;
Expand All @@ -8,8 +10,21 @@ public static class ReportExtensions
{
public static void SaveAsReport(this IReadOnlyList<ScenarioInfo> scenarios, string outputPath)
{
var callingAssembly = Assembly.GetCallingAssembly();
var sourceControlInfo = RepoPathResolver.GetSourceControlInfo(callingAssembly);
var repositoryRootDir = RepoPathResolver.FindRepoRootDirectory(scenarios.FirstOrDefault()?.FilePath);
var template = ResourceExtractor.GetEmbeddedResourceContent("NScenario.report-browser-template.html");
var report = template.Replace("//[DATA_PLACEHOLDER]", JsonSerializer.Serialize(scenarios));
var scenariosPayload = JsonSerializer.Serialize(new
{
SourceControlInfo = new
{
RepositoryUrl = sourceControlInfo?.RepositoryUrl,
Revision = sourceControlInfo?.Revision,
RepositoryRootDir = repositoryRootDir
},
Scenarios = scenarios
});
var report = template.Replace("//[DATA_PLACEHOLDER]", scenariosPayload);
File.WriteAllText(outputPath, report);
}
}
40 changes: 40 additions & 0 deletions src/NScenario/Reporting/RepoPathResolver.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
using System.IO;
using System.Linq;
using System.Reflection;

namespace NScenario;

internal static class RepoPathResolver
{
public static string? FindRepoRootDirectory(string? fileInTheRepo)
{
if (fileInTheRepo == null)
return null;

var currentDirectory = new DirectoryInfo(Path.GetDirectoryName(fileInTheRepo) ?? string.Empty);

while (currentDirectory is { Exists: true })
{
if (Directory.Exists(Path.Combine(currentDirectory.FullName, ".git")))
{
return currentDirectory.FullName;
}
currentDirectory = currentDirectory.Parent;
}

return null;
}

public static SourceControlInfo? GetSourceControlInfo(Assembly assembly)
{
var repositoryUrl = assembly.GetCustomAttributes<AssemblyMetadataAttribute>().FirstOrDefault(x => x.Key == "RepositoryUrl")?.Value;
var revision = assembly.GetCustomAttributes<AssemblyInformationalVersionAttribute>().FirstOrDefault()?.InformationalVersion?.Split('+').LastOrDefault();

if (string.IsNullOrWhiteSpace(repositoryUrl) == false && string.IsNullOrWhiteSpace(revision) == false)
{
return new SourceControlInfo(repositoryUrl, revision);
}

return null;
}
}
3 changes: 3 additions & 0 deletions src/NScenario/Reporting/SourceControlInfo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
namespace NScenario;

internal record SourceControlInfo(string RepositoryUrl, string Revision);
8 changes: 4 additions & 4 deletions src/NScenario/report-browser-template.html

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion src/nscenario-report-browser/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject",
"inline": "inliner -nm --preserve-comments build/index.html > build/report-browser-template.html"
"inline": "inliner -nm --preserve-comments build/index.html > ../NScenario/report-browser-template.html"
},
"eslintConfig": {
"extends": [
Expand Down
Loading
Loading