-
Notifications
You must be signed in to change notification settings - Fork 952
/
ActionCommandL0.cs
237 lines (216 loc) · 9.48 KB
/
ActionCommandL0.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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
using System;
using System.Collections.Generic;
using System.Linq;
using Xunit;
namespace GitHub.Runner.Common.Tests.Worker
{
public class LoggingCommandL0
{
[Fact]
[Trait("Level", "L0")]
[Trait("Category", "LoggingCommand")]
public void CommandParserTest()
{
//Arrange
using (var hc = new TestHostContext(this))
{
string message;
ActionCommand test;
ActionCommand verify;
HashSet<string> commands = new() { "do-something" };
//##[do-something k1=v1;]msg
message = "##[do-something k1=v1;]msg";
test = new ActionCommand("do-something")
{
Data = "msg",
};
test.Properties.Add("k1", "v1");
Assert.True(ActionCommand.TryParse(message, commands, out verify));
Assert.True(IsEqualCommand(hc, test, verify));
message = "";
test = null;
verify = null;
//##[do-something]
message = "##[do-something]";
test = new ActionCommand("do-something");
Assert.True(ActionCommand.TryParse(message, commands, out verify));
Assert.True(IsEqualCommand(hc, test, verify));
message = "";
test = null;
verify = null;
//##[do-something k1=%3B=%0D=%0A=%5D;]%3B-%0D-%0A-%5D
message = "##[do-something k1=%3B=%0D=%0A=%5D;]%3B-%0D-%0A-%5D";
test = new ActionCommand("do-something")
{
Data = ";-\r-\n-]",
};
test.Properties.Add("k1", ";=\r=\n=]");
Assert.True(ActionCommand.TryParse(message, commands, out verify));
Assert.True(IsEqualCommand(hc, test, verify));
message = "";
test = null;
verify = null;
//##[do-something k1=%253B=%250D=%250A=%255D;]%253B-%250D-%250A-%255D
message = "##[do-something k1=%253B=%250D=%250A=%255D;]%253B-%250D-%250A-%255D";
test = new ActionCommand("do-something")
{
Data = "%3B-%0D-%0A-%5D",
};
test.Properties.Add("k1", "%3B=%0D=%0A=%5D");
Assert.True(ActionCommand.TryParse(message, commands, out verify));
Assert.True(IsEqualCommand(hc, test, verify));
message = "";
test = null;
verify = null;
//##[do-something k1=;k2=;]
message = "##[do-something k1=;k2=;]";
test = new ActionCommand("do-something");
Assert.True(ActionCommand.TryParse(message, commands, out verify));
Assert.True(IsEqualCommand(hc, test, verify));
message = "";
test = null;
verify = null;
//>>> ##[do-something k1=;k2=;]
message = ">>> ##[do-something k1=v1;]msg";
test = new ActionCommand("do-something")
{
Data = "msg",
};
test.Properties.Add("k1", "v1");
Assert.True(ActionCommand.TryParse(message, commands, out verify));
Assert.True(IsEqualCommand(hc, test, verify));
}
}
[Fact]
[Trait("Level", "L0")]
[Trait("Category", "LoggingCommand")]
public void CommandParserV2Test()
{
//Arrange
using (var hc = new TestHostContext(this))
{
string message;
ActionCommand test;
ActionCommand verify;
HashSet<string> commands = new() { "do-something" };
//::do-something k1=v1;]msg
message = "::do-something k1=v1,::msg";
test = new ActionCommand("do-something")
{
Data = "msg",
};
test.Properties.Add("k1", "v1");
Assert.True(ActionCommand.TryParseV2(message, commands, out verify));
Assert.True(IsEqualCommand(hc, test, verify));
message = "";
test = null;
verify = null;
//::do-something::
message = "::do-something::";
test = new ActionCommand("do-something");
Assert.True(ActionCommand.TryParseV2(message, commands, out verify));
Assert.True(IsEqualCommand(hc, test, verify));
message = "";
test = null;
verify = null;
//::do-something k1=;=%2C=%0D=%0A=]=%3A,::;-%0D-%0A-]-:-,
message = "::do-something k1=;=%2C=%0D=%0A=]=%3A,::;-%0D-%0A-]-:-,";
test = new ActionCommand("do-something")
{
Data = ";-\r-\n-]-:-,",
};
test.Properties.Add("k1", ";=,=\r=\n=]=:");
Assert.True(ActionCommand.TryParseV2(message, commands, out verify));
Assert.True(IsEqualCommand(hc, test, verify));
message = "";
test = null;
verify = null;
//::do-something k1=;=%252C=%250D=%250A=]=%253A,::;-%250D-%250A-]-:-,
message = "::do-something k1=;=%252C=%250D=%250A=]=%253A,::;-%250D-%250A-]-:-,";
test = new ActionCommand("do-something")
{
Data = ";-%0D-%0A-]-:-,",
};
test.Properties.Add("k1", ";=%2C=%0D=%0A=]=%3A");
Assert.True(ActionCommand.TryParseV2(message, commands, out verify));
Assert.True(IsEqualCommand(hc, test, verify));
message = "";
test = null;
verify = null;
//::do-something k1=,k2=,::
message = "::do-something k1=,k2=,::";
test = new ActionCommand("do-something");
Assert.True(ActionCommand.TryParseV2(message, commands, out verify));
Assert.True(IsEqualCommand(hc, test, verify));
message = "";
test = null;
verify = null;
//::do-something k1=v1::
message = "::do-something k1=v1::";
test = new ActionCommand("do-something");
test.Properties.Add("k1", "v1");
Assert.True(ActionCommand.TryParseV2(message, commands, out verify));
Assert.True(IsEqualCommand(hc, test, verify));
message = "";
test = null;
verify = null;
// ::do-something k1=v1,::
message = " ::do-something k1=v1,::msg";
test = new ActionCommand("do-something")
{
Data = "msg",
};
test.Properties.Add("k1", "v1");
Assert.True(ActionCommand.TryParseV2(message, commands, out verify));
Assert.True(IsEqualCommand(hc, test, verify));
message = "";
verify = null;
// >>> ::do-something k1=v1,::
message = " >>> ::do-something k1=v1,::msg";
Assert.False(ActionCommand.TryParseV2(message, commands, out verify));
}
}
private bool IsEqualCommand(IHostContext hc, ActionCommand e1, ActionCommand e2)
{
try
{
if (!string.Equals(e1.Command, e2.Command, StringComparison.OrdinalIgnoreCase))
{
hc.GetTrace("CommandEqual").Info("Command 1={0}, Command 2={1}", e1.Command, e2.Command);
return false;
}
if (!string.Equals(e1.Data, e2.Data, StringComparison.OrdinalIgnoreCase) && (!string.IsNullOrEmpty(e1.Data) && !string.IsNullOrEmpty(e2.Data)))
{
hc.GetTrace("CommandEqual").Info("Data 1={0}, Data 2={1}", e1.Data, e2.Data);
return false;
}
if (e1.Properties.Count != e2.Properties.Count)
{
hc.GetTrace("CommandEqual").Info("Logging events contain different numbers of Properties,{0} to {1}", e1.Properties.Count, e2.Properties.Count);
return false;
}
if (!e1.Properties.SequenceEqual(e2.Properties))
{
hc.GetTrace("CommandEqual").Info("Logging events contain different Properties");
hc.GetTrace("CommandEqual").Info("Properties for event 1:");
foreach (var data in e1.Properties)
{
hc.GetTrace("CommandEqual").Info("Key={0}, Value={1}", data.Key, data.Value);
}
hc.GetTrace("CommandEqual").Info("Properties for event 2:");
foreach (var data in e2.Properties)
{
hc.GetTrace("CommandEqual").Info("Key={0}, Value={1}", data.Key, data.Value);
}
return false;
}
}
catch (Exception ex)
{
hc.GetTrace("CommandEqual").Info("Catch Exception during compare:{0}", ex.ToString());
return false;
}
return true;
}
}
}