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

Commit 0560286

Browse files
committed
Closes #32: Respect NUGET_PACKAGES env var
Also adds argument support for --specflow-path and --working-directory.
1 parent b9e4578 commit 0560286

File tree

5 files changed

+115
-23
lines changed

5 files changed

+115
-23
lines changed

src/SpecFlow.NetCore/AppConfig.cs

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,12 @@ internal class AppConfig
2020
public const string SpecFlowSection = @" <specFlow>
2121
<" + SpecFlowSectionElement + @" name=""xUnit"" />
2222
</specFlow>";
23+
24+
public static string Content = $@"<?xml version=""1.0"" encoding=""utf-8""?>
25+
<configuration>
26+
{SpecFlowSectionDefinition}
27+
{SpecFlowSection}
28+
</configuration>";
2329
#endregion
2430

2531
public string Path { get; }
@@ -37,17 +43,9 @@ public static AppConfig CreateIn(DirectoryInfo directory)
3743
return config;
3844

3945
WriteLine("Generating app.config.");
40-
41-
// Ignore the strange indentation; it is like that so the final file looks right.
42-
var content = $@"<?xml version=""1.0"" encoding=""utf-8""?>
43-
<configuration>
44-
{SpecFlowSectionDefinition}
45-
{SpecFlowSection}
46-
</configuration>";
47-
48-
WriteLine(content);
46+
WriteLine(Content);
4947
WriteLine("Saving: " + config.Path);
50-
File.WriteAllText(config.Path, content);
48+
File.WriteAllText(config.Path, Content);
5149

5250
return config;
5351
}

src/SpecFlow.NetCore/Args.cs

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
using System;
2+
using System.IO;
3+
using System.Linq;
4+
using static System.Console;
5+
6+
namespace Specflow.NetCore
7+
{
8+
public class Args
9+
{
10+
public const string SpecFlowPathArgName = "--specflow-path";
11+
public const string WorkingDirectoryArgName = "--working-directory";
12+
13+
public string SpecFlowPath { get; }
14+
public DirectoryInfo WorkingDirectory { get; }
15+
16+
public Args(string[] args)
17+
{
18+
WorkingDirectory = new DirectoryInfo(Directory.GetCurrentDirectory());
19+
20+
if (args == null || !args.Any())
21+
return;
22+
23+
// Very basic arg parsing:
24+
// - Assume odd elements are arg names.
25+
// - Assume even elements are arg values.
26+
// - Paths with spaces will probably blow up.
27+
// - Duplicates, last one wins.
28+
29+
for (var i = 0; i < args.Length; i++)
30+
{
31+
if (IsOdd(i))
32+
continue;
33+
34+
if (i + 1 >= args.Length)
35+
throw new Exception("Uneven arguments");
36+
37+
var name = args[i];
38+
var value = args[i + 1];
39+
40+
switch (name)
41+
{
42+
case SpecFlowPathArgName:
43+
SpecFlowPath = value;
44+
break;
45+
46+
case WorkingDirectoryArgName:
47+
if (!Directory.Exists(value))
48+
throw new Exception("Working directory doesn't exist: " + value);
49+
WorkingDirectory = new DirectoryInfo(value);
50+
break;
51+
52+
default:
53+
throw new Exception("Unknown argument: " + name);
54+
}
55+
}
56+
57+
WriteLine("SpecFlowPath: " + SpecFlowPath);
58+
WriteLine("WorkingDirectory: " + WorkingDirectory.FullName);
59+
}
60+
61+
private bool IsOdd(int i)
62+
{
63+
return i % 2 != 0;
64+
}
65+
}
66+
}

src/SpecFlow.NetCore/Fixer.cs

Lines changed: 36 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using System.IO;
44
using System.Linq;
55
using System.Text;
6+
using Specflow.NetCore;
67
using static System.Console;
78

89
namespace SpecFlow.NetCore
@@ -12,23 +13,51 @@ internal class Fixer
1213
private readonly string _specFlowExe;
1314
private FileInfo[] _featureFiles;
1415

