Skip to content

Commit

Permalink
Strip leading .\ from filenames from powerhell
Browse files Browse the repository at this point in the history
Fixes #20 hopefully
  • Loading branch information
timabell committed Nov 20, 2024
1 parent 12cdbda commit 84b0138
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 1 deletion.
4 changes: 3 additions & 1 deletion src/SlnSync.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,9 @@ public string SyncSlnText(string contents, string slnFolder, IEnumerable<string>

var solutionItems = FindOrCreateSolutionFolder(solution.RootProjects, slnFolder);

foreach (var path in paths.Select(Path.TrimEndingDirectorySeparator))
foreach (var path in paths
.Select(Path.TrimEndingDirectorySeparator)
.Select(path => path.StartsWith(@".\") ? path.Remove(0,2) : path)) // remove .\ prefix that powershell adds - https://github.com/timabell/sln-items-sync/issues/20
{
if (File.Exists(path))
{
Expand Down
40 changes: 40 additions & 0 deletions tests/IntegrationTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -603,6 +603,46 @@ public void FindsSlnFileInSameFolder()
// todo: assert message written to stderr
}

/// <summary>
/// This one is a bit different, it drives the logic a layer in from the rest
/// in order to force the input path to have the leading .\ that powershell likes
/// to add.
/// https://github.com/timabell/sln-items-sync/issues/20
/// </summary>
[Fact]
public void PowershellStripsLeadingDotSlash()
{
var slnSync = new SlnSync();
SetupFile("sln-items-file.txt");
const string contents = @"
Microsoft Visual Studio Solution File, Format Version
# Visual Studio Version
VisualStudioVersion =
MinimumVisualStudioVersion =
Project(""{2150E333-8FDC-42A3-9474-1A3956D46DE8}"") = ""SolutionItems"", ""SolutionItems"", ""{5507E0EE-86B8-41FA-813B-8D61B679BA24}""
ProjectSection(SolutionItems) = preProject
EndProjectSection
EndProject
Global
EndGlobal
";

var result = slnSync.SyncSlnText(contents, "SolutionItems", [@".\sln-items-file.txt"]);
result.Should().Be(@"
Microsoft Visual Studio Solution File, Format Version
# Visual Studio Version
VisualStudioVersion =
MinimumVisualStudioVersion =
Project(""{2150E333-8FDC-42A3-9474-1A3956D46DE8}"") = ""SolutionItems"", ""SolutionItems"", ""{5507E0EE-86B8-41FA-813B-8D61B679BA24}""
ProjectSection(SolutionItems) = preProject
sln-items-file.txt = sln-items-file.txt
EndProjectSection
EndProject
Global
EndGlobal
");
}

private string ModifiedSln() => File.ReadAllText(Path.Combine(_testFolder, TargetSlnFile));

private void SetupFilesystem(IEnumerable<string> paths)
Expand Down

0 comments on commit 84b0138

Please sign in to comment.