Skip to content

Commit 29c23c9

Browse files
authored
feat(commons): globalLabels config property (#598)
1 parent 46254bd commit 29c23c9

File tree

13 files changed

+129
-7
lines changed

13 files changed

+129
-7
lines changed

Allure.NUnit/Core/AllureNUnitHelper.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@ internal static TestResult CreateTestResult(ITest test)
108108
GetClassName(test.ClassName)
109109
),
110110
..ModelFunctions.EnumerateEnvironmentLabels(),
111+
..ModelFunctions.EnumerateGlobalLabels(),
111112
]
112113
};
113114
UpdateTestDataFromAllureAttributes(test, testResult);

Allure.Net.Commons.Tests/ConfigurationTests.cs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using System;
33
using System.IO;
44
using Newtonsoft.Json.Linq;
5+
using System.Collections.Generic;
56

67
namespace Allure.Net.Commons.Tests
78
{
@@ -57,5 +58,22 @@ public void ShouldConfigureIndentation()
5758
var json = @"{""allure"":{""indentOutput"": true}}";
5859
Assert.That(new AllureLifecycle(JObject.Parse(json)).AllureConfiguration.IndentOutput, Is.True);
5960
}
61+
62+
[Test]
63+
public void ShouldConfigureGlobalLabels()
64+
{
65+
var json = @"{""allure"":{""globalLabels"":{""foo"":""bar"",""baz"":""qux""}}}";
66+
67+
var config = new AllureLifecycle(JObject.Parse(json)).AllureConfiguration;
68+
69+
Assert.That(
70+
config.GlobalLabels,
71+
Is.EqualTo(new Dictionary<string, string>
72+
{
73+
{ "foo", "bar" },
74+
{ "baz", "qux" },
75+
})
76+
);
77+
}
6078
}
6179
}
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
using NUnit.Framework;
2+
using Allure.Net.Commons.Functions;
3+
using System.Collections.Generic;
4+
using System.Linq;
5+
using Allure.Net.Commons.Configuration;
6+
7+
namespace Allure.Net.Commons.Tests.FunctionTests.ModelFunctionTests;
8+
9+
class GlobalLabelsTests
10+
{
11+
AllureConfiguration config;
12+
13+
[SetUp]
14+
public void SetUpEnvSource()
15+
{
16+
this.config = new();
17+
ModelFunctions.Config = this.config;
18+
}
19+
20+
[TearDown]
21+
public void RemoveEnvSource()
22+
{
23+
ModelFunctions.Config = null;
24+
}
25+
26+
[Test]
27+
public void ShouldNotThrowIfGlobalLabelsNull()
28+
{
29+
this.config.GlobalLabels = null;
30+
31+
Assert.That(
32+
() => ModelFunctions.EnumerateGlobalLabels().ToList(),
33+
Throws.Nothing
34+
);
35+
}
36+
37+
[Test]
38+
public void ShouldIncludeGlobalLabel()
39+
{
40+
this.config.GlobalLabels["foo"] = "bar";
41+
this.config.GlobalLabels["baz"] = "qux";
42+
43+
Assert.That(
44+
ModelFunctions.EnumerateGlobalLabels(),
45+
Is.EquivalentTo(new Label[]
46+
{
47+
new() { name = "foo", value = "bar" },
48+
new() { name = "baz", value = "qux" },
49+
}).UsingPropertiesComparer()
50+
);
51+
}
52+
53+
[Test]
54+
public void ShouldIgnoreNullValues()
55+
{
56+
this.config.GlobalLabels["foo"] = null;
57+
58+
Assert.That(ModelFunctions.EnumerateGlobalLabels(), Is.Empty);
59+
}
60+
61+
[Test]
62+
public void ShouldIgnoreEmptyValues()
63+
{
64+
this.config.GlobalLabels["foo"] = "";
65+
66+
Assert.That(ModelFunctions.EnumerateGlobalLabels(), Is.Empty);
67+
}
68+
69+
[Test]
70+
public void ShouldIgnoreEmptyNames()
71+
{
72+
this.config.GlobalLabels[""] = "foo";
73+
74+
Assert.That(ModelFunctions.EnumerateGlobalLabels(), Is.Empty);
75+
}
76+
}

Allure.Net.Commons/AllureLifecycle.cs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -687,9 +687,6 @@ void UpdateContext(Func<AllureContext, AllureContext> updateFn)
687687
this.Context = updateFn(this.Context);
688688
}
689689

