Skip to content

Commit 1860775

Browse files
committed
Preserve line ending characters
1 parent 90570e7 commit 1860775

7 files changed

Lines changed: 76 additions & 12 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
## TBD
44

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

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

src/PackageReferenceVersionToAttribute/ProjectConverter.cs

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -146,11 +146,14 @@ private async Task ConvertAsync(string projectFilePath)
146146
this.fileService.RemoveReadOnlyAttribute(projectFilePath);
147147
}
148148

149+
string detectedLineEnding = DetectLineEnding(projectFilePath);
150+
149151
var settings = new XmlWriterSettings
150152
{
151153
OmitXmlDeclaration = document.Declaration == null, // Preserve the XML declaration if it exists.
152-
Indent = false, // Prevents adding any extra indentation
153-
NewLineHandling = NewLineHandling.Replace,
154+
Indent = false, // Prevents adding any extra indentation.
155+
NewLineHandling = NewLineHandling.Replace, // Preserve the same line endings.
156+
NewLineChars = detectedLineEnding, // Preserve the same line endings.
154157
};
155158

156159
if (this.options.DryRun)
@@ -169,5 +172,27 @@ private async Task ConvertAsync(string projectFilePath)
169172
document.Save(writer); // Preserves original formatting, avoids extra lines
170173
}
171174
}
175+
176+
public static string DetectLineEnding(string filePath)
177+
{
178+
using var reader = new StreamReader(filePath);
179+
int ch;
180+
char previousChar = '\0';
181+
182+
while ((ch = reader.Read()) != -1)
183+
{
184+
char currentChar = (char)ch;
185+
186+
if (currentChar == '\n')
187+
{
188+
return previousChar == '\r' ? "\r\n" : "\n";
189+
}
190+
191+
previousChar = currentChar;
192+
}
193+
194+
// Default to system line ending if no line ending found
195+
return Environment.NewLine;
196+
}
172197
}
173198
}

src/PackageReferenceVersionToAttributeExtensionTests/ConvertPackageReferenceVersionElementsToAttributesCommandTests.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -161,8 +161,7 @@ public async Task ExecuteAsync_WithOneSelectedProject_SucceedsAsync()
161161
this.command.Invoke(null);
162162

163163
// Assert
164-
Assert.AreEqual(
165-
"""
164+
string expectedContents = """
166165
<Project Sdk="Microsoft.NET.Sdk">
167166
<PropertyGroup>
168167
<TargetFramework>net8.0</TargetFramework>
@@ -171,12 +170,13 @@ public async Task ExecuteAsync_WithOneSelectedProject_SucceedsAsync()
171170
<PackageReference Include="PackageA" Version="1.2.3" />
172171
</ItemGroup>
173172
</Project>
174-
""",
175-
project.Contents);
173+
""";
174+
Assert.AreEqual(expectedContents, project.Contents);
176175

177176
string backupFilePath = $"{project.Path}.bak";
178177
Assert.IsTrue(File.Exists(backupFilePath));
179-
Assert.AreEqual(Contents, File.ReadAllText(backupFilePath));
178+
string actualContents = File.ReadAllText(backupFilePath);
179+
Assert.AreEqual(Contents, actualContents);
180180
}
181181

182182
/// <summary>

src/PackageReferenceVersionToAttributeTool/ProgramCommand.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public ProgramCommand()
2020
{
2121
var inputsArgument = new Argument<string[]>(
2222
name: "inputs",
23-
description: "The project files or wildcard patterns to convert.")
23+
description: "The file paths and patterns which match solution and project files to convert.")
2424
{
2525
Arity = ArgumentArity.OneOrMore,
2626
};
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
// <copyright file="UsageTests.cs" company="Rami Abughazaleh">
2+
// Copyright (c) Rami Abughazaleh. All rights reserved.
3+
// </copyright>
4+
5+
namespace PackageReferenceVersionToAttributeToolTests
6+
{
7+
using System.IO;
8+
using Microsoft.VisualStudio.TestTools.UnitTesting;
9+
using PackageReferenceVersionToAttributeTool;
10+
using static PackageReferenceVersionToAttributeToolTests.ToolRunner.ToolRunner;
11+
12+
/// <summary>
13+
/// Contains unit tests for verifying the expected behavior for the `--help` parameter of the tool.
14+
/// </summary>
15+
[TestClass]
16+
public class HelpTests
17+
{
18+
/// <summary>
19+
/// Verifies that a call to <see cref="Program.Main"/>
20+
/// with the `--help` parameter,
21+
/// returns a successful exit code and displays the command line usage on the console.
22+
/// </summary>
23+
/// <returns>A <see cref="Task"/> representing the asynchronous unit test.</returns>
24+
[TestMethod]
25+
public async Task Run_WithHelpParameter_DisplaysUsage()
26+
{
27+
// Act
28+
var result = await RunToolAsync("--help");
29+
30+
// Assert
31+
Assert.AreEqual(0, result.ExitCode, result.OutputAndError);
32+
33+
var expectedOutput = await File.ReadAllTextAsync("Usage.txt");
34+
Assert.AreEqual(expectedOutput, result.Output.Trim(), result.OutputAndError);
35+
}
36+
}
37+
}

src/PackageReferenceVersionToAttributeToolTests/InputTests.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ Converting PackageReference Version child elements to attributes in the project
8686
result.OutputAndError);
8787
Assert.AreEqual(string.Empty, result.Error.Trim());
8888

89-
Assert.AreEqual(
89+
string expectedContents =
9090
"""
9191
<Project Sdk="Microsoft.NET.Sdk">
9292
<PropertyGroup>
@@ -96,8 +96,9 @@ Converting PackageReference Version child elements to attributes in the project
9696
<PackageReference Include="PackageA" Version="1.2.3" />
9797
</ItemGroup>
9898
</Project>
99-
""",
100-
projectA.ReadAllText());
99+
""";
100+
string actualContents = projectA.ReadAllText();
101+
Assert.AreEqual(expectedContents, actualContents);
101102
}
102103

103104
/// <summary>

src/PackageReferenceVersionToAttributeToolTests/Usage.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ Usage:
55
PackageReferenceVersionToAttributeTool <inputs>... [options]
66

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

1010
Options:
1111
-b, --backup Create a backup of the project files.

0 commit comments

Comments
 (0)