Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TestConverter: added check for attachment path - if it starts with file:// scheme #527

Merged
merged 2 commits into from
Jul 7, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/NUnitTestAdapter/TestConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -240,13 +240,19 @@ private VSTestResult GetBasicResult(XmlNode resultNode)
/// <returns>attachments to be added to the test, it will be empty if no attachments are found</returns>
private AttachmentSet ParseAttachments(XmlNode resultNode)
{
const string fileUriScheme = "file://";
var attachmentSet = new AttachmentSet(new Uri(NUnitTestAdapter.ExecutorUri), "Attachments");

foreach (XmlNode attachment in resultNode.SelectNodes("attachments/attachment"))
{
var path = attachment.SelectSingleNode("filePath")?.InnerText ?? string.Empty;
var description = attachment.SelectSingleNode("description")?.InnerText;

if ( !(string.IsNullOrEmpty(path) || path.StartsWith(fileUriScheme, StringComparison.OrdinalIgnoreCase)))
{
path = fileUriScheme + path;
}

try
{
// We only support absolute paths since we dont lookup working directory here
Expand Down
144 changes: 83 additions & 61 deletions src/NUnitTestAdapterTests/Fakes/FakeTestData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
Expand Down Expand Up @@ -46,7 +46,7 @@ private static void FakeTestCase() { } // LineNumber should be this line
<property name='Category' value='super' />
</properties>
<test-case
id='123'
id='123'
name='FakeTestCase'
fullname='NUnit.VisualStudio.TestAdapter.Tests.Fakes.FakeTestData.FakeTestCase'
methodname='FakeTestCase'
Expand All @@ -59,61 +59,61 @@ private static void FakeTestCase() { } // LineNumber should be this line
</test-suite>";

public const string HierarchyTestXml = @"<test-run id='2' name='nUnitClassLibrary.dll' fullname='C:\Users\navb\source\repos\nUnitClassLibrary\nUnitClassLibrary\bin\Debug\nUnitClassLibrary.dll' testcasecount='5'>
<test-suite type='Assembly' id='0-1009' name='nUnitClassLibrary.dll' fullname='C:\Users\navb\source\repos\nUnitClassLibrary\nUnitClassLibrary\bin\Debug\nUnitClassLibrary.dll' runstate='Runnable' testcasecount='5'>
<properties>
<property name='Category' value='AsmCat' />
<property name='_PID' value='6164' />
<property name='_APPDOMAIN' value='domain-71b2ab93-nUnitClassLibrary.dll' />
</properties>
<test-suite type='TestSuite' id='0-1010' name='nUnitClassLibrary' fullname='nUnitClassLibrary' runstate='Runnable' testcasecount='5'>
<test-suite type='TestFixture' id='0-1000' name='Class1' fullname='nUnitClassLibrary.Class1' classname='nUnitClassLibrary.Class1' runstate='Runnable' testcasecount='1'>
<properties>
<property name='Category' value='BaseClass' />
</properties>
<test-case id='0-1001' name='nUnitTest' fullname='nUnitClassLibrary.Class1.nUnitTest' methodname='nUnitTest' classname='nUnitClassLibrary.Class1' runstate='Runnable' seed='113395783'>
<properties>
<property name='Category' value='Base' />
</properties>
</test-case>
</test-suite>
<test-suite type='TestFixture' id='0-1002' name='ClassD' fullname='nUnitClassLibrary.ClassD' classname='nUnitClassLibrary.ClassD' runstate='Runnable' testcasecount='2'>
<properties>
<property name='Category' value='DerivedClass' />
<property name='Category' value='BaseClass' />
</properties>
<test-case id='0-1003' name='dNunitTest' fullname='nUnitClassLibrary.ClassD.dNunitTest' methodname='dNunitTest' classname='nUnitClassLibrary.ClassD' runstate='Runnable' seed='405714082'>
<properties>
<property name='Category' value='Derived' />
</properties>
</test-case>
<test-case id='0-1004' name='nUnitTest' fullname='nUnitClassLibrary.ClassD.nUnitTest' methodname='nUnitTest' classname='nUnitClassLibrary.Class1' runstate='Runnable' seed='1553985978'>
<properties>
<property name='Category' value='Base' />
</properties>
</test-case>
</test-suite>
<test-suite type='TestFixture' id='0-1005' name='NestedClasses' fullname='nUnitClassLibrary.NestedClasses' classname='nUnitClassLibrary.NestedClasses' runstate='Runnable' testcasecount='1'>
<properties>
<property name='Category' value='NS1' />
</properties>
<test-case id='0-1006' name='NC11' fullname='nUnitClassLibrary.NestedClasses.NC11' methodname='NC11' classname='nUnitClassLibrary.NestedClasses' runstate='Runnable' seed='1107340752'>
<properties>
<property name='Category' value='NS11' />
</properties>
</test-case>
</test-suite>
<test-suite type='TestFixture' id='0-1007' name='NestedClasses+NestedClass2' fullname='nUnitClassLibrary.NestedClasses+NestedClass2' classname='nUnitClassLibrary.NestedClasses+NestedClass2' runstate='Runnable' testcasecount='1'>
<properties>
<property name='Category' value='NS2' />
</properties>
<test-case id='0-1008' name='NC21' fullname='nUnitClassLibrary.NestedClasses+NestedClass2.NC21' methodname='NC21' classname='nUnitClassLibrary.NestedClasses+NestedClass2' runstate='Runnable' seed='1823789309'>
<properties>
<property name='Category' value='NS21' />
</properties>
</test-case>
</test-suite>
</test-suite>
</test-suite>
<test-suite type='Assembly' id='0-1009' name='nUnitClassLibrary.dll' fullname='C:\Users\navb\source\repos\nUnitClassLibrary\nUnitClassLibrary\bin\Debug\nUnitClassLibrary.dll' runstate='Runnable' testcasecount='5'>
<properties>
<property name='Category' value='AsmCat' />
<property name='_PID' value='6164' />
<property name='_APPDOMAIN' value='domain-71b2ab93-nUnitClassLibrary.dll' />
</properties>
<test-suite type='TestSuite' id='0-1010' name='nUnitClassLibrary' fullname='nUnitClassLibrary' runstate='Runnable' testcasecount='5'>
<test-suite type='TestFixture' id='0-1000' name='Class1' fullname='nUnitClassLibrary.Class1' classname='nUnitClassLibrary.Class1' runstate='Runnable' testcasecount='1'>
<properties>
<property name='Category' value='BaseClass' />
</properties>
<test-case id='0-1001' name='nUnitTest' fullname='nUnitClassLibrary.Class1.nUnitTest' methodname='nUnitTest' classname='nUnitClassLibrary.Class1' runstate='Runnable' seed='113395783'>
<properties>
<property name='Category' value='Base' />
</properties>
</test-case>
</test-suite>
<test-suite type='TestFixture' id='0-1002' name='ClassD' fullname='nUnitClassLibrary.ClassD' classname='nUnitClassLibrary.ClassD' runstate='Runnable' testcasecount='2'>
<properties>
<property name='Category' value='DerivedClass' />
<property name='Category' value='BaseClass' />
</properties>
<test-case id='0-1003' name='dNunitTest' fullname='nUnitClassLibrary.ClassD.dNunitTest' methodname='dNunitTest' classname='nUnitClassLibrary.ClassD' runstate='Runnable' seed='405714082'>
<properties>
<property name='Category' value='Derived' />
</properties>
</test-case>
<test-case id='0-1004' name='nUnitTest' fullname='nUnitClassLibrary.ClassD.nUnitTest' methodname='nUnitTest' classname='nUnitClassLibrary.Class1' runstate='Runnable' seed='1553985978'>
<properties>
<property name='Category' value='Base' />
</properties>
</test-case>
</test-suite>
<test-suite type='TestFixture' id='0-1005' name='NestedClasses' fullname='nUnitClassLibrary.NestedClasses' classname='nUnitClassLibrary.NestedClasses' runstate='Runnable' testcasecount='1'>
<properties>
<property name='Category' value='NS1' />
</properties>
<test-case id='0-1006' name='NC11' fullname='nUnitClassLibrary.NestedClasses.NC11' methodname='NC11' classname='nUnitClassLibrary.NestedClasses' runstate='Runnable' seed='1107340752'>
<properties>
<property name='Category' value='NS11' />
</properties>
</test-case>
</test-suite>
<test-suite type='TestFixture' id='0-1007' name='NestedClasses+NestedClass2' fullname='nUnitClassLibrary.NestedClasses+NestedClass2' classname='nUnitClassLibrary.NestedClasses+NestedClass2' runstate='Runnable' testcasecount='1'>
<properties>
<property name='Category' value='NS2' />
</properties>
<test-case id='0-1008' name='NC21' fullname='nUnitClassLibrary.NestedClasses+NestedClass2.NC21' methodname='NC21' classname='nUnitClassLibrary.NestedClasses+NestedClass2' runstate='Runnable' seed='1823789309'>
<properties>
<property name='Category' value='NS21' />
</properties>
</test-case>
</test-suite>
</test-suite>
</test-suite>
</test-run>";
#endregion

Expand All @@ -127,7 +127,7 @@ private static void FakeTestCase() { } // LineNumber should be this line
<property name='Category' value='super' />
</properties>
<test-case
id='123'
id='123'
name='FakeTestCase'
fullname='NUnit.VisualStudio.TestAdapter.Tests.Fakes.FakeTestData.FakeTestCase'
methodname='FakeTestCase'
Expand All @@ -143,19 +143,41 @@ private static void FakeTestCase() { } // LineNumber should be this line
<reason>
<message>It passed!</message>
</reason>
<attachments>
<attachment>
<filePath>c:\results\att.log</filePath>
<description>win, no scheme</description>
</attachment>
<attachment>
<filePath>file://c:\results\att.log</filePath>
<description>win, with scheme</description>
</attachment>
<attachment>
<filePath>/home/results/att.log</filePath>
<description>lin, no scheme</description>
</attachment>
<attachment>
<filePath>file:///home/results/att.log</filePath>
<description>lin, with scheme</description>
</attachment>
<attachment>
<filePath></filePath>
<description>empty path</description>
</attachment>
</attachments>
</test-case>
</test-suite>";

public const string DisplayName = "FakeTestCase";

public const string FullyQualifiedName = "NUnit.VisualStudio.TestAdapter.Tests.Fakes.FakeTestData.FakeTestCase";

public static readonly string AssemblyPath =
public static readonly string AssemblyPath =
typeof(FakeTestData).GetTypeInfo().Assembly.ManifestModule.FullyQualifiedName;

public static readonly string CodeFile = Path.Combine(Path.GetDirectoryName(AssemblyPath), @"..\..\..\Fakes\FakeTestData.cs");

// NOTE: If the location of the FakeTestCase method defined
// NOTE: If the location of the FakeTestCase method defined
// above changes, update the value of LineNumber.
public const int LineNumber = 36;

Expand Down
50 changes: 50 additions & 0 deletions src/NUnitTestAdapterTests/TestConverterTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,56 @@ public void CanMakeTestResultFromNUnitTestResult()
Assert.That(testResult.Duration, Is.EqualTo(TimeSpan.FromSeconds(1.234)));
}

#region Attachment tests

[Test]
public void Attachments_CorrectAmountOfConvertedAttachments()
{
var cachedTestCase = testConverter.ConvertTestCase(fakeTestNode);
var fakeResultNode = FakeTestData.GetResultNode();

var testResults = testConverter.GetVSTestResults(fakeResultNode);

var fakeAttachments = fakeResultNode.SelectNodes("attachments/attachment")
.OfType<XmlNode>()
.Where(n => !string.IsNullOrEmpty(n.SelectSingleNode("filePath")?.InnerText))
.ToArray();

var convertedAttachments = testResults.TestResults
.SelectMany(tr => tr.Attachments.SelectMany(ats => ats.Attachments))
.ToArray();

Assert.Multiple(() =>
{
Assert.That(convertedAttachments.Length, Is.GreaterThan(0), "Some converted attachments were expected");
Assert.That(convertedAttachments.Length, Is.EqualTo(fakeAttachments.Length), "Attachments are not converted");
});
}

[Test]
public void Attachments_AllFilePathesStartWithFileScheme()
{
const string fileUriScheme = "file://";
const string errorMessage = "Path must start with file:// uri scheme";

var cachedTestCase = testConverter.ConvertTestCase(fakeTestNode);
var fakeResultNode = FakeTestData.GetResultNode();

var testResults = testConverter.GetVSTestResults(fakeResultNode);

var convertedAttachments = testResults.TestResults
.SelectMany(tr => tr.Attachments.SelectMany(ats => ats.Attachments))
.ToArray();

foreach (var attachment in convertedAttachments)
{
var originalPath = attachment.Uri.OriginalString;
Assert.That(originalPath.LastIndexOf(fileUriScheme), Is.EqualTo(0), errorMessage);
}
}

#endregion Attachment tests

private void CheckTestCase(TestCase testCase)
{
Assert.That(testCase.FullyQualifiedName, Is.EqualTo(FakeTestData.FullyQualifiedName));
Expand Down