11using System ;
2- using System . Collections . Generic ;
32using System . Diagnostics ;
43using System . IO ;
54using System . Linq ;
65using System . Text ;
76using System . Xml . Linq ;
87using System . Xml . XPath ;
8+ using static System . Console ;
99
1010namespace SpecFlow . NetCore
1111{
1212 internal class Fixer
1313 {
14- public readonly string SpecFlowExe ;
15-
1614 private const string AppConfigSpecFlowSectionDefinitionType = "TechTalk.SpecFlow.Configuration.ConfigurationSectionHandler, TechTalk.SpecFlow" ;
1715
1816 private const string AppConfigSpecFlowSectionDefinition = @" <configSections>
@@ -26,53 +24,25 @@ internal class Fixer
2624 <" + AppConfigSpecFlowSectionElement + @" name=""xUnit"" />
2725 </specFlow>" ;
2826
27+ private readonly string _specFlowExe ;
28+
2929 public Fixer ( )
3030 {
31- // TODO: Allow for other DNX install locations.
32- var users = Directory . GetDirectories ( @"C:\Users" ) ;
33-
34- var dnxUser = users . SingleOrDefault ( u =>
35- {
36- try
37- {
38- return Directory . GetDirectories ( u ) . Any ( d => d . Contains ( ".dnx" ) ) ;
39- }
40- catch ( UnauthorizedAccessException )
41- {
42- return false ;
43- }
44- } ) ;
45- if ( dnxUser == null )
46- {
47- throw new Exception ( "Can't find a user with a .dnx folder in his/her home directory" ) ;
48- }
31+ // For full .NET Framework, you can get the user profile with: Environment.GetFolderPath(Environment.SpecialFolder.UserProfile)
32+ // This isn't available yet in .NET Core, so rely on the environment variable for now.
33+ var userProfile = Environment . GetEnvironmentVariable ( "USERPROFILE" ) ;
4934
50- var SpecFlowPaths = new List < string >
51- {
52- Path . Combine ( dnxUser , @".nuget\packages\SpecFlow\2.1.0\tools\specflow.exe" ) ,
53- Path . Combine ( dnxUser , @".dnx\packages\SpecFlow\2.1.0\tools\specflow.exe" ) ,
54- Path . Combine ( dnxUser , @".nuget\packages\SpecFlow\2.0.0\tools\specflow.exe" ) ,
55- Path . Combine ( dnxUser , @".dnx\packages\SpecFlow\2.0.0\tools\specflow.exe" ) ,
56- Path . Combine ( dnxUser , @".nuget\packages\SpecFlow\1.9.0\tools\specflow.exe" ) ,
57- Path . Combine ( dnxUser , @".dnx\packages\SpecFlow\1.9.0\tools\specflow.exe" )
58- } ;
59-
60- for ( var i = 0 ; i < SpecFlowPaths . Count && ! File . Exists ( SpecFlowExe ) ; i ++ )
61- {
62- SpecFlowExe = SpecFlowPaths [ i ] ;
63- }
35+ _specFlowExe = Path . Combine ( userProfile , @".nuget\packages\SpecFlow\2.1.0\tools\specflow.exe" ) ;
6436
65- if ( ! File . Exists ( SpecFlowExe ) )
66- {
67- throw new Exception ( "Can't find SpecFlow: " + SpecFlowExe ) ;
68- }
37+ if ( ! File . Exists ( _specFlowExe ) )
38+ throw new Exception ( "Can't find SpecFlow: " + _specFlowExe ) ;
6939
70- Console . WriteLine ( "Found: " + SpecFlowExe ) ;
40+ WriteLine ( "Found: " + _specFlowExe ) ;
7141 }
7242
7343 public void Fix ( DirectoryInfo directory )
7444 {
75- Console . WriteLine ( "Current directory: " + directory ) ;
45+ WriteLine ( "Current directory: " + directory ) ;
7646 var xproj = GetXproj ( directory ) ;
7747 var fakeCsproj = SaveFakeCsProj ( directory , xproj ) ;
7848 GenerateSpecFlowGlue ( directory , fakeCsproj ) ;
@@ -82,19 +52,19 @@ public void Fix(DirectoryInfo directory)
8252
8353 private void DeleteFakeCsProj ( FileInfo fakeCsproj )
8454 {
85- Console . WriteLine ( "Removing: " + fakeCsproj . FullName ) ;
55+ WriteLine ( "Removing: " + fakeCsproj . FullName ) ;
8656 fakeCsproj . Delete ( ) ;
8757 }
8858
8959 private void FixXunit ( DirectoryInfo directory )
9060 {
91- Console . WriteLine ( "Fixing SpecFlow generated files for xUnit 2" ) ;
61+ WriteLine ( "Fixing SpecFlow generated files for xUnit 2" ) ;
9262
9363 var glueFiles = directory . GetFiles ( "*.feature.cs" , SearchOption . AllDirectories ) ;
9464
9565 foreach ( var glueFile in glueFiles )
9666 {
97- Console . WriteLine ( "Fixed: " + glueFile . FullName ) ;
67+ WriteLine ( "Fixed: " + glueFile . FullName ) ;
9868 var content = File . ReadAllText ( glueFile . FullName ) ;
9969 content = content . Replace ( " : Xunit.IUseFixture<" , " : Xunit.IClassFixture<" ) ;
10070 content = content . Replace ( "[Xunit.Extensions" , "[Xunit" ) ;
@@ -107,13 +77,13 @@ private string SaveSpecFlowConfig()
10777 // Target later version of .NET.
10878 // Credit: http://stackoverflow.com/questions/11363202/specflow-fails-when-trying-to-generate-test-execution-report
10979
110- Console . WriteLine ( "Generating specflow.exe.config." ) ;
80+ WriteLine ( "Generating specflow.exe.config." ) ;
11181
112- var configPath = SpecFlowExe + ".config" ;
82+ var configPath = _specFlowExe + ".config" ;
11383 var content = "<?xml version=\" 1.0\" encoding=\" utf-8\" ?><configuration><startup><supportedRuntime version=\" v4.0.30319\" /></startup></configuration>" ;
114- Console . WriteLine ( content ) ;
84+ WriteLine ( content ) ;
11585
116- Console . WriteLine ( "Saving: " + configPath ) ;
86+ WriteLine ( "Saving: " + configPath ) ;
11787 File . WriteAllText ( configPath , content ) ;
11888
11989 return configPath ;
@@ -126,25 +96,25 @@ private string EnsureAppConfig(DirectoryInfo directory)
12696 if ( File . Exists ( path ) )
12797 return path ;
12898
129- Console . WriteLine ( "Generating app.config." ) ;
99+ WriteLine ( "Generating app.config." ) ;
130100
131101 var content = $@ "<?xml version=""1.0"" encoding=""utf-8""?>
132102<configuration>
133103{ AppConfigSpecFlowSectionDefinition }
134104{ AppConfigSpecFlowSection }
135105</configuration>" ;
136106
137- Console . WriteLine ( content ) ;
107+ WriteLine ( content ) ;
138108
139- Console . WriteLine ( "Saving: " + path ) ;
109+ WriteLine ( "Saving: " + path ) ;
140110 File . WriteAllText ( path , content ) ;
141111
142112 return path ;
143113 }
144114
145115 private void ValidateAppConfig ( string path )
146116 {
147- Console . WriteLine ( "Validating app.config." ) ;
117+ WriteLine ( "Validating app.config." ) ;
148118
149119 // I would rather use the ConfigurationBuilder, but as of beta8 it fails to read an element without
150120 // a value, e.g.: <unitTestProvider name="xUnit" />
@@ -169,15 +139,15 @@ private void RunSpecFlow(string csproj)
169139 {
170140 // Credit: http://www.marcusoft.net/2010/12/specflowexe-and-mstest.html
171141 var arguments = $ "generateall { csproj } /force /verbose";
172- Console . WriteLine ( $ "Calling: { SpecFlowExe } { arguments } ") ;
142+ WriteLine ( $ "Calling: { _specFlowExe } { arguments } ") ;
173143
174144 var p = new Process
175145 {
176146 StartInfo =
177147 {
178148 UseShellExecute = false ,
179149 RedirectStandardOutput = true ,
180- FileName = SpecFlowExe ,
150+ FileName = _specFlowExe ,
181151 Arguments = arguments
182152 }
183153 } ;
@@ -187,15 +157,15 @@ private void RunSpecFlow(string csproj)
187157 var output = p . StandardOutput . ReadToEnd ( ) ;
188158 p . WaitForExit ( ) ;
189159
190- Console . WriteLine ( output ) ;
160+ WriteLine ( output ) ;
191161
192162 if ( output . Contains ( "-> test generation failed" ) )
193163 throw new Exception ( "SpecFlow generation failed (review the output)." ) ;
194164 }
195165
196166 private void DeleteSpecFlowConfig ( string configPath )
197167 {
198- Console . WriteLine ( "Removing the SpecFlow config file." ) ;
168+ WriteLine ( "Removing the SpecFlow config file." ) ;
199169 File . Delete ( configPath ) ;
200170 }
201171
@@ -210,7 +180,7 @@ private void GenerateSpecFlowGlue(DirectoryInfo directory, FileInfo fakeCsproj)
210180
211181 private FileInfo SaveFakeCsProj ( DirectoryInfo directory , FileInfo xproj )
212182 {
213- Console . WriteLine ( "Generating fake csproj." ) ;
183+ WriteLine ( "Generating fake csproj." ) ;
214184
215185 var featureFiles = directory . GetFiles ( "*.feature" , SearchOption . AllDirectories ) ;
216186 var sb = new StringBuilder ( ) ;
@@ -241,10 +211,10 @@ private FileInfo SaveFakeCsProj(DirectoryInfo directory, FileInfo xproj)
241211</Project>" ) ;
242212
243213 var content = sb . ToString ( ) ;
244- Console . WriteLine ( content ) ;
214+ WriteLine ( content ) ;
245215
246216 var csprojPath = xproj . FullName + ".fake.csproj" ;
247- Console . WriteLine ( "Saving: " + csprojPath ) ;
217+ WriteLine ( "Saving: " + csprojPath ) ;
248218 File . WriteAllText ( csprojPath , content ) ;
249219
250220 return new FileInfo ( csprojPath ) ;
@@ -261,7 +231,7 @@ private FileInfo GetXproj(DirectoryInfo directory)
261231 throw new Exception ( "More than one '.xproj' found." ) ;
262232
263233 var xproj = xprojs . Single ( ) ;
264- Console . WriteLine ( "Found: " + xproj . FullName ) ;
234+ WriteLine ( "Found: " + xproj . FullName ) ;
265235
266236 return xproj ;
267237 }
0 commit comments