Skip to content

Commit

Permalink
feat: add AppleUtilsTest
Browse files Browse the repository at this point in the history
  • Loading branch information
okwasniewski committed Sep 10, 2024
1 parent 6ca3bd0 commit 547310c
Show file tree
Hide file tree
Showing 4 changed files with 123 additions and 2 deletions.
4 changes: 4 additions & 0 deletions MiniSim.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@
76BF0AEE2C905C43003BE568 /* IOSDeviceService.swift in Sources */ = {isa = PBXBuildFile; fileRef = 76BF0AED2C905C43003BE568 /* IOSDeviceService.swift */; };
76BF0AF02C9061E8003BE568 /* DeviceDiscoveryService.swift in Sources */ = {isa = PBXBuildFile; fileRef = 76BF0AEF2C9061E8003BE568 /* DeviceDiscoveryService.swift */; };
76BF0AF22C907033003BE568 /* DeviceServiceFactory.swift in Sources */ = {isa = PBXBuildFile; fileRef = 76BF0AF12C907032003BE568 /* DeviceServiceFactory.swift */; };
76BF0AF42C90A74E003BE568 /* AppleUtilsTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 76BF0AF32C90A74E003BE568 /* AppleUtilsTests.swift */; };
76C1396A2C849A3F006CD80C /* MenuIcons.swift in Sources */ = {isa = PBXBuildFile; fileRef = 76C139692C849A3F006CD80C /* MenuIcons.swift */; };
76E4451229D4391000039025 /* Onboarding.swift in Sources */ = {isa = PBXBuildFile; fileRef = 76E4451129D4391000039025 /* Onboarding.swift */; };
76E4451429D4403F00039025 /* NSNotificationName.swift in Sources */ = {isa = PBXBuildFile; fileRef = 76E4451329D4403F00039025 /* NSNotificationName.swift */; };
Expand Down Expand Up @@ -198,6 +199,7 @@
76BF0AED2C905C43003BE568 /* IOSDeviceService.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = IOSDeviceService.swift; sourceTree = "<group>"; };
76BF0AEF2C9061E8003BE568 /* DeviceDiscoveryService.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DeviceDiscoveryService.swift; sourceTree = "<group>"; };
76BF0AF12C907032003BE568 /* DeviceServiceFactory.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DeviceServiceFactory.swift; sourceTree = "<group>"; };
76BF0AF32C90A74E003BE568 /* AppleUtilsTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppleUtilsTests.swift; sourceTree = "<group>"; };
76C139692C849A3F006CD80C /* MenuIcons.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = MenuIcons.swift; sourceTree = "<group>"; };
76E4451129D4391000039025 /* Onboarding.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Onboarding.swift; sourceTree = "<group>"; };
76E4451329D4403F00039025 /* NSNotificationName.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NSNotificationName.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -448,6 +450,7 @@
76B70F812B0D50FE009D87A4 /* ADBTests.swift */,
76BF0ADC2C8DF660003BE568 /* AccessibilityElementTests.swift */,
76BF0AE22C8E041C003BE568 /* CustomCommandServiceTests.swift */,
76BF0AF32C90A74E003BE568 /* AppleUtilsTests.swift */,
);
path = MiniSimTests;
sourceTree = "<group>";
Expand Down Expand Up @@ -718,6 +721,7 @@
7699511F2C845CBA00462287 /* DeviceParserTests.swift in Sources */,
76BF0ADD2C8DF660003BE568 /* AccessibilityElementTests.swift in Sources */,
76BF0AE32C8E041C003BE568 /* CustomCommandServiceTests.swift in Sources */,
76BF0AF42C90A74E003BE568 /* AppleUtilsTests.swift in Sources */,
76B70F7E2B0D361A009D87A4 /* UserDefaultsTests.swift in Sources */,
760DEACE2B0DFB6600253576 /* ShellStub.swift in Sources */,
76B70F822B0D50FE009D87A4 /* ADBTests.swift in Sources */,
Expand Down
3 changes: 2 additions & 1 deletion MiniSim/Service/AppleUtils.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import AppKit

