From 419da932e584ef2b55fa189a2e26407d749472e4 Mon Sep 17 00:00:00 2001 From: Nikolay Pianikov Date: Fri, 14 Oct 2016 11:20:49 +0300 Subject: [PATCH] #94 Allow users to pass arguments via file(s) instead parameters of a command line - fix according to review, support the lazy expansion --- .../nunit3-console.tests/CommandLineTests.cs | 5 +- .../nunit3-console.tests/VirtualFileSystem.cs | 2 +- .../nunit3-console/ConsoleOptions.cs | 56 +++++++++++++------ 3 files changed, 42 insertions(+), 21 deletions(-) diff --git a/src/NUnitConsole/nunit3-console.tests/CommandLineTests.cs b/src/NUnitConsole/nunit3-console.tests/CommandLineTests.cs index 253a7c393..3fa692038 100644 --- a/src/NUnitConsole/nunit3-console.tests/CommandLineTests.cs +++ b/src/NUnitConsole/nunit3-console.tests/CommandLineTests.cs @@ -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 @@ -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(options.Expand(args.Split(' '))).ToArray()); // Then Assert.AreEqual(expectedExpandedArgs, actualExpectedExpandedArgs); - Assert.AreEqual(options.ErrorMessages, expectedErrors); + Assert.AreEqual(expectedErrors, options.ErrorMessages); } [Test] diff --git a/src/NUnitConsole/nunit3-console.tests/VirtualFileSystem.cs b/src/NUnitConsole/nunit3-console.tests/VirtualFileSystem.cs index 7b8515ed4..bf3ce8497 100644 --- a/src/NUnitConsole/nunit3-console.tests/VirtualFileSystem.cs +++ b/src/NUnitConsole/nunit3-console.tests/VirtualFileSystem.cs @@ -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)); } } } diff --git a/src/NUnitConsole/nunit3-console/ConsoleOptions.cs b/src/NUnitConsole/nunit3-console/ConsoleOptions.cs index d8a7bdddc..4760b92a1 100644 --- a/src/NUnitConsole/nunit3-console/ConsoleOptions.cs +++ b/src/NUnitConsole/nunit3-console/ConsoleOptions.cs @@ -32,7 +32,7 @@ namespace NUnit.Common /// public class ConsoleOptions : CommandLineOptions { - private readonly IFileSystem fileSystem; + private readonly IFileSystem _fileSystem; #region Constructors @@ -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) { } @@ -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; } @@ -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); @@ -165,16 +165,15 @@ protected override void ConfigureOptions() #endregion - public string[] Expand(string[] args) + public IEnumerable Expand(IEnumerable args) { if (args == null) throw new ArgumentNullException("args"); - var expandedArgs = new List(); foreach (var arg in args) { if (arg.Length == 0 || arg[0] != '@') { - expandedArgs.Add(arg); + yield return arg; continue; } @@ -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 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(); } } }