Skip to content

Commit

Permalink
Preserve line ending characters
Browse files Browse the repository at this point in the history
  • Loading branch information
icnocop committed Nov 4, 2024
1 parent 90570e7 commit 1860775
Show file tree
Hide file tree
Showing 7 changed files with 76 additions and 12 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
## TBD

- Added support for converting projects within sln files on the command line
- Preserve line ending characters

## v1.0.1104.33 (November 4<sup>th</sup>, 2024)

Expand Down
29 changes: 27 additions & 2 deletions src/PackageReferenceVersionToAttribute/ProjectConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -146,11 +146,14 @@ private async Task ConvertAsync(string projectFilePath)
this.fileService.RemoveReadOnlyAttribute(projectFilePath);
}

string detectedLineEnding = DetectLineEnding(projectFilePath);

var settings = new XmlWriterSettings
{
OmitXmlDeclaration = document.Declaration == null, // Preserve the XML declaration if it exists.
Indent = false, // Prevents adding any extra indentation
NewLineHandling = NewLineHandling.Replace,
Indent = false, // Prevents adding any extra indentation.
NewLineHandling = NewLineHandling.Replace, // Preserve the same line endings.
NewLineChars = detectedLineEnding, // Preserve the same line endings.
};

if (this.options.DryRun)
Expand All @@ -169,5 +172,27 @@ private async Task ConvertAsync(string projectFilePath)
document.Save(writer); // Preserves original formatting, avoids extra lines
}
}

public static string DetectLineEnding(string filePath)
{
using var reader = new StreamReader(filePath);
int ch;
char previousChar = '\0';

while ((ch = reader.Read()) != -1)
{
char currentChar = (char)ch;

if (currentChar == '\n')
{
return previousChar == '\r' ? "\r\n" : "\n";
}

previousChar = currentChar;
}

// Default to system line ending if no line ending found
return Environment.NewLine;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -161,8 +161,7 @@ public async Task ExecuteAsync_WithOneSelectedProject_SucceedsAsync()
this.command.Invoke(null);

// Assert
Assert.AreEqual(
"""
string expectedContents = """
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
Expand All @@ -171,12 +170,13 @@ public async Task ExecuteAsync_WithOneSelectedProject_SucceedsAsync()
<PackageReference Include="PackageA" Version="1.2.3" />
</ItemGroup>
</Project>
""",
project.Contents);
""";
Assert.AreEqual(expectedContents, project.Contents);

string backupFilePath = $"{project.Path}.bak";
Assert.IsTrue(File.Exists(backupFilePath));
Assert.AreEqual(Contents, File.ReadAllText(backupFilePath));
string actualContents = File.ReadAllText(backupFilePath);
Assert.AreEqual(Contents, actualContents);
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public ProgramCommand()
{
var inputsArgument = new Argument<string[]>(
name: "inputs",
description: "The project files or wildcard patterns to convert.")
description: "The file paths and patterns which match solution and project files to convert.")
{
Arity = ArgumentArity.OneOrMore,
};
Expand Down
37 changes: 37 additions & 0 deletions src/PackageReferenceVersionToAttributeToolTests/HelpTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// <copyright file="UsageTests.cs" company="Rami Abughazaleh">
// Copyright (c) Rami Abughazaleh. All rights reserved.
// </copyright>

namespace PackageReferenceVersionToAttributeToolTests
{
using System.IO;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using PackageReferenceVersionToAttributeTool;
using static PackageReferenceVersionToAttributeToolTests.ToolRunner.ToolRunner;

/// <summary>
/// Contains unit tests for verifying the expected behavior for the `--help` parameter of the tool.
/// </summary>
[TestClass]
public class HelpTests
{
/// <summary>
/// Verifies that a call to <see cref="Program.Main"/>
/// with the `--help` parameter,
/// returns a successful exit code and displays the command line usage on the console.
/// </summary>
/// <returns>A <see cref="Task"/> representing the asynchronous unit test.</returns>
[TestMethod]
public async Task Run_WithHelpParameter_DisplaysUsage()
{
// Act
var result = await RunToolAsync("--help");

// Assert
Assert.AreEqual(0, result.ExitCode, result.OutputAndError);

var expectedOutput = await File.ReadAllTextAsync("Usage.txt");
Assert.AreEqual(expectedOutput, result.Output.Trim(), result.OutputAndError);
}
}
}
7 changes: 4 additions & 3 deletions src/PackageReferenceVersionToAttributeToolTests/InputTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ Converting PackageReference Version child elements to attributes in the project
result.OutputAndError);
Assert.AreEqual(string.Empty, result.Error.Trim());

Assert.AreEqual(
string expectedContents =
"""
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
Expand All @@ -96,8 +96,9 @@ Converting PackageReference Version child elements to attributes in the project
<PackageReference Include="PackageA" Version="1.2.3" />
</ItemGroup>
</Project>
""",
projectA.ReadAllText());
""";
string actualContents = projectA.ReadAllText();
Assert.AreEqual(expectedContents, actualContents);
}

/// <summary>
Expand Down
2 changes: 1 addition & 1 deletion src/PackageReferenceVersionToAttributeToolTests/Usage.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ Usage:
PackageReferenceVersionToAttributeTool <inputs>... [options]

Arguments:
<inputs> The project files or wildcard patterns to convert.
<inputs> The file paths and patterns which match solution and project files to convert.

Options:
-b, --backup Create a backup of the project files.
Expand Down

0 comments on commit 1860775

Please sign in to comment.