|
| 1 | +import { assert } from "chai"; |
| 2 | +import * as fs from "fs"; |
| 3 | +import * as os from "os"; |
| 4 | +import * as path from "path"; |
| 5 | +import { ICliGlobal } from "../../lib/common/definitions/cli-global"; |
| 6 | + |
| 7 | +// Pins the published extension contract: an extension is require()d and |
| 8 | +// registers its contributions by mutating global.$injector — commands via |
| 9 | +// requireCommand/registerCommand (lazy, path-based), services via register. |
| 10 | +// extending-cli.md advertises this surface. |
| 11 | + |
| 12 | +const cliGlobal = <ICliGlobal>(<unknown>global); |
| 13 | + |
| 14 | +describe("legacy extension contract", () => { |
| 15 | + let extDir: string; |
| 16 | + let capture: any; |
| 17 | + |
| 18 | + beforeEach(() => { |
| 19 | + extDir = fs.mkdtempSync(path.join(os.tmpdir(), "ns-compat-ext-")); |
| 20 | + capture = (<any>global).__extCapture = {}; |
| 21 | + }); |
| 22 | + |
| 23 | + afterEach(() => { |
| 24 | + fs.rmSync(extDir, { recursive: true, force: true }); |
| 25 | + delete (<any>global).__extCapture; |
| 26 | + }); |
| 27 | + |
| 28 | + it("an unmodified extension contributes services and hierarchical commands via global.$injector", async () => { |
| 29 | + const commandPath = path.join(extDir, "meow-command"); |
| 30 | + fs.writeFileSync( |
| 31 | + commandPath + ".js", |
| 32 | + `class MeowPurrCommand { |
| 33 | + constructor($extCompatService) { |
| 34 | + this.extCompatService = $extCompatService; |
| 35 | + this.allowedParameters = []; |
| 36 | + } |
| 37 | + async execute(args) { |
| 38 | + global.__extCapture.executedWith = args; |
| 39 | + global.__extCapture.service = this.extCompatService; |
| 40 | + } |
| 41 | + } |
| 42 | + global.$injector.registerCommand("meowcompat|purr", MeowPurrCommand);`, |
| 43 | + ); |
| 44 | + fs.writeFileSync( |
| 45 | + path.join(extDir, "main.js"), |
| 46 | + `global.$injector.register("extCompatService", { name: "ext-compat-service" }); |
| 47 | + global.$injector.requireCommand("meowcompat|purr", ${JSON.stringify( |
| 48 | + commandPath, |
| 49 | + )});`, |
| 50 | + ); |
| 51 | + |
| 52 | + require(path.join(extDir, "main.js")); |
| 53 | + |
| 54 | + // Registration is lazy: the command module must not load until resolved. |
| 55 | + assert.isUndefined(capture.executedWith); |
| 56 | + |
| 57 | + // Services registered by the extension resolve under both spellings. |
| 58 | + const service = cliGlobal.$injector.resolve("extCompatService"); |
| 59 | + assert.strictEqual( |
| 60 | + cliGlobal.$injector.resolve("$extCompatService"), |
| 61 | + service, |
| 62 | + ); |
| 63 | + assert.equal(service.name, "ext-compat-service"); |
| 64 | + |
| 65 | + // The command participates in the hierarchical router. |
| 66 | + assert.include( |
| 67 | + cliGlobal.$injector.getRegisteredCommandsNames(false), |
| 68 | + "meowcompat|purr", |
| 69 | + ); |
| 70 | + assert.include( |
| 71 | + cliGlobal.$injector.getChildrenCommandsNames("meowcompat"), |
| 72 | + "purr", |
| 73 | + ); |
| 74 | + const built = cliGlobal.$injector.buildHierarchicalCommand("meowcompat", [ |
| 75 | + "purr", |
| 76 | + "extra-arg", |
| 77 | + ]); |
| 78 | + assert.equal(built.commandName, "meowcompat|purr"); |
| 79 | + assert.deepEqual(built.remainingArguments, ["extra-arg"]); |
| 80 | + |
| 81 | + // The leaf command resolves, gets its DI-injected service, and executes. |
| 82 | + const command = cliGlobal.$injector.resolveCommand("meowcompat|purr"); |
| 83 | + assert.isOk(command); |
| 84 | + await command.execute(["fluffy"]); |
| 85 | + assert.deepEqual(capture.executedWith, ["fluffy"]); |
| 86 | + assert.equal(capture.service.name, "ext-compat-service"); |
| 87 | + |
| 88 | + // registerCommand on a hierarchical name synthesized a parent dispatcher. |
| 89 | + const parent = <any>cliGlobal.$injector.resolveCommand("meowcompat"); |
| 90 | + assert.isTrue(parent.isHierarchicalCommand); |
| 91 | + }); |
| 92 | + |
| 93 | + it("claiming an already-required command name throws unless overrideAlreadyRequiredModule is set", () => { |
| 94 | + const firstPath = path.join(extDir, "first"); |
| 95 | + fs.writeFileSync(firstPath + ".js", `module.exports = {};`); |
| 96 | + |
| 97 | + cliGlobal.$injector.requireCommand("conflictcompat", firstPath); |
| 98 | + assert.throws( |
| 99 | + () => cliGlobal.$injector.requireCommand("conflictcompat", firstPath), |
| 100 | + /require'd twice/, |
| 101 | + ); |
| 102 | + |
| 103 | + cliGlobal.$injector.overrideAlreadyRequiredModule = true; |
| 104 | + try { |
| 105 | + cliGlobal.$injector.requireCommand("conflictcompat", firstPath); |
| 106 | + } finally { |
| 107 | + cliGlobal.$injector.overrideAlreadyRequiredModule = false; |
| 108 | + } |
| 109 | + }); |
| 110 | +}); |
0 commit comments