|
| 1 | +import { assert } from "chai"; |
| 2 | +import { EventEmitter } from "events"; |
| 3 | +import { PassThrough } from "stream"; |
| 4 | +import { IOSSimulatorLogProvider } from "../../../mobile/ios/simulator/ios-simulator-log-provider"; |
| 5 | +import { DevicePlatformsConstants } from "../../../mobile/device-platforms-constants"; |
| 6 | +import { CommonLoggerStub } from "../stubs"; |
| 7 | + |
| 8 | +const deviceId = "simulator-id"; |
| 9 | + |
| 10 | +class LogProcessStub extends EventEmitter { |
| 11 | + public stdout = new PassThrough(); |
| 12 | + public stderr = new PassThrough(); |
| 13 | + public kill(): void {} |
| 14 | +} |
| 15 | + |
| 16 | +const nextTick = () => new Promise((resolve) => setImmediate(resolve)); |
| 17 | + |
| 18 | +describe("IOSSimulatorLogProvider", () => { |
| 19 | + let logProcess: LogProcessStub; |
| 20 | + let logger: CommonLoggerStub; |
| 21 | + let entries: Mobile.IiOSLogEntry[]; |
| 22 | + let provider: IOSSimulatorLogProvider; |
| 23 | + |
| 24 | + beforeEach(() => { |
| 25 | + logProcess = new LogProcessStub(); |
| 26 | + logger = new CommonLoggerStub(); |
| 27 | + entries = []; |
| 28 | + provider = new IOSSimulatorLogProvider( |
| 29 | + <any>{}, |
| 30 | + logger, |
| 31 | + new DevicePlatformsConstants(), |
| 32 | + <any>{ |
| 33 | + getProjectNameForDevice: () => "app", |
| 34 | + logEntry: (entry: Mobile.IiOSLogEntry) => entries.push(entry), |
| 35 | + logData: () => assert.fail("structured logs never go through logData"), |
| 36 | + }, |
| 37 | + <any>{ spawn: () => logProcess }, |
| 38 | + ); |
| 39 | + }); |
| 40 | + |
| 41 | + it("maps log stream ndjson events to log entries", async () => { |
| 42 | + await provider.startLogProcess(deviceId); |
| 43 | + logProcess.stdout.write( |
| 44 | + 'Filtering the log data using "process == \\"app\\""\n' + |
| 45 | + JSON.stringify({ |
| 46 | + eventType: "logEvent", |
| 47 | + processID: 42, |
| 48 | + threadID: 7, |
| 49 | + processImagePath: "/apps/app.app/app", |
| 50 | + senderImagePath: |
| 51 | + "/apps/app.app/Frameworks/NativeScript.framework/NativeScript", |
| 52 | + messageType: "Default", |
| 53 | + eventMessage: "CONSOLE LOG: hello", |
| 54 | + }) + |
| 55 | + "\n" + |
| 56 | + JSON.stringify({ eventType: "activityCreateEvent" }) + |
| 57 | + "\n", |
| 58 | + ); |
| 59 | + await nextTick(); |
| 60 | + |
| 61 | + assert.deepEqual(entries, [ |
| 62 | + { |
| 63 | + pid: 42, |
| 64 | + thread: 7, |
| 65 | + process: "/apps/app.app/app", |
| 66 | + image: "/apps/app.app/Frameworks/NativeScript.framework/NativeScript", |
| 67 | + level: "default", |
| 68 | + message: "CONSOLE LOG: hello", |
| 69 | + }, |
| 70 | + ]); |
| 71 | + }); |
| 72 | + |
| 73 | + it("traces what log stream writes to stderr", async () => { |
| 74 | + await provider.startLogProcess(deviceId); |
| 75 | + logProcess.stderr.write("log: Invalid predicate\n"); |
| 76 | + await nextTick(); |
| 77 | + |
| 78 | + assert.isEmpty(entries); |
| 79 | + assert.include(logger.traceOutput, "log: Invalid predicate"); |
| 80 | + }); |
| 81 | +}); |
0 commit comments