8000 Test issues when running from a folder containing a space character in its name by hrumhurum · Pull Request #629 · dotnet-script/dotnet-script · GitHub
[go: up one dir, main page]

Skip to content

Test issues when running from a folder containing a space character in its name #629

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Aug 18, 2021
3 changes: 2 additions & 1 deletion src/Dotnet.Script.DependencyModel/Context/DotnetRestorer.cs
8000
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Dotnet.Script.DependencyModel.Environment;
using Dotnet.Script.DependencyModel.Internal;
using Dotnet.Script.DependencyModel.Logging;
using Dotnet.Script.DependencyModel.Process;
using Dotnet.Script.DependencyModel.ProjectSystem;
Expand Down Expand Up @@ -44,7 +45,7 @@ string CreatePackageSourcesArguments()
{
return packageSources.Length == 0
? string.Empty
: packageSources.Select(s => $"-s {s}")
: packageSources.Select(s => $"-s {CommandLine.EscapeArgument(s)}")
.Aggregate((current, next) => $"{current} {next}");
}

Expand Down
93 changes: 93 additions & 0 deletions src/Dotnet.Script.DependencyModel/Internal/CommandLine.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
using System;
using System.Text;
using System.Text.RegularExpressions;

namespace Dotnet.Script.DependencyModel.Internal
{
/// <summary>
/// <para>
/// Performs operations on <see cref="System.String"/> instances that contain command line information.
/// </para>
/// <para>
/// Tip: a ready-to-use package with this functionality is available at https://www.nuget.org/packages/Gapotchenko.FX.Diagnostics.CommandLine.
/// </para>
/// </summary>
/// <summary>
/// Available
/// </summary>
static class CommandLine
{
/// <summary>
/// Escapes and optionally quotes a command line argument.
/// </summary>
/// <param name="value">The command line argument.</param>
/// <returns>The escaped and optionally quoted command line argument.</returns>
public static string EscapeArgument(string value)
{
if (value == null)
return null;

int length = value.Length;
if (length == 0)
return string.Empty;

var sb = new StringBuilder();
Escape.AppendQuotedText(sb, value);

if (sb.Length == length)
return value;

return sb.ToString();
}

static class Escape
{
public static void AppendQuotedText(StringBuilder sb, string text)
{
bool quotingRequired = IsQuotingRequired(text);
if (quotingRequired)
sb.Append('"');

int numberOfQuotes = 0;
for (int i = 0; i < text.Length; i++)
{
if (text[i] == '"')
numberOfQuotes++;
}

if (numberOfQuotes > 0)
{
if ((numberOfQuotes % 2) != 0)
throw new Exception("Command line parameter cannot contain an odd number of double quotes.");
text = text.Replace("\\\"", "\\\\\"").Replace("\"", "\\\"");
}

sb.Append(text);

if (quotingRequired && text.EndsWith("\\"))
sb.Append('\\');

if (quotingRequired)
sb.Append('"');
}

static bool IsQuotingRequired(string parameter) =>
!AllowedUnquotedRegex.IsMatch(parameter) ||
DefinitelyNeedQuotesRegex.IsMatch(parameter);

static Regex m_CachedAllowedUnquotedRegex;

static Regex AllowedUnquotedRegex =>
m_CachedAllowedUnquotedRegex ??= new Regex(
@"^[a-z\\/:0-9\._\-+=]*$",
RegexOptions.IgnoreCase | RegexOptions.CultureInvariant);

static Regex m_CachedDefinitelyNeedQuotesRegex;

static Regex DefinitelyNeedQuotesRegex =>
m_CachedDefinitelyNeedQuotesRegex ??= new Regex(
"[|><\\s,;\"]+",
RegexOptions.CultureInvariant);
}
}
}
4 changes: 2 additions & 2 deletions src/Dotnet.Script.Tests/PackageSourceTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public void ShouldHandleSpecifyingPackageSource()
{
var fixture = "ScriptPackage/WithNoNuGetConfig";
var pathToScriptPackages = ScriptPackagesFixture.GetPathToPackagesFolder();
var result = ScriptTestRunner.Default.ExecuteFixture(fixture, $"--no-cache -s {pathToScriptPackages}");
var result = ScriptTestRunner.Default.ExecuteFixture(fixture, $"--no-cache -s \"{pathToScriptPackages}\"");
Assert.Contains("Hello", result.output);
Assert.Equal(0, result.exitCode);
}
Expand All @@ -27,7 +27,7 @@ public void ShouldHandleSpecifyingPackageSourceWhenEvaluatingCode()
{
var pathToScriptPackages = ScriptPackagesFixture.GetPathToPackagesFolder();
var code = @"#load \""nuget:ScriptPackageWithMainCsx,1.0.0\"" SayHello();";
var result = ScriptTestRunner.Default.Execute($"--no-cache -s {pathToScriptPackages} eval \"{code}\"");
var result = ScriptTestRunner.Default.Execute($"--no-cache -s \"{pathToScriptPackages}\" eval \"{code}\"");
Assert.Contains("Hello", result.output);
Assert.Equal(0, result.exitCode);
}
Expand Down
2 changes: 1 addition & 1 deletion src/Dotnet.Script.Tests/ScriptExecutionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ public void ShouldHandleIssue181()
[Fact]
public void ShouldHandleIssue189()
{
var result = ScriptTestRunner.Default.Execute(Path.Combine(TestPathUtils.GetPathToTestFixtureFolder("Issue189"), "SomeFolder", "Script.csx"));
var result = ScriptTestRunner.Default.Execute($"\"{Path.Combine(TestPathUtils.GetPathToTestFixtureFolder("Issue189"), "SomeFolder", "Script.csx")}\"");
Assert.Contains("Newtonsoft.Json.JsonConvert", result.output);
}

Expand Down
4 changes: 2 additions & 2 deletions src/Dotnet.Script.Tests/ScriptPackagesFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,12 @@ private void BuildScriptPackages()
if (_scriptEnvironment.IsWindows)
{
command = pathtoNuget430;
var result = ProcessHelper.RunAndCaptureOutput(command, $"pack {specFile} -OutputDirectory {pathToPackagesOutputFolder}");
var result = ProcessHelper.RunAndCaptureOutput(command, $"pack \"{specFile}\" -OutputDirectory \"{pathToPackagesOutputFolder}\"");
}
else
{
command = "mono";
var result = ProcessHelper.RunAndCaptureOutput(command, $"{pathtoNuget430} pack {specFile} -OutputDirectory {pathToPackagesOutputFolder}");
var result = ProcessHelper.RunAndCaptureOutput(command, $"\"{pathtoNuget430}\" pack \"{specFile}\" -OutputDirectory \"{pathToPackagesOutputFolder}\"");
}

}
Expand Down
6 changes: 3 additions & 3 deletions src/Dotnet.Script.Tests/ScriptTestRunner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,15 @@ public int ExecuteInProcess(string arguments = null)
public (string output, int exitCode) ExecuteFixture(string fixture, string arguments = null, string workingDirectory = null)
{
var pathToFixture = TestPathUtils.GetPathToTestFixture(fixture);
var result = ProcessHelper.RunAndCaptureOutput("dotnet", GetDotnetScriptArguments($"{pathToFixture} {arguments}"), workingDirectory);
var result = ProcessHelper.RunAndCaptureOutput("dotnet", GetDotnetScriptArguments($"\"{pathToFixture}\" {arguments}"), workingDirectory);
return result;
}

public (string output, int exitcode) ExecuteWithScriptPackage(string fixture, string arguments = null, string workingDirectory = null)
{
var pathToScriptPackageFixtures = TestPathUtils.GetPathToTestFixtureFolder("ScriptPackage");
var pathToFixture = Path.Combine(pathToScriptPackageFixtures, fixture, $"{fixture}.csx");
return ProcessHelper.RunAndCaptureOutput("dotnet", GetDotnetScriptArguments($"{pathToFixture} {arguments}"), workingDirectory);
return ProcessHelper.RunAndCaptureOutput("dotnet", GetDotnetScriptArguments($"\"{pathToFixture}\" {arguments}"), workingDirectory);
}

public int ExecuteFixtureInProcess(string fixture, string arguments = null)
Expand Down Expand Up @@ -88,7 +88,7 @@ private string GetDotnetScriptArguments(string arguments)
configuration = "Release";
#endif

var allArgs = $"exec {Path.Combine(Directory.GetCurrentDirectory(), "..", "..", "..", "..", "Dotnet.Script", "bin", configuration, _scriptEnvironment.TargetFramework, "dotnet-script.dll")} {arguments}";
var allArgs = $"exec \"{Path.Combine(Directory.GetCurrentDirectory(), "..", "..", "..", "..", "Dotnet.Script", "bin", configuration, _scriptEnvironment.TargetFramework, "dotnet-script.dll")}\" {arguments}";

return allArgs;
}
Expand Down
0