1- using System ;
1+ using Specflow . NetCore ;
2+ using System ;
3+ using System . Collections . Generic ;
24using System . Diagnostics ;
35using System . IO ;
46using System . Linq ;
57using System . Text ;
68using System . Text . RegularExpressions ;
79using System . Xml ;
8- using Specflow . NetCore ;
910using static System . Console ;
1011
1112namespace SpecFlow . NetCore
@@ -38,7 +39,7 @@ private static string FindSpecFlow(string version)
3839 if ( File . Exists ( path ) )
3940 return path ;
4041
41- throw new Exception ( "NUGET_PACKAGES environment variable found, but SpecFlow doesn't exist: " + path ) ;
42+ throw new FileNotFoundException ( "NUGET_PACKAGES environment variable found, but SpecFlow doesn't exist: " + path ) ;
4243 }
4344
4445 // For full .NET Framework, you can get the user profile with: Environment.GetFolderPath(Environment.SpecialFolder.UserProfile)
@@ -50,7 +51,7 @@ private static string FindSpecFlow(string version)
5051 if ( File . Exists ( path ) )
5152 return path ;
5253
53- throw new Exception ( $ "Can't find SpecFlow: { path } \n Try specifying the path with { Args . SpecFlowPathArgName } .") ;
54+ throw new FileNotFoundException ( $ "Can't find SpecFlow: { path } \n Try specifying the path with { Args . SpecFlowPathArgName } .") ;
5455 }
5556
5657 private static bool TryGetSpecFlowVersion ( FileInfo csproj , out string version )
@@ -69,14 +70,36 @@ private static bool TryGetSpecFlowVersion(FileInfo csproj, out string version)
6970 return true ;
7071 }
7172
73+ public IEnumerable < FileInfo > GetFeatureFromLinks ( FileInfo csproj )
74+ {
75+ var doc = new XmlDocument ( ) ;
76+ doc . Load ( csproj . FullName ) ;
77+
78+ var nodes = doc . DocumentElement . SelectNodes ( "//ItemGroup/*[string(@Include) and string(@Link)]" ) ;
79+
80+ foreach ( XmlNode node in nodes )
81+ {
82+ var include = node . Attributes [ "Include" ] . Value ;
83+
84+ if ( File . Exists ( include ) && Path . GetExtension ( include ) . Equals ( ".feature" , StringComparison . OrdinalIgnoreCase ) )
85+ {
86+ yield return new FileInfo ( include ) ;
87+ }
88+ }
89+ }
90+
7291 public void Fix ( DirectoryInfo directory )
7392 {
7493 WriteLine ( "Current directory: " + directory . FullName ) ;
75- _featureFiles = directory . GetFiles ( "*.feature" , SearchOption . AllDirectories ) ;
76- var missingGeneratedFiles = _featureFiles . Where ( f => ! File . Exists ( f . FullName + ".cs" ) ) . ToList ( ) ;
7794
7895 var csproj = GetCsProj ( directory ) ;
7996
97+ _featureFiles = directory . GetFiles ( "*.feature" , SearchOption . AllDirectories )
98+ . Concat ( GetFeatureFromLinks ( csproj ) )
99+ . ToArray ( ) ;
100+
101+ var missingGeneratedFiles = _featureFiles . Where ( f => ! File . Exists ( f . FullName + ".cs" ) ) . ToList ( ) ;
102+
80103 EnsureSpecFlow ( csproj ) ;
81104
82105 var fakeCsproj = SaveFakeCsProj ( directory , csproj ) ;
@@ -104,7 +127,7 @@ private void EnsureSpecFlow(FileInfo csproj)
104127 string specFlowVersion ;
105128 if ( ! TryGetSpecFlowVersion ( csproj , out specFlowVersion ) )
106129 {
107- throw new Exception ( "Could not get SpecFlow version from: " + csproj . FullName ) ;
130+ throw new XmlException ( "Could not get SpecFlow version from: " + csproj . FullName ) ;
108131 }
109132
110133 _specFlowExe = FindSpecFlow ( specFlowVersion ) ;
@@ -116,12 +139,12 @@ private void EnsureSpecFlow(FileInfo csproj)
116139 {
117140 return ;
118141 }
119- throw new Exception ( "Path to SpecFlow was supplied as an argument, but doesn't exist: " + _specFlowExe ) ;
142+ throw new FileNotFoundException ( "Path to SpecFlow was supplied as an argument, but doesn't exist: " + _specFlowExe ) ;
120143 }
121144
122145 private void WarnNotExists ( FileInfo featureFile )
123146 {
124- WriteLine ( $@ "New file generated: { featureFile . FullName } .cs. No tests in { featureFile . Name } will be discovered by 'dotnet test'") ;
147+ WriteLine ( $ "New file generated: { featureFile . FullName } .cs. No tests in { featureFile . Name } will be discovered by 'dotnet test'") ;
125148 }
126149
127150 private void DeleteFakeCsProj ( FileInfo fakeCsproj )
@@ -231,7 +254,7 @@ private FileInfo SaveFakeCsProj(DirectoryInfo directory, FileInfo csproj)
231254 sb . Append ( $@ "
232255 <None Include=""{ featureFile . FullName . Replace ( directory . FullName + Path . DirectorySeparatorChar , "" ) } "">
233256 <Generator>SpecFlowSingleFileGenerator</Generator>
234- <LastGenOutput>{ featureFile . Name } .cs</LastGenOutput>
257+ <LastGenOutput>{ featureFile . FullName } .cs</LastGenOutput>
235258 </None>" ) ;
236259 }
237260
@@ -254,7 +277,7 @@ private FileInfo GetCsProj(DirectoryInfo directory)
254277 var csprojs = directory . GetFiles ( "*.csproj" , SearchOption . TopDirectoryOnly ) ;
255278
256279 if ( csprojs . Length == 0 )
257- throw new Exception ( "Could not find '.csproj'." ) ;
280+ throw new FileNotFoundException ( "Could not find '.csproj'." ) ;
258281
259282 if ( csprojs . Length > 1 )
260283 throw new Exception ( "More than one '.csproj' found." ) ;
0 commit comments