Skip to content

Commit d675a6b

Browse files
authored
feat(commons): make JSON pretty printing optional via indentOutput (#573)
* test(commons): make config more testable * feat(commons): add indentOutput option * test(commons): fix fs writer tests cleanup * feat(commons): compress jsons by default * feat(commons): support indentOutput by fs writer * test(specflow, xunit): fix schema link in allureConfig.json * test: set indentOutput to true for test projects
1 parent 25676e9 commit d675a6b

File tree

12 files changed

+104
-15
lines changed

12 files changed

+104
-15
lines changed

Allure.NUnit.Examples/allureConfig.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
"https://github.com/nunit/docs/issues?utf8=✓&q={issue}",
77
"https://example.org/{tms}",
88
"{link}"
9-
]
9+
],
10+
"indentOutput": true
1011
}
1112
}

Allure.Net.Commons.Tests/ConfigurationTests.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ public void ShouldConfigureDefaultValues(string json)
2121
Path.Combine(Environment.CurrentDirectory, AllureConstants.DEFAULT_RESULTS_FOLDER)
2222
));
2323
Assert.That(allureLifecycle.AllureConfiguration.Links, Is.Not.Null);
24+
Assert.That(allureLifecycle.AllureConfiguration.UseLegacyIds, Is.False);
25+
Assert.That(allureLifecycle.AllureConfiguration.IndentOutput, Is.False);
2426
});
2527
}
2628

@@ -48,5 +50,12 @@ public void ShouldConfigureTitle()
4850
var json = @"{""allure"":{""title"": ""hello Allure""}}";
4951
Assert.That(new AllureLifecycle(JObject.Parse(json)).AllureConfiguration.Title, Is.EqualTo("hello Allure"));
5052
}
53+
54+
[Test]
55+
public void ShouldConfigureIndentation()
56+
{
57+
var json = @"{""allure"":{""indentOutput"": true}}";
58+
Assert.That(new AllureLifecycle(JObject.Parse(json)).AllureConfiguration.IndentOutput, Is.True);
59+
}
5160
}
5261
}

Allure.Net.Commons.Tests/FileSystemResultsWriterTests.cs

Lines changed: 57 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,38 @@
66
using System;
77
using System.IO;
88
using Newtonsoft.Json.Linq;
9+
using System.Linq;
10+
using System.Text;
911

1012
namespace Allure.Net.Commons.Tests
1113
{
1214
[TestFixture]
1315
public class FileSystemResultsWriterTests
1416
{
17+
DirectoryInfo tmpDir;
18+
19+
[SetUp]
20+
public void CreateTempDir()
21+
{
22+
this.tmpDir = Directory.CreateTempSubdirectory();
23+
}
24+
25+
[TearDown]
26+
public void DeleteTmpDir()
27+
{
28+
if (this.tmpDir.Exists)
29+
{
30+
try
31+
{
32+
this.tmpDir.Delete(true);
33+
}
34+
catch (Exception e)
35+
{
36+
TestContext.WriteLine(e.ToString());
37+
}
38+
}
39+
}
40+
1541
[Test, Description("Should use temp path if no access to output directory")]
1642
public void ShouldUseTempPathIfNoAccessToResultsDirectory()
1743
{
@@ -25,14 +51,40 @@ public void ShouldUseTempPathIfNoAccessToResultsDirectory()
2551
[Test, Description("Cleanup test")]
2652
public void ShouldCleanupTempResultsFolder()
2753
{
28-
var resultsDirectory = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString());
29-
var json = $"{{\"allure\":{{\"directory\": {JsonConvert.ToString(resultsDirectory)}}}}}";
54+
var json = $"{{\"allure\":{{\"directory\": {JsonConvert.ToString(this.tmpDir.FullName)}}}}}";
3055
var config = AllureConfiguration.ReadFromJObject(JObject.Parse(json));
31-
Directory.CreateDirectory(resultsDirectory);
32-
File.WriteAllText(Path.Combine(resultsDirectory, Path.GetRandomFileName()), "");
56+
File.WriteAllText(Path.Combine(this.tmpDir.FullName, Path.GetRandomFileName()), "");
3357

3458
new FileSystemResultsWriter(config).CleanUp();
35-
Assert.That(Directory.GetFiles(resultsDirectory), Is.Empty);
59+
Assert.That(this.tmpDir.EnumerateFiles(), Is.Empty);
60+
}
61+
62+
[Test]
63+
public void ShouldWriteCompressedJsonByDefault()
64+
{
65+
var config = new AllureConfiguration { Directory = this.tmpDir.FullName };
66+
var writer = new FileSystemResultsWriter(config);
67+
var testResult = new TestResult { name = "foo" };
68+
69+
writer.Write(testResult);
70+
71+
var resultFile = this.tmpDir.EnumerateFiles().Single();
72+
var resultJson = File.ReadAllText(resultFile.FullName, Encoding.UTF8);
73+
Assert.That(resultJson, Does.Not.Match(@"\s"));
74+
}
75+
76+
[Test]
77+
public void ShouldPrettyPrintJsonIfConfigured()
78+
{
79+
var config = new AllureConfiguration { Directory = this.tmpDir.FullName, IndentOutput = true };
80+
var writer = new FileSystemResultsWriter(config);
81+
var testResult = new TestResult { name = "foo" };
82+
83+
writer.Write(testResult);
84+
85+
var resultFile = this.tmpDir.EnumerateFiles().Single();
86+
var resultJson = File.ReadAllText(resultFile.FullName, Encoding.UTF8);
87+
Assert.That(resultJson, Does.Contain(" "));
3688
}
3789
}
3890
}

