UNPKG

@atomist/sdm-core

Version:

Atomist Software Delivery Machine - Implementation

132 lines 6.53 kB
"use strict"; /* * Copyright © 2019 Atomist, Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); const automation_client_1 = require("@atomist/automation-client"); const ActionResult_1 = require("@atomist/automation-client/lib/action/ActionResult"); const BuildableAutomationServer_1 = require("@atomist/automation-client/lib/server/BuildableAutomationServer"); const sdm_1 = require("@atomist/sdm"); const assert = require("assert"); const flatten = require("flat"); const _ = require("lodash"); const defaultSoftwareDeliveryMachineConfiguration_1 = require("../../../machine/defaultSoftwareDeliveryMachineConfiguration"); const array_1 = require("../../../util/misc/array"); const invokeCommand_1 = require("../../job/invokeCommand"); const generator_1 = require("../generator"); function assertUniversalGenerator(generatorUnderTest, transformsUnderTest, initialParams, promptForParams = {}) { return __awaiter(this, void 0, void 0, function* () { try { // Prepare the result of generator run let project; let id; let params; // Set up configuration and overwrite the projectPersister to capture the result of the generator const configuration = _.merge({}, Object.assign(Object.assign({}, defaultSoftwareDeliveryMachineConfiguration_1.defaultSoftwareDeliveryMachineConfiguration({})), { name: "test" }), { sdm: { projectPersister: (p, credentials, targetId, parameters) => __awaiter(this, void 0, void 0, function* () { project = p; id = targetId; params = parameters; return ActionResult_1.successOn(p); }), }, }); const automationServer = new BuildableAutomationServer_1.BuildableAutomationServer({}); const sdm = new TestSoftwareDeliveryMachine("test", configuration); global.__runningAutomationClient = { configuration, automationServer, }; // Create the generator instance and register it with the underlying automation client const generator = generator_1.universalGenerator(sdm, generatorUnderTest, array_1.toArray(transformsUnderTest)); automationServer.registerCommandHandler(sdm_1.generatorRegistrationToCommand(sdm, generator)); let parameterSpecs = []; const messageClient = { respond: (msg) => __awaiter(this, void 0, void 0, function* () { if (!!msg.parameter_specs) { parameterSpecs = msg.parameter_specs; } }), send: () => __awaiter(this, void 0, void 0, function* () { }), delete: () => __awaiter(this, void 0, void 0, function* () { }), }; // Invoke the generator with the initial set of parameters let result = yield invokeCommand_1.invokeCommand(generatorUnderTest, initialParams, mockHandlerContext(messageClient, initialParams)); assert.deepStrictEqual(result.code, 0, `Generator failed during initial execution: ${result.message}`); assert.deepStrictEqual(parameterSpecs.map(p => p.name).sort(), _.keys(promptForParams).sort()); if (!!project) { return { id, project, parameters: params, }; } // Now invoke generator with additional parameters needed by the matched transformsAndParameters const allParameters = Object.assign(Object.assign({}, initialParams), promptForParams); result = yield invokeCommand_1.invokeCommand(generatorUnderTest, allParameters, mockHandlerContext(messageClient, allParameters)); assert.deepStrictEqual(result.code, 0, `Generator failed during parameter prompt execution: ${result.message}`); assert(!!project, "Generated project is undefined"); return { id, project, parameters: params, }; } finally { delete global.__runningAutomationClient; } }); } exports.assertUniversalGenerator = assertUniversalGenerator; function mockHandlerContext(messageClient, params) { return { messageClient, trigger: { parameters: flattenParameters(params), }, invocationId: automation_client_1.guid(), correlationId: automation_client_1.guid(), graphClient: undefined, workspaceId: `A${automation_client_1.guid().slice(0, 7).toUpperCase()}`, }; } function flattenParameters(params) { const parameters = []; _.forEach(flatten(params), (v, k) => { parameters.push({ name: k, value: v }); }); return parameters; } class TestSoftwareDeliveryMachine extends sdm_1.AbstractSoftwareDeliveryMachine { constructor(name, configuration) { super(name, configuration, []); this.commandHandlers = []; this.eventHandlers = []; this.ingesters = []; } } //# sourceMappingURL=assertGenerator.js.map