-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Preserve file encoding in file-based app source files #52055
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 2 commits
92f12aa
9b7a4f7
13dd3b6
c061b9e
643569f
07074b6
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -517,6 +517,77 @@ public void RemoveMultiple() | |
| """)); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Verifies that files without UTF-8 BOM don't get one added when saved. | ||
| /// This is critical for shebang (#!) scripts on Unix-like systems. | ||
| /// </summary> | ||
jjonescz marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| [Fact] | ||
| public void PreservesNoBomEncoding() | ||
| { | ||
| var tempFile = Path.Combine(Path.GetTempPath(), $"test_{Guid.NewGuid()}.cs"); | ||
jjonescz marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| try | ||
| { | ||
| // Create a file without BOM | ||
| var content = "#!/usr/bin/env dotnet run\nConsole.WriteLine();"; | ||
| File.WriteAllText(tempFile, content, new UTF8Encoding(encoderShouldEmitUTF8Identifier: false)); | ||
|
|
||
| // Load, modify, and save | ||
| var sourceFile = SourceFile.Load(tempFile); | ||
| var editor = FileBasedAppSourceEditor.Load(sourceFile); | ||
| editor.Add(new CSharpDirective.Package(default) { Name = "MyPackage", Version = "1.0.0" }); | ||
| editor.SourceFile.Save(); | ||
|
|
||
| // Verify no BOM was added | ||
| var bytes = File.ReadAllBytes(tempFile); | ||
| Assert.False(bytes.Length >= 3 && bytes[0] == 0xEF && bytes[1] == 0xBB && bytes[2] == 0xBF, | ||
| "File should not have UTF-8 BOM"); | ||
|
|
||
| // Verify shebang is still first | ||
| var savedContent = File.ReadAllText(tempFile); | ||
| Assert.StartsWith("#!/usr/bin/env dotnet run", savedContent); | ||
| } | ||
| finally | ||
| { | ||
| if (File.Exists(tempFile)) | ||
| { | ||
| File.Delete(tempFile); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Verifies that files with UTF-8 BOM preserve it when saved. | ||
| /// </summary> | ||
| [Fact] | ||
| public void PreservesBomEncoding() | ||
| { | ||
| var tempFile = Path.Combine(Path.GetTempPath(), $"test_{Guid.NewGuid()}.cs"); | ||
| try | ||
| { | ||
| // Create a file with BOM | ||
| var content = "Console.WriteLine();"; | ||
| File.WriteAllText(tempFile, content, new UTF8Encoding(encoderShouldEmitUTF8Identifier: true)); | ||
|
|
||
| // Load, modify, and save | ||
| var sourceFile = SourceFile.Load(tempFile); | ||
| var editor = FileBasedAppSourceEditor.Load(sourceFile); | ||
| editor.Add(new CSharpDirective.Package(default) { Name = "MyPackage", Version = "1.0.0" }); | ||
| editor.SourceFile.Save(); | ||
|
|
||
| // Verify BOM is still present | ||
| var bytes = File.ReadAllBytes(tempFile); | ||
| Assert.True(bytes.Length >= 3 && bytes[0] == 0xEF && bytes[1] == 0xBB && bytes[2] == 0xBF, | ||
| "File should have UTF-8 BOM"); | ||
| } | ||
| finally | ||
| { | ||
| if (File.Exists(tempFile)) | ||
| { | ||
| File.Delete(tempFile); | ||
| } | ||
| } | ||
| } | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It would be good to verify the behavior when original file uses some other encoding besides UTF-8.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Added in 643569f. The new |
||
|
|
||
| private void Verify( | ||
| string input, | ||
| params ReadOnlySpan<(Action<FileBasedAppSourceEditor> action, string expectedOutput)> verify) | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.