@sprucelabs/spruce-cli
Version:
Command line interface for building Spruce skills.
171 lines • 7.21 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 CommandService_1 = __importDefault(require("../../../services/CommandService"));
const AbstractCliTest_1 = __importDefault(require("../../../tests/AbstractCliTest"));
const ScriptUpdater_1 = __importDefault(require("../../../updaters/ScriptUpdater"));
const WriterFactory_1 = __importDefault(require("../../../writers/WriterFactory"));
class UpgradingANonSpruceNodeModuleTest extends AbstractCliTest_1.default {
static pkg;
static async beforeEach() {
await super.beforeEach();
ScriptUpdater_1.default.Class = MockScriptUpdater;
MockScriptUpdater.reset();
WriterFactory_1.default.setWriterClass('node', MockNodeWriter);
MockNodeWriter.reset();
this.cwd = this.resolvePath((0, test_utils_1.generateId)() + '_module');
spruce_skill_utils_1.diskUtil.createDir(this.cwd);
await this.initModule();
this.fakeYarnCommands();
this.pkg = this.Service('pkg');
}
static async doesNotCreateHashSpruceOnUpdate() {
await this.upgrade();
const doesHashSpruceExist = spruce_skill_utils_1.diskUtil.doesDirExist(spruce_skill_utils_1.diskUtil.resolvePath(this.cwd, spruce_skill_utils_1.HASH_SPRUCE_DIR));
test_utils_1.assert.isFalse(doesHashSpruceExist, 'Should not have created any hash_spruce_dir');
}
static async doesNotTryToUpdateScripts() {
await this.upgrade();
MockScriptUpdater.assertWasNotCalled();
}
static async doesNotWriteNodeFiles() {
await this.upgrade();
MockNodeWriter.assertWasNotCalled();
}
static async passesTheWFlagIfInWorkspace() {
this.setWorkspaces(['packages/*']);
let wasHit = false;
CommandService_1.default.fakeCommand(/yarn.*?-W/, {
code: 0,
callback: () => {
wasHit = true;
},
});
await this.pkg.install(['test']);
test_utils_1.assert.isTrue(wasHit, `Did not add the -W flag when in a workspace`);
}
static async shouldNotInstallDependenciesNotAlreadyInstalled() {
this.addDependencyDirectlyToPackage();
const commands = [];
CommandService_1.default.fakeCommand(/yarn.*?add .*/gis, {
code: 0,
callback: (command, args) => {
commands.push([command, ...args].join(' '));
},
});
await this.upgrade();
test_utils_1.assert.doesInclude(commands[0], '@sprucelabs/spruce-core');
}
static async shouldNotTryAndBuildAfterUpgrade() {
this.commandFaker.makeCommandThrow(`yarn build.dev`);
await this.upgrade();
}
static async shouldNotCleanBuild() {
this.commandFaker.makeCommandThrow(`yarn clean.build`);
await this.upgrade();
}
static async runsUpgradeInEveryPackageInWorkspace() {
this.setWorkspaces(['packages/*']);
CommandService_1.default.clearFakedResponses();
const moduleName = 'test1';
const path = this.resolvePath('packages', moduleName);
spruce_skill_utils_1.diskUtil.createDir(path);
const commands = this.Service('command', path);
await commands.execute('yarn init -y');
const pkg = this.Service('pkg', path);
this.addDependencyDirectlyToPackage(pkg);
this.fakeYarnCommands();
await this.emitter.on('feature.did-execute', async (payload) => {
console.log(payload);
});
await this.upgrade();
}
static setWorkspaces(packages) {
this.pkg.set({
path: 'workspaces',
value: packages,
});
}
static addDependencyDirectlyToPackage(pkgService = this.pkg) {
pkgService.set({
path: 'dependencies',
value: {
'@sprucelabs/spruce-core': '1.0.0',
},
});
}
static async upgrade() {
const upgrade = this.Action('node', 'upgrade', {});
const results = await upgrade.execute({});
test_utils_1.assert.isLength(results.errors ?? [], 0, `Should not have had any errors, but got '${results?.errors?.[0]?.message}'`);
}
static fakeYarnCommands() {
this.commandFaker.fakeCommand(/yarn/, 0);
this.commandFaker.makeCommandThrow(`yarn fix.lint`);
}
static async initModule() {
await this.Service('command').execute('yarn init -y');
}
}
exports.default = UpgradingANonSpruceNodeModuleTest;
__decorate([
(0, test_utils_1.test)()
], UpgradingANonSpruceNodeModuleTest, "doesNotCreateHashSpruceOnUpdate", null);
__decorate([
(0, test_utils_1.test)()
], UpgradingANonSpruceNodeModuleTest, "doesNotTryToUpdateScripts", null);
__decorate([
(0, test_utils_1.test)()
], UpgradingANonSpruceNodeModuleTest, "doesNotWriteNodeFiles", null);
__decorate([
(0, test_utils_1.test)()
], UpgradingANonSpruceNodeModuleTest, "passesTheWFlagIfInWorkspace", null);
__decorate([
(0, test_utils_1.test)()
], UpgradingANonSpruceNodeModuleTest, "shouldNotInstallDependenciesNotAlreadyInstalled", null);
__decorate([
(0, test_utils_1.test)()
], UpgradingANonSpruceNodeModuleTest, "shouldNotTryAndBuildAfterUpgrade", null);
__decorate([
(0, test_utils_1.test)()
], UpgradingANonSpruceNodeModuleTest, "shouldNotCleanBuild", null);
__decorate([
test_utils_1.test.skip('revisit when really ready to loop through workspaces')
], UpgradingANonSpruceNodeModuleTest, "runsUpgradeInEveryPackageInWorkspace", null);
class MockScriptUpdater {
static wasCalled = false;
static assertWasNotCalled() {
test_utils_1.assert.isFalse(this.wasCalled, 'ScriptUpdater was called when it should not have been');
}
static reset() {
this.wasCalled = false;
}
async update() {
MockScriptUpdater.wasCalled = true;
test_utils_1.assert.fail('MockScriptUpdater should not be called');
}
}
class MockNodeWriter {
static wasCalled = false;
static assertWasNotCalled() {
test_utils_1.assert.isFalse(this.wasCalled, 'NodeWriter was called when it should not have been');
}
async writeNodeModule() {
MockNodeWriter.wasCalled = true;
return [];
}
static reset() {
this.wasCalled = false;
}
}
//# sourceMappingURL=UpgradingANonSpruceNodeModule.test.js.map