Skip to content

Commit

Permalink
Merge pull request #571 from rgoldberg/555-testing
Browse files Browse the repository at this point in the history
Improve tests
  • Loading branch information
rgoldberg authored Oct 14, 2024
2 parents 751e47b + 265326d commit ac3599d
Show file tree
Hide file tree
Showing 35 changed files with 247 additions and 323 deletions.
1 change: 1 addition & 0 deletions .swiftformat
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
# Disabled rules
--disable blankLinesAroundMark
--disable consecutiveSpaces
--disable hoistAwait
--disable hoistPatternLet
--disable hoistTry
--disable indent
Expand Down
26 changes: 22 additions & 4 deletions Package.resolved
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@
"kind" : "remoteSourceControl",
"location" : "https://github.com/Quick/Nimble.git",
"state" : {
"revision" : "1f3bde57bde12f5e7b07909848c071e9b73d6edc",
"version" : "10.0.0"
"revision" : "6416749c3c0488664fff6b42f8bf3ea8dc282ca1",
"version" : "13.6.0"
}
},
{
Expand All @@ -41,8 +41,8 @@
"kind" : "remoteSourceControl",
"location" : "https://github.com/Quick/Quick.git",
"state" : {
"revision" : "f9d519828bb03dfc8125467d8f7b93131951124c",
"version" : "5.0.1"
"revision" : "1163a1b1b114a657c7432b63dd1f92ce99fe11a6",
"version" : "7.6.2"
}
},
{
Expand All @@ -54,6 +54,15 @@
"version" : "2.1.1"
}
},
{
"identity" : "swift-algorithms",
"kind" : "remoteSourceControl",
"location" : "https://github.com/apple/swift-algorithms.git",
"state" : {
"revision" : "f6919dfc309e7f1b56224378b11e28bab5bccc42",
"version" : "1.2.0"
}
},
{
"identity" : "swift-argument-parser",
"kind" : "remoteSourceControl",
Expand All @@ -63,6 +72,15 @@
"version" : "1.5.0"
}
},
{
"identity" : "swift-numerics",
"kind" : "remoteSourceControl",
"location" : "https://github.com/apple/swift-numerics.git",
"state" : {
"revision" : "0a5bc04095a675662cf24757cc0640aa2204253b",
"version" : "1.0.2"
}
},
{
"identity" : "version",
"kind" : "remoteSourceControl",
Expand Down
4 changes: 2 additions & 2 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ let package = Package(
],
dependencies: [
// Dependencies declare other packages that this package depends on.
.package(url: "https://github.com/Quick/Nimble.git", from: "10.0.0"),
.package(url: "https://github.com/Quick/Quick.git", from: "5.0.0"),
.package(url: "https://github.com/Quick/Nimble.git", from: "13.6.0"),
.package(url: "https://github.com/Quick/Quick.git", from: "7.6.2"),
.package(url: "https://github.com/apple/swift-argument-parser.git", from: "1.5.0"),
.package(url: "https://github.com/mxcl/PromiseKit.git", from: "6.22.1"),
.package(url: "https://github.com/mxcl/Version.git", from: "2.1.0"),
Expand Down
84 changes: 27 additions & 57 deletions Sources/mas/Formatters/Utilities.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,63 +13,6 @@ import Foundation
/// Terminal Control Sequence Indicator
let csi = "\u{001B}["

#if DEBUG

var printObserver: ((String) -> Void)?

// Override global print for testability.
// See masTests/OutputListener.swift.
func print(
_ items: Any...,
separator: String = " ",
terminator: String = "\n"
) {
if let observer = printObserver {
let output =
items
.map { "\($0)" }
.joined(separator: separator)
.appending(terminator)
observer(output)
}

var prefix = ""
for item in items {
Swift.print(prefix, terminator: "")
Swift.print(item, terminator: "")
prefix = separator
}

Swift.print(terminator, terminator: "")
}

func print(
_ items: Any...,
separator: String = " ",
terminator: String = "\n",
to output: inout some TextOutputStream
) {
if let observer = printObserver {
let output =
items
.map { "\($0)" }
.joined(separator: separator)
.appending(terminator)
observer(output)
}

var prefix = ""
for item in items {
Swift.print(prefix, terminator: "", to: &output)
Swift.print(item, terminator: "", to: &output)
prefix = separator
}

Swift.print(terminator, terminator: "", to: &output)
}

#endif

private var standardError = FileHandle.standardError

extension FileHandle: TextOutputStream {
Expand Down Expand Up @@ -121,3 +64,30 @@ func clearLine() {
print("\(csi)2K\(csi)0G", terminator: "")
fflush(stdout)
}

func captureStream(
_ stream: UnsafeMutablePointer<FILE>,
encoding: String.Encoding = .utf8,
_ block: @escaping () throws -> Void
) throws -> String {
let originalFd = fileno(stream)
let duplicateFd = dup(originalFd)
defer {
close(duplicateFd)
}

let pipe = Pipe()
dup2(pipe.fileHandleForWriting.fileDescriptor, originalFd)

do {
defer {
fflush(stream)
dup2(duplicateFd, originalFd)
pipe.fileHandleForWriting.closeFile()
}

try block()
}

return String(data: pipe.fileHandleForReading.readDataToEndOfFile(), encoding: encoding) ?? ""
}
8 changes: 4 additions & 4 deletions Tests/masTests/Commands/AccountSpec.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,17 @@ import Quick

// Deprecated test
public class AccountSpec: QuickSpec {
override public func spec() {
override public static func spec() {
beforeSuite {
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))
}
}
}
Expand Down
22 changes: 11 additions & 11 deletions Tests/masTests/Commands/HomeSpec.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,7 @@ import Quick
@testable import mas

public class HomeSpec: QuickSpec {
override public func spec() {
let result = SearchResult(
trackId: 1111,
trackViewUrl: "mas preview url",
version: "0.0"
)
override public static func spec() {
let storeSearch = StoreSearchMock()
let openCommand = OpenSystemCommandMock()

Expand All @@ -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]
}
}
}
Expand Down
52 changes: 26 additions & 26 deletions Tests/masTests/Commands/InfoSpec.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,34 +6,15 @@
// Copyright © 2018 mas-cli. All rights reserved.
//

import Foundation
import Nimble
import Quick

@testable import mas

public class InfoSpec: QuickSpec {
override public 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"
)
override public static func spec() {
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()
Expand All @@ -55,13 +36,32 @@ public class InfoSpec: QuickSpec {
.to(throwError(MASError.noSearchResultsFound))
}
it("displays app details") {
storeSearch.apps[result.trackId] = result
let output = OutputListener()
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
expect {
try Mas.Info.parse([String(result.trackId)]).run(storeSearch: storeSearch)
try captureStream(stdout) {
try Mas.Info.parse([String(mockResult.trackId)]).run(storeSearch: storeSearch)
}
}
.toNot(throwError())
expect(output.contents) == 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
"""
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion Tests/masTests/Commands/InstallSpec.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import Quick
@testable import mas

public class InstallSpec: QuickSpec {
override public func spec() {
override public static func spec() {
beforeSuite {
Mas.initialize()
}
Expand Down
9 changes: 6 additions & 3 deletions Tests/masTests/Commands/ListSpec.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,25 @@
// Copyright © 2018 mas-cli. All rights reserved.
//

import Foundation
import Nimble
import Quick

@testable import mas

public class ListSpec: QuickSpec {
override public func spec() {
override public static func spec() {
beforeSuite {
Mas.initialize()
}
describe("list command") {
it("lists apps") {
expect {
try Mas.List.parse([]).run(appLibrary: AppLibraryMock())
try captureStream(stderr) {
try Mas.List.parse([]).run(appLibrary: AppLibraryMock())
}
}
.toNot(throwError())
== "Error: No installed apps found\n"
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions Tests/masTests/Commands/LuckySpec.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,14 @@ import Quick
@testable import mas

public class LuckySpec: QuickSpec {
override public func spec() {
override public static func spec() {
let networkSession = NetworkSessionMockFromFile(responseFile: "search/slack.json")
let storeSearch = MasStoreSearch(networkManager: NetworkManager(session: networkSession))

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)
Expand Down
Loading

0 comments on commit ac3599d

Please sign in to comment.