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 - implementation allowing to parse argument with spaces and multiple elements per line
  • Loading branch information
Nikolay Pianikov authored and Nikolay Pianikov committed Oct 17, 2016
1 parent 3a5e6ca commit 99892d1
Show file tree
Hide file tree
Showing 9 changed files with 198 additions and 8 deletions.
63 changes: 63 additions & 0 deletions src/NUnitConsole/nunit3-console.tests/ArgumentsFileParserTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
// ***********************************************************************
// Copyright (c) 2011 Charlie Poole
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
// ***********************************************************************

using System;
using NUnit.Framework;

namespace NUnit.ConsoleRunner.Tests
{
[TestFixture]
public class ArgumentsFileParserTests
{
[Test]
[TestCase("--arg", "--arg")]
[TestCase("--arg1 --arg2", "--arg1\n--arg2")]
[TestCase("--arg1\n--arg2", "--arg1\n--arg2")]
[TestCase("", "")]
[TestCase(" ", "")]
[TestCase("--arg1 --arg2", "--arg1\n--arg2")]
[TestCase("\"--arg 1\" --arg2", "--arg 1\n--arg2")]
[TestCase("\"--arg 1\"\n--arg2", "--arg 1\n--arg2")]
[TestCase("--arg1 \"--arg 2\"", "--arg1\n--arg 2")]
[TestCase("\"--arg 1\" \"--arg 2\"", "--arg 1\n--arg 2")]
[TestCase("\"--arg 1\" \"--arg 2\" arg3 \"arg 4\"", "--arg 1\n--arg 2\narg3\narg 4")]
[TestCase("--arg1 \"--arg 2\" arg3 \"arg 4\"", "--arg1\n--arg 2\narg3\narg 4")]
[TestCase("\"--arg 1\" \"--arg 2\" arg3 \"arg 4\"\n\"--arg 1\" \"--arg 2\" arg3 \"arg 4\"", "--arg 1\n--arg 2\narg3\narg 4\n--arg 1\n--arg 2\narg3\narg 4")]
[TestCase("\"--arg\"", "--arg")]
[TestCase("\"--arg 1\"", "--arg 1")]
[TestCase("\"--arg\"1\"", "--arg\n1")]
public void ShouldParseArgumentsFromLines(string linesStr, string expecdetArgsStr)
{
// Given
var parser = new ArgumentsFileParser();
var lines = linesStr.Split(new[] { '\n' }, StringSplitOptions.RemoveEmptyEntries);
var expectedArgs = expecdetArgsStr.Split(new[] { '\n' }, StringSplitOptions.RemoveEmptyEntries);

// When
var actualArgs = parser.Convert(lines);

// Then
Assert.AreEqual(expectedArgs, actualArgs);
}
}
}
9 changes: 4 additions & 5 deletions src/NUnitConsole/nunit3-console.tests/CommandLineTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,11 @@
using System.Reflection;
using NUnit.Common;
using NUnit.Options;
using System.Collections.Generic;
using NUnit.Framework;

