Skip to content

Commit

Permalink
nunit#94 Allow users to pass arguments via file(s) instead parameters…
Browse files Browse the repository at this point in the history
… of a command line - fix according to review, support the lazy expansion
  • Loading branch information
Nikolay Pianikov authored and Nikolay Pianikov committed Oct 14, 2016
1 parent 9d1f37c commit 419da93
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 21 deletions.
5 changes: 3 additions & 2 deletions src/NUnitConsole/nunit3-console.tests/CommandLineTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ public class CommandLineTests
[TestCase("--arg1 @ --arg2", "", "--arg1 --arg2", "The file name should not be empty.")]
[TestCase("--arg1 @file1.txt --arg2 @file2.txt", "file1.txt:--fileArg1\n--fileArg2,file2.txt:--fileArg3", "--arg1 --fileArg1 --fileArg2 --arg2 --fileArg3", "")]
[TestCase("--arg1 @file1.txt --arg2", "file1.txt:", "--arg1 --arg2", "")]
[TestCase("--arg1 @file1.txt --arg2", "file1.txt:--fileArg1\n\n\n--fileArg2", "--arg1 --fileArg1 --fileArg2 --arg2", "")]
public void ArgumentsFromFilesTests(string args, string files, string expectedExpandedArgs, string expectedErrorMessages)
{
// Given
Expand All @@ -54,11 +55,11 @@ public void ArgumentsFromFilesTests(string args, string files, string expectedEx
var options = new ConsoleOptions(new DefaultOptionsProviderStub(false), fileSystem);

// When
var actualExpectedExpandedArgs = string.Join(" ", options.Expand(args.Split(' ')));
var actualExpectedExpandedArgs = string.Join(" ", new List<string>(options.Expand(args.Split(' '))).ToArray());

// Then
Assert.AreEqual(expectedExpandedArgs, actualExpectedExpandedArgs);
Assert.AreEqual(options.ErrorMessages, expectedErrors);
Assert.AreEqual(expectedErrors, options.ErrorMessages);
}

[Test]
Expand Down
2 changes: 1 addition & 1 deletion src/NUnitConsole/nunit3-console.tests/VirtualFileSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ internal void SetupFiles(string files)
foreach (var file in files.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries))
{
var fileParts = file.Split(':');
SetupFile(fileParts[0], fileParts[1].Split(new[] { '\n' }, StringSplitOptions.RemoveEmptyEntries));
SetupFile(fileParts[0], fileParts[1].Split(new[] { '\n' }, StringSplitOptions.None));
}
}
}
Expand Down
56 changes: 38 additions & 18 deletions src/NUnitConsole/nunit3-console/ConsoleOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ namespace NUnit.Common
/// </summary>
public class ConsoleOptions : CommandLineOptions
{
private readonly IFileSystem fileSystem;
private readonly IFileSystem _fileSystem;

#region Constructors

Expand All @@ -43,7 +43,7 @@ internal ConsoleOptions(
: base(provider, args)
{
if (fileSystem == null) throw new ArgumentNullException("fileSystem");
this.fileSystem = fileSystem;
this._fileSystem = fileSystem;
}

public ConsoleOptions(params string[] args) : base(args) { }
Expand Down Expand Up @@ -76,9 +76,9 @@ public ConsoleOptions(params string[] args) : base(args) { }

public bool LoadUserProfile { get; private set; }

private int maxAgents = -1;
public int MaxAgents { get { return maxAgents; } }
public bool MaxAgentsSpecified { get { return maxAgents >= 0; } }
private int _maxAgents = -1;
public int MaxAgents { get { return _maxAgents; } }
public bool MaxAgentsSpecified { get { return _maxAgents >= 0; } }

public bool DebugTests { get; private set; }

Expand Down Expand Up @@ -146,7 +146,7 @@ protected override void ConfigureOptions()
v => LoadUserProfile = v != null);

this.Add("agents=", "Specify the maximum {NUMBER} of test assembly agents to run at one time. If not specified, there is no limit.",
v => maxAgents = RequiredInt(v, "--agents"));
v => _maxAgents = RequiredInt(v, "--agents"));

this.Add("debug", "Launch debugger to debug tests.",
v => DebugTests = v != null);
Expand All @@ -165,16 +165,15 @@ protected override void ConfigureOptions()

#endregion

public string[] Expand(string[] args)
public IEnumerable<string> Expand(IEnumerable<string> args)
{
if (args == null) throw new ArgumentNullException("args");

var expandedArgs = new List<string>();
foreach (var arg in args)
{
if (arg.Length == 0 || arg[0] != '@')
{
expandedArgs.Add(arg);
yield return arg;
continue;
}

Expand All @@ -185,31 +184,52 @@ public string[] Expand(string[] args)
continue;
}

if (!fileSystem.FileExists(fileName))
if (!_fileSystem.FileExists(fileName))
{
ErrorMessages.Add("The file \"" + fileName + "\" was not found.");
continue;
}


IEnumerator<string> linesEnumerator;
try
{
foreach (var line in fileSystem.ReadLines(fileName))
linesEnumerator = _fileSystem.ReadLines(fileName).GetEnumerator();
}
catch (Exception)
{
ErrorMessages.Add("Error occurred while opening the file \"" + fileName + "\".");
continue;
}

try
{
while (true)
{
if (string.IsNullOrEmpty(line))
try
{
if (!linesEnumerator.MoveNext())
{
break;
}
}
catch (Exception)
{
continue;
ErrorMessages.Add("Error occurred while reading the file \"" + fileName + "\".");
break;
}

expandedArgs.Add(line);
if (!string.IsNullOrEmpty(linesEnumerator.Current))
{
yield return linesEnumerator.Current;
}
}
}
catch (Exception)
finally
{
ErrorMessages.Add("Error occurred while reading the file \"" + fileName + "\".");
linesEnumerator.Dispose();
}
}

return expandedArgs.ToArray();
}
}
}

0 comments on commit 419da93

Please sign in to comment.