Add checks parser

This commit is contained in:
Franco Meloni 2019-10-28 22:39:30 +00:00
parent 5dc4ad12bd
commit 086160c002
6 changed files with 74 additions and 4 deletions

View File

@ -0,0 +1,14 @@
enum PreReleseCheck: String {
case cleanGit = "clean_git"
func check(dictionary _: [String: Any]?) -> Check {
return checkType.init()
}
private var checkType: Check.Type {
switch self {
case .cleanGit:
return CleanGitStatusCheck.self
}
}
}

View File

@ -0,0 +1,4 @@
protocol Check {
init()
func check() -> Bool
}

View File

@ -1,8 +1,16 @@
import Foundation import Foundation
struct CleanGitStatusCheck: PreReleaseCheck { struct CleanGitStatusCheck: Check {
let launcher: ScriptLaunching let launcher: ScriptLaunching
init() {
self.init(launcher: ScriptLauncher())
}
init(launcher: ScriptLaunching) {
self.launcher = launcher
}
func check() -> Bool { func check() -> Bool {
let result = try? launcher.launchScript(withContent: "git diff --name-only", version: nil) let result = try? launcher.launchScript(withContent: "git diff --name-only", version: nil)

View File

@ -1,3 +0,0 @@
protocol PreReleaseCheck {
func check() -> Bool
}

View File

@ -0,0 +1,21 @@
import Logger
enum ChecksParser {
enum CodingKeys: String {
case preReleaseChecks = "pre_release_checks"
}
public static func parsePreReleaseChecks(fromDictionary dictionary: [String: Any]) -> [Check] {
let checksArray: [Any] = dictionary[CodingKeys.preReleaseChecks] ?? []
return checksArray.compactMap(parseElement)
}
private static func parseElement(_ element: Any) -> Check? {
if let string = element as? String,
let check = PreReleseCheck(rawValue: string) {
return check.check(dictionary: nil)
} else {
return nil
}
}
}

View File

@ -0,0 +1,26 @@
import Nimble
@testable import RocketLib
import XCTest
final class ChecksParserTests: XCTestCase {
func testItParsesCorrectlyAValidDictionary() {
let dictionary = ["pre_release_checks": ["clean_git"]]
let checks = ChecksParser.parsePreReleaseChecks(fromDictionary: dictionary)
expect(checks.count) == 1
expect(checks[0]).to(beAKindOf(CleanGitStatusCheck.self))
}
func testItIgnoresTheInvalidSteps() {
let dictionary = ["pre_release_checks": [
"clean_git",
"invalid",
]]
let checks = ChecksParser.parsePreReleaseChecks(fromDictionary: dictionary)
expect(checks.count) == 1
expect(checks[0]).to(beAKindOf(CleanGitStatusCheck.self))
}
}