Skip to content

Commit 646afdd

Browse files
committed
Display line numbers and code file for .Net Core tests. Fixes #329
1 parent 0b08641 commit 646afdd

File tree

9 files changed

+17
-39
lines changed

9 files changed

+17
-39
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ TestResults
1414
*.suo
1515
*.user
1616
*.sln.docstates
17+
/.vs/
1718

1819
# Build results
1920
[Dd]ebug/

Source/Machine.VSTestAdapter.Specs/Discovery/BuiltIn/When_discovering_behaviors.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,8 @@ public class When_discovering_specs_using_behaviors : With_DiscoverySetup<BuiltI
1616
"BehaviorSampleSpec".Equals(x.ClassName, StringComparison.Ordinal));
1717
discoveredSpec.ShouldNotBeNull();
1818

19-
#if !NETSTANDARD
2019
discoveredSpec.LineNumber.ShouldEqual(14);
2120
discoveredSpec.CodeFilePath.EndsWith("BehaviorSample.cs", StringComparison.Ordinal);
22-
#endif
2321
};
2422
}
2523
}

Source/Machine.VSTestAdapter.Specs/Discovery/BuiltIn/When_discovering_specs.cs

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,32 +14,26 @@ public class When_discovering_specs : With_DiscoverySetup<BuiltInSpecificationDi
1414
"StandardSpec".Equals(x.ClassName, StringComparison.Ordinal));
1515
discoveredSpec.ShouldNotBeNull();
1616

17-
#if !NETSTANDARD
1817
discoveredSpec.LineNumber.ShouldEqual(14);
1918
discoveredSpec.CodeFilePath.EndsWith("StandardSpec.cs", StringComparison.Ordinal);
20-
#endif
2119
};
2220

2321
It should_find_empty_spec = () => {
2422
MSpecTestCase discoveredSpec = Results.SingleOrDefault(x => "should_be_ignored".Equals(x.SpecificationName, StringComparison.Ordinal) &&
2523
"StandardSpec".Equals(x.ClassName, StringComparison.Ordinal));
2624
discoveredSpec.ShouldNotBeNull();
2725

28-
#if !NETSTANDARD
2926
discoveredSpec.LineNumber.ShouldEqual(20);
3027
discoveredSpec.CodeFilePath.EndsWith("StandardSpec.cs", StringComparison.Ordinal);
31-
#endif
3228
};
3329

3430
It should_find_ignored_spec_but_will_not_find_line_number = () => {
3531
MSpecTestCase discoveredSpec = Results.SingleOrDefault(x => "not_implemented".Equals(x.SpecificationName, StringComparison.Ordinal) &&
3632
"StandardSpec".Equals(x.ClassName, StringComparison.Ordinal));
3733
discoveredSpec.ShouldNotBeNull();
3834

39-
#if !NETSTANDARD
4035
discoveredSpec.LineNumber.ShouldEqual(0);
4136
discoveredSpec.CodeFilePath.ShouldBeNull();
42-
#endif
4337
};
4438
}
4539
}

Source/Machine.VSTestAdapter.Specs/Discovery/BuiltIn/When_discovering_specs_in_nested_types.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,8 @@ public class When_discovering_specs_in_nested_types : With_DiscoverySetup<BuiltI
1616

1717
discoveredSpec.ContextDisplayName.ShouldEqual("Parent NestedSpec");
1818

19-
#if !NETSTANDARD
2019
discoveredSpec.LineNumber.ShouldEqual(14);
2120
discoveredSpec.CodeFilePath.EndsWith("NestedSpecSample.cs", StringComparison.Ordinal);
22-
#endif
2321
};
2422
}
2523
}

Source/Machine.VSTestAdapter.Specs/Discovery/BuiltIn/When_discovering_specs_with_custom_act_assert_delegates.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,8 @@ public class When_discovering_specs_with_custom_act_assert_delegates : With_Disc
1414
"CustomActAssertDelegateSpec".Equals(x.ClassName, StringComparison.Ordinal));
1515
discoveredSpec.ShouldNotBeNull();
1616

17-
#if !NETSTANDARD
1817
discoveredSpec.LineNumber.ShouldEqual(31);
1918
discoveredSpec.CodeFilePath.EndsWith("CustomActAssertDelegateSpec.cs", StringComparison.Ordinal);
20-
#endif
2119
};
2220
}
2321
}

Source/Machine.VSTestAdapter.Specs/Machine.VSTestAdapter.Specs.csproj

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,6 @@
55
<AssemblyName>Machine.VSTestAdapter.Specs</AssemblyName>
66
</PropertyGroup>
77

8-
<PropertyGroup Condition="'$(TargetFramework)'=='netcoreapp1.1'">
9-
<DefineConstants>NETSTANDARD</DefineConstants>
10-
</PropertyGroup>
11-
128
<ItemGroup>
139
<PackageReference Include="Machine.Fakes.Moq" Version="2.8.0" />
1410
<PackageReference Include="Machine.Specifications" Version="0.11.0" />

Source/Machine.VSTestAdapter/Discovery/BuiltIn/SourceCodeLocationFinder.cs

Lines changed: 9 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,23 @@
11
using System;
2+
using System.Collections.Generic;
23
using System.Linq;
34
using System.Reflection;
4-
#if !NETSTANDARD
55
using Mono.Cecil;
66
using Mono.Cecil.Cil;
7-
#endif
87

