Skip to content

Commit

Permalink
chore: update test name
Browse files Browse the repository at this point in the history
  • Loading branch information
guitarrapc committed Dec 17, 2024
1 parent b42dda7 commit 74fdc61
Show file tree
Hide file tree
Showing 3 changed files with 136 additions and 138 deletions.
12 changes: 6 additions & 6 deletions src/Actions.Tests/UpdateVersionCommand.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
using Actions.Commands;
using Actions.Commands;
using FluentAssertions;

namespace Actions.Tests;

public class UpdateVersionCommandTest
{
[Fact]
public void UpdateVersionUpmTest()
public void UnityUpmTest()
{
var version = "1.0.0";
var path = $"{nameof(UpdateVersionCommandTest)}/package.json";
Expand Down Expand Up @@ -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]
Expand Down Expand Up @@ -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 = """
<Project>
<PropertyGroup>
Expand Down
99 changes: 49 additions & 50 deletions src/Actions/Commands/CreateDummyCommand.cs
Original file line number Diff line number Diff line change
@@ -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": "[email protected]",
"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": "[email protected]",
"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", """
<Project>
<PropertyGroup>
<VersionPrefix>1.2.310</VersionPrefix>
</PropertyGroup>
</Project>
""");
}
""");
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", """
<Project>
<PropertyGroup>
<VersionPrefix>1.2.310</VersionPrefix>
</PropertyGroup>
</Project>
""");

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);
}
}
}
163 changes: 81 additions & 82 deletions src/Actions/Commands/UpdateVersionCommand.cs
Original file line number Diff line number Diff line change
@@ -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<UpmPackageJson>(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<UpmPackageJson>(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<Range> destination = stackalloc Range[2];
foreach (var line in lines)
{
var lines = contents.Split("\n");
Span<Range> 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, @"<VersionPrefix>.*</VersionPrefix>", $@"<VersionPrefix>{version}</VersionPrefix>", writeBack);
private UpdateVersionCommandResult HandleDirectoryBuildProps(bool writeBack)
{
// replace
var (before, after) = Sed.Replace(path, @"<VersionPrefix>.*</VersionPrefix>", $@"<VersionPrefix>{version}</VersionPrefix>", 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; }
}
}

0 comments on commit 74fdc61

Please sign in to comment.