Improve tests.

Signed-off-by: Ross Goldberg <484615+rgoldberg@users.noreply.github.com>
This commit is contained in:
Ross Goldberg 2024-10-01 22:16:06 -04:00
parent f8d7a36a4c
commit dccac33abb
No known key found for this signature in database
20 changed files with 116 additions and 142 deletions

View file

@ -10,6 +10,7 @@
# Disabled rules
--disable blankLinesAroundMark
--disable consecutiveSpaces
--disable hoistAwait
--disable hoistPatternLet
--disable hoistTry
--disable indent

View file

@ -18,12 +18,12 @@ public class AccountSpec: QuickSpec {
Mas.initialize()
}
// account command disabled since macOS 12 Monterey https://github.com/mas-cli/mas#%EF%B8%8F-known-issues
xdescribe("Account command") {
xit("displays active account") {
describe("Account command") {
it("displays active account") {
expect {
try Mas.Account.parse([]).run()
}
.toNot(throwError())
.to(throwError(MASError.notSupported))
}
}
}

View file

@ -13,11 +13,6 @@ import Quick
public class HomeSpec: QuickSpec {
override public static func spec() {
let result = SearchResult(
trackId: 1111,
trackViewUrl: "mas preview url",
version: "0.0"
)
let storeSearch = StoreSearchMock()
let openCommand = OpenSystemCommandMock()
@ -41,13 +36,18 @@ public class HomeSpec: QuickSpec {
.to(throwError(MASError.noSearchResultsFound))
}
it("opens app on MAS Preview") {
storeSearch.apps[result.trackId] = result
let mockResult = SearchResult(
trackId: 1111,
trackViewUrl: "mas preview url",
version: "0.0"
)
storeSearch.apps[mockResult.trackId] = mockResult
expect {
try Mas.Home.parse([String(result.trackId)]).run(storeSearch: storeSearch, openCommand: openCommand)
try Mas.Home.parse([String(mockResult.trackId)])
.run(storeSearch: storeSearch, openCommand: openCommand)
return openCommand.arguments
}
.toNot(throwError())
expect(openCommand.arguments).toNot(beNil())
expect(openCommand.arguments!.first!) == result.trackViewUrl
== [mockResult.trackViewUrl]
}
}
}

View file

@ -13,27 +13,7 @@ import Quick
public class InfoSpec: QuickSpec {
override public static func spec() {
let result = SearchResult(
currentVersionReleaseDate: "2019-01-07T18:53:13Z",
fileSizeBytes: "1024",
minimumOsVersion: "10.14",
price: 2.0,
sellerName: "Awesome Dev",
trackId: 1111,
trackName: "Awesome App",
trackViewUrl: "https://awesome.app",
version: "1.0"
)
let storeSearch = StoreSearchMock()
let expectedOutput = """
Awesome App 1.0 [2.0]
By: Awesome Dev
Released: 2019-01-07
Minimum OS: 10.14
Size: 1 KB
From: https://awesome.app
"""
beforeSuite {
Mas.initialize()
@ -55,13 +35,32 @@ public class InfoSpec: QuickSpec {
.to(throwError(MASError.noSearchResultsFound))
}
it("displays app details") {
storeSearch.apps[result.trackId] = result
let mockResult = SearchResult(
currentVersionReleaseDate: "2019-01-07T18:53:13Z",
fileSizeBytes: "1024",
minimumOsVersion: "10.14",
price: 2.0,
sellerName: "Awesome Dev",
trackId: 1111,
trackName: "Awesome App",
trackViewUrl: "https://awesome.app",
version: "1.0"
)
storeSearch.apps[mockResult.trackId] = mockResult
let output = OutputListener()
expect {
try Mas.Info.parse([String(result.trackId)]).run(storeSearch: storeSearch)
try Mas.Info.parse([String(mockResult.trackId)]).run(storeSearch: storeSearch)
}
.toNot(throwError())
expect(output.contents) == expectedOutput
expect(output.contents) == """
Awesome App 1.0 [2.0]
By: Awesome Dev
Released: 2019-01-07
Minimum OS: 10.14
Size: 1 KB
From: https://awesome.app
"""
}
}
}

View file

@ -19,7 +19,7 @@ public class LuckySpec: QuickSpec {
beforeSuite {
Mas.initialize()
}
describe("lucky command") {
xdescribe("lucky command") {
xit("installs the first app matching a search") {
expect {
try Mas.Lucky.parse(["Slack"]).run(appLibrary: AppLibraryMock(), storeSearch: storeSearch)

View file

@ -14,11 +14,6 @@ import Quick
public class OpenSpec: QuickSpec {
override public static func spec() {
let result = SearchResult(
trackId: 1111,
trackViewUrl: "fakescheme://some/url",
version: "0.0"
)
let storeSearch = StoreSearchMock()
let openCommand = OpenSystemCommandMock()
@ -42,26 +37,25 @@ public class OpenSpec: QuickSpec {
.to(throwError(MASError.noSearchResultsFound))
}
it("opens app in MAS") {
storeSearch.apps[result.trackId] = result
let mockResult = SearchResult(
trackId: 1111,
trackViewUrl: "fakescheme://some/url",
version: "0.0"
)
storeSearch.apps[mockResult.trackId] = mockResult
expect {
try Mas.Open.parse([result.trackId.description])
try Mas.Open.parse([mockResult.trackId.description])
.run(storeSearch: storeSearch, openCommand: openCommand)
return openCommand.arguments
}
.toNot(throwError())
expect(openCommand.arguments).toNot(beNil())
let url = URL(string: openCommand.arguments!.first!)
expect(url).toNot(beNil())
expect(url?.scheme) == "macappstore"
== ["macappstore://some/url"]
}
it("just opens MAS if no app specified") {
expect {
try Mas.Open.parse([]).run(storeSearch: storeSearch, openCommand: openCommand)
return openCommand.arguments
}
.toNot(throwError())
expect(openCommand.arguments).toNot(beNil())
let url = URL(string: openCommand.arguments!.first!)
expect(url).toNot(beNil())
expect(url) == URL(string: "macappstore://")
== ["macappstore://"]
}
}
}

View file

@ -19,8 +19,7 @@ public class OutdatedSpec: QuickSpec {
describe("outdated command") {
it("displays apps with pending updates") {
expect {
try Mas.Outdated.parse(["--verbose"])
.run(appLibrary: AppLibraryMock(), storeSearch: StoreSearchMock())
try Mas.Outdated.parse([]).run(appLibrary: AppLibraryMock(), storeSearch: StoreSearchMock())
}
.toNot(throwError())
}

View file

@ -13,12 +13,6 @@ import Quick
public class SearchSpec: QuickSpec {
override public static func spec() {
let result = SearchResult(
trackId: 1111,
trackName: "slack",
trackViewUrl: "mas preview url",
version: "0.0"
)
let storeSearch = StoreSearchMock()
beforeSuite {
@ -29,7 +23,13 @@ public class SearchSpec: QuickSpec {
storeSearch.reset()
}
it("can find slack") {
storeSearch.apps[result.trackId] = result
let mockResult = SearchResult(
trackId: 1111,
trackName: "slack",
trackViewUrl: "mas preview url",
version: "0.0"
)
storeSearch.apps[mockResult.trackId] = mockResult
expect {
try Mas.Search.parse(["slack"]).run(storeSearch: storeSearch)
}

View file

@ -18,12 +18,12 @@ public class SignInSpec: QuickSpec {
Mas.initialize()
}
// account command disabled since macOS 10.13 High Sierra https://github.com/mas-cli/mas#%EF%B8%8F-known-issues
xdescribe("signin command") {
xit("signs in") {
describe("signin command") {
it("signs in") {
expect {
try Mas.SignIn.parse(["", ""]).run()
}
.toNot(throwError())
.to(throwError(MASError.notSupported))
}
}
}

View file

@ -13,11 +13,6 @@ import Quick
public class VendorSpec: QuickSpec {
override public static func spec() {
let result = SearchResult(
trackId: 1111,
trackViewUrl: "https://awesome.app",
version: "0.0"
)
let storeSearch = StoreSearchMock()
let openCommand = OpenSystemCommandMock()
@ -41,14 +36,19 @@ public class VendorSpec: QuickSpec {
.to(throwError(MASError.noSearchResultsFound))
}
it("opens vendor app page in browser") {
storeSearch.apps[result.trackId] = result
let mockResult = SearchResult(
sellerUrl: "https://awesome.app",
trackId: 1111,
trackViewUrl: "https://apps.apple.com/us/app/awesome/id1111?mt=12&uo=4",
version: "0.0"
)
storeSearch.apps[mockResult.trackId] = mockResult
expect {
try Mas.Vendor.parse([String(result.trackId)])
try Mas.Vendor.parse([String(mockResult.trackId)])
.run(storeSearch: storeSearch, openCommand: openCommand)
return openCommand.arguments
}
.toNot(throwError())
expect(openCommand.arguments).toNot(beNil())
expect(openCommand.arguments!.first!) == result.sellerUrl
== [mockResult.sellerUrl]
}
}
}

View file

@ -20,12 +20,11 @@ public class MasAppLibrarySpec: QuickSpec {
}
describe("mas app library") {
it("contains all installed apps") {
expect(library.installedApps.count) == apps.count
expect(library.installedApps).to(haveCount(apps.count))
expect(library.installedApps.first!.appName) == myApp.appName
}
it("can locate an app by bundle id") {
let app = library.installedApp(forBundleId: "com.example")!
expect(app.bundleIdentifier) == myApp.bundleIdentifier
expect(library.installedApp(forBundleId: "com.example")!.bundleIdentifier) == myApp.bundleIdentifier
}
}
}

View file

@ -19,18 +19,16 @@ public class MasStoreSearchSpec: QuickSpec {
describe("url string") {
it("contains the app name") {
let appName = "myapp"
let urlString = MasStoreSearch().searchURL(for: appName, inCountry: "US")?.absoluteString
expect(urlString) == """
https://itunes.apple.com/search?media=software&entity=macSoftware&term=\(appName)&country=US
"""
expect {
MasStoreSearch().searchURL(for: appName, inCountry: "US")?.absoluteString
}
== "https://itunes.apple.com/search?media=software&entity=macSoftware&term=\(appName)&country=US"
}
it("contains the encoded app name") {
let appName = "My App"
let appNameEncoded = "My%20App"
let urlString = MasStoreSearch().searchURL(for: appName, inCountry: "US")?.absoluteString
expect(urlString) == """
https://itunes.apple.com/search?media=software&entity=macSoftware&term=\(appNameEncoded)&country=US
"""
expect {
MasStoreSearch().searchURL(for: "My App", inCountry: "US")?.absoluteString
}
== "https://itunes.apple.com/search?media=software&entity=macSoftware&term=My%20App&country=US"
}
}
describe("store") {
@ -39,16 +37,10 @@ public class MasStoreSearchSpec: QuickSpec {
let networkSession = NetworkSessionMockFromFile(responseFile: "search/slack.json")
let storeSearch = MasStoreSearch(networkManager: NetworkManager(session: networkSession))
var results: [SearchResult]
do {
results = try storeSearch.search(for: "slack").wait()
expect(results.count) == 39
} catch {
let maserror = error as! MASError
if case .jsonParsing(let nserror) = maserror {
fail("\(maserror) \(nserror!)")
}
expect {
try storeSearch.search(for: "slack").wait()
}
.to(haveCount(39))
}
}
@ -58,9 +50,9 @@ public class MasStoreSearchSpec: QuickSpec {
let networkSession = NetworkSessionMockFromFile(responseFile: "lookup/slack.json")
let storeSearch = MasStoreSearch(networkManager: NetworkManager(session: networkSession))
var lookup: SearchResult?
var result: SearchResult?
do {
lookup = try storeSearch.lookup(appID: appID).wait()
result = try storeSearch.lookup(appID: appID).wait()
} catch {
let maserror = error as! MASError
if case .jsonParsing(let nserror) = maserror {
@ -68,7 +60,9 @@ public class MasStoreSearchSpec: QuickSpec {
}
}
guard let result = lookup else { fatalError("lookup result was nil") }
guard let result else {
fatalError("lookup result was nil")
}
expect(result.trackId) == appID
expect(result.bundleId) == "com.tinyspeck.slackmacgap"

View file

@ -14,9 +14,7 @@ class StoreSearchMock: StoreSearch {
var apps: [AppID: SearchResult] = [:]
func search(for appName: String) -> Promise<[SearchResult]> {
let filtered = apps.filter { $1.trackName.contains(appName) }
let results = filtered.map { $1 }
return .value(results)
.value(apps.filter { $1.trackName.contains(appName) }.map { $1 })
}
func lookup(appID: AppID) -> Promise<SearchResult?> {

View file

@ -13,7 +13,7 @@ import Foundation
class OpenSystemCommandMock: ExternalCommand {
// Stub out protocol logic
var succeeded = true
var arguments: [String]?
var arguments: [String] = []
// unused
var binaryPath = "/dev/null"

View file

@ -19,12 +19,10 @@ public class OpenSystemCommandSpec: QuickSpec {
describe("open system command") {
context("binary path") {
it("defaults to the macOS open command") {
let cmd = OpenSystemCommand()
expect(cmd.binaryPath) == "/usr/bin/open"
expect(OpenSystemCommand().binaryPath) == "/usr/bin/open"
}
it("can be overridden") {
let cmd = OpenSystemCommand(binaryPath: "/dev/null")
expect(cmd.binaryPath) == "/dev/null"
expect(OpenSystemCommand(binaryPath: "/dev/null").binaryPath) == "/dev/null"
}
}
}

View file

@ -25,8 +25,7 @@ public class AppListsFormatterSpec: QuickSpec {
products = []
}
it("formats nothing as empty string") {
let output = format(products)
expect(output) == ""
expect(format(products)) == ""
}
it("can format a single product") {
let product = SoftwareProductMock(
@ -36,8 +35,7 @@ public class AppListsFormatterSpec: QuickSpec {
bundleVersion: "19.2.1",
itemIdentifier: 12345
)
let output = format([product])
expect(output) == "12345 Awesome App (19.2.1)"
expect(format([product])) == "12345 Awesome App (19.2.1)"
}
it("can format two products") {
products = [
@ -56,8 +54,8 @@ public class AppListsFormatterSpec: QuickSpec {
itemIdentifier: 67890
),
]
let output = format(products)
expect(output) == "12345 Awesome App (19.2.1)\n67890 Even Better App (1.2.0)"
expect(format(products))
== "12345 Awesome App (19.2.1)\n67890 Even Better App (1.2.0)"
}
}
}

View file

@ -25,8 +25,7 @@ public class SearchResultsFormatterSpec: QuickSpec {
results = []
}
it("formats nothing as empty string") {
let output = format(results, false)
expect(output) == ""
expect(format(results, false)) == ""
}
it("can format a single result") {
let result = SearchResult(
@ -35,8 +34,7 @@ public class SearchResultsFormatterSpec: QuickSpec {
trackName: "Awesome App",
version: "19.2.1"
)
let output = format([result], false)
expect(output) == " 12345 Awesome App (19.2.1)"
expect(format([result], false)) == " 12345 Awesome App (19.2.1)"
}
it("can format a single result with price") {
let result = SearchResult(
@ -45,8 +43,7 @@ public class SearchResultsFormatterSpec: QuickSpec {
trackName: "Awesome App",
version: "19.2.1"
)
let output = format([result], true)
expect(output) == " 12345 Awesome App $ 9.87 (19.2.1)"
expect(format([result], true)) == " 12345 Awesome App $ 9.87 (19.2.1)"
}
it("can format a two results") {
results = [
@ -63,8 +60,8 @@ public class SearchResultsFormatterSpec: QuickSpec {
version: "1.2.0"
),
]
let output = format(results, false)
expect(output) == " 12345 Awesome App (19.2.1)\n 67890 Even Better App (1.2.0)"
expect(format(results, false))
== " 12345 Awesome App (19.2.1)\n 67890 Even Better App (1.2.0)"
}
it("can format a two results with prices") {
results = [
@ -81,8 +78,7 @@ public class SearchResultsFormatterSpec: QuickSpec {
version: "1.2.0"
),
]
let output = format(results, true)
expect(output)
expect(format(results, true))
== " 12345 Awesome App $ 9.87 (19.2.1)\n 67890 Even Better App $ 0.01 (1.2.0)"
}
}

View file

@ -19,18 +19,16 @@ public class SearchResultListSpec: QuickSpec {
}
describe("search result list") {
it("can parse bbedit") {
let data = Data(from: "search/bbedit.json")
let decoder = JSONDecoder()
let results = try decoder.decode(SearchResultList.self, from: data)
expect(results.resultCount) == 1
expect(
try JSONDecoder().decode(SearchResultList.self, from: Data(from: "search/bbedit.json")).resultCount
)
== 1
}
it("can parse things") {
let data = Data(from: "search/things.json")
let decoder = JSONDecoder()
let results = try decoder.decode(SearchResultList.self, from: data)
expect(results.resultCount) == 50
expect(
try JSONDecoder().decode(SearchResultList.self, from: Data(from: "search/things.json")).resultCount
)
== 50
}
}
}

View file

@ -19,11 +19,12 @@ public class SearchResultSpec: QuickSpec {
}
describe("search result") {
it("can parse things") {
let data = Data(from: "search/things-that-go-bump.json")
let decoder = JSONDecoder()
let result = try decoder.decode(SearchResult.self, from: data)
expect(result.bundleId) == "uikitformac.com.tinybop.thingamabops"
expect(
try JSONDecoder()
.decode(SearchResult.self, from: Data(from: "search/things-that-go-bump.json"))
.bundleId
)
== "uikitformac.com.tinybop.thingamabops"
}
}
}

View file

@ -31,8 +31,7 @@ class NetworkSessionMockFromFile: NetworkSessionMock {
else { fatalError("Unable to load file \(responseFile)") }
do {
let data = try Data(contentsOf: fileURL, options: .mappedIfSafe)
return .value(data)
return .value(try Data(contentsOf: fileURL, options: .mappedIfSafe))
} catch {
print("Error opening file: \(error)")
return Promise(error: error)