namespace NUnit.ConsoleRunner.Tests
{
using System.Collections.Generic;
using Framework;

[TestFixture]
public class CommandLineTests
{
Expand All @@ -52,7 +51,7 @@ public void ArgumentsFromFilesTests(string args, string files, string expectedEx

var expectedErrors = expectedErrorMessages.Split(new [] {'\n'}, StringSplitOptions.RemoveEmptyEntries);

var options = new ConsoleOptions(new DefaultOptionsProviderStub(false), fileSystem);
var options = new ConsoleOptions(new DefaultOptionsProviderStub(false), fileSystem, new SimpleArgumentsFileParser());

// When
var actualExpectedExpandedArgs = string.Join(" ", new List<string>(options.Expand(args.Split(' '))).ToArray());
Expand Down Expand Up @@ -547,7 +546,7 @@ public void ShouldSetTeamCityFlagAccordingToArgsAndDefauls(bool hasTeamcityInCmd
ConsoleOptions options;
if (defaultTeamcity.HasValue)
{
options = new ConsoleOptions(new DefaultOptionsProviderStub(defaultTeamcity.Value), new VirtualFileSystem(), args.ToArray());
options = new ConsoleOptions(new DefaultOptionsProviderStub(defaultTeamcity.Value), new VirtualFileSystem(), new SimpleArgumentsFileParser(), args.ToArray());
}
else
{
Expand Down
35 changes: 35 additions & 0 deletions src/NUnitConsole/nunit3-console.tests/SimpleArgumentsFileParser.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// ***********************************************************************
// Copyright (c) 2011 Charlie Poole
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
// ***********************************************************************

using System.Collections.Generic;

namespace NUnit.ConsoleRunner.Tests
{
internal class SimpleArgumentsFileParser: IConverter<IEnumerable<string>, IEnumerable<string>>
{
public IEnumerable<string> Convert(IEnumerable<string> src)
{
return src;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
<Compile Include="..\ConsoleVersion.cs">
<Link>Properties\ConsoleVersion.cs</Link>
</Compile>
<Compile Include="ArgumentsFileParserTests.cs" />
<Compile Include="ColorConsoleTests.cs" />
<Compile Include="ColorStyleTests.cs" />
<Compile Include="CommandLineTests.cs" />
Expand All @@ -65,6 +66,7 @@
<Compile Include="OutputSpecificationTests.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="ResultReporterTests.cs" />
<Compile Include="SimpleArgumentsFileParser.cs" />
<Compile Include="TestEventHandlerTests.cs" />
<Compile Include="TestNameParserTests.cs" />
<Compile Include="VirtualFileSystem.cs" />
Expand Down
53 changes: 53 additions & 0 deletions src/NUnitConsole/nunit3-console/ArgumentsFileParser.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
// ***********************************************************************
// Copyright (c) 2011 Charlie Poole
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
// ***********************************************************************

using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;

namespace NUnit.ConsoleRunner
{
internal class ArgumentsFileParser: IConverter<IEnumerable<string>, IEnumerable<string>>
{
private static readonly Regex ArgsRegex = new Regex("(\\\"(?<arg>.+?)\\\"|(?<arg>[^\\s\"]+))", RegexOptions.Compiled | RegexOptions.CultureInvariant);

public IEnumerable<string> Convert(IEnumerable<string> src)
{
if (src == null) throw new ArgumentNullException("src");

foreach (var line in src)
{
foreach (Match argMatch in ArgsRegex.Matches(line))
{
if (!argMatch.Success && argMatch.Groups.Count != 1)
{
continue;
}

yield return argMatch.Groups["arg"].Value;
}

}
}
}
}
10 changes: 8 additions & 2 deletions src/NUnitConsole/nunit3-console/ConsoleOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,24 +26,30 @@

namespace NUnit.Common
{
using ConsoleRunner;

/// <summary>
/// ConsoleOptions encapsulates the option settings for
/// the nunit3-console program.
/// </summary>
public class ConsoleOptions : CommandLineOptions
{
private readonly IFileSystem _fileSystem;
private readonly IConverter<IEnumerable<string>, IEnumerable<string>> _argumentsFileParser;

#region Constructors

internal ConsoleOptions(
IDefaultOptionsProvider provider,
IFileSystem fileSystem,
IConverter<IEnumerable<string>, IEnumerable<string>> argumentsFileParser,
params string[] args)
: base(provider, args)
{
if (fileSystem == null) throw new ArgumentNullException("fileSystem");
this._fileSystem = fileSystem;
if (fileSystem == null) throw new ArgumentNullException("argumentsFileParser");
_fileSystem = fileSystem;
_argumentsFileParser = argumentsFileParser;
}

public ConsoleOptions(params string[] args) : base(args) { }
Expand Down Expand Up @@ -191,7 +197,7 @@ public IEnumerable<string> Expand(IEnumerable<string> args)
}


using(var linesEnumerator = _fileSystem.ReadLines(fileName).GetEnumerator())
using (var linesEnumerator = _argumentsFileParser.Convert(_fileSystem.ReadLines(fileName)).GetEnumerator())
{
while (true)
{
Expand Down
30 changes: 30 additions & 0 deletions src/NUnitConsole/nunit3-console/IConverter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// ***********************************************************************
// Copyright (c) 2011 Charlie Poole
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
// ***********************************************************************

namespace NUnit.ConsoleRunner
{
public interface IConverter<in TSrc, out TDst>
{
TDst Convert(TSrc src);
}
}
2 changes: 1 addition & 1 deletion src/NUnitConsole/nunit3-console/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ namespace NUnit.ConsoleRunner
public class Program
{
//static Logger log = InternalTrace.GetLogger(typeof(Runner));
static ConsoleOptions Options = new ConsoleOptions(new DefaultOptionsProvider(), new FileSystem());
static ConsoleOptions Options = new ConsoleOptions(new DefaultOptionsProvider(), new FileSystem(), new ArgumentsFileParser());
private static ExtendedTextWriter _outWriter;

// This has to be lazy otherwise NoColor command line option is not applied correctly
Expand Down
2 changes: 2 additions & 0 deletions src/NUnitConsole/nunit3-console/nunit3-console.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
<Compile Include="..\ConsoleVersion.cs">
<Link>Properties\ConsoleVersion.cs</Link>
</Compile>
<Compile Include="ArgumentsFileParser.cs" />
<Compile Include="ColorConsole.cs" />
<Compile Include="ColorConsoleWriter.cs" />
<Compile Include="ColorStyle.cs" />
Expand All @@ -68,6 +69,7 @@
<Compile Include="ExtendedTextWriter.cs" />
<Compile Include="FileSystem.cs" />
<Compile Include="FrameworkPackageSettings.cs" />
<Compile Include="IConverter.cs" />
<Compile Include="IDefaultOptionsProvider.cs" />
<Compile Include="IFileSystem.cs" />
<Compile Include="Options.cs" />
Expand Down

0 comments on commit 99892d1

Please sign in to comment.