-
Notifications
You must be signed in to change notification settings - Fork 1k
/
GivenAAllowEmptyTelemetry.cs
156 lines (132 loc) · 5.07 KB
/
GivenAAllowEmptyTelemetry.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using FluentAssertions;
using Microsoft.Build.Framework;
using Microsoft.Build.Tasks;
using Microsoft.Build.Utilities;
using Xunit;
#nullable enable
namespace Microsoft.NET.Build.Tasks.UnitTests
{
public class GivenAAllowEmptyTelemetry
{
private static ITaskItem CreateHashItem(string key, string? value = null, bool? hash = null) {
var item = new TaskItem(key);
item.SetMetadata("Value", value);
if(hash is not null) {
item.SetMetadata("Hash", hash.Value.ToString());
}
return item;
}
[Fact]
public void WhenInvokeWithoutValueItSendValueAsNull()
{
var engine = new MockBuildEngine5();
AllowEmptyTelemetry telemetryTask = new AllowEmptyTelemetry
{
BuildEngine = engine,
EventName = "My event name",
EventData = new ITaskItem[] {
CreateHashItem("Property1"),
CreateHashItem("Property2", "")
}
};
telemetryTask.Execute();
engine.Log.Should().Contain("'Property1' = 'null'");
engine.Log.Should().Contain("'Property2' = 'null'");
}
[Fact]
public void WhenInvokeWithDuplicatedEventDataItKeepsTheLastOne()
{
var engine = new MockBuildEngine5();
AllowEmptyTelemetry telemetryTask = new AllowEmptyTelemetry
{
BuildEngine = engine,
EventName = "My event name",
EventData = new ITaskItem[] {
CreateHashItem("Property1", "EE2493A167D24F00996DE7C8E769EAE6"),
CreateHashItem("Property1", "4ADE3D2622CA400B8B95A039DF540037")
}
};
bool retVal = telemetryTask.Execute();
retVal.Should().BeTrue();
engine.Log.Should().NotContain("EE2493A167D24F00996DE7C8E769EAE6");
engine.Log.Should().Contain("4ADE3D2622CA400B8B95A039DF540037");
}
[Fact]
public void WhenInvokeWithNoEventDataItSendsEvents()
{
var engine = new MockBuildEngine5();
AllowEmptyTelemetry telemetryTask = new AllowEmptyTelemetry
{
BuildEngine = engine,
EventName = "My event name"
};
bool retVal = telemetryTask.Execute();
retVal.Should().BeTrue();
engine.Log.Should().Contain(telemetryTask.EventName);
engine.Log.Should().NotContain("Property"); // shouldn't have any logged properties since none were supplied
}
[Fact]
public void WhenHashIsRequestedValueIsHashed()
{
var engine = new MockBuildEngine5();
AllowEmptyTelemetry telemetryTask = new AllowEmptyTelemetry
{
BuildEngine = engine,
EventName = "My event name",
EventData = new ITaskItem[] {
CreateHashItem("Property1", "hi", true),
CreateHashItem("Property2", "hello", false)
}
};
telemetryTask.Execute();
// first property should be hashed
engine.Log.Should().Contain("'Property1' = 'cd6f6854353f68f47c9c93217c5084bc66ea1af918ae1518a2d715a1885e1fcb'");
engine.Log.Should().Contain("'Property2' = 'hello'");
}
/// <summary>
/// Only implement telemetry related API
/// </summary>
private class MockBuildEngine5 : MockBuildEngine, IBuildEngine5
{
private readonly object _lockObj = new object();
private readonly StringBuilder _log = new StringBuilder();
internal string Log
{
get
{
lock (_lockObj)
{
return _log.ToString();
}
}
set
{
if (!string.IsNullOrEmpty(value))
{
throw new ArgumentException("Expected log setter to be used only to reset the log to empty.");
}
lock (_lockObj)
{
_log.Clear();
}
}
}
public void LogTelemetry(string eventName, IDictionary<string, string> properties)
{
string message = $"Received telemetry event '{eventName}'{Environment.NewLine}";
if (properties is not null) {
foreach (string key in properties.Keys)
{
message += $" Property '{key}' = '{properties[key]}'{Environment.NewLine}";
}
}
lock (_lockObj)
{
_log.AppendLine(message);
}
}
}
}
}