Skip to content

Commit

Permalink
feedback (added extension method to format string with double quotes)
Browse files Browse the repository at this point in the history
  • Loading branch information
Kartheek Penagamuri committed Oct 21, 2022
1 parent fcd3951 commit 43e303d
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/NuGet.Core/NuGet.Common/Migrations/Migration1.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
13 changes: 13 additions & 0 deletions src/NuGet.Core/NuGet.Common/StringExtensions.cs
Original file line number Diff line number Diff line change
@@ -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}""";
}
}
}
35 changes: 35 additions & 0 deletions test/NuGet.Core.Tests/NuGet.Common.Test/StringExtensionsTests.cs
Original file line number Diff line number Diff line change
@@ -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);
}
}
}

0 comments on commit 43e303d

Please sign in to comment.