Merge pull request #207 from mas-cli/lucky-json-error

🐛 Fix Lucky command error
This commit is contained in:
Ben Chatelain 2019-01-18 17:45:12 -07:00 committed by GitHub
commit ebafed11f5
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
14 changed files with 42 additions and 39 deletions

View file

@ -7,7 +7,8 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
## [Unreleased]
- 🐛 Restore search table output #205
- 🐛 Fix `lucky` command #207
- 🐛 Restore `search` table output #205
## [v1.6.0] 🔗 Links - 2019-01-12

View file

@ -60,9 +60,9 @@ public struct HomeCommand: CommandProtocol {
}
public struct HomeOptions: OptionsProtocol {
let appId: String
let appId: Int
static func create(_ appId: String) -> HomeOptions {
static func create(_ appId: Int) -> HomeOptions {
return HomeOptions(appId: appId)
}

View file

@ -46,14 +46,14 @@ public struct InfoCommand: CommandProtocol {
}
public struct InfoOptions: OptionsProtocol {
let appId: String
let appId: Int
static func create(_ appId: String) -> InfoOptions {
static func create(_ appId: Int) -> InfoOptions {
return InfoOptions(appId: appId)
}
public static func evaluate(_ mode: CommandMode) -> Result<InfoOptions, CommandantError<MASError>> {
return create
<*> mode <| Argument(usage: "the app id to show info")
<*> mode <| Argument(usage: "ID of app to show info")
}
}

View file

@ -35,10 +35,10 @@ public struct LuckyCommand: CommandProtocol {
var appId: Int?
do {
guard let result = try storeSearch.lookup(app: options.appName)
else {
print("No results found")
return .failure(.noSearchResultsFound)
let results = try storeSearch.search(for: options.appName)
guard let result = results.results.first else {
print("No results found")
return .failure(.noSearchResultsFound)
}
appId = result.trackId

View file

@ -39,7 +39,13 @@ public struct OpenCommand: CommandProtocol {
return .success(())
}
guard let result = try storeSearch.lookup(app: options.appId)
guard let appId = Int(options.appId)
else {
print("Invalid app ID")
return .failure(.noSearchResultsFound)
}
guard let result = try storeSearch.lookup(app: appId)
else {
print("No results found")
return .failure(.noSearchResultsFound)
@ -83,6 +89,6 @@ public struct OpenOptions: OptionsProtocol {
public static func evaluate(_ mode: CommandMode) -> Result<OpenOptions, CommandantError<MASError>> {
return create
<*> mode <| Argument(defaultValue: markerValue, usage: "the app id")
<*> mode <| Argument(defaultValue: markerValue, usage: "the app ID")
}
}

View file

@ -63,14 +63,14 @@ public struct VendorCommand: CommandProtocol {
}
public struct VendorOptions: OptionsProtocol {
let appId: String
let appId: Int
static func create(_ appId: String) -> VendorOptions {
static func create(_ appId: Int) -> VendorOptions {
return VendorOptions(appId: appId)
}
public static func evaluate(_ mode: CommandMode) -> Result<VendorOptions, CommandantError<MASError>> {
return create
<*> mode <| Argument(usage: "the app id to show the vendor's website")
<*> mode <| Argument(usage: "the app ID to show the vendor's website")
}
}

View file

@ -48,7 +48,7 @@ public class MasStoreSearch: StoreSearch {
/// - Parameter appId: MAS ID of app
/// - Returns: Search result record of app or nil if no apps match the ID.
/// - Throws: Error if there is a problem with the network request.
public func lookup(app appId: String) throws -> SearchResult? {
public func lookup(app appId: Int) throws -> SearchResult? {
guard let url = lookupURL(forApp: appId)
else { throw MASError.urlEncoding }

View file

@ -8,7 +8,7 @@
/// Protocol for searching the MAS catalog.
public protocol StoreSearch {
func lookup(app appId: String) throws -> SearchResult?
func lookup(app appId: Int) throws -> SearchResult?
func search(for appName: String) throws -> SearchResultList
}
@ -38,7 +38,7 @@ extension StoreSearch {
///
/// - Parameter appId: MAS app identifier.
/// - Returns: URL for the lookup service or nil if appId can't be encoded.
public func lookupURL(forApp appId: String) -> URL? {
public func lookupURL(forApp appId: Int) -> URL? {
guard let urlString = lookupURLString(forApp: appId) else { return nil }
return URL(string: urlString)
}
@ -46,9 +46,8 @@ extension StoreSearch {
/// Builds the lookup URL for an app.
///
/// - Parameter appId: MAS app identifier.
/// - Returns: String URL for the lookup service or nil if appId can't be encoded.
func lookupURLString(forApp appId: String) -> String? {
guard let urlEncodedAppId = appId.URLEncodedString else { return nil }
return "https://itunes.apple.com/lookup?id=\(urlEncodedAppId)"
/// - Returns: String URL for the lookup service.
func lookupURLString(forApp appId: Int) -> String? {
return "https://itunes.apple.com/lookup?id=\(appId)"
}
}

View file

@ -27,13 +27,13 @@ class HomeCommandSpec: QuickSpec {
storeSearch.reset()
}
it("fails to open app with invalid ID") {
let result = cmd.run(HomeCommand.Options(appId: "-999"))
let result = cmd.run(HomeCommand.Options(appId: -999))
expect(result).to(beFailure { error in
expect(error) == .searchFailed
})
}
it("can't find app with unknown ID") {
let result = cmd.run(HomeCommand.Options(appId: "999"))
let result = cmd.run(HomeCommand.Options(appId: 999))
expect(result).to(beFailure { error in
expect(error) == .noSearchResultsFound
})
@ -41,7 +41,7 @@ class HomeCommandSpec: QuickSpec {
it("opens app on MAS Preview") {
storeSearch.apps[result.trackId] = result
let cmdResult = cmd.run(HomeCommand.Options(appId: result.trackId.description))
let cmdResult = cmd.run(HomeCommand.Options(appId: result.trackId))
expect(cmdResult).to(beSuccess())
expect(openCommand.arguments).toNot(beNil())
expect(openCommand.arguments!.first!) == result.trackViewUrl

View file

@ -41,13 +41,13 @@ class InfoCommandSpec: QuickSpec {
storeSearch.reset()
}
it("fails to open app with invalid ID") {
let result = cmd.run(InfoCommand.Options(appId: "-999"))
let result = cmd.run(InfoCommand.Options(appId: -999))
expect(result).to(beFailure { error in
expect(error) == .searchFailed
})
}
it("can't find app with unknown ID") {
let result = cmd.run(InfoCommand.Options(appId: "999"))
let result = cmd.run(InfoCommand.Options(appId: 999))
expect(result).to(beFailure { error in
expect(error) == .noSearchResultsFound
})
@ -57,7 +57,7 @@ class InfoCommandSpec: QuickSpec {
let output = OutputListener()
output.openConsolePipe()
let result = cmd.run(InfoCommand.Options(appId: result.trackId.description))
let result = cmd.run(InfoCommand.Options(appId: result.trackId))
expect(result).to(beSuccess())
// output is async so need to wait for contents to be updated

View file

@ -27,13 +27,13 @@ class VendorCommandSpec: QuickSpec {
storeSearch.reset()
}
it("fails to open app with invalid ID") {
let result = cmd.run(VendorCommand.Options(appId: "-999"))
let result = cmd.run(VendorCommand.Options(appId: -999))
expect(result).to(beFailure { error in
expect(error) == .searchFailed
})
}
it("can't find app with unknown ID") {
let result = cmd.run(VendorCommand.Options(appId: "999"))
let result = cmd.run(VendorCommand.Options(appId: 999))
expect(result).to(beFailure { error in
expect(error) == .noSearchResultsFound
})
@ -41,7 +41,7 @@ class VendorCommandSpec: QuickSpec {
it("opens vendor app page in browser") {
storeSearch.apps[result.trackId] = result
let cmdResult = cmd.run(VendorCommand.Options(appId: result.trackId.description))
let cmdResult = cmd.run(VendorCommand.Options(appId: result.trackId))
expect(cmdResult).to(beSuccess())
expect(openCommand.arguments).toNot(beNil())
expect(openCommand.arguments!.first!) == result.sellerUrl

View file

@ -39,7 +39,7 @@ class MasStoreSearchSpec: QuickSpec {
var lookup: SearchResult?
do {
lookup = try storeSearch.lookup(app: appId.description)
lookup = try storeSearch.lookup(app: appId)
} catch {
let maserror = error as! MASError
if case .jsonParsing(let nserror) = maserror {

View file

@ -16,16 +16,13 @@ class StoreSearchMock: StoreSearch {
return SearchResultList(resultCount: filtered.count, results: filtered.map { $1 })
}
func lookup(app appId: String) throws -> SearchResult? {
guard let number = Int(appId)
else { throw MASError.searchFailed }
func lookup(app appId: Int) throws -> SearchResult? {
// Negative numbers are invalid
if number <= 0 {
if appId <= 0 {
throw MASError.searchFailed
}
guard let result = apps[number]
guard let result = apps[appId]
else { throw MASError.noSearchResultsFound }
return result

View file

@ -12,7 +12,7 @@ import Nimble
/// Protocol minimal implementation
struct StoreSearchForTesting: StoreSearch {
func lookup(app appId: String) throws -> SearchResult? { return nil }
func lookup(app appId: Int) throws -> SearchResult? { return nil }
func search(for appName: String) throws -> SearchResultList { return SearchResultList(resultCount: 0, results: []) }
}