@pobuca/xsc
Version:
A cli tool with common git command sets.
220 lines • 9.63 kB
JavaScript
;
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 chai_1 = require("chai");
const del = require("del");
const fs_1 = require("fs");
const gulp_1 = require("gulp");
const path_1 = require("path");
const CLI_1 = require("./CLI");
const GitRemoteHost_1 = require("./interfaces/GitRemoteHost");
const TMP_PATH = './tmp';
const TEST_PROJECTS = [
'csharp-solution',
'nodejs-project'
];
const GIT_REMOTE_HOST_CONFIGS = [
{
gitConfig: [
'[remote "origin"]',
'\turl = https://testorg@dev.azure.com/testorg/test%20project/_git/testrepo',
''
].join('\n'),
remoteHost: GitRemoteHost_1.GitRemoteHost[GitRemoteHost_1.GitRemoteHost.AzureDevOps]
},
{
gitConfig: [
'[remote "origin"]',
'\turl = https://github.com/testuser/testrepo',
''
].join('\n'),
remoteHost: GitRemoteHost_1.GitRemoteHost[GitRemoteHost_1.GitRemoteHost.GitHub]
}
];
const UNKNOWN_HOST_GIT_CONFIG = [
'[remote "origin"]',
'\t',
'\turl = https://unknown.com/testuser/testrepo',
''
].join('\n');
describe('CLI', () => {
let cli;
const terminal = {
cwd: '.',
writeFileSync() { },
readFileSync() { return Buffer.from([]); },
readdirSync() { return []; },
execSync() { return Buffer.from([]); }
};
before(() => __awaiter(void 0, void 0, void 0, function* () {
yield del(TMP_PATH);
yield new Promise((r) => gulp_1.src('test/data/**').pipe(gulp_1.dest(TMP_PATH)).on('finish', r));
}));
it('should instantiate', () => {
cli = new CLI_1.default(terminal);
});
it('should error on unknown command', () => __awaiter(void 0, void 0, void 0, function* () {
let errored = false;
yield cli.invoke('unknown', []).catch(() => errored = true);
if (!errored) {
throw new Error('Did not error');
}
}));
it('should invoke a command', () => __awaiter(void 0, void 0, void 0, function* () {
yield cli.invoke('xcommit', ['Test']);
}));
for (const remoteHostGitConfig of GIT_REMOTE_HOST_CONFIGS) {
describe(`remote host: ${remoteHostGitConfig.remoteHost}`, () => {
for (const project of TEST_PROJECTS) {
describe(project, () => {
const cwd = path_1.resolve(__dirname, `../${TMP_PATH}/${project}`);
const projectCLI = new CLI_1.default(Object.assign({}, terminal, {
cwd,
readFileSync(filePath) {
if (/\.git[\/\\]config/.test(filePath)) {
return Buffer.from(remoteHostGitConfig.gitConfig);
}
else {
return fs_1.readFileSync(filePath);
}
},
readdirSync: fs_1.readdirSync, writeFileSync: fs_1.writeFileSync
}));
it(`should start a release`, () => __awaiter(void 0, void 0, void 0, function* () {
yield projectCLI.invoke('xrelease', ['start']);
}));
it(`should finish a release`, () => __awaiter(void 0, void 0, void 0, function* () {
yield projectCLI.invoke('xrelease', ['finish']);
}));
it(`should start a hotfix`, () => __awaiter(void 0, void 0, void 0, function* () {
yield projectCLI.invoke('xhotfix', ['start']);
}));
it(`should finish a hotfix`, () => __awaiter(void 0, void 0, void 0, function* () {
yield projectCLI.invoke('xhotfix', ['finish']);
}));
it(`should error on invalid command`, () => __awaiter(void 0, void 0, void 0, function* () {
let errored = false;
yield projectCLI.invoke('xhotfix', ['invalid']).catch((e) => errored = true);
if (!errored) {
throw new Error('Did not error');
}
}));
it('should start a feature', () => __awaiter(void 0, void 0, void 0, function* () {
yield projectCLI.invoke('xfeature', ['start', 'my-feature']);
}));
it('should finish a feature', () => __awaiter(void 0, void 0, void 0, function* () {
yield projectCLI.invoke('xfeature', ['finish']);
}));
});
}
});
}
describe('unknown host', () => {
const cwd = path_1.resolve(__dirname, `../${TMP_PATH}/${TEST_PROJECTS[0]}`);
const projectCLI = new CLI_1.default(Object.assign({}, terminal, {
cwd,
readFileSync(filePath) {
if (/\.git[\/\\]config/.test(filePath)) {
return Buffer.from(UNKNOWN_HOST_GIT_CONFIG);
}
else {
return fs_1.readFileSync(filePath);
}
},
readdirSync: fs_1.readdirSync, writeFileSync: fs_1.writeFileSync
}));
it(`should error when finishing a release`, () => __awaiter(void 0, void 0, void 0, function* () {
let errored = false;
yield projectCLI.invoke('xrelease', ['finish']).catch(() => errored = true);
chai_1.expect(errored).to.be.true;
}));
});
describe('unknown project', () => {
const cwd = path_1.resolve(__dirname, `../${TMP_PATH}/unknown-project`);
const projectCLI = new CLI_1.default(Object.assign({}, terminal, {
cwd, readFileSync: fs_1.readFileSync, readdirSync: fs_1.readdirSync, writeFileSync: fs_1.writeFileSync
}));
it(`should start a release and throw`, () => __awaiter(void 0, void 0, void 0, function* () {
try {
yield projectCLI.invoke('xrelease', ['start']);
throw new Error('Did not throw as expected');
}
catch (e) { }
}));
it(`should finish a release and throw`, () => __awaiter(void 0, void 0, void 0, function* () {
try {
yield projectCLI.invoke('xrelease', ['finish']);
throw new Error('Did not throw as expected');
}
catch (e) { }
}));
it(`should start a hotfix and throw`, () => __awaiter(void 0, void 0, void 0, function* () {
try {
yield projectCLI.invoke('xhotfix', ['start']);
throw new Error('Did not throw as expected');
}
catch (e) { }
}));
it(`should finish a hotfix and throw`, () => __awaiter(void 0, void 0, void 0, function* () {
try {
yield projectCLI.invoke('xhotfix', ['finish']);
throw new Error('Did not throw as expected');
}
catch (e) { }
}));
});
it('should show version', () => __awaiter(void 0, void 0, void 0, function* () {
yield cli.invoke('xsc', []);
}));
describe('init command', () => {
const cliWithGitflow = new CLI_1.default({
cwd: '.',
writeFileSync() { },
readFileSync(filePath) {
if (filePath === '.git/config') {
return Buffer.from('[gitflow ');
}
return Buffer.from([]);
},
readdirSync() { return []; },
execSync(cmd) {
if (cmd === 'git status') {
throw new Error('Not git repo');
}
return Buffer.from([]);
}
});
it('should init with git flow', () => __awaiter(void 0, void 0, void 0, function* () {
yield cliWithGitflow.invoke('xsc', ['init']);
}));
it('should init without git flow', () => __awaiter(void 0, void 0, void 0, function* () {
yield cli.invoke('xsc', ['init']);
}));
});
describe('without parameters', () => {
it('should show x if required command errors', () => __awaiter(void 0, void 0, void 0, function* () {
const cli = new CLI_1.default({
cwd: '.',
writeFileSync() { },
readFileSync(cmd) { return Buffer.from([]); },
readdirSync() { return []; },
execSync(cmd) {
if (cmd === 'git --version') {
throw new Error('Not git repo');
}
return Buffer.from([]);
}
});
yield cli.invoke('xsc', []);
}));
});
});
//# sourceMappingURL=CLI.test.js.map