microsoft/vscode-react-native
Publicmirrored from https://github.com/microsoft/vscode-react-nativeAvailable
src/test/common/ios/simulatorPlist.test.ts
75lines · modecode
| 1 | // Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | // Licensed under the MIT license. See LICENSE file in the project root for details. |
| 3 | |
| 4 | import {SimulatorPlist} from "../../../common/ios/simulatorPlist"; |
| 5 | |
| 6 | import * as path from "path"; |
| 7 | import * as Q from "q"; |
| 8 | import * as sinon from "sinon"; |
| 9 | |
| 10 | suite("plistBuddy", function() { |
| 11 | suite("commonContext", function() { |
| 12 | test("Should correctly find the NSUserDefaults plist file for the simulator", function() { |
| 13 | const projectRoot = path.join("project", "root"); |
| 14 | |
| 15 | const bundleId = "com.contoso.app"; |
| 16 | |
| 17 | const findSimulatorHomeCommand = "xcrun simctl getenv booted HOME"; |
| 18 | // The emulator's home folder is /simulator/home |
| 19 | const findSimulatorHomeResult = path.join("/", "simulator", "home"); |
| 20 | |
| 21 | const prefix = path.join("Containers", "Data", "Application"); |
| 22 | const suffix = path.join("Library", "Preferences"); |
| 23 | |
| 24 | // The emulator has 3 apps, app1 app2 and app3 |
| 25 | const appIds = ["app1", "app2", "app3"]; |
| 26 | |
| 27 | // readdir finds appIds |
| 28 | const mockReadDir = sinon.stub(); |
| 29 | mockReadDir.withArgs(path.join(findSimulatorHomeResult, prefix)).returns(Q.resolve(appIds)); |
| 30 | mockReadDir.throws(); |
| 31 | |
| 32 | // Only app2 has a plist file with thus bundle name |
| 33 | const existingPlistFile = path.join(findSimulatorHomeResult, prefix, "app2", suffix, `${bundleId}.plist`); |
| 34 | |
| 35 | // existsSync only finds existingPlistFile to exist |
| 36 | const mockExistsSync = sinon.stub(); |
| 37 | mockExistsSync.withArgs(existingPlistFile).returns(true); |
| 38 | mockExistsSync.returns(false); |
| 39 | |
| 40 | const mockFS: any = { |
| 41 | existsSync: mockExistsSync, |
| 42 | readDir: mockReadDir |
| 43 | }; |
| 44 | |
| 45 | // getBundleId returns bundleId |
| 46 | const bundleIdStub = sinon.stub(); |
| 47 | bundleIdStub.withArgs(projectRoot).returns(Q.resolve(bundleId)); |
| 48 | bundleIdStub.returns(Q.reject("Incorrect project root")); |
| 49 | |
| 50 | const mockPlistBuddy: any = { |
| 51 | getBundleId: bundleIdStub |
| 52 | }; |
| 53 | |
| 54 | // exec-ing the correct command returns the simulator home |
| 55 | const execStub = sinon.stub(); |
| 56 | execStub.withArgs(findSimulatorHomeCommand).returns({ outcome: Q.resolve(findSimulatorHomeResult) }); |
| 57 | execStub.throws(); |
| 58 | const mockChildProcess: any = { |
| 59 | exec: execStub |
| 60 | }; |
| 61 | |
| 62 | const simulatorPlist = new SimulatorPlist(projectRoot, { |
| 63 | nodeFileSystem: mockFS, |
| 64 | plistBuddy: mockPlistBuddy, |
| 65 | nodeChildProcess: mockChildProcess |
| 66 | }); |
| 67 | |
| 68 | return simulatorPlist.findPlistFile().then((plistFile) => { |
| 69 | if (plistFile !== existingPlistFile) { |
| 70 | throw new Error("Returned incorrect value"); |
| 71 | } |
| 72 | }); |
| 73 | }); |
| 74 | }); |
| 75 | }); |