Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
93 changes: 93 additions & 0 deletions Assets/Tests/Editor/SkillFileContentNormalizerTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
using System.Linq;
using System.Text;

using NUnit.Framework;

using io.github.hatayama.UnityCliLoop.Infrastructure;

namespace io.github.hatayama.UnityCliLoop.Tests.Editor
{
/// <summary>
/// Verifies deterministic line-ending normalization for generated skill files.
/// </summary>
[TestFixture]
public class SkillFileContentNormalizerTests
{
// Tests that CRLF and lone CR line endings become LF in single-byte text files.
[Test]
public void NormalizeSkillFileContent_WhenSingleByteTextUsesCarriageReturns_ReturnsLfContent()
{
byte[] sourceBytes = Encoding.UTF8.GetBytes("line1\r\nline2\rline3\n");
byte[] expectedBytes = Encoding.UTF8.GetBytes("line1\nline2\nline3\n");

byte[] actualBytes = SkillFileContentNormalizer.NormalizeSkillFileContent("reference.md", sourceBytes);

Assert.That(actualBytes, Is.EqualTo(expectedBytes));
}

// Tests that UTF-16 little-endian text keeps its BOM and encoding while line endings become LF.
[Test]
public void NormalizeSkillFileContent_WhenUtf16LittleEndianTextUsesCrlf_PreservesEncoding()
{
byte[] sourceBytes = Encoding.Unicode.GetPreamble()
.Concat(Encoding.Unicode.GetBytes("line1\r\nline2\r\n"))
.ToArray();
byte[] expectedBytes = Encoding.Unicode.GetPreamble()
.Concat(Encoding.Unicode.GetBytes("line1\nline2\n"))
.ToArray();

byte[] actualBytes = SkillFileContentNormalizer.NormalizeSkillFileContent("install.ps1", sourceBytes);

Assert.That(actualBytes, Is.EqualTo(expectedBytes));
}

// Tests that UTF-16 big-endian text keeps its BOM and encoding while line endings become LF.
[Test]
public void NormalizeSkillFileContent_WhenUtf16BigEndianTextUsesCrlf_PreservesEncoding()
{
byte[] sourceBytes = Encoding.BigEndianUnicode.GetPreamble()
.Concat(Encoding.BigEndianUnicode.GetBytes("line1\r\nline2\r\n"))
.ToArray();
byte[] expectedBytes = Encoding.BigEndianUnicode.GetPreamble()
.Concat(Encoding.BigEndianUnicode.GetBytes("line1\nline2\n"))
.ToArray();

byte[] actualBytes = SkillFileContentNormalizer.NormalizeSkillFileContent("install.ps1", sourceBytes);

Assert.That(actualBytes, Is.EqualTo(expectedBytes));
}

// Tests that files outside the text extension allowlist keep their original bytes.
[Test]
public void NormalizeSkillFileContent_WhenExtensionIsNotText_ReturnsOriginalContent()
{
byte[] sourceBytes = Encoding.UTF8.GetBytes("line1\r\nline2\r\n");

byte[] actualBytes = SkillFileContentNormalizer.NormalizeSkillFileContent("image.png", sourceBytes);

Assert.That(actualBytes, Is.EqualTo(sourceBytes));
}

// Tests that NUL-containing binary data is not rewritten even when its extension is textual.
[Test]
public void NormalizeSkillFileContent_WhenTextExtensionContainsBinaryData_ReturnsOriginalContent()
{
byte[] sourceBytes = { 0x41, 0x42, 0x00, 0x43, 0x0D, 0x44 };

byte[] actualBytes = SkillFileContentNormalizer.NormalizeSkillFileContent("reference.md", sourceBytes);

Assert.That(actualBytes, Is.EqualTo(sourceBytes));
}

// Tests that LF-only text content remains byte-identical.
[Test]
public void NormalizeSkillFileContent_WhenTextAlreadyUsesLf_ReturnsOriginalContent()
{
byte[] sourceBytes = Encoding.UTF8.GetBytes("line1\nline2\n");

byte[] actualBytes = SkillFileContentNormalizer.NormalizeSkillFileContent("reference.md", sourceBytes);

Assert.That(actualBytes, Is.EqualTo(sourceBytes));
}
}
}
11 changes: 11 additions & 0 deletions Assets/Tests/Editor/SkillFileContentNormalizerTests.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 0 additions & 17 deletions Assets/Tests/Editor/SkillInstallLayoutTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Newtonsoft.Json.Linq;
Expand Down Expand Up @@ -388,22 +387,6 @@ public void GetToolSettingsCatalogForProjectRoot_WhenSkillIsInternal_HidesToolFr
Assert.That(registeredToolNames, Does.Contain("public-tool"));
}

