@sprucelabs/spruce-cli
Version:
Command line interface for building Spruce skills.
191 lines • 8.24 kB
JavaScript
;
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const spruce_skill_utils_1 = require("@sprucelabs/spruce-skill-utils");
const test_utils_1 = require("@sprucelabs/test-utils");
const constants_1 = require("../../../constants");
const AbstractCliTest_1 = __importDefault(require("../../../tests/AbstractCliTest"));
const BROKEN_SKILL_INDEX_CONTENTS = "throw new Error('cheese!')\n";
class UpgradingASkillTest extends AbstractCliTest_1.default {
static async beforeEach() {
await super.beforeEach();
this.commandFaker.fakeRebuild();
}
static async forceEverythingUpgradeOverwritesWhatHasChanged() {
const cli = await this.installAndBreakSkill('skills');
this.commandFaker.fakeCleanBuild();
this.commandFaker.fakeBuild();
const files = [
{
name: 'index.ts',
path: 'src/index.ts',
forceEverythingAction: 'updated',
forceRequiredSkipRestAction: 'updated',
},
{
name: 'SpruceError.ts',
path: 'src/errors/SpruceError.ts',
forceEverythingAction: 'updated',
forceRequiredSkipRestAction: 'skipped',
},
{
name: 'options.types.ts',
path: 'src/.spruce/errors/options.types.ts',
forceEverythingAction: 'updated',
forceRequiredSkipRestAction: 'skipped',
},
];
for (const upgradeMode of [
'forceRequiredSkipRest',
'forceEverything',
]) {
for (const file of files) {
this.clearFileIfAboutToBeUpdated(file, upgradeMode);
}
const results = await this.Action('node', 'upgrade').execute({
upgradeMode,
});
if (upgradeMode === 'forceRequiredSkipRest') {
const passedHealthCheck = await cli.checkHealth();
test_utils_1.assert.isEqualDeep(passedHealthCheck, {
skill: { status: 'passed' },
});
}
for (const file of files) {
//@ts-ignore
const action = file[`${upgradeMode}Action`];
test_utils_1.assert.doesInclude(results.files, {
name: file.name,
action,
}, `${file.name} was not ${action} when ${upgradeMode} in \n\n ${JSON.stringify(results.files ?? [], null, 2)}`);
}
}
const passedHealthCheck = await cli.checkHealth();
test_utils_1.assert.doesInclude(passedHealthCheck, {
'skill.status': 'passed',
});
}
static async upgradeWillAskIfYouWantToOverwriteFiles() {
const cli = await this.installAndBreakSkill('skills');
const promise = this.Action('node', 'upgrade').execute({
upgradeMode: 'askForChanged',
});
await this.waitForInput();
await this.assertFailedHealthCheck(cli);
const last = this.ui.getLastInvocation();
test_utils_1.assert.doesInclude(last, {
'options.options.choices[].value': constants_1.FILE_ACTION_OVERWRITE,
});
await this.ui.sendInput(constants_1.FILE_ACTION_OVERWRITE);
const results = await promise;
test_utils_1.assert.isFalsy(results.errors);
const health = await cli.checkHealth();
test_utils_1.assert.isEqual(health.skill.status, 'passed');
}
static async canSkipFile() {
const { last, promise } = await this.installBreakAndUpgradeSkill();
test_utils_1.assert.doesInclude(last, {
'options.options.choices[].value': constants_1.FILE_ACTION_SKIP,
});
await this.ui.sendInput(constants_1.FILE_ACTION_SKIP);
await promise;
this.assertSkillIsBroken();
}
static async canAlwaysSkipFiles() {
const { last, promise } = await this.installBreakAndUpgradeSkill();
test_utils_1.assert.doesInclude(last, {
'options.options.choices[].value': constants_1.FILE_ACTION_ALWAYS_SKIP,
});
await this.ui.sendInput(constants_1.FILE_ACTION_ALWAYS_SKIP);
await promise;
this.assertSkillIsBroken();
const results = await this.Action('node', 'upgrade').execute({
upgradeMode: 'askForChanged',
});
test_utils_1.assert.isFalsy(results.errors);
this.assertSkillIsBroken();
}
static async upgradesUpdatesPackageScripts() {
const cli = await this.installSkill('schemas');
const pkgService = this.Service('pkg');
pkgService.set({ path: 'scripts', value: {} });
const failedHealth = await cli.checkHealth();
test_utils_1.assert.doesInclude(failedHealth, {
'skill.errors[].message': '"health.local" not found',
});
await this.Action('node', 'upgrade').execute({});
const passedHealth = await cli.checkHealth();
test_utils_1.assert.isEqual(passedHealth.skill.status, 'passed');
}
static clearFileIfAboutToBeUpdated(file, upgradeMode) {
//@ts-ignore
if (file[`${upgradeMode}Action`] === 'updated') {
spruce_skill_utils_1.diskUtil.writeFile(this.resolvePath(file.path), '');
}
}
static async installAndBreakSkill(cacheKey) {
const cli = await this.installSkill(cacheKey);
const indexFile = this.resolvePath('src/index.ts');
spruce_skill_utils_1.diskUtil.writeFile(indexFile, BROKEN_SKILL_INDEX_CONTENTS);
await this.assertFailedHealthCheck(cli);
return cli;
}
static async installSkill(cacheKey) {
const fixture = this.FeatureFixture();
const cli = await fixture.installFeatures([
{
code: 'skill',
options: {
name: 'testing events',
description: 'this too, is a great test!',
},
},
], cacheKey);
return cli;
}
static async assertFailedHealthCheck(cli) {
const failedHealthCheck = await cli.checkHealth();
test_utils_1.assert.doesInclude(failedHealthCheck, {
'skill.errors[].message': 'cheese',
});
}
static async installBreakAndUpgradeSkill() {
await this.installAndBreakSkill('skills');
const promise = this.Action('node', 'upgrade').execute({
upgradeMode: 'askForChanged',
});
await this.waitForInput();
const last = this.ui.getLastInvocation();
return { last, promise };
}
static assertSkillIsBroken() {
const indexFile = this.resolvePath('src/index.ts');
const contents = spruce_skill_utils_1.diskUtil.readFile(indexFile);
test_utils_1.assert.isEqual(contents, BROKEN_SKILL_INDEX_CONTENTS);
}
}
exports.default = UpgradingASkillTest;
__decorate([
(0, test_utils_1.test)()
], UpgradingASkillTest, "forceEverythingUpgradeOverwritesWhatHasChanged", null);
__decorate([
(0, test_utils_1.test)()
], UpgradingASkillTest, "upgradeWillAskIfYouWantToOverwriteFiles", null);
__decorate([
(0, test_utils_1.test)()
], UpgradingASkillTest, "canSkipFile", null);
__decorate([
(0, test_utils_1.test)()
], UpgradingASkillTest, "canAlwaysSkipFiles", null);
__decorate([
(0, test_utils_1.test)()
], UpgradingASkillTest, "upgradesUpdatesPackageScripts", null);
//# sourceMappingURL=UpgradingASkill.test.js.map