From 74fdc61512d17d74d895e1801a13027f20b473c6 Mon Sep 17 00:00:00 2001 From: Ikiru Yoshizaki <3856350+guitarrapc@users.noreply.github.com> Date: Tue, 17 Dec 2024 12:36:58 +0900 Subject: [PATCH] chore: update test name --- src/Actions.Tests/UpdateVersionCommand.cs | 12 +- src/Actions/Commands/CreateDummyCommand.cs | 99 ++++++----- src/Actions/Commands/UpdateVersionCommand.cs | 163 +++++++++---------- 3 files changed, 136 insertions(+), 138 deletions(-) diff --git a/src/Actions.Tests/UpdateVersionCommand.cs b/src/Actions.Tests/UpdateVersionCommand.cs index 51b71a2b..96a95b7b 100644 --- a/src/Actions.Tests/UpdateVersionCommand.cs +++ b/src/Actions.Tests/UpdateVersionCommand.cs @@ -1,4 +1,4 @@ -using Actions.Commands; +using Actions.Commands; using FluentAssertions; namespace Actions.Tests; @@ -6,7 +6,7 @@ namespace Actions.Tests; public class UpdateVersionCommandTest { [Fact] - public void UpdateVersionUpmTest() + public void UnityUpmTest() { var version = "1.0.0"; var path = $"{nameof(UpdateVersionCommandTest)}/package.json"; @@ -67,10 +67,10 @@ public void UpdateVersionUpmTest() } [Fact] - public void UpdateVersionGodotTest() + public void GodotPluginTest() { var version = "1.0.0"; - var path = $"{nameof(UpdateVersionGodotTest)}/plugin.cfg"; + var path = $"{nameof(GodotPluginTest)}/plugin.cfg"; var contents = """ [plugin] @@ -102,10 +102,10 @@ public void UpdateVersionGodotTest() } [Fact] - public void UpdateVersionDirectoryBuildPropsTest() + public void DirectoryBuildPropsTest() { var version = "1.0.0"; - var path = $"{nameof(UpdateVersionDirectoryBuildPropsTest)}/Directory.Build.props"; + var path = $"{nameof(DirectoryBuildPropsTest)}/Directory.Build.props"; var contents = """ diff --git a/src/Actions/Commands/CreateDummyCommand.cs b/src/Actions/Commands/CreateDummyCommand.cs index 68b5dc22..06431ef1 100644 --- a/src/Actions/Commands/CreateDummyCommand.cs +++ b/src/Actions/Commands/CreateDummyCommand.cs @@ -1,58 +1,57 @@ -namespace Actions.Commands +namespace Actions.Commands; + +public class CreateDummyCommand { - public class CreateDummyCommand + public void CreateDummyFiles(string basePath) { - public void CreateDummyFiles(string basePath) - { - var upm = ("package.json", """ - { - "name": "com.unity.plugin.example", - "version": "1.2.310", - "displayName": "Package Example Plugin", - "description": "This is an example package", - "unity": "2019.1", - "unityRelease": "0b5", - "dependencies": { - "com.unity.example": "1.0.0" - }, - "keywords": [ - "keyword1", - "keyword2", - "keyword3" - ], - "author": { - "name": "Unity", - "email": "unity@example.com", - "url": "https://www.unity3d.com" - } + var upm = ("package.json", """ + { + "name": "com.unity.plugin.example", + "version": "1.2.310", + "displayName": "Package Example Plugin", + "description": "This is an example package", + "unity": "2019.1", + "unityRelease": "0b5", + "dependencies": { + "com.unity.example": "1.0.0" + }, + "keywords": [ + "keyword1", + "keyword2", + "keyword3" + ], + "author": { + "name": "Unity", + "email": "unity@example.com", + "url": "https://www.unity3d.com" } - """); - var godot = ("plugin.cfg", """ - [plugin] - name="Sandbox.Godot" - description="Sample." - author="Cysharp" - version="1.2.310" - language="C-sharp" - script="GodotPlugin.cs" - """); - var directoryBuildProps = ("Directory.Build.props", """ - - - 1.2.310 - - - """); + } + """); + var godot = ("plugin.cfg", """ + [plugin] + name="Sandbox.Godot" + description="Sample." + author="Cysharp" + version="1.2.310" + language="C-sharp" + script="GodotPlugin.cs" + """); + var directoryBuildProps = ("Directory.Build.props", """ + + + 1.2.310 + + + """); - foreach (var (file, contents) in new[] { upm, godot, directoryBuildProps }) - { - var path = Path.Combine(basePath, file); - if (!Directory.Exists(basePath)) - Directory.CreateDirectory(basePath); + foreach (var (file, contents) in new[] { upm, godot, directoryBuildProps }) + { + var path = Path.Combine(basePath, file); + if (!Directory.Exists(basePath)) + Directory.CreateDirectory(basePath); - Console.WriteLine($"- {path} ..."); - File.WriteAllText(path, contents); - } + Console.WriteLine($"- {path} ..."); + File.WriteAllText(path, contents); } } } diff --git a/src/Actions/Commands/UpdateVersionCommand.cs b/src/Actions/Commands/UpdateVersionCommand.cs index 1b937c8a..46d65b10 100644 --- a/src/Actions/Commands/UpdateVersionCommand.cs +++ b/src/Actions/Commands/UpdateVersionCommand.cs @@ -1,111 +1,110 @@ -using Actions.Utils; +using Actions.Utils; using System.Text.Json; using System.Text.Json.Serialization; -namespace Actions.Commands +namespace Actions.Commands; + +public record struct UpdateVersionCommandResult(string Before, string After); +public class UpdateVersionCommand(string version, string path) { - public record struct UpdateVersionCommandResult(string Before, string After); - public class UpdateVersionCommand(string version, string path) + public UpdateVersionCommandResult UpdateVersion(bool dryRun) { - public UpdateVersionCommandResult UpdateVersion(bool dryRun) - { - if (!File.Exists(path)) throw new FileNotFoundException(path); + if (!File.Exists(path)) throw new FileNotFoundException(path); - var writeBack = !dryRun; - var fileName = Path.GetFileName(path); - return fileName switch - { - // UPM - "package.json" => HandleUpm(writeBack), - // Godot - "plugin.cfg" => HandleGodot(writeBack), - // .NET - "Directory.Build.props" => HandleDirectoryBuildProps(writeBack), - // Other - _ => throw new NotImplementedException(fileName), - }; - } - - private UpdateVersionCommandResult HandleUpm(bool writeBack) + var writeBack = !dryRun; + var fileName = Path.GetFileName(path); + return fileName switch { - // replace - var (before, after) = Sed.Replace(path, @"""version"":\s*""(.*?)""", $@"""version"": ""{version}""", writeBack); + // UPM + "package.json" => HandleUpm(writeBack), + // Godot + "plugin.cfg" => HandleGodot(writeBack), + // .NET + "Directory.Build.props" => HandleDirectoryBuildProps(writeBack), + // Other + _ => throw new NotImplementedException(fileName), + }; + } - // validate - Validate(after, version); + private UpdateVersionCommandResult HandleUpm(bool writeBack) + { + // replace + var (before, after) = Sed.Replace(path, @"""version"":\s*""(.*?)""", $@"""version"": ""{version}""", writeBack); - return new UpdateVersionCommandResult(before, after); + // validate + Validate(after, version); - static void Validate(string contents, string version) - { - var packageJson = JsonSerializer.Deserialize(contents) ?? throw new ActionCommandException($"UPM package.json updated, but failed to load as valid JSON. contents: {contents}"); - if (packageJson.Version != version) - throw new ActionCommandException($"UPM package.json updated, but version miss-match. actual {packageJson?.Version}, expected {version}"); - } - } + return new UpdateVersionCommandResult(before, after); - private UpdateVersionCommandResult HandleGodot(bool writeBack) + static void Validate(string contents, string version) { - // replace - var (before, after) = Sed.Replace(path, @"(version=)""(.*?)""", $@"$1""{version}""", writeBack); + var packageJson = JsonSerializer.Deserialize(contents) ?? throw new ActionCommandException($"UPM package.json updated, but failed to load as valid JSON. contents: {contents}"); + if (packageJson.Version != version) + throw new ActionCommandException($"UPM package.json updated, but version miss-match. actual {packageJson?.Version}, expected {version}"); + } + } - // validate - Validate(after, version); + private UpdateVersionCommandResult HandleGodot(bool writeBack) + { + // replace + var (before, after) = Sed.Replace(path, @"(version=)""(.*?)""", $@"$1""{version}""", writeBack); - return new UpdateVersionCommandResult(before, after); + // validate + Validate(after, version); - static void Validate(string contents, string version) + return new UpdateVersionCommandResult(before, after); + + static void Validate(string contents, string version) + { + var lines = contents.Split("\n"); + Span destination = stackalloc Range[2]; + foreach (var line in lines) { - var lines = contents.Split("\n"); - Span destination = stackalloc Range[2]; - foreach (var line in lines) + // find the line befin with "version=", then split with = to get version + if (!line.StartsWith("version=")) + continue; + + var span = line.AsSpan(); + var range = span.Split(destination, '=', StringSplitOptions.TrimEntries); + if (range != 2) + continue; + + // validate version is expceted + var versionValue = span[destination[1]].ToString(); + if (versionValue != $"\"{version}\"") { - // find the line befin with "version=", then split with = to get version - if (!line.StartsWith("version=")) - continue; - - var span = line.AsSpan(); - var range = span.Split(destination, '=', StringSplitOptions.TrimEntries); - if (range != 2) - continue; - - // validate version is expceted - var versionValue = span[destination[1]].ToString(); - if (versionValue != $"\"{version}\"") - { - throw new ActionCommandException($"Godot plugin.cfg updated, but version miss-match. actual {versionValue}, expected {version}"); - } - return; + throw new ActionCommandException($"Godot plugin.cfg updated, but version miss-match. actual {versionValue}, expected {version}"); } - throw new ActionCommandException($"Godot plugin.cfg updated, but version key not found."); + return; } + throw new ActionCommandException($"Godot plugin.cfg updated, but version key not found."); } + } - private UpdateVersionCommandResult HandleDirectoryBuildProps(bool writeBack) - { - // replace - var (before, after) = Sed.Replace(path, @".*", $@"{version}", writeBack); + private UpdateVersionCommandResult HandleDirectoryBuildProps(bool writeBack) + { + // replace + var (before, after) = Sed.Replace(path, @".*", $@"{version}", writeBack); - // validate - Validate(after, version); + // validate + Validate(after, version); - return new UpdateVersionCommandResult(before, after); + return new UpdateVersionCommandResult(before, after); - static void Validate(string contents, string version) - { - var xmlDoc = new System.Xml.XmlDocument(); - xmlDoc.LoadXml(contents); - var versionPrefixNode = xmlDoc.SelectSingleNode("//VersionPrefix") ?? throw new ActionCommandException($"Directory.Build.props updated, but VersionPrefix key not found."); - if (versionPrefixNode.InnerText != version) - throw new ActionCommandException($"Directory.Build.props updated, but version miss-match. actual {versionPrefixNode.InnerText}, expected {version}"); + static void Validate(string contents, string version) + { + var xmlDoc = new System.Xml.XmlDocument(); + xmlDoc.LoadXml(contents); + var versionPrefixNode = xmlDoc.SelectSingleNode("//VersionPrefix") ?? throw new ActionCommandException($"Directory.Build.props updated, but VersionPrefix key not found."); + if (versionPrefixNode.InnerText != version) + throw new ActionCommandException($"Directory.Build.props updated, but version miss-match. actual {versionPrefixNode.InnerText}, expected {version}"); - } } + } - private record UpmPackageJson - { - [JsonPropertyName("version")] - public required string Version { get; set; } - } + private record UpmPackageJson + { + [JsonPropertyName("version")] + public required string Version { get; set; } } }