690-
static string CreateUuid() =>
691-
Guid.NewGuid().ToString("N");
692-
693690
static Action<T> Chain<T>(params Action<T>[] actions) => v =>
694691
{
695692
foreach (var action in actions)

Allure.Net.Commons/Configuration/AllureConfiguration.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,11 @@ protected AllureConfiguration(string title, string directory, HashSet<string> li
2020

2121
public string Title { get; init; }
2222
public string Directory { get; init; } = AllureConstants.DEFAULT_RESULTS_FOLDER;
23-
public HashSet<string> Links { get; } = new HashSet<string>();
23+
public HashSet<string> Links { get; } = [];
2424
public List<string> FailExceptions { get; set; }
2525
public bool UseLegacyIds { get; set; } = false;
2626
public bool IndentOutput { get; set; } = false;
27+
public Dictionary<string, string> GlobalLabels { get; set; } = [];
2728

2829
public static AllureConfiguration ReadFromJObject(JObject jObject)
2930
{

Allure.Net.Commons/Functions/ModelFunctions.cs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using System.Diagnostics.CodeAnalysis;
55
using System.Linq;
66
using System.Threading;
7+
using Allure.Net.Commons.Configuration;
78

89
#nullable enable
910

@@ -127,6 +128,15 @@ public static IEnumerable<Label> EnumerateEnvironmentLabels()
127128
}
128129
}
129130

131+
/// <summary>
132+
/// Returns a sequence of labels defined by the <c>globalLabels</c>
133+
/// configuration property.
134+
/// </summary>
135+
public static IEnumerable<Label> EnumerateGlobalLabels() =>
136+
from kv in Config.GlobalLabels ?? []
137+
where !string.IsNullOrEmpty(kv.Key) && !string.IsNullOrEmpty(kv.Value)
138+
select new Label { name = kv.Key, value = kv.Value };
139+
130140
static bool ShouldAddEnvVarAsLabel(
131141
[NotNullWhen(true)] string? name,
132142
[NotNullWhen(true)] string? value
@@ -165,8 +175,17 @@ static IDictionary GetEnvironmentVariables() =>
165175
internal static void SetGetEnvironmentVariables(Func<IDictionary>? getEnvVars) =>
166176
GetEnvironmentVariablesBox.Value = getEnvVars;
167177

178+
internal static AllureConfiguration Config
179+
{
180+
get => ConfigBox.Value ?? AllureLifecycle.Instance.AllureConfiguration;
181+
set => ConfigBox.Value = value;
182+
}
183+
168184
// To decouple from Environment.GetEnvironmentVariable
169185
static AsyncLocal<Func<IDictionary>?> GetEnvironmentVariablesBox { get; set; } = new();
170186

187+
// To decouple from AllureLifecycle.Instance.AllureConfiguration
188+
static AsyncLocal<AllureConfiguration?> ConfigBox { get; set; } = new();
189+
171190
#endregion
172191
}

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,13 @@
4141
"type": "boolean",
4242
"description": "If set to true, the output JSON files (*-result.json, *-container.json) will be indented to be more readable",
4343
"default": false
44+
},
45+
"globalLabels": {
46+
"type": "object",
47+
"description": "An object that defines the global labels, i.e., labels that are added to every test result produced during the test run. Empty values are ignored",
48+
"additionalProperties": {
49+
"type": "string"
50+
}
4451
}
4552
}
4653
}

Allure.Reqnroll.Tests/Integration/TestSetup.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
using System;
33
using System.IO;
44

5-
namespace Allure.SpecFlowPlugin.Tests
5+
namespace Allure.ReqnrollPlugin.Tests
66
{
77
[SetUpFixture]
88
public class TestSetup

Allure.Reqnroll/Functions/MappingFunctions.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -439,6 +439,7 @@ static List<Label> InitializeTestLabels(
439439
) => [
440440
.. CreateDefaultLabels(featureInfo),
441441
.. ModelFunctions.EnumerateEnvironmentLabels(),
442+
.. ModelFunctions.EnumerateGlobalLabels(),
442443
.. scenarioLabels ?? [],
443444
];
444445

Allure.SpecFlow.Tests.Samples/allureConfig.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"$schema": "https://raw.githubusercontent.com/allure-framework/allure-csharp/2.13.0/Allure.SpecFlowPlugin/Schemas/allureConfig.schema.json",
2+
"$schema": "https://raw.githubusercontent.com/allure-framework/allure-csharp/2.13.0/Allure.SpecFlow/Schemas/allureConfig.schema.json",
33
"allure": {
44
"title": "5994A3F7-AF84-46AD-9393-000BB45553CC",
55
"directory": "allure-results",

0 commit comments

Comments
 (0)