98
namespace Machine.VSTestAdapter.Discovery.BuiltIn
109
{
1110
public class SourceCodeLocationFinder
1211
{
13-
#if !NETSTANDARD
1412
private readonly Lazy<AssemblyDefinition> assemblyDefinition;
15-
#endif
1613

1714
public SourceCodeLocationFinder(string assemblyFilePath)
1815
{
19-
#if !NETSTANDARD
2016
assemblyDefinition = new Lazy<AssemblyDefinition>(() => { return LoadAssembly(assemblyFilePath); });
21-
#endif
2217
}
2318

2419
public SourceCodeLocationInfo GetFieldLocation(string fullTypeName, string fieldName)
2520
{
26-
#if !NETSTANDARD
2721
TypeDefinition type = Assembly.MainModule.GetType(HandleNestedTypeName(fullTypeName));
2822
if (type == null)
2923
return null;
@@ -33,12 +27,8 @@ public SourceCodeLocationInfo GetFieldLocation(string fullTypeName, string field
3327
return null;
3428

3529
return GetFieldLocationCore(type, fieldName);
36-
#else
37-
return null;
38-
#endif
3930
}
4031

41-
#if !NETSTANDARD
4232
private AssemblyDefinition LoadAssembly(string assemblyFilePath)
4333
{
4434
return AssemblyDefinition.ReadAssembly(assemblyFilePath, new ReaderParameters() {
@@ -67,28 +57,31 @@ private SourceCodeLocationInfo GetFieldLocationCore(TypeDefinition type, string
6757
if (!type.HasMethods)
6858
return null;
6959

70-
7160
MethodDefinition constructorDefinition = type.Methods
7261
.SingleOrDefault(x => x.IsConstructor && !x.Parameters.Any() && x.Name.EndsWith(".ctor", StringComparison.Ordinal));
7362

7463
if (!constructorDefinition.HasBody)
7564
return null;
7665

66+
if (constructorDefinition.DebugInformation == null)
67+
return null;
68+
69+
7770
Instruction instruction = constructorDefinition.Body.Instructions
7871
.Where(x => x.Operand != null &&
7972
x.Operand.GetType().IsAssignableFrom(typeof(FieldDefinition)) &&
8073
((MemberReference)x.Operand).Name == fieldFullName).SingleOrDefault();
8174

8275
while (instruction != null)
8376
{
84-
const int PdbHiddenLine = 0xFEEFEE;
77+
SequencePoint sequencePoint = constructorDefinition.DebugInformation?.GetSequencePoint(instruction);
8578

86-
if (instruction.SequencePoint != null && instruction.SequencePoint.StartLine != PdbHiddenLine)
79+
if (sequencePoint != null && !sequencePoint.IsHidden)
8780
{
8881
return new SourceCodeLocationInfo()
8982
{
90-
CodeFilePath = instruction.SequencePoint.Document.Url,
91-
LineNumber = instruction.SequencePoint.StartLine
83+
CodeFilePath = sequencePoint.Document.Url,
84+
LineNumber = sequencePoint.StartLine
9285
};
9386
}
9487

@@ -97,7 +90,6 @@ private SourceCodeLocationInfo GetFieldLocationCore(TypeDefinition type, string
9790

9891
return null;
9992
}
100-
#endif
10193
}
10294

10395
public class SourceCodeLocationInfo

Source/Machine.VSTestAdapter/Machine.VSTestAdapter.csproj

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,10 @@
2323
<DefineConstants>NETSTANDARD</DefineConstants>
2424
</PropertyGroup>
2525

26+
<ItemGroup>
27+
<PackageReference Include="Mono.Cecil" Version="[0.10-*, 0.11)"/>
28+
</ItemGroup>
29+
2630
<ItemGroup Condition="'$(TargetFramework)'==$(_TargetDotNet)">
2731
<PackageReference Include="Microsoft.TestPlatform.ObjectModel" Version="[15.0.0,16)">
2832
<PrivateAssets>All</PrivateAssets> <!-- Only a reference during build/dev time -->
@@ -32,14 +36,11 @@
3236
<!-- Only a reference during build/dev time -->
3337
</PackageReference>
3438
</ItemGroup>
35-
39+
3640
<ItemGroup Condition="'$(TargetFramework)'=='$(_TargetDotNetFramework)'">
3741
<PackageReference Include="Microsoft.TestPlatform.ObjectModel" Version="[11.0.0,16.0.0)">
3842
<PrivateAssets>All</PrivateAssets> <!-- Only a reference during build/dev time -->
3943
</PackageReference>
40-
<PackageReference Include="Mono.Cecil" Version="[0.9.6.1, 0.10)">
41-
<PrivateAssets>All</PrivateAssets> <!-- Only a reference during build/dev time -->
42-
</PackageReference>
4344
<PackageReference Include="Machine.Specifications" Version="[0.11.0, 1.0)">
4445
<PrivateAssets>All</PrivateAssets> <!-- Only a reference during build/dev time -->
4546
</PackageReference>

appveyor.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
environment:
2-
nuget_version: '2.2.3'
2+
nuget_version: '2.3.0'
33
nuget_prerelease: false
44
# vsix_version: '2.1.0'
55
assembly_version: '2.1.0'
@@ -9,7 +9,7 @@ version: '$(nuget_version)+{build}'
99
deploy:
1010
- provider: GitHub
1111
description: |
12-
* Fixed test discovery for "old-style" .csproj.
12+
* Display line numbers and code file for .Net Core tests (#329)
1313
1414
on:
1515
appveyor_repo_tag: true

0 commit comments

Comments
 (0)