-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
bff2c1b
commit e639f10
Showing
6 changed files
with
253 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters