-
Notifications
You must be signed in to change notification settings - Fork 315
/
BlameDataCollectorTests.cs
148 lines (128 loc) · 6.48 KB
/
BlameDataCollectorTests.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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
namespace Microsoft.TestPlatform.AcceptanceTests
{
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.IO;
using System.Text.RegularExpressions;
using System.Xml;
[TestClass]
public class BlameDataCollectorTests : AcceptanceTestBase
{
private readonly string resultsDir;
public BlameDataCollectorTests()
{
this.resultsDir = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString());
}
[TestCleanup]
public void Cleanup()
{
Environment.SetEnvironmentVariable("PROCDUMP_PATH", null);
if (Directory.Exists(this.resultsDir))
{
Directory.Delete(this.resultsDir, true);
}
}
[TestMethod]
[NetFullTargetFrameworkDataSource]
[NetCoreTargetFrameworkDataSource]
public void BlameDataCollectorShouldGiveCorrectTestCaseName(RunnerInfo runnerInfo)
{
AcceptanceTestBase.SetTestEnvironment(this.testEnvironment, runnerInfo);
var assemblyPaths = this.GetAssetFullPath("BlameUnitTestProject.dll");
var arguments = PrepareArguments(assemblyPaths, this.GetTestAdapterPath(), string.Empty, this.FrameworkArgValue, runnerInfo.InIsolationValue);
arguments = string.Concat(arguments, $" /Blame");
arguments = string.Concat(arguments, $" /ResultsDirectory:{resultsDir}");
this.InvokeVsTest(arguments);
this.VaildateOutput("BlameUnitTestProject.UnitTest1.TestMethod2");
}
[TestMethod]
[NetFullTargetFrameworkDataSource]
[NetCoreTargetFrameworkDataSource]
public void BlameDataCollectorShouldOutputDumpFile(RunnerInfo runnerInfo)
{
Environment.SetEnvironmentVariable("PROCDUMP_PATH", Path.Combine(this.testEnvironment.PackageDirectory, @"procdump\0.0.1\bin"));
AcceptanceTestBase.SetTestEnvironment(this.testEnvironment, runnerInfo);
var assemblyPaths = this.BuildMultipleAssemblyPath("SimpleTestProject3.dll").Trim('\"');
var arguments = PrepareArguments(assemblyPaths, this.GetTestAdapterPath(), string.Empty, string.Empty, runnerInfo.InIsolationValue);
arguments = string.Concat(arguments, $" /Blame:CollectDump");
arguments = string.Concat(arguments, $" /ResultsDirectory:{resultsDir}");
arguments = string.Concat(arguments, " /testcasefilter:ExitWithStackoverFlow");
this.InvokeVsTest(arguments);
this.VaildateOutput("SampleUnitTestProject3.UnitTest1.ExitWithStackoverFlow", validateDumpFile: true);
}
[TestMethod]
[NetFullTargetFrameworkDataSource]
[NetCoreTargetFrameworkDataSource]
public void BlameDataCollectorShouldNotOutputDumpFileWhenNoCrashOccurs(RunnerInfo runnerInfo)
{
Environment.SetEnvironmentVariable("PROCDUMP_PATH", Path.Combine(this.testEnvironment.PackageDirectory, @"procdump\0.0.1\bin"));
AcceptanceTestBase.SetTestEnvironment(this.testEnvironment, runnerInfo);
var assemblyPaths = this.BuildMultipleAssemblyPath("SimpleTestProject.dll").Trim('\"');
var arguments = PrepareArguments(assemblyPaths, this.GetTestAdapterPath(), string.Empty, string.Empty, runnerInfo.InIsolationValue);
arguments = string.Concat(arguments, $" /Blame:CollectDump");
arguments = string.Concat(arguments, $" /ResultsDirectory:{resultsDir}");
arguments = string.Concat(arguments, " /testcasefilter:PassingTest");
this.InvokeVsTest(arguments);
StringAssert.DoesNotMatch(this.StdOut, new Regex( @"\.dmp"), "it should not collect a dump, because nothing crashed");
}
[TestMethod]
[NetFullTargetFrameworkDataSource]
[NetCoreTargetFrameworkDataSource]
public void BlameDataCollectorShouldOutputDumpFileWhenNoCrashOccursButCollectAlwaysIsEnabled(RunnerInfo runnerInfo)
{
Environment.SetEnvironmentVariable("PROCDUMP_PATH", Path.Combine(this.testEnvironment.PackageDirectory, @"procdump\0.0.1\bin"));
AcceptanceTestBase.SetTestEnvironment(this.testEnvironment, runnerInfo);
var assemblyPaths = this.BuildMultipleAssemblyPath("SimpleTestProject.dll").Trim('\"');
var arguments = PrepareArguments(assemblyPaths, this.GetTestAdapterPath(), string.Empty, string.Empty, runnerInfo.InIsolationValue);
arguments = string.Concat(arguments, $" /Blame:CollectDump;CollectAlways=True");
arguments = string.Concat(arguments, $" /ResultsDirectory:{resultsDir}");
arguments = string.Concat(arguments, " /testcasefilter:PassingTest");
this.InvokeVsTest(arguments);
StringAssert.Matches(this.StdOut, new Regex(@"\.dmp"), "it should collect dump, even if nothing crashed");
}
private void VaildateOutput(string testName, bool validateDumpFile = false)
{
bool isSequenceAttachmentReceived = false;
bool isDumpAttachmentReceived = false;
bool isValid = false;
this.StdErrorContains(testName);
this.StdOutputContains("Sequence_");
var resultFiles = Directory.GetFiles(this.resultsDir, "*", SearchOption.AllDirectories);
foreach(var file in resultFiles)
{
if (file.Contains("Sequence_"))
{
isSequenceAttachmentReceived = true;
isValid = IsValidXml(file);
}
else if (validateDumpFile && file.Contains(".dmp"))
{
isDumpAttachmentReceived = true;
}
}
Assert.IsTrue(isSequenceAttachmentReceived);
Assert.IsTrue(!validateDumpFile || isDumpAttachmentReceived);
Assert.IsTrue(isValid);
}
private bool IsValidXml(string xmlFilePath)
{
var file = File.OpenRead(xmlFilePath);
var reader = XmlReader.Create(file);
try
{
while (reader.Read())
{
}
file.Dispose();
return true;
}
catch (XmlException)
{
file.Dispose();
return false;
}
}
}
}