|
| 1 | +import { Yok } from "../../lib/common/yok"; |
| 2 | +import { assert } from "chai"; |
| 3 | +import { Options } from "../../lib/options"; |
| 4 | +import { TestIosCommand } from "../../lib/commands/test"; |
| 5 | +import { IOptions } from "../../lib/declarations"; |
| 6 | +import { ICommand } from "../../lib/common/definitions/commands"; |
| 7 | +import { IInjector } from "../../lib/common/definitions/yok"; |
| 8 | +import { IConfigurationSettings } from "../../lib/common/declarations"; |
| 9 | + |
| 10 | +const CI_ENVIRONMENT_VARIABLES = ["CI", "JENKINS_HOME"]; |
| 11 | + |
| 12 | +function createTestInjector(): IInjector { |
| 13 | + const testInjector = new Yok(); |
| 14 | + testInjector.register("settingsService", { |
| 15 | + setSettings: (settings: IConfigurationSettings): any => undefined, |
| 16 | + getProfileDir: () => "profileDir", |
| 17 | + }); |
| 18 | + testInjector.register("errors", { |
| 19 | + fail: (message: string): never => { |
| 20 | + throw new Error(message); |
| 21 | + }, |
| 22 | + failWithHelp: (message: string): never => { |
| 23 | + throw new Error(message); |
| 24 | + }, |
| 25 | + }); |
| 26 | + testInjector.register("logger", { |
| 27 | + warn: (message: string): void => undefined, |
| 28 | + }); |
| 29 | + |
| 30 | + testInjector.register("projectData", {}); |
| 31 | + testInjector.register("testExecutionService", {}); |
| 32 | + testInjector.register("vitestExecutionService", {}); |
| 33 | + testInjector.register("analyticsService", {}); |
| 34 | + testInjector.register("platformEnvironmentRequirements", {}); |
| 35 | + testInjector.register("cleanupService", {}); |
| 36 | + testInjector.register("liveSyncCommandHelper", {}); |
| 37 | + testInjector.register("devicesService", {}); |
| 38 | + testInjector.register("migrateController", {}); |
| 39 | + |
| 40 | + return testInjector; |
| 41 | +} |
| 42 | + |
| 43 | +interface IResolvedTestCommand { |
| 44 | + command: ICommand; |
| 45 | + options: IOptions; |
| 46 | +} |
| 47 | + |
| 48 | +function resolveTestIosCommand(): IResolvedTestCommand { |
| 49 | + const testInjector = createTestInjector(); |
| 50 | + const options = testInjector.resolve(Options); |
| 51 | + testInjector.register("options", options); |
| 52 | + testInjector.registerCommand("test|ios", TestIosCommand); |
| 53 | + const command = testInjector.resolveCommand("test|ios"); |
| 54 | + return { command, options }; |
| 55 | +} |
| 56 | + |
| 57 | +function validateWithArgs( |
| 58 | + resolved: IResolvedTestCommand, |
| 59 | + args: string[] = [], |
| 60 | +): void { |
| 61 | + args.forEach((arg) => process.argv.push(arg)); |
| 62 | + resolved.options.validateOptions(resolved.command.dashedOptions); |
| 63 | + args.forEach(() => process.argv.pop()); |
| 64 | +} |
| 65 | + |
| 66 | +describe("test ios command", () => { |
| 67 | + const savedCiEnvironment: { [key: string]: string } = {}; |
| 68 | + |
| 69 | + beforeEach(() => { |
| 70 | + CI_ENVIRONMENT_VARIABLES.forEach((name) => { |
| 71 | + savedCiEnvironment[name] = process.env[name]; |
| 72 | + delete process.env[name]; |
| 73 | + }); |
| 74 | + }); |
| 75 | + |
| 76 | + afterEach(() => { |
| 77 | + CI_ENVIRONMENT_VARIABLES.forEach((name) => { |
| 78 | + if (savedCiEnvironment[name] === undefined) { |
| 79 | + delete process.env[name]; |
| 80 | + } else { |
| 81 | + process.env[name] = savedCiEnvironment[name]; |
| 82 | + } |
| 83 | + }); |
| 84 | + }); |
| 85 | + |
| 86 | + describe("--watch option", () => { |
| 87 | + it("defaults to off in CI environments", () => { |
| 88 | + process.env.CI = "true"; |
| 89 | + const resolved = resolveTestIosCommand(); |
| 90 | + validateWithArgs(resolved); |
| 91 | + assert.isFalse(resolved.options.argv.watch); |
| 92 | + }); |
| 93 | + |
| 94 | + it("defaults to off when JENKINS_HOME is set", () => { |
| 95 | + process.env.JENKINS_HOME = "/var/jenkins"; |
| 96 | + const resolved = resolveTestIosCommand(); |
| 97 | + validateWithArgs(resolved); |
| 98 | + assert.isFalse(resolved.options.argv.watch); |
| 99 | + }); |
| 100 | + |
| 101 | + it("defaults to on outside CI environments", () => { |
| 102 | + const resolved = resolveTestIosCommand(); |
| 103 | + validateWithArgs(resolved); |
| 104 | + assert.isTrue(resolved.options.argv.watch); |
| 105 | + }); |
| 106 | + |
| 107 | + it("stays on when --watch is passed explicitly in CI", () => { |
| 108 | + process.env.CI = "true"; |
| 109 | + const resolved = resolveTestIosCommand(); |
| 110 | + validateWithArgs(resolved, ["--watch"]); |
| 111 | + assert.isTrue(resolved.options.argv.watch); |
| 112 | + }); |
| 113 | + |
| 114 | + it("stays off when --no-watch is passed outside CI", () => { |
| 115 | + const resolved = resolveTestIosCommand(); |
| 116 | + validateWithArgs(resolved, ["--no-watch"]); |
| 117 | + assert.isFalse(resolved.options.argv.watch); |
| 118 | + }); |
| 119 | + }); |
| 120 | +}); |
0 commit comments