Update current version with patch minor and major

This commit is contained in:
Franco Meloni 2019-02-10 15:53:26 +00:00
parent 170baa077b
commit 4a92de41bc
8 changed files with 132 additions and 9 deletions

View File

@ -0,0 +1,29 @@
import Foundation
protocol CurrentVersionProviding {
func currentVersion() throws -> String
}
struct CurrentVersionProvider: CurrentVersionProviding {
enum Errors: Error {
case versionNotFound
}
let scriptLauncher: ScriptLaunching
init(scriptLauncher: ScriptLaunching = ScriptLauncher()) {
self.scriptLauncher = scriptLauncher
}
func currentVersion() throws -> String {
let tags = try scriptLauncher.launchScript(withContent: "git tag --sort=-v:refname", version: nil).split(separator: "\n").lazy.map({ String($0) })
let regex = try NSRegularExpression(pattern: "\\d.\\d.\\d", options: .caseInsensitive)
if let version = tags.filter({ regex.firstMatch(in: $0, options: .anchored, range: NSRange(location: 0, length: $0.count)) != nil }).first {
return version
} else {
throw Errors.versionNotFound
}
}
}

View File

@ -7,13 +7,12 @@ struct ScriptLauncherError: Error {
}
protocol ScriptLaunching {
func launchScript(withContent content: String, version: String?, logger: Logger) throws
@discardableResult
func launchScript(withContent content: String, version: String?) throws -> String
}
final class ScriptLauncher: ScriptLaunching {
init() {}
func launchScript(withContent content: String, version: String?, logger _: Logger) throws {
struct ScriptLauncher: ScriptLaunching {
func launchScript(withContent content: String, version: String?) throws -> String {
var contents: [String] = []
if let version = version {
@ -25,6 +24,8 @@ final class ScriptLauncher: ScriptLaunching {
let outputs = contents.map { run(bash: $0) }
if let errorString = outputs.lazy.filter({ $0.exitcode != 0 }).map({ $0.stderror }).first {
throw ScriptLauncherError(errorString: errorString)
} else {
return outputs.last?.stdout ?? ""
}
}
}

View File

@ -9,7 +9,7 @@ protocol ScriptLauncherContainer {
extension ScriptLauncherContainer {
func launchScript(content: String, version: String? = nil, errorMessage: String, logger: Logger) {
do {
try scriptLauncher.launchScript(withContent: content, version: version, logger: logger)
try scriptLauncher.launchScript(withContent: content, version: version)
} catch {
logger.logInfo("")

View File

@ -1 +1,7 @@
struct NewVersionProvider {}
public enum NewVersionProvider {
static func newVersion(_ versionBumpOption: VersionBumpOption, currentVersionProvider: CurrentVersionProviding = CurrentVersionProvider()) throws -> String {
let currentVersion = try currentVersionProvider.currentVersion()
return versionBumpOption.newVersion(currentVersion: currentVersion)
}
}

View File

@ -1,5 +1,24 @@
enum VersionBumbOption: String {
public enum VersionBumpOption: String {
case patch
case minor
case major
func newVersion(currentVersion: String) -> String {
let componentIndex: Int
switch self {
case .major:
componentIndex = 0
case .minor:
componentIndex = 1
case .patch:
componentIndex = 2
}
var versionComponent = currentVersion.split(separator: ".").map { String($0) }
let newComponent = Int(versionComponent[componentIndex])?.advanced(by: 1) ?? 0
versionComponent[componentIndex] = newComponent.description
return versionComponent.joined(separator: ".")
}
}

View File

@ -0,0 +1,38 @@
@testable import RocketLib
import XCTest
final class CurrentVersionProviderTests: XCTestCase {
var scriptLaucher: SpyScriptLauncher!
var currentVersionProvider: CurrentVersionProvider!
override func setUp() {
super.setUp()
scriptLaucher = SpyScriptLauncher()
currentVersionProvider = CurrentVersionProvider(scriptLauncher: scriptLaucher)
}
func testItReturnsTheCorrectTag() {
let tags = """
tag1
tag2
1.0.0
0.10.0
0.9.0
"""
scriptLaucher.result = tags
XCTAssertEqual(try currentVersionProvider.currentVersion(), "1.0.0")
}
func testItThrowsAnErrorIfThereAreNoTags() {
let tags = """
tag1
tag2
"""
scriptLaucher.result = tags
XCTAssertThrowsError(try currentVersionProvider.currentVersion())
}
}

View File

@ -0,0 +1,28 @@
@testable import RocketLib
import XCTest
final class NewVersionProviderTests: XCTestCase {
func testReturnsTheCorrectNewVersion() throws {
let versionProvider = FakeCurrentVersionProvider()
XCTAssertEqual(
try NewVersionProvider.newVersion(.patch, currentVersionProvider: versionProvider),
"1.0.1"
)
XCTAssertEqual(
try NewVersionProvider.newVersion(.minor, currentVersionProvider: versionProvider),
"1.1.0"
)
XCTAssertEqual(
try NewVersionProvider.newVersion(.major, currentVersionProvider: versionProvider),
"2.0.0"
)
}
}
private class FakeCurrentVersionProvider: CurrentVersionProviding {
func currentVersion() throws -> String {
return "1.0.0"
}
}

View File

@ -8,8 +8,10 @@ final class SpyScriptLauncher: ScriptLaunching, TestSpy {
}
var callstack = CallstackContainer<Method>()
var result = ""
func launchScript(withContent content: String, version: String?, logger _: Logger) throws {
func launchScript(withContent content: String, version: String?) throws -> String {
callstack.record(.launchScript(content: content, version: version))
return result
}
}