Allure.Net.Commons/Configuration/AllureConfiguration.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ namespace Allure.Net.Commons.Configuration
66
{
77
public class AllureConfiguration
88
{
9-
private AllureConfiguration()
9+
internal AllureConfiguration()
1010
{
1111
}
1212

@@ -18,11 +18,12 @@ protected AllureConfiguration(string title, string directory, HashSet<string> li
1818
Links = links ?? Links;
1919
}
2020

21-
public string Title { get; }
22-
public string Directory { get; } = AllureConstants.DEFAULT_RESULTS_FOLDER;
21+
public string Title { get; init; }
22+
public string Directory { get; init; } = AllureConstants.DEFAULT_RESULTS_FOLDER;
2323
public HashSet<string> Links { get; } = new HashSet<string>();
2424
public List<string> FailExceptions { get; set; }
2525
public bool UseLegacyIds { get; set; } = false;
26+
public bool IndentOutput { get; set; } = false;
2627

2728
public static AllureConfiguration ReadFromJObject(JObject jObject)
2829
{

Allure.Net.Commons/Schemas/allureConfig.schema.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,11 @@
3636
"type": "boolean",
3737
"description": "Calculate fullNames, historyIds and uuids compatible with ones generated by the versions prior to 2.10.",
3838
"default": false
39+
},
40+
"indentOutput": {
41+
"type": "boolean",
42+
"description": "If set to true, the output JSON files (*-result.json, *-container.json) will be indented to be more readable",
43+
"default": false
3944
}
4045
}
4146
}

Allure.Net.Commons/Writer/FileSystemResultsWriter.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,11 @@ internal FileSystemResultsWriter(AllureConfiguration configuration)
2323
outputDirectory = GetResultsDirectory(configuration.Directory);
2424

2525
serializer.NullValueHandling = NullValueHandling.Ignore;
26-
serializer.Formatting = Formatting.Indented;
26+
if (configuration.IndentOutput)
27+
{
28+
serializer.Formatting = Formatting.Indented;
29+
}
30+
2731
serializer.Converters.Add(
2832
new StringEnumConverter(
2933
new CamelCaseNamingStrategy()

Allure.Reqnroll.Tests.Samples/allureConfig.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
"https://example.org/{issue}",
88
"https://example.org/{tms}"
99
],
10+
"indentOutput": true,
1011
"gherkinPatterns": {
1112
"stepArguments": {
1213
"createFromDataTables": true,

Allure.Reqnroll.Tests/Allure.Reqnroll.Tests.csproj

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,10 @@
1616
<ProjectReference Include="..\Allure.Reqnroll\Allure.Reqnroll.csproj" />
1717
</ItemGroup>
1818

19+
<ItemGroup>
20+
<None Update="allureConfig.json">
21+
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
22+
</None>
23+
</ItemGroup>
24+
1925
</Project>
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"$schema": "https://raw.githubusercontent.com/allure-framework/allure-csharp/2.12.1/Allure.Reqnroll/Schemas/allureConfig.schema.json",
3+
"allure": {
4+
"indentOutput": true
5+
}
6+
}

Allure.SpecFlow.Tests.Samples/allureConfig.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66
"links": [
77
"https://example.org/{issue}",
88
"https://example.org/{tms}"
9-
]
9+
],
10+
"indentOutput": true
1011
},
1112
"specflow": {
1213
"stepArguments": {

0 commit comments

Comments
 (0)