microsoft/vscode-react-native
Publicmirrored fromhttps://github.com/microsoft/vscode-react-nativeAvailable
test/extension/ios/simulatorPlist.test.ts
77lines · 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 "../../../src/extension/ios/simulatorPlist"; |
| 5 | |
| 6 | import * as assert from "assert"; |
| 7 | import * as path from "path"; |
| 8 | import * as Q from "q"; |
| 9 | import * as sinon from "sinon"; |
| 10 | |
| 11 | suite("plistBuddy", function() { |
| 12 | suite("extensionContext", function() { |
| 13 | test("findPlistFile should correctly find the NSUserDefaults plist file for the simulator", function() { |
| 14 | const projectRoot = path.join("/", "tmp", "myProject"); |
| 15 | |
| 16 | const bundleId = "com.contoso.app"; |
| 17 | |
| 18 | const findSimulatorHomeCommand = "xcrun simctl getenv booted HOME"; |
| 19 | // The emulator's home folder is /simulator/home |
| 20 | const findSimulatorHomeResult = path.join("/", "Users", "theUser", "Library", "Developer", "CoreSimulaotr", "Devices", "FA511653-BA51-479F-A218-1DBD1910D5E5/data"); |
| 21 | |
| 22 | const prefix = path.join("Containers", "Data", "Application"); |
| 23 | const suffix = path.join("Library", "Preferences"); |
| 24 | |
| 25 | // The emulator has 3 apps |
| 26 | const appIds = ["17F3AED1-5B1D-4F97-B419-D1F079D9DE2D", |
| 27 | "957660FD-3417-474E-B2AC-8AA0A05AD9A0", |
| 28 | "18319C8B-0583-4967-8023-15859A0BF0F3", |
| 29 | ]; |
| 30 | |
| 31 | // readdir finds appIds |
| 32 | const mockReadDir = sinon.stub(); |
| 33 | mockReadDir.withArgs(path.join(findSimulatorHomeResult, prefix)).returns(Q.resolve(appIds)); |
| 34 | mockReadDir.throws(); |
| 35 | |
| 36 | // Only the second app has a plist file with thus bundle name |
| 37 | const existingPlistFile = path.join(findSimulatorHomeResult, prefix, "957660FD-3417-474E-B2AC-8AA0A05AD9A0", suffix, `${bundleId}.plist`); |
| 38 | |
| 39 | // existsSync only finds existingPlistFile to exist |
| 40 | const mockExistsSync = sinon.stub(); |
| 41 | mockExistsSync.withArgs(existingPlistFile).returns(true); |
| 42 | mockExistsSync.returns(false); |
| 43 | |
| 44 | const mockFS: any = { |
| 45 | existsSync: mockExistsSync, |
| 46 | readDir: mockReadDir, |
| 47 | }; |
| 48 | |
| 49 | // getBundleId returns bundleId |
| 50 | const bundleIdStub = sinon.stub(); |
| 51 | bundleIdStub.withArgs(projectRoot).returns(Q.resolve(bundleId)); |
| 52 | bundleIdStub.returns(Q.reject("Incorrect project root")); |
| 53 | |
| 54 | const mockPlistBuddy: any = { |
| 55 | getBundleId: bundleIdStub, |
| 56 | }; |
| 57 | |
| 58 | // exec-ing the correct command returns the simulator home |
| 59 | const execStub = sinon.stub(); |
| 60 | execStub.withArgs(findSimulatorHomeCommand).returns({ outcome: Q.resolve(findSimulatorHomeResult) }); |
| 61 | execStub.throws(); |
| 62 | const mockChildProcess: any = { |
| 63 | exec: execStub, |
| 64 | }; |
| 65 | |
| 66 | const simulatorPlist = new SimulatorPlist(projectRoot, { |
| 67 | nodeFileSystem: mockFS, |
| 68 | plistBuddy: mockPlistBuddy, |
| 69 | nodeChildProcess: mockChildProcess, |
| 70 | }); |
| 71 | |
| 72 | return simulatorPlist.findPlistFile().then((plistFile) => { |
| 73 | assert(plistFile === existingPlistFile, "Returned incorrect value"); |
| 74 | }); |
| 75 | }); |
| 76 | }); |
| 77 | }); |