// Tests that PowerShell scripts keep their source encoding while line endings are normalized.
[Test]
public void NormalizeSkillFileContent_WhenPowerShellScriptUsesUtf16LittleEndian_PreservesEncoding()
{
byte[] sourceBytes = Encoding.Unicode.GetPreamble()
.Concat(Encoding.Unicode.GetBytes("line1\r\nline2\r\n"))
.ToArray();
byte[] expectedBytes = Encoding.Unicode.GetPreamble()
.Concat(Encoding.Unicode.GetBytes("line1\nline2\n"))
.ToArray();

byte[] actualBytes = SkillInstallLayout.NormalizeSkillFileContent("install.ps1", sourceBytes);

Assert.That(actualBytes, Is.EqualTo(expectedBytes));
}

// Tests that skill setup file scans share the same generated-file exclusion behavior.
[Test]
public void IsExcludedSkillFile_WhenGeneratedSkillFileNameIsPassed_ReturnsTrue()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,193 @@
using System;
using System.Collections.Generic;
using System.IO;

using UnityEngine;

namespace io.github.hatayama.UnityCliLoop.Infrastructure
{
/// <summary>
/// Normalizes line endings in generated text skill files without changing their encoding.
/// </summary>
internal static class SkillFileContentNormalizer
{
private const byte Utf16LittleEndianBomFirstByte = 0xFF;
private const byte Utf16LittleEndianBomSecondByte = 0xFE;
private const byte Utf16BigEndianBomFirstByte = 0xFE;
private const byte Utf16BigEndianBomSecondByte = 0xFF;
private const int Utf16CodeUnitByteCount = 2;
private const ushort CarriageReturnCodeUnit = 0x000D;
private const ushort LineFeedCodeUnit = 0x000A;
private static readonly HashSet<string> TextSkillFileExtensions = new(StringComparer.OrdinalIgnoreCase)
{
".json",
".md",
".ps1",
".sh",
".txt",
".yaml",
".yml"
};

internal static byte[] NormalizeSkillFileContent(string relativePath, byte[] content)
{
Debug.Assert(!string.IsNullOrEmpty(relativePath), "relativePath must not be null or empty");
Debug.Assert(content != null, "content must not be null");

if (!ShouldNormalizeLineEndings(relativePath) || Array.IndexOf(content, (byte)'\r') < 0)
{
return content;
}

if (HasUtf16LittleEndianBom(content) || HasUtf16LittleEndianLineEnding(content))
Comment thread
hatayama marked this conversation as resolved.
{
return NormalizeUtf16LineEndings(content, isLittleEndian: true);
}

if (HasUtf16BigEndianBom(content) || HasUtf16BigEndianLineEnding(content))
{
return NormalizeUtf16LineEndings(content, isLittleEndian: false);
}

if (Array.IndexOf(content, (byte)0) >= 0)
{
return content;
}

return NormalizeSingleByteLineEndings(content);
}

private static bool ShouldNormalizeLineEndings(string relativePath)
{
string extension = Path.GetExtension(relativePath);
return TextSkillFileExtensions.Contains(extension);
}

private static byte[] NormalizeSingleByteLineEndings(byte[] content)
{
List<byte> normalized = new(content.Length);
for (int index = 0; index < content.Length; index++)
{
byte current = content[index];
if (current != (byte)'\r')
{
normalized.Add(current);
continue;
}

normalized.Add((byte)'\n');
if (index + 1 < content.Length && content[index + 1] == (byte)'\n')
{
index++;
}
}

return normalized.ToArray();
}

private static byte[] NormalizeUtf16LineEndings(byte[] content, bool isLittleEndian)
{
List<byte> normalized = new(content.Length);
int index = 0;
if (HasMatchingUtf16Bom(content, isLittleEndian))
{
normalized.Add(content[0]);
normalized.Add(content[1]);
index = Utf16CodeUnitByteCount;
}

while (index + 1 < content.Length)
{
ushort codeUnit = ReadUtf16CodeUnit(content, index, isLittleEndian);
if (codeUnit == CarriageReturnCodeUnit)
{
WriteUtf16CodeUnit(normalized, LineFeedCodeUnit, isLittleEndian);
int nextIndex = index + Utf16CodeUnitByteCount;
if (nextIndex + 1 < content.Length &&
ReadUtf16CodeUnit(content, nextIndex, isLittleEndian) == LineFeedCodeUnit)
{
index += Utf16CodeUnitByteCount * 2;
continue;
}

index += Utf16CodeUnitByteCount;
continue;
}

WriteUtf16CodeUnit(normalized, codeUnit, isLittleEndian);
index += Utf16CodeUnitByteCount;
}

if (index < content.Length)
{
normalized.Add(content[index]);
}

return normalized.ToArray();
}

private static bool HasUtf16LittleEndianBom(byte[] content)
{
return content.Length >= Utf16CodeUnitByteCount &&
content[0] == Utf16LittleEndianBomFirstByte &&
content[1] == Utf16LittleEndianBomSecondByte;
}

private static bool HasUtf16BigEndianBom(byte[] content)
{
return content.Length >= Utf16CodeUnitByteCount &&
content[0] == Utf16BigEndianBomFirstByte &&
content[1] == Utf16BigEndianBomSecondByte;
}

private static bool HasMatchingUtf16Bom(byte[] content, bool isLittleEndian)
{
return isLittleEndian ? HasUtf16LittleEndianBom(content) : HasUtf16BigEndianBom(content);
}

private static bool HasUtf16LittleEndianLineEnding(byte[] content)
{
return HasUtf16LineEnding(content, isLittleEndian: true);
}

private static bool HasUtf16BigEndianLineEnding(byte[] content)
{
return HasUtf16LineEnding(content, isLittleEndian: false);
}

private static bool HasUtf16LineEnding(byte[] content, bool isLittleEndian)
{
int startIndex = HasMatchingUtf16Bom(content, isLittleEndian) ? Utf16CodeUnitByteCount : 0;
for (int index = startIndex; index + 1 < content.Length; index += Utf16CodeUnitByteCount)
{
ushort codeUnit = ReadUtf16CodeUnit(content, index, isLittleEndian);
if (codeUnit == CarriageReturnCodeUnit || codeUnit == LineFeedCodeUnit)
{
return true;
}
}

return false;
}

private static ushort ReadUtf16CodeUnit(byte[] content, int index, bool isLittleEndian)
{
return isLittleEndian
? (ushort)(content[index] | (content[index + 1] << 8))
: (ushort)((content[index] << 8) | content[index + 1]);
}

private static void WriteUtf16CodeUnit(List<byte> output, ushort codeUnit, bool isLittleEndian)
{
if (isLittleEndian)
{
output.Add((byte)(codeUnit & 0xFF));
output.Add((byte)(codeUnit >> 8));
return;
}

output.Add((byte)(codeUnit >> 8));
output.Add((byte)(codeUnit & 0xFF));
}
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading
Loading