class AppleUtils {

Check warning on line 3 in MiniSim/Service/AppleUtils.swift

View workflow job for this annotation

GitHub Actions / lint

Convenience Type Violation: Types used for hosting only static members should be implemented as a caseless enum to avoid instantiation (convenience_type)
static var shell: ShellProtocol = Shell()
static var workspace: NSWorkspace = .shared

static func clearDerivedData(
completionQueue: DispatchQueue = .main,
Expand All @@ -24,7 +25,7 @@ class AppleUtils {
}

static func launchSimulatorApp(uuid: String) throws {
let isSimulatorRunning = NSWorkspace.shared.runningApplications
let isSimulatorRunning = workspace.runningApplications
.contains { $0.bundleIdentifier == "com.apple.iphonesimulator" }

if !isSimulatorRunning {
Expand Down
2 changes: 1 addition & 1 deletion MiniSim/Service/CustomErrors/DeviceError.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
//
import Foundation

enum DeviceError: Error {
enum DeviceError: Error, Equatable {
// Throw when device was not found
case deviceNotFound

Expand Down
116 changes: 116 additions & 0 deletions MiniSimTests/AppleUtilsTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
@testable import MiniSim
import XCTest

class MockNSWorkspace: NSWorkspace {
var mockRunningApplications: [NSRunningApplication] = []

override var runningApplications: [NSRunningApplication] {
mockRunningApplications
}
}

class MockNSRunningApplication: NSRunningApplication {
let mockBundleIdentifier: String?

init(bundleIdentifier: String?) {
self.mockBundleIdentifier = bundleIdentifier
super.init()
}

override var bundleIdentifier: String? {
mockBundleIdentifier
}
}

class AppleUtilsTests: XCTestCase {
var shellStub: ShellStub!
var mockWorkspace: MockNSWorkspace!

override func setUp() {
super.setUp()
shellStub = ShellStub()

mockWorkspace = MockNSWorkspace()
AppleUtils.shell = shellStub
AppleUtils.workspace = mockWorkspace
}

override func tearDown() {
shellStub.tearDown()
super.tearDown()
}

func testClearDerivedData() {
let expectation = self.expectation(description: "Completion handler called")

shellStub.mockedExecute = { command, _, _ in
if command.contains("du -sh") {
return "100M \(DeviceConstants.derivedDataLocation)"
}
return ""
}

AppleUtils.clearDerivedData { amountCleared, error in
XCTAssertEqual(amountCleared, "100M")
XCTAssertNil(error)
expectation.fulfill()
}

waitForExpectations(timeout: 5, handler: nil)

XCTAssertTrue(shellStub.lastExecutedCommand.contains("rm -rf"))
XCTAssertTrue(shellStub.lastExecutedCommand.contains(DeviceConstants.derivedDataLocation))
}

func testClearDerivedDataWithError() {
let expectation = self.expectation(description: "Completion handler called")

shellStub.mockedExecute = { _, _, _ in
throw NSError(domain: "TestError", code: 1, userInfo: nil)
}

AppleUtils.clearDerivedData { amountCleared, error in
XCTAssertEqual(amountCleared, "")
XCTAssertNotNil(error)
expectation.fulfill()
}

waitForExpectations(timeout: 5, handler: nil)
}

func testLaunchSimulatorAppWhenNotRunning() {
let uuid = "test-uuid"
mockWorkspace.mockRunningApplications = [] // Simulator not running

shellStub.mockedExecute = { command, _, _ in
if command == DeviceConstants.ProcessPaths.xcodeSelect.rawValue {
return "/Applications/Xcode.app/Contents/Developer"
}
return ""
}

XCTAssertNoThrow(try AppleUtils.launchSimulatorApp(uuid: uuid))

XCTAssertEqual(shellStub.lastExecutedCommand, "/Applications/Xcode.app/Contents/Developer/Applications/Simulator.app/Contents/MacOS/Simulator")
XCTAssertEqual(shellStub.lastPassedArguments, ["--args", "-CurrentDeviceUDID", uuid])
}

func testLaunchSimulatorAppWhenAlreadyRunning() {
let uuid = "test-uuid"
mockWorkspace.mockRunningApplications = [MockNSRunningApplication(bundleIdentifier: "com.apple.iphonesimulator")]

XCTAssertNoThrow(try AppleUtils.launchSimulatorApp(uuid: uuid))

XCTAssertTrue(shellStub.lastExecutedCommand.isEmpty, "Should not execute any command when simulator is already running")
}

func testLaunchSimulatorAppWithXcodeError() {
shellStub.mockedExecute = { _, _, _ in
throw DeviceError.xcodeError
}

XCTAssertThrowsError(try AppleUtils.launchSimulatorApp(uuid: "test-uuid")) { error in
XCTAssertEqual(error as? DeviceError, DeviceError.xcodeError)
}
}
}

0 comments on commit 547310c

Please sign in to comment.