15-
public Fixer()
16+
public Fixer(string specFlowPath = null)
1617
{
18+
_specFlowExe = FindSpecFlow(specFlowPath);
19+
WriteLine("Found: " + _specFlowExe);
20+
}
21+
22+
private string FindSpecFlow(string path)
23+
{
24+
if (!string.IsNullOrWhiteSpace(path))
25+
{
26+
if (File.Exists(path))
27+
return path;
28+
29+
throw new Exception("Path to SpecFlow was supplied as an argument, but doesn't exist: " + path);
30+
}
31+
32+
const string relativePathToSpecFlow = @"SpecFlow\2.1.0\tools\specflow.exe";
33+
34+
var nugetPackagesPath = Environment.GetEnvironmentVariable("NUGET_PACKAGES");
35+
36+
if (!string.IsNullOrWhiteSpace(nugetPackagesPath))
37+
{
38+
path = Path.Combine(nugetPackagesPath, relativePathToSpecFlow);
39+
40+
if (File.Exists(path))
41+
return path;
42+
43+
throw new Exception("NUGET_PACKAGES environment variable found, but SpecFlow doesn't exist: " + path);
44+
}
45+
1746
// For full .NET Framework, you can get the user profile with: Environment.GetFolderPath(Environment.SpecialFolder.UserProfile)
1847
// This isn't available yet in .NET Core, so rely on the environment variable for now.
1948
var userProfile = Environment.GetEnvironmentVariable("USERPROFILE");
2049

21-
_specFlowExe = Path.Combine(userProfile, @".nuget\packages\SpecFlow\2.1.0\tools\specflow.exe");
50+
path = Path.Combine(userProfile, ".nuget", "packages", relativePathToSpecFlow);
2251

23-
if (!File.Exists(_specFlowExe))
24-
throw new Exception("Can't find SpecFlow: " + _specFlowExe);
52+
if (File.Exists(path))
53+
return path;
2554

26-
WriteLine("Found: " + _specFlowExe);
55+
throw new Exception($"Can't find SpecFlow: {path}\nTry specifying the path with {Args.SpecFlowPathArgName}.");
2756
}
2857

2958
public void Fix(DirectoryInfo directory)
3059
{
31-
WriteLine("Current directory: " + directory);
60+
WriteLine("Current directory: " + directory.FullName);
3261
_featureFiles = directory.GetFiles("*.feature", SearchOption.AllDirectories);
3362
var missingGeneratedFiles = _featureFiles.Where(f => !File.Exists(f.FullName + ".cs")).ToList();
3463

@@ -103,7 +132,7 @@ private void RunSpecFlow(string csproj)
103132
private void GenerateSpecFlowGlue(DirectoryInfo directory, FileInfo fakeCsproj)
104133
{
105134
AppConfig.CreateIn(directory).Validate();
106-
RunSpecFlow(fakeCsproj.Name);
135+
RunSpecFlow(fakeCsproj.FullName);
107136
}
108137

109138
private FileInfo SaveFakeCsProj(DirectoryInfo directory, FileInfo xproj)

src/SpecFlow.NetCore/Program.cs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
using System;
2-
using System.IO;
2+
using Specflow.NetCore;
33

44
namespace SpecFlow.NetCore
55
{
@@ -9,10 +9,9 @@ public static int Main(string[] args)
99
{
1010
try
1111
{
12-
var path = args.Length == 1 ? args[0] : Directory.GetCurrentDirectory();
13-
var directory = new DirectoryInfo(path);
14-
15-
new Fixer().Fix(directory);
12+
var a = new Args(args);
13+
var fixer = new Fixer(a.SpecFlowPath);
14+
fixer.Fix(a.WorkingDirectory);
1615

1716
PrintUsingColor("SpecFlow fixed.", ConsoleColor.Green);
1817
return 0;

src/SpecFlow.NetCore/project.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"version": "1.0.0-rc4",
2+
"version": "1.0.0-rc5",
33
"description": "Generate tests from SpecFlow feature files inside ASP.NET Core projects (.xproj's).",
44
"authors": [ "stajs" ],
55

0 commit comments

Comments
 (0)