Skip to content

Commit

Permalink
feat: add ValidateArtifactCommand
Browse files Browse the repository at this point in the history
  • Loading branch information
guitarrapc committed Dec 17, 2024
1 parent bff2c1b commit e639f10
Show file tree
Hide file tree
Showing 6 changed files with 253 additions and 6 deletions.
100 changes: 100 additions & 0 deletions src/Actions.Tests/FileExistsCommandTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
using Actions.Commands;

namespace Actions.Tests;

public class FileExistsCommandTest
{
[Fact]
public void EmptyPathTest()
{
var command = new FileExsistsCommand("");
Assert.Throws<ActionCommandException>(() => command.Validate());
}

[Fact]
public void FullPathTest()
{
var path = $".tests/{nameof(FileExistsCommandTest)}/{nameof(FullPathTest)}/dummy.nupkg";
var dir = Path.GetDirectoryName(path)!;
if (!Directory.Exists(dir)) Directory.CreateDirectory(dir);
File.WriteAllText(path, "");

var command = new FileExsistsCommand(path);
command.Validate();
}

[Fact]
public void WildcardFileTest()
{
var dir = $".tests/{nameof(FileExistsCommandTest)}/{nameof(WildcardFileTest)}";
var items = new[] { "foo", "bar", "piyo" };
foreach (var item in items) {
if (!Directory.Exists(dir)) Directory.CreateDirectory(dir);
File.WriteAllText(Path.Combine(dir, item), "");
}

var command = new FileExsistsCommand($"{dir}/*");
command.Validate();

var command2 = new FileExsistsCommand($"{dir}/foo");
command2.Validate();
}

[Fact]
public void WildcardDirectoryTest()
{
var dir = $".tests/{nameof(FileExistsCommandTest)}/{nameof(WildcardDirectoryTest)}";
var items = new[] { "foo", "bar", "piyo" };
foreach (var item in items)
{
if (!Directory.Exists(dir)) Directory.CreateDirectory(dir);
File.WriteAllText(Path.Combine(dir, item), "");
}

var command = new FileExsistsCommand($".tests/{nameof(FileExistsCommandTest)}/*/foo");
command.Validate();

var command2 = new FileExsistsCommand($".tests/{nameof(FileExistsCommandTest)}/{nameof(WildcardDirectoryTest)}/foo");
command2.Validate();

var failCommand = new FileExsistsCommand($".tests/{nameof(FileExistsCommandTest)}/{nameof(WildcardDirectoryTest)}/*/foo");
Assert.Throws<ActionCommandException>(() => failCommand.Validate());
}

[Fact]
public void RecursiveWildcardDirectoryTest()
{
var dir = $".tests/{nameof(FileExistsCommandTest)}/{nameof(RecursiveWildcardDirectoryTest)}";
var items = new[] { "foo", "bar", "piyo" };
foreach (var item in items)
{
if (!Directory.Exists(dir)) Directory.CreateDirectory(dir);
File.WriteAllText(Path.Combine(dir, item), "");
}

var command = new FileExsistsCommand($".tests/{nameof(FileExistsCommandTest)}/**/foo");
command.Validate();

var command2 = new FileExsistsCommand($".tests/{nameof(FileExistsCommandTest)}/{nameof(WildcardDirectoryTest)}/**/foo");
command2.Validate();
}

[Fact]
public void RecursiveWildcardDirectoryAndFileTest()
{
var dirBase = $".tests/{nameof(FileExistsCommandTest)}/{nameof(RecursiveWildcardDirectoryAndFileTest)}";
var items = new[] { "foo", "bar", "piyo" };
foreach (var item in items)
{
var dir = Path.Combine(dirBase, item);
if (!Directory.Exists(dir)) Directory.CreateDirectory(dir);
File.WriteAllText(Path.Combine(dir, item), "");
}

var command = new FileExsistsCommand($"{dirBase}/**/*");
command.Validate();

var command2 = new FileExsistsCommand($"{dirBase}/foo/foo");
command2.Validate();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ public class UpdateVersionCommandTest
public void UnityUpmTest()
{
var version = "1.0.0";
var path = $"{nameof(UpdateVersionCommandTest)}/package.json";
var path = $".tests/{nameof(UpdateVersionCommandTest)}{nameof(UnityUpmTest)}/package.json";
var contents = """
{
"name": "com.unity.plugin.example",
Expand Down Expand Up @@ -70,7 +70,7 @@ public void UnityUpmTest()
public void GodotPluginTest()
{
var version = "1.0.0";
var path = $"{nameof(GodotPluginTest)}/plugin.cfg";
var path = $".tests/{nameof(UpdateVersionCommandTest)}/{nameof(GodotPluginTest)}/plugin.cfg";
var contents = """
[plugin]
Expand Down Expand Up @@ -105,7 +105,7 @@ public void GodotPluginTest()
public void DirectoryBuildPropsTest()
{
var version = "1.0.0";
var path = $"{nameof(DirectoryBuildPropsTest)}/Directory.Build.props";
var path = $".tests/{nameof(UpdateVersionCommandTest)}/{nameof(DirectoryBuildPropsTest)}/Directory.Build.props";
var contents = """
<Project>
<PropertyGroup>
Expand Down
24 changes: 23 additions & 1 deletion src/Actions/Commands/CreateDummyCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,13 @@

public class CreateDummyCommand
{
public void CreateDummyFiles(string basePath)
public void CreateDummy(string basePath)
{
DummyVersionFiles(basePath);
DummyAssetFiles(Path.Combine(basePath, "downloads/"));
}

private void DummyVersionFiles(string basePath)
{
var upm = ("package.json", """
{
Expand Down Expand Up @@ -44,6 +50,7 @@ public void CreateDummyFiles(string basePath)
</Project>
""");

Console.WriteLine($"{nameof(DummyVersionFiles)}");
foreach (var (file, contents) in new[] { upm, godot, directoryBuildProps })
{
var path = Path.Combine(basePath, file);
Expand All @@ -54,4 +61,19 @@ public void CreateDummyFiles(string basePath)
File.WriteAllText(path, contents);
}
}

private void DummyAssetFiles(string basePath)
{
Console.WriteLine($"{nameof(DummyAssetFiles)}");
var items = new[] { "foo", "bar", "piyo" };
foreach (var item in items)
{
var path = Path.Combine(basePath, item);
if (!Directory.Exists(basePath))
Directory.CreateDirectory(basePath);

Console.WriteLine($"- {path} ...");
File.WriteAllText(path, "");
}
}
}
95 changes: 95 additions & 0 deletions src/Actions/Commands/FileExsistsCommand.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
namespace Actions.Commands;

public class FileExsistsCommand(string pathPattern)
{
public void Validate()
{
// do nothing for empty input
if (string.IsNullOrWhiteSpace(pathPattern))
{
throw new ActionCommandException("Input path was empty.");
}

// Handle glob path pattern.
// /foo/bar/**/*
// /foo/bar/*.txt
if (ContainsGlobPattern(pathPattern))
{
if (!FileExistsWithGlob(pathPattern))
{
throw new ActionCommandException(pathPattern, new FileNotFoundException(pathPattern));
}
return;
}

// Specified full path pattern...
// /foo/bar/piyo/poyo.txt
if (!File.Exists(pathPattern))
{
throw new ActionCommandException(pathPattern, new FileNotFoundException(pathPattern));
}
}

private static bool ContainsGlobPattern(string pattern) => pattern.Contains('*') || pattern.Contains('?');
private static bool FileExistsWithGlob(string pattern)
{
try
{
pattern = pattern.Replace("\\", "/");
string directory = GetParentDirectoryPath(pattern) ?? Directory.GetCurrentDirectory();
string searchPattern = Path.GetFileName(pattern);

if (pattern.Contains("/**/"))
{
return Directory.EnumerateFiles(directory, searchPattern, SearchOption.AllDirectories).Any();
}
if (pattern.Contains("*/"))
{
return EnumerateFileWildcard(pattern, searchPattern, SearchOption.AllDirectories).Any();
}
else
{
return Directory.EnumerateFiles(directory, searchPattern, SearchOption.TopDirectoryOnly).Any();
}
}
catch (Exception ex)
{
throw new ActionCommandException($"Error happen on checking file. Is specified path correct? path: {pattern}", ex);
}
}

private static string GetParentDirectoryPath(string path)
{
var limit = 5;
var current = 0;
var dir = path;
var fileName = Path.GetFileName(dir);
var next = false;
do
{
dir = Path.GetDirectoryName(dir) ?? "";
fileName = Path.GetFileName(dir);
current++;
if (current >= limit)
{
throw new ActionCommandException($"Recursively approaced parent directory but reached limit. {current}/{limit}");
}
next = fileName == "**" || fileName == "*";
} while (next);

return dir;
}

private static IEnumerable<string> EnumerateFileWildcard(string path, string searchPattern, SearchOption searchOption)
{
var fileName = Path.GetFileName(path);
if (fileName == "*")
{
return Directory.GetDirectories(Path.GetDirectoryName(path)!).SelectMany(x => Directory.GetFiles(x, searchPattern, searchOption));
}
else
{
return EnumerateFileWildcard(Path.GetDirectoryName(path)!, searchPattern, searchOption);
}
}
}
17 changes: 15 additions & 2 deletions src/Actions/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,19 @@ public void UpdateVersion(string version, string path, bool dryRun)
Console.WriteLine($"Completed ...");
}

/// <summary>
/// Validate specified path contains file
/// </summary>
/// <param name="pathPattern"></param>
[Command("validate-file-exists")]
public void ValidateFileExists(string pathPattern)
{
Console.WriteLine($"Validating path, {pathPattern}");
var command = new FileExsistsCommand(pathPattern);
command.Validate();
Console.WriteLine($"Completed ...");
}

/// <summary>
/// Create dummy files
/// </summary>
Expand All @@ -68,7 +81,7 @@ public void CreateDummy(string basePath)
Console.WriteLine($"Creating dummy files, under {basePath} ...");

var command = new CreateDummyCommand();
command.CreateDummyFiles(basePath);
command.CreateDummy(basePath);

Console.WriteLine($"Completed ...");
}
Expand All @@ -83,7 +96,7 @@ public void CreateDummy(string basePath)
};
}

public class ActionCommandException(string message) : Exception(message)
public class ActionCommandException(string message, Exception? innterException = null) : Exception(message, innterException)
{
}
}
17 changes: 17 additions & 0 deletions src/Actions/Properties/launchSettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,23 @@
"commandName": "Project",
"commandLineArgs": "update-version --version 1.0.0 --path ./dummy/Directory.Build.props"
},
// validate-file-exsits
"validate-file-exsits (path: foo)": {
"commandName": "Project",
"commandLineArgs": "validate-file-exists --path-pattern ./dummy/downloads/foo"
},
"validate-file-exsits (path: *)": {
"commandName": "Project",
"commandLineArgs": "validate-file-exists --path-pattern ./dummy/downloads/*"
},
"validate-file-exsits (path: */foo)": {
"commandName": "Project",
"commandLineArgs": "validate-file-exists --path-pattern ./dummy/*/foo"
},
"validate-file-exsits (path: **/foo)": {
"commandName": "Project",
"commandLineArgs": "validate-file-exists --path-pattern ./dummy/**/foo"
},
// create-dummy
"create-dummy (help)": {
"commandName": "Project",
Expand Down

0 comments on commit e639f10

Please sign in to comment.