Skip to content
This repository was archived by the owner on Nov 7, 2019. It is now read-only.

Commit 4fd8d66

Browse files
Soarcstajs
authored andcommitted
Added support for csproj's import tag. (#73)
1 parent 721b896 commit 4fd8d66

File tree

2 files changed

+46
-7
lines changed

2 files changed

+46
-7
lines changed

src/SpecFlow.NetCore/AppConfig.cs

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
using System;
2+
using System.Collections.Generic;
23
using System.IO;
34
using System.Linq;
45
using System.Xml.Linq;
56
using System.Xml.XPath;
67
using static System.Console;
8+
using IOPath = System.IO.Path;
79

810
namespace SpecFlow.NetCore
911
{
@@ -99,11 +101,7 @@ public void Validate(FileInfo csproj)
99101

100102
private static string GetProjectTestRunner(string csproj)
101103
{
102-
var project = XElement.Load(csproj);
103-
var packageReferences = project
104-
.Descendants("PackageReference")
105-
.Select(e => e.Attribute("Include")?.Value)
106-
.ToList();
104+
var packageReferences = GetProjectPackageReferences(csproj).ToList();
107105

108106
if (packageReferences.Contains("xunit"))
109107
{
@@ -125,5 +123,26 @@ private static string GetProjectTestRunner(string csproj)
125123

126124
throw new Exception($"{csproj} does not contain a reference to mstest, xunit or nunit");
127125
}
126+
127+
private static IEnumerable<string> GetProjectPackageReferences(string csproj)
128+
{
129+
var project = XElement.Load(csproj);
130+
var packageReferences = project
131+
.Descendants("PackageReference")
132+
.Select(e => e.Attribute("Include")?.Value);
133+
134+
var csprojdirectory = IOPath.GetDirectoryName(csproj);
135+
var imports = project
136+
.Descendants("Import")
137+
.Select(e => IOPath.Combine(csprojdirectory, e.Attribute("Project").Value));
138+
139+
foreach (var import in imports)
140+
{
141+
packageReferences = packageReferences.Concat(GetProjectPackageReferences(import));
142+
}
143+
144+
return packageReferences;
145+
}
146+
128147
}
129148
}

src/SpecFlow.NetCore/Fixer.cs

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,13 +65,34 @@ private static bool TryGetSpecFlowVersion(FileInfo csproj, out string version)
6565
var node = root.SelectSingleNode("//ItemGroup/PackageReference[translate(@Include, 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz')='specflow']"); // case-insensitive for XPath version 1.0
6666
if (node == null)
6767
{
68+
if (TryGetSpecflowVersionFromImports(csproj, root, out version))
69+
return true;
70+
6871
version = default(string);
6972
return false;
7073
}
7174
version = node.Attributes["Version"].Value;
7275
return true;
7376
}
7477

78+
private static bool TryGetSpecflowVersionFromImports(FileInfo csproj, XmlElement root, out string version)
79+
{
80+
var importNodes = root.SelectNodes("//Import");
81+
foreach (XmlNode import in importNodes)
82+
{
83+
var relativePath = import.Attributes["Project"].Value;
84+
var fullPath = Path.Combine(csproj.DirectoryName, relativePath);
85+
if (!File.Exists(fullPath))
86+
continue;
87+
var importInfo = new FileInfo(fullPath);
88+
if (TryGetSpecFlowVersion(importInfo, out version))
89+
return true;
90+
}
91+
92+
version = default(string);
93+
return false;
94+
}
95+
7596
public IEnumerable<FileInfo> GetFeatureFromLinks(FileInfo csproj)
7697
{
7798
var doc = new XmlDocument();
@@ -126,8 +147,7 @@ private void EnsureSpecFlow(FileInfo csproj)
126147
{
127148
if (string.IsNullOrWhiteSpace(_specFlowExe))
128149
{
129-
string specFlowVersion;
130-
if (!TryGetSpecFlowVersion(csproj, out specFlowVersion))
150+
if (!TryGetSpecFlowVersion(csproj, out string specFlowVersion))
131151
{
132152
throw new XmlException("Could not get SpecFlow version from: " + csproj.FullName);
133153
}

0 commit comments

Comments
 (0)