-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Implement uninstall-hook * Implement uninstall-hook: naming refactor * Add uninstall-hook unit test
- Loading branch information
1 parent
e320495
commit b68ea21
Showing
4 changed files
with
154 additions
and
1 deletion.
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
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,84 @@ | ||
import Foundation | ||
import Commander | ||
import PathKit | ||
|
||
public class UninstallHookCommand: CommandProtocol { | ||
|
||
public static func make() -> CommandType { | ||
let uninstallCommand = command { | ||
let action = UnInstallHookCommandAction() | ||
|
||
action.doAction() | ||
} | ||
|
||
return uninstallCommand | ||
} | ||
|
||
} | ||
|
||
enum UninstallHookCommandActionError: Error { | ||
case gitRepoNotExist | ||
case deleteHookFileFail | ||
} | ||
|
||
class UnInstallHookCommandAction { | ||
|
||
lazy var system: SystemProtocol = System() | ||
|
||
lazy var postCommitHookJob = HookInstallJob(repoPath: Path.current + Path(".git"), | ||
hookName: "post-commit", | ||
hookContent: DefaultTemplate.postCommitHook()) | ||
lazy var postRewriteHookJob = HookInstallJob(repoPath: Path.current + Path(".git"), | ||
hookName: "post-rewrite", | ||
hookContent: DefaultTemplate.postRewriteHook()) | ||
|
||
func doAction() { | ||
let uninstaller = HookUninstaller(system) | ||
|
||
do { | ||
try uninstaller.uninstall([postCommitHookJob, postRewriteHookJob]) | ||
} catch { | ||
system.printFatalError(error.localizedDescription) | ||
} | ||
} | ||
} | ||
|
||
class HookUninstaller { | ||
|
||
var system: SystemProtocol | ||
|
||
init(_ system: SystemProtocol) { | ||
self.system = system | ||
} | ||
|
||
func uninstall(_ jobs: [HookInstallJob]) throws { | ||
for job in jobs { | ||
try checkRepoExist(job) | ||
try uninstallHook(to: job.hookPath, hookContent: job.hookContent) | ||
} | ||
} | ||
|
||
private func checkRepoExist(_ job: HookInstallJob) throws { | ||
if (!job.repoPath.exists) { | ||
throw UninstallHookCommandActionError.gitRepoNotExist | ||
} | ||
} | ||
|
||
private func uninstallHook(to path: Path, hookContent: String) throws { | ||
if (path.exists) { | ||
try deleteExistHook(to: path) | ||
} else { | ||
system.printWarning("The file \(path.absolute()) is not exists.") | ||
} | ||
} | ||
|
||
private func deleteExistHook(to path: Path) throws { | ||
do { | ||
try path.delete() | ||
} catch { | ||
throw UninstallHookCommandActionError.deleteHookFileFail | ||
} | ||
system.printWarning("Hook \(path.lastComponent) uninstall to path \(path) success.") | ||
} | ||
|
||
} |
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,62 @@ | ||
// | ||
// UnInstallHookCommandAction.swift | ||
// CircuitBreaker | ||
// | ||
// Created by Tony Yang on 2018/7/7. | ||
// | ||
|
||
import XCTest | ||
import Foundation | ||
import PathKit | ||
@testable import RedmineBotCore | ||
|
||
class UnInstallHookCommandActionTest: XCTestCase { | ||
|
||
override class func setUp() { | ||
TempGitRepo.setUP() | ||
} | ||
|
||
func test__uninstallHookWhenHookIsEmpty__shouldShowWarning() { | ||
let mockSystem = MockSystem() | ||
let unInstallAction = UnInstallHookCommandAction() | ||
unInstallAction.system = mockSystem | ||
|
||
XCTAssertFalse(unInstallAction.postCommitHookJob.hookPath.exists) | ||
XCTAssertFalse(unInstallAction.postRewriteHookJob.hookPath.exists) | ||
|
||
unInstallAction.doAction() | ||
|
||
XCTAssertFalse(unInstallAction.postCommitHookJob.hookPath.exists) | ||
XCTAssertFalse(unInstallAction.postRewriteHookJob.hookPath.exists) | ||
XCTAssertTrue(mockSystem.printWarningBeInvoke) | ||
} | ||
|
||
func test__uninstallHookWhenHookAlreadyExist__shouldDeleteHook() { | ||
let mockSystem = MockSystem() | ||
let installAction = InstallHookCommandAction() | ||
installAction.system = mockSystem | ||
|
||
XCTAssertFalse(installAction.postCommitHookJob.hookPath.exists) | ||
XCTAssertFalse(installAction.postRewriteHookJob.hookPath.exists) | ||
|
||
installAction.doAction() | ||
|
||
XCTAssertTrue(installAction.postCommitHookJob.hookPath.exists) | ||
XCTAssertTrue(installAction.postRewriteHookJob.hookPath.exists) | ||
|
||
let unInstallAction = UnInstallHookCommandAction() | ||
unInstallAction.system = mockSystem | ||
|
||
unInstallAction.doAction() | ||
|
||
XCTAssertFalse(installAction.postCommitHookJob.hookPath.exists) | ||
XCTAssertFalse(installAction.postRewriteHookJob.hookPath.exists) | ||
} | ||
|
||
override class func tearDown() { | ||
TempGitRepo.tearDown() | ||
|
||
super.tearDown() | ||
} | ||
|
||
} |