-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #225 from NoxOrg/feature/copy-file-plugin
Feature/copy file plugin
- Loading branch information
Showing
10 changed files
with
333 additions
and
11 deletions.
There are no files selected for viewing
116 changes: 116 additions & 0 deletions
116
src/Nox.Cli.Plugins/Nox.Cli.Plugin.File/FileCopyFile_v1.cs
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,116 @@ | ||
using Nox.Cli.Abstractions; | ||
using Nox.Cli.Abstractions.Extensions; | ||
|
||
namespace Nox.Cli.Plugin.File; | ||
|
||
public class FileCopyFile_v1: INoxCliAddin | ||
{ | ||
public NoxActionMetaData Discover() | ||
{ | ||
return new NoxActionMetaData | ||
{ | ||
Name = "file/copy-file@v1", | ||
Author = "Jan Schutte", | ||
Description = "Copy a file from one folder to another.", | ||
|
||
Inputs = | ||
{ | ||
["source-path"] = new NoxActionInput { | ||
Id = "source-path", | ||
Description = "The path to the file to copy", | ||
Default = string.Empty, | ||
IsRequired = true | ||
}, | ||
|
||
["target-path"] = new NoxActionInput { | ||
Id = "target-path", | ||
Description = "The path of the folder into which to copy the source file", | ||
Default = string.Empty, | ||
IsRequired = true | ||
}, | ||
["is-overwrite"] = new NoxActionInput { | ||
Id = "is-overwrite", | ||
Description = "Indicate whether the copy must overwrite the target file, if it exists.", | ||
Default = false, | ||
IsRequired = false | ||
} | ||
} | ||
}; | ||
} | ||
|
||
private string? _sourcePath; | ||
private string? _targetPath; | ||
private bool? _isOverwrite; | ||
|
||
public Task BeginAsync(IDictionary<string,object> inputs) | ||
{ | ||
_sourcePath = inputs.Value<string>("source-path"); | ||
_targetPath = inputs.Value<string>("target-path"); | ||
_isOverwrite = inputs.ValueOrDefault<bool>("is-overwrite", this); | ||
return Task.CompletedTask; | ||
} | ||
|
||
public Task<IDictionary<string, object>> ProcessAsync(INoxWorkflowContext ctx) | ||
{ | ||
var outputs = new Dictionary<string, object>(); | ||
|
||
ctx.SetState(ActionState.Error); | ||
|
||
if (string.IsNullOrEmpty(_sourcePath) || | ||
string.IsNullOrEmpty(_targetPath)) | ||
{ | ||
ctx.SetErrorMessage("The File copy-file action was not initialized"); | ||
} | ||
else | ||
{ | ||
try | ||
{ | ||
var fullSourcePath = Path.GetFullPath(_sourcePath); | ||
var filename = Path.GetFileName(fullSourcePath); | ||
if (!System.IO.File.Exists(fullSourcePath)) | ||
{ | ||
ctx.SetErrorMessage($"Source file: {fullSourcePath} does not exist!"); | ||
} | ||
else | ||
{ | ||
var isValid = true; | ||
var fullTargetPath = Path.Combine(Path.GetFullPath(_targetPath), filename); | ||
Directory.CreateDirectory(Path.GetDirectoryName(fullTargetPath)!); | ||
var createDate = DateTime.Now; | ||
if (System.IO.File.Exists(fullTargetPath)) | ||
{ | ||
if (_isOverwrite!.Value) | ||
{ | ||
createDate = System.IO.File.GetCreationTime(fullTargetPath); | ||
System.IO.File.Delete(fullTargetPath); | ||
} | ||
else | ||
{ | ||
ctx.SetErrorMessage($"File: {fullTargetPath} already exists, and is-overwrite was not specified."); | ||
isValid = false; | ||
} | ||
|
||
} | ||
if (isValid) | ||
{ | ||
System.IO.File.Copy(fullSourcePath, fullTargetPath); | ||
System.IO.File.SetCreationTime(fullTargetPath, createDate); | ||
ctx.SetState(ActionState.Success); | ||
} | ||
} | ||
|
||
} | ||
catch (Exception ex) | ||
{ | ||
ctx.SetErrorMessage(ex.Message); | ||
} | ||
} | ||
|
||
return Task.FromResult<IDictionary<string, object>>(outputs); | ||
} | ||
|
||
public Task EndAsync() | ||
{ | ||
return Task.CompletedTask; | ||
} | ||
} |
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
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,102 @@ | ||
using Moq; | ||
using Nox.Cli.Abstractions.Caching; | ||
using Nox.Cli.Actions; | ||
using Nox.Cli.Configuration; | ||
using Nox.Cli.Plugin.File; | ||
using Nox.Cli.Variables.Secrets; | ||
using Nox.Secrets.Abstractions; | ||
using Nox.Solution; | ||
using Xunit; | ||
|
||
namespace Plugin.File.Tests; | ||
|
||
public class CopyFileTests | ||
{ | ||
[Fact] | ||
public async Task Can_copy_a_File_if_destination_folder_does_not_exist() | ||
{ | ||
var path = "./files/copy-file"; | ||
//Ensure the target folder does not exist | ||
Helpers.PurgeFolderRecursive(Path.Combine(path, "new-target"), true); | ||
|
||
var plugin = new FileCopyFile_v1(); | ||
var inputs = new Dictionary<string, object> | ||
{ | ||
{"source-path", Path.Combine(path, "source/Sample.txt")}, | ||
{"target-path", Path.Combine(path, "new-target")} | ||
}; | ||
await plugin.BeginAsync(inputs); | ||
var wfConfig = new WorkflowConfiguration(); | ||
var sln = Mock.Of<NoxSolution>(); | ||
var orgResolver = Mock.Of<IOrgSecretResolver>(); | ||
var cacheMan = Mock.Of<INoxCliCacheManager>(); | ||
var lteConfig = Mock.Of<LocalTaskExecutorConfiguration>(); | ||
var secretsResolver = Mock.Of<INoxSecretsResolver>(); | ||
var ctx = new NoxWorkflowContext(wfConfig, sln, orgResolver, cacheMan, lteConfig, secretsResolver); | ||
await plugin.ProcessAsync(ctx); | ||
Assert.True(System.IO.File.Exists(Path.Combine(path, "new-target/Sample.txt"))); | ||
} | ||
|
||
[Fact] | ||
public async Task Must_not_copy_if_destination_folder_exists_and_overwrite_not_specified() | ||
{ | ||
var path = "./files/copy-file"; | ||
//Ensure the target folder exists | ||
Helpers.PurgeFolderRecursive(Path.Combine(path, "new-target"), true); | ||
Directory.CreateDirectory(Path.Combine(path, "new-target")); | ||
await System.IO.File.WriteAllTextAsync(Path.Combine(path, "new-target/Sample.txt"), "Hello World"); | ||
|
||
var plugin = new FileCopyFile_v1(); | ||
var inputs = new Dictionary<string, object> | ||
{ | ||
{"source-path", Path.Combine(path, "source/Sample.txt")}, | ||
{"target-path", Path.Combine(path, "new-target")} | ||
}; | ||
await plugin.BeginAsync(inputs); | ||
var wfConfig = new WorkflowConfiguration(); | ||
var sln = Mock.Of<NoxSolution>(); | ||
var orgResolver = Mock.Of<IOrgSecretResolver>(); | ||
var cacheMan = Mock.Of<INoxCliCacheManager>(); | ||
var lteConfig = Mock.Of<LocalTaskExecutorConfiguration>(); | ||
var secretsResolver = Mock.Of<INoxSecretsResolver>(); | ||
var ctx = new NoxWorkflowContext(wfConfig, sln, orgResolver, cacheMan, lteConfig, secretsResolver); | ||
await plugin.ProcessAsync(ctx); | ||
await plugin.EndAsync(); | ||
Assert.True(System.IO.File.Exists(Path.Combine(path, "new-target/Sample.txt"))); | ||
var fileSize = new FileInfo(Path.Combine(path, "new-target/Sample.txt")).Length; | ||
Assert.Equal(11, fileSize); | ||
} | ||
|
||
[Fact] | ||
public async Task Can_copy_a_File_if_destination_folder_exists_and_overwrite_specified() | ||
{ | ||
var path = "./files/copy-file"; | ||
//Ensure the target folder exists | ||
Helpers.PurgeFolderRecursive(Path.Combine(path, "new-target"), true); | ||
Directory.CreateDirectory(Path.Combine(path, "new-target")); | ||
await System.IO.File.WriteAllTextAsync(Path.Combine(path, "new-target/Sample.txt"), "Hello World"); | ||
|
||
var plugin = new FileCopyFile_v1(); | ||
var inputs = new Dictionary<string, object> | ||
{ | ||
{"source-path", Path.Combine(path, "source/Sample.txt")}, | ||
{"target-path", Path.Combine(path, "new-target")}, | ||
{"is-overwrite", true} | ||
}; | ||
await plugin.BeginAsync(inputs); | ||
var wfConfig = new WorkflowConfiguration(); | ||
var sln = Mock.Of<NoxSolution>(); | ||
var orgResolver = Mock.Of<IOrgSecretResolver>(); | ||
var cacheMan = Mock.Of<INoxCliCacheManager>(); | ||
var lteConfig = Mock.Of<LocalTaskExecutorConfiguration>(); | ||
var secretsResolver = Mock.Of<INoxSecretsResolver>(); | ||
var ctx = new NoxWorkflowContext(wfConfig, sln, orgResolver, cacheMan, lteConfig, secretsResolver); | ||
await plugin.ProcessAsync(ctx); | ||
await plugin.EndAsync(); | ||
Assert.True(System.IO.File.Exists(Path.Combine(path, "new-target/Sample.txt"))); | ||
var fileSize = new FileInfo(Path.Combine(path, "new-target/Sample.txt")).Length; | ||
Assert.Equal(0, fileSize); | ||
} | ||
|
||
|
||
} |
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
Oops, something went wrong.