diff --git a/src/NuGet.Core/NuGet.Common/Migrations/Migration1.cs b/src/NuGet.Core/NuGet.Common/Migrations/Migration1.cs index d33b6668e47..e0d86cc4c8e 100644 --- a/src/NuGet.Core/NuGet.Common/Migrations/Migration1.cs +++ b/src/NuGet.Core/NuGet.Common/Migrations/Migration1.cs @@ -156,13 +156,13 @@ private static void FixPermissions(string path, PosixPermissions umask) { PosixPermissions correctedPermissions = permissions.Value.WithUmask(umask); string correctedPermissionsString = correctedPermissions.ToString(); - Exec("chmod", correctedPermissionsString + " " + "\"" + path + "\""); + Exec("chmod", correctedPermissionsString + " " + path.FormatWithDoubleQuotes()); } } internal static PosixPermissions? GetPermissions(string path) { - string output = Exec("ls", "-ld " + "\"" + path + "\""); + string output = Exec("ls", "-ld " + path.FormatWithDoubleQuotes()); if (output == null) { return null; diff --git a/src/NuGet.Core/NuGet.Common/StringExtensions.cs b/src/NuGet.Core/NuGet.Common/StringExtensions.cs new file mode 100644 index 00000000000..6c740ff2b29 --- /dev/null +++ b/src/NuGet.Core/NuGet.Common/StringExtensions.cs @@ -0,0 +1,13 @@ +// Copyright (c) .NET Foundation. All rights reserved. +// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. + +namespace NuGet.Common +{ + internal static class StringExtensions + { + internal static string FormatWithDoubleQuotes(this string s) + { + return s == null ? s : $@"""{s}"""; + } + } +} diff --git a/test/NuGet.Core.Tests/NuGet.Common.Test/StringExtensionsTests.cs b/test/NuGet.Core.Tests/NuGet.Common.Test/StringExtensionsTests.cs new file mode 100644 index 00000000000..c16f34e7b52 --- /dev/null +++ b/test/NuGet.Core.Tests/NuGet.Common.Test/StringExtensionsTests.cs @@ -0,0 +1,35 @@ +// Copyright (c) .NET Foundation. All rights reserved. +// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. + +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Xunit; + +namespace NuGet.Common.Test +{ + public class StringExtensionsTests + { + [Fact] + public void FormatWithDoubleQuotes_WhenNullIsPassedReturnsNull_Success() + { + string actual = null; + string formatted = actual.FormatWithDoubleQuotes(); + Assert.Equal(actual, formatted); + } + + [Theory] + [InlineData("")] + [InlineData("/home/user/NuGet AppData/share")] + [InlineData("/home/user/NuGet/share")] + [InlineData(@"c:\users\NuGet App\Share")] + [InlineData(@"c:\users\NuGet\Share")] + public void FormatWithDoubleQuotes_WhenNonNullStringIsPassedReturnsFormattedString_Success(string actual) + { + string formatted = actual.FormatWithDoubleQuotes(); + Assert.Equal($@"""{actual}""", formatted); + } + } +}