microsoft/vscode-react-native
Publicmirrored from https://github.com/microsoft/vscode-react-nativeAvailable
src/test/common/ios/simulatorPlist.test.ts
77lines · modeblame
db80cd4eJimmy Thomson10 years ago | 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 | | |
f5edf87fJimmy Thomson10 years ago | 6 | import * as assert from "assert"; |
db80cd4eJimmy Thomson10 years ago | 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("commonContext", function() { | |
2bc72bf2Jimmy Thomson10 years ago | 13 | test("findPlistFile should correctly find the NSUserDefaults plist file for the simulator", function() { |
| 14 | const projectRoot = path.join("/", "tmp", "myProject"); | |
db80cd4eJimmy Thomson10 years ago | 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 | |
2bc72bf2Jimmy Thomson10 years ago | 20 | const findSimulatorHomeResult = path.join("/", "Users", "theUser", "Library", "Developer", "CoreSimulaotr", "Devices", "FA511653-BA51-479F-A218-1DBD1910D5E5/data"); |
db80cd4eJimmy Thomson10 years ago | 21 | |
| 22 | const prefix = path.join("Containers", "Data", "Application"); | |
| 23 | const suffix = path.join("Library", "Preferences"); | |
| 24 | | |
2bc72bf2Jimmy Thomson10 years ago | 25 | // The emulator has 3 apps |
| 26 | const appIds = ["17F3AED1-5B1D-4F97-B419-D1F079D9DE2D", | |
| 27 | "957660FD-3417-474E-B2AC-8AA0A05AD9A0", | |
cdf34447digeff10 years ago | 28 | "18319C8B-0583-4967-8023-15859A0BF0F3", |
| 29 | ]; | |
db80cd4eJimmy Thomson10 years ago | 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 | | |
2bc72bf2Jimmy Thomson10 years ago | 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`); | |
db80cd4eJimmy Thomson10 years ago | 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, | |
cdf34447digeff10 years ago | 46 | readDir: mockReadDir, |
db80cd4eJimmy Thomson10 years ago | 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 = { | |
cdf34447digeff10 years ago | 55 | getBundleId: bundleIdStub, |
db80cd4eJimmy Thomson10 years ago | 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 = { | |
cdf34447digeff10 years ago | 63 | exec: execStub, |
db80cd4eJimmy Thomson10 years ago | 64 | }; |
| 65 | | |
| 66 | const simulatorPlist = new SimulatorPlist(projectRoot, { | |
| 67 | nodeFileSystem: mockFS, | |
| 68 | plistBuddy: mockPlistBuddy, | |
cdf34447digeff10 years ago | 69 | nodeChildProcess: mockChildProcess, |
db80cd4eJimmy Thomson10 years ago | 70 | }); |
| 71 | | |
| 72 | return simulatorPlist.findPlistFile().then((plistFile) => { | |
f5edf87fJimmy Thomson10 years ago | 73 | assert(plistFile === existingPlistFile, "Returned incorrect value"); |
db80cd4eJimmy Thomson10 years ago | 74 | }); |
| 75 | }); | |
| 76 | }); | |
| 77 | }); |