Skip to content

Commit

Permalink
chore(release): 1.3.0
Browse files Browse the repository at this point in the history
* Add --retry-delay and --max-retries options to delete command
  • Loading branch information
domsleee committed Dec 3, 2023
1 parent 543f091 commit 8d6c9e7
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 6 deletions.
2 changes: 1 addition & 1 deletion Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<PropertyGroup>
<Description>By hook or by crook, perform operations on files and directories. If they are
in use by a process, kill the process.</Description>
<Version>1.2.1</Version>
<Version>1.3.0</Version>
<PackageProjectUrl>https://github.com/domsleee/forceops</PackageProjectUrl>
<RepositoryUrl>https://github.com/domsleee/forceops</RepositoryUrl>
<RepositoryType>git</RepositoryType>
Expand Down
15 changes: 15 additions & 0 deletions ForceOps.Test/src/ProgramTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,21 @@ public void SuccessfulChildDoesntThrowException()
testContext.relaunchAsElevatedMock.Verify(t => t.RelaunchAsElevated(It.IsAny<List<string>>(), It.IsAny<string>()), Times.Once());
}

[Fact]
public void RetryDelayAndMaxRetriesWork()
{
using var launchedProcess = LaunchProcessInDirectory(tempDirectoryPath);
var testContext = new TestContext();
testContext.relaunchAsElevatedMock.Setup(t => t.RelaunchAsElevated(It.IsAny<List<string>>(), It.IsAny<string>())).Returns(0);
testContext.forceOpsContext.processKiller = new Mock<IProcessKiller>().Object;

var forceOps = new ForceOps(new[] { "delete", tempDirectoryPath, "--retry-delay", "33", "--max-retries", "8" }, testContext.forceOpsContext);
Assert.Equal(0, forceOps.Run());

testContext.relaunchAsElevatedMock.Verify(t => t.RelaunchAsElevated(It.IsAny<List<string>>(), It.IsAny<string>()), Times.Once());
Assert.Contains("Beginning retry 1/8 in 33ms.", testContext.fakeLoggerFactory.GetAllLogsString());
}

[Fact]
public void ExceptionThrownIfAlreadyElevated()
{
Expand Down
16 changes: 11 additions & 5 deletions ForceOps/src/ForceOps.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,26 +61,32 @@ Command CreateDeleteCommand()
Arity = ArgumentArity.OneOrMore
};

var forceOption = new Option<bool>(new[] { "-f", "--force" }, "Ignore nonexistent files and arguments");
var disableElevate = new Option<bool>(new[] { "-e", "--disable-elevate" }, "Do not attempt to elevate if the file can't be deleted");
var forceOption = new Option<bool>(new[] { "-f", "--force" }, "Ignore nonexistent files and arguments.");
var disableElevate = new Option<bool>(new[] { "-e", "--disable-elevate" }, "Do not attempt to elevate if the file can't be deleted.");
var retryDelay = new Option<int>(new[] { "-d", "--retry-delay" }, () => 500, "Delay when retrying to delete a file, after deleting processes holding a lock. (default: 500)");
var maxRetries = new Option<int>(new[] { "-n", "--max-retries" }, () => 5, "Number of retries when deleting a locked file. (default: 5)");

var deleteCommand = new Command("delete", "Delete files or a directories recursively.")
{
filesToDeleteArgument,
forceOption,
disableElevate
disableElevate,
retryDelay,
maxRetries
};

deleteCommand.AddAlias("rm");
deleteCommand.AddAlias("remove");
deleteCommand.SetHandler(DeleteCommand, filesToDeleteArgument, forceOption, disableElevate);
deleteCommand.SetHandler(DeleteCommand, filesToDeleteArgument, forceOption, disableElevate, retryDelay, maxRetries);
return deleteCommand;
}

void DeleteCommand(string[] filesOrDirectoriesToDelete, bool force, bool disableElevate)
void DeleteCommand(string[] filesOrDirectoriesToDelete, bool force, bool disableElevate, int retryDelay, int maxRetries)
{
RunWithRelaunchAsElevated(() =>
{
forceOpsContext.maxRetries = maxRetries;
forceOpsContext.retryDelay = TimeSpan.FromMilliseconds(retryDelay);
var deleter = new FileAndDirectoryDeleter(forceOpsContext);
filesOrDirectoriesToDelete = filesOrDirectoriesToDelete.Select(file => DirectoryUtils.CombineWithCWDAndGetAbsolutePath(file)).ToArray();
foreach (var file in filesOrDirectoriesToDelete)
Expand Down

0 comments on commit 8d6c9e7

Please sign in to comment.