@sprucelabs/spruce-cli
Version:
Command line interface for building Spruce skills.
135 lines • 5.98 kB
JavaScript
"use strict";
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"));
class UpgradingAMonorepoTest extends AbstractCliTest_1.default {
static monorepoDir;
static packageADir;
static packageBDir;
static wackyPackageName;
static pkg;
static monoRepoPkg;
static passedYarnArgs = [];
static yarnHitCount = 0;
static async beforeAll() {
await super.beforeAll();
await this.setupMonorepo();
}
static async beforeEach() {
await super.beforeEach();
this.passedYarnArgs = [];
this.yarnHitCount = 0;
this.cwd = this.monorepoDir;
this.monoRepoPkg = this.Service('pkg', this.monorepoDir);
}
static async canUpgradePackageAInMonorepo() {
this.setCwd(this.packageADir);
this.fakeYarnCommands();
await this.assertUpgradePasses();
}
static async canUpgradePackageBWithCrossDependency() {
this.setCwd(this.packageBDir);
this.fakeYarnCommands();
await this.assertUpgradePasses();
test_utils_1.assert.isEqual(this.yarnHitCount, 3, 'Should not try and install if only module is ignored');
this.assertDidNotUpgradePackageA();
}
static async canUpgradeWithRandomlyNamedSiblingPackage() {
this.wackyPackageName = `package-${Math.random()}`;
this.createMinimalPackage(this.wackyPackageName);
this.addDependency(this.packageADir, this.wackyPackageName, '^1.0.0');
this.addDependency(this.packageBDir, this.wackyPackageName, '^1.0.0');
this.setCwd(this.packageADir);
this.fakeYarnCommands();
await this.assertUpgradePasses();
this.assertPackageSkipped(this.wackyPackageName);
}
static assertDidNotUpgradePackageA() {
this.assertPackageSkipped('package-a');
}
static assertPackageSkipped(packageName) {
test_utils_1.assert.doesNotInclude(this.passedYarnArgs, packageName, `Should not try to upgrade ${packageName}`);
}
static fakeYarnCommands() {
CommandService_1.default.fakeCommand(/yarn*/, {
code: 0,
callback: (_, args) => {
this.passedYarnArgs.push(...args);
this.yarnHitCount++;
},
});
}
static async assertUpgradePasses() {
const results = await this.Action('node', 'upgrade').execute({});
test_utils_1.assert.isFalsy(results.errors, 'Should not have errors');
}
static async setupMonorepo() {
this.monorepoDir = this.freshTmpDir();
this.setCwd(this.monorepoDir);
this.createRootPackageJson();
this.packageADir = await this.createModule('package-a');
this.packageBDir = await this.createModule('package-b');
this.addDependency(this.packageBDir, 'package-a', '^1.0.0');
}
static createRootPackageJson() {
this.setCwd(this.monorepoDir);
const pkgPath = this.resolvePath('package.json');
spruce_skill_utils_1.diskUtil.writeFile(pkgPath, '{}');
this.monoRepoPkg = this.Service('pkg');
this.monoRepoPkg.set({ path: ['name'], value: 'test-monorepo' });
//@ts-ignore REMOVE WHEN YOU FIND THIS, TYPES UPDATED
this.monoRepoPkg.set({ path: ['private'], value: true });
this.monoRepoPkg.set({ path: ['workspaces'], value: ['packages/*'] });
}
static async createModule(name) {
const packageDir = this.resolvePackageDir(name);
spruce_skill_utils_1.diskUtil.createDir(packageDir);
this.setCwd(packageDir);
await this.Action('node', 'create', {
shouldAutoHandleDependencies: true,
}).execute({
name,
description: `Test module ${name}`,
});
return packageDir;
}
static resolvePackageDir(name) {
return spruce_skill_utils_1.diskUtil.resolvePath(this.monorepoDir, 'packages', name);
}
static addDependency(packageDir, depName, version) {
this.pkg = this.Service('pkg', packageDir);
this.pkg.set({
path: ['dependencies', depName],
value: version,
});
}
static createMinimalPackage(name) {
const packageDir = this.resolvePackageDir(name);
spruce_skill_utils_1.diskUtil.createDir(packageDir);
const pkgPath = spruce_skill_utils_1.diskUtil.resolvePath(packageDir, 'package.json');
spruce_skill_utils_1.diskUtil.writeFile(pkgPath, JSON.stringify({ name, version: '1.0.0' }, null, 2));
return packageDir;
}
}
exports.default = UpgradingAMonorepoTest;
__decorate([
(0, test_utils_1.test)()
], UpgradingAMonorepoTest, "canUpgradePackageAInMonorepo", null);
__decorate([
(0, test_utils_1.test)()
], UpgradingAMonorepoTest, "canUpgradePackageBWithCrossDependency", null);
__decorate([
(0, test_utils_1.test)()
], UpgradingAMonorepoTest, "canUpgradeWithRandomlyNamedSiblingPackage", null);
//# sourceMappingURL=UpgradingAMonorepo.test.js.map