Browse Source

Merge pull request #24 from beaussan/feat/fix-tests

add add dep and fix tests
pull/25/head
Nicolas Beaussart 6 years ago
committed by GitHub
parent
commit
dba2c2a930
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 1
      .prettierignore
  2. 44
      src/commands/add/dep.ts
  3. 5
      src/commands/add/prettier.ts
  4. 5
      src/commands/add/tailwind.ts
  5. 45
      src/tests/add/__snapshots__/dep.spec.ts.snap
  6. 145
      src/tests/add/dep.spec.ts
  7. 10
      src/tests/add/prettier.spec.ts
  8. 10
      src/tests/add/tailwind.spec.ts
  9. 2
      src/tests/wall.spec.ts
  10. 21
      src/utls/base-add-command.spec.ts
  11. 5
      src/utls/base-add-command.ts

1
.prettierignore

@ -24,3 +24,4 @@ yarn-error.log*
.history .history
CHANGELOG.md CHANGELOG.md
README.md

44
src/commands/add/dep.ts

@ -0,0 +1,44 @@
// import {flags} from '@oclif/command'
import { BaseCommand } from '../../utls/base-command';
import { BaseAddCommand } from '../../utls/base-add-command';
import { flags } from '@oclif/command';
export default class Dep extends BaseAddCommand {
static description = 'proxy to yarn add, plus gitmoji commit';
static examples = [
`$ nbx add:dep -D eslint`,
`$ nbx add:dep --dev eslint`,
`$ nbx add:dep chalk`,
];
static flags = {
...BaseCommand.flags,
dev: flags.boolean({ default: false, char: 'D', description: 'install as a dev dependency' }),
};
static args = [{ name: 'dep', description: 'The dependency to install', required: true }];
async run() {
if (!this.hasDirPackageJson()) {
this.error('There is no package.json not found in the current folder');
}
const {
args: { dep },
flags: { dev },
} = this.parse(Dep);
if ((dev && this.hasDevDependencyInPackageJson(dep)) || this.hasDependencyInPackageJson(dep)) {
if (this.hasDevDependencyInPackageJson(dep)) {
this.error(`${dep} is already installed in this project.`);
}
}
await this.initGit();
if (dev) {
await this.addDevDependency(dep, true);
} else {
await this.addDependency(dep, true);
}
}
}

5
src/commands/add/prettier.ts

@ -2,12 +2,9 @@
import { BaseCommand } from '../../utls/base-command'; import { BaseCommand } from '../../utls/base-command';
import { system, filesystem } from 'gluegun'; import { system, filesystem } from 'gluegun';
import * as prompts from 'prompts'; import * as prompts from 'prompts';
import * as fs from 'fs';
import { plugins, add, commit } from 'isomorphic-git';
import { add, commit } from 'isomorphic-git';
import { BaseAddCommand } from '../../utls/base-add-command'; import { BaseAddCommand } from '../../utls/base-add-command';
plugins.set('fs', fs);
export default class Prettier extends BaseAddCommand { export default class Prettier extends BaseAddCommand {
static description = 'add prettier to project and format it'; static description = 'add prettier to project and format it';

5
src/commands/add/tailwind.ts

@ -1,12 +1,9 @@
import { BaseCommand } from '../../utls/base-command'; import { BaseCommand } from '../../utls/base-command';
import { system, filesystem, patching } from 'gluegun'; import { system, filesystem, patching } from 'gluegun';
import * as prompts from 'prompts'; import * as prompts from 'prompts';
import * as fs from 'fs';
import { plugins, add, commit } from 'isomorphic-git';
import { add, commit } from 'isomorphic-git';
import { BaseAddCommand } from '../../utls/base-add-command'; import { BaseAddCommand } from '../../utls/base-add-command';
plugins.set('fs', fs);
export default class Tailwind extends BaseAddCommand { export default class Tailwind extends BaseAddCommand {
static description = 'add tailwindcss to a project'; static description = 'add tailwindcss to a project';

45
src/tests/add/__snapshots__/dep.spec.ts.snap

@ -0,0 +1,45 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP
exports[`dep should fail if dependancy is already in package.json 1`] = `[Error: Config not found. Tried to look for a .nbxrc, .nbxrc.json, .nbxrc.yaml, .nbxrc.yml, .nbxrc.js]`;
exports[`dep should fail if dev dependancy is already in package.json 1`] = `[Error: chalk is already installed in this project.]`;
exports[`dep should fail if no package.json found 1`] = `[Error: There is no package.json not found in the current folder]`;
exports[`dep should print help correctly 1`] = `
Array [
"proxy to yarn add, plus gitmoji commit
",
"USAGE
$ nbx add:dep DEP
ARGUMENTS
DEP The dependency to install
OPTIONS
-D, --dev install as a dev dependency
-h, --help show CLI help
-v, --verbose Verbose output
--[no-]spinner Enable spinner in cli output, true by default
EXAMPLES
$ nbx add:dep -D eslint
$ nbx add:dep --dev eslint
$ nbx add:dep chalk",
"",
]
`;
exports[`dep should work with a dev dependency 1`] = `
Array [
"Adding chalk as a dev dependency",
"The step was done without any error.",
]
`;
exports[`dep should work with dependency 1`] = `
Array [
"Adding chalk as a dependency",
"The step was done without any error.",
]
`;

145
src/tests/add/dep.spec.ts

@ -0,0 +1,145 @@
import { filesystem, system } from 'gluegun';
const stripANSI = require('strip-ansi');
import * as latestVersion from 'latest-version';
import Dep from '../../commands/add/dep';
jest.setTimeout(90000);
/* eslint-disable no-console,max-nested-callbacks,@typescript-eslint/ban-ts-ignore */
let consoleLogOutput: string[];
let execPath: string;
const originalCwd = process.cwd();
const originalLog = console.log;
beforeEach(async () => {
consoleLogOutput = [];
console.log = (x: any) => consoleLogOutput.push(stripANSI(x));
const tmpDirName = `${new Date().getTime()}-${Math.random() * 100}-add-nbx-test`;
execPath = filesystem.path('/', 'tmp', tmpDirName);
filesystem.dir(execPath);
process.chdir(execPath);
});
afterEach(() => {
console.log = originalLog;
process.chdir(originalCwd);
jest.restoreAllMocks();
filesystem.remove(execPath);
});
const initWithConfigAndCommit = async () => {
await system.run('git init');
filesystem.write('.nbxrc', { git: { user: 'aaa', email: 'bbb' } });
filesystem.write('package.json', {});
await system.run('touch yarn.lock');
await system.run('echo node_modules > .gitignore');
await system.run('git add * .nbxrc .gitignore');
try {
await system.run('git config user.email "you@example.com"');
await system.run('git config user.name "Your Name"');
} catch {}
await system.run('git commit -m "init state"');
};
describe('dep', () => {
it('should print help correctly', async () => {
try {
await Dep.run(['-h']);
} catch {}
expect(consoleLogOutput).toMatchSnapshot();
});
it('should fail if no package.json found', async () => {
expect.assertions(1);
try {
filesystem.remove('package.json');
await Dep.run(['chalk', '--no-spinner']);
// eslint-disable-next-line unicorn/catch-error-name
} catch (e) {
expect(e).toMatchSnapshot();
}
});
it('should fail if dependancy is already in package.json', async () => {
expect.assertions(1);
try {
filesystem.write('package.json', { dependencies: { chalk: '2.3.2' } });
await Dep.run(['chalk', '--no-spinner']);
// eslint-disable-next-line unicorn/catch-error-name
} catch (e) {
expect(e).toMatchSnapshot();
}
});
it('should fail if dev dependancy is already in package.json', async () => {
expect.assertions(1);
try {
filesystem.write('package.json', { devDependencies: { chalk: '2.3.2' } });
await Dep.run(['chalk', '--dev', '--no-spinner']);
// eslint-disable-next-line unicorn/catch-error-name
} catch (e) {
expect(e).toMatchSnapshot();
}
});
it('should work with dependency', async () => {
await initWithConfigAndCommit();
const latestChalk = await latestVersion('chalk');
await Dep.run(['chalk', '--no-spinner']);
const packageJson = filesystem.read('package.json', 'json');
expect(consoleLogOutput).toMatchSnapshot();
expect(packageJson).toStrictEqual({
dependencies: {
chalk: latestChalk,
},
});
const after = await system.run('git log --name-status --format="%s"');
const afterSlitted = after
.split('\n')
.map(val => val.trim())
.map(val => stripANSI(val))
.filter(val => val !== '');
expect(afterSlitted).toStrictEqual([
`:heavy_plus_sign: add chalk@${latestChalk} as a dependency`,
'M\tpackage.json',
'M\tyarn.lock',
'init state',
'A\t.gitignore',
'A\t.nbxrc',
'A\tpackage.json',
'A\tyarn.lock',
]);
});
it('should work with a dev dependency', async () => {
await initWithConfigAndCommit();
const latestChalk = await latestVersion('chalk');
await Dep.run(['chalk', '-D', '--no-spinner']);
const packageJson = filesystem.read('package.json', 'json');
expect(consoleLogOutput).toMatchSnapshot();
expect(packageJson).toStrictEqual({
devDependencies: {
chalk: latestChalk,
},
});
const after = await system.run('git log --name-status --format="%s"');
const afterSlitted = after
.split('\n')
.map(val => val.trim())
.map(val => stripANSI(val))
.filter(val => val !== '');
expect(afterSlitted).toStrictEqual([
`:heavy_plus_sign: add chalk@${latestChalk} as a dev dependency`,
'M\tpackage.json',
'M\tyarn.lock',
'init state',
'A\t.gitignore',
'A\t.nbxrc',
'A\tpackage.json',
'A\tyarn.lock',
]);
});
});

10
src/tests/add/prettier.spec.ts

@ -4,7 +4,7 @@ import * as prompts from 'prompts';
import * as latestVersion from 'latest-version'; import * as latestVersion from 'latest-version';
import Prettier from '../../commands/add/prettier'; import Prettier from '../../commands/add/prettier';
jest.setTimeout(30000);
jest.setTimeout(90000);
/* eslint-disable no-console,max-nested-callbacks,@typescript-eslint/ban-ts-ignore */ /* eslint-disable no-console,max-nested-callbacks,@typescript-eslint/ban-ts-ignore */
let consoleLogOutput: string[]; let consoleLogOutput: string[];
@ -20,10 +20,6 @@ beforeEach(async () => {
execPath = filesystem.path('/', 'tmp', tmpDirName); execPath = filesystem.path('/', 'tmp', tmpDirName);
filesystem.dir(execPath); filesystem.dir(execPath);
process.chdir(execPath); process.chdir(execPath);
try {
await system.run('git config --global user.email "you@example.com"');
await system.run('git config --global user.name "Your Name"');
} catch {}
}); });
afterEach(() => { afterEach(() => {
console.log = originalLog; console.log = originalLog;
@ -40,6 +36,10 @@ const initWithConfigAndCommit = async () => {
await system.run('touch yarn.lock'); await system.run('touch yarn.lock');
await system.run('echo node_modules > .gitignore'); await system.run('echo node_modules > .gitignore');
await system.run('git add * .nbxrc .gitignore'); await system.run('git add * .nbxrc .gitignore');
try {
await system.run('git config user.email "you@example.com"');
await system.run('git config user.name "Your Name"');
} catch {}
await system.run('git commit -m "init state"'); await system.run('git commit -m "init state"');
}; };

10
src/tests/add/tailwind.spec.ts

@ -4,7 +4,7 @@ import * as prompts from 'prompts';
import * as latestVersion from 'latest-version'; import * as latestVersion from 'latest-version';
import Tailwind from '../../commands/add/tailwind'; import Tailwind from '../../commands/add/tailwind';
jest.setTimeout(60000);
jest.setTimeout(90000);
/* eslint-disable no-console,max-nested-callbacks,@typescript-eslint/ban-ts-ignore */ /* eslint-disable no-console,max-nested-callbacks,@typescript-eslint/ban-ts-ignore */
let consoleLogOutput: string[]; let consoleLogOutput: string[];
@ -20,10 +20,6 @@ beforeEach(async () => {
execPath = filesystem.path('/', 'tmp', tmpDirName); execPath = filesystem.path('/', 'tmp', tmpDirName);
filesystem.dir(execPath); filesystem.dir(execPath);
process.chdir(execPath); process.chdir(execPath);
try {
await system.run('git config --global user.email "you@example.com"');
await system.run('git config --global user.name "Your Name"');
} catch {}
}); });
afterEach(() => { afterEach(() => {
console.log = originalLog; console.log = originalLog;
@ -40,6 +36,10 @@ const initWithConfigAndCommit = async (packageJson = {}) => {
await system.run('touch yarn.lock'); await system.run('touch yarn.lock');
await system.run('echo node_modules > .gitignore'); await system.run('echo node_modules > .gitignore');
await system.run('git add * .nbxrc .gitignore'); await system.run('git add * .nbxrc .gitignore');
try {
await system.run('git config user.email "you@example.com"');
await system.run('git config user.name "Your Name"');
} catch {}
await system.run('git commit -m "init state"'); await system.run('git commit -m "init state"');
}; };

2
src/tests/wall.spec.ts

@ -2,7 +2,7 @@ import { filesystem } from 'gluegun';
import Wall from '../commands/wall'; import Wall from '../commands/wall';
const stripANSI = require('strip-ansi'); const stripANSI = require('strip-ansi');
jest.setTimeout(30000);
jest.setTimeout(90000);
/* eslint-disable no-console,max-nested-callbacks,@typescript-eslint/ban-ts-ignore */ /* eslint-disable no-console,max-nested-callbacks,@typescript-eslint/ban-ts-ignore */
let consoleLogOutput: string[]; let consoleLogOutput: string[];
let execPath: string; let execPath: string;

21
src/utls/base-add-command.spec.ts

@ -18,10 +18,6 @@ beforeEach(async () => {
execPath = filesystem.path('/', 'tmp', tmpDirName); execPath = filesystem.path('/', 'tmp', tmpDirName);
filesystem.dir(execPath); filesystem.dir(execPath);
process.chdir(execPath); process.chdir(execPath);
try {
await system.run('git config --global user.email "you@example.com"');
await system.run('git config --global user.name "Your Name"');
} catch {}
}); });
afterEach(() => { afterEach(() => {
console.log = originalLog; console.log = originalLog;
@ -58,6 +54,10 @@ const initWithConfigAndCommit = async () => {
filesystem.write('package.json', {}); filesystem.write('package.json', {});
await system.run('touch yarn.lock'); await system.run('touch yarn.lock');
await system.run('git add * .nbxrc'); await system.run('git add * .nbxrc');
try {
await system.run('git config user.email "you@example.com"');
await system.run('git config user.name "Your Name"');
} catch {}
await system.run('git commit -m "init state"'); await system.run('git commit -m "init state"');
}; };
@ -148,6 +148,10 @@ describe('BaseAddCommand', () => {
it(`should erro when ${action.name}`, async () => { it(`should erro when ${action.name}`, async () => {
filesystem.write('.nbxrc', { git: { user: 'aaa', email: 'bbb' } }); filesystem.write('.nbxrc', { git: { user: 'aaa', email: 'bbb' } });
await system.run('git init'); await system.run('git init');
try {
await system.run('git config user.email "you@example.com"');
await system.run('git config user.name "Your Name"');
} catch {}
for (const step of action.actions) { for (const step of action.actions) {
// eslint-disable-next-line no-await-in-loop // eslint-disable-next-line no-await-in-loop
await system.run(step); await system.run(step);
@ -178,7 +182,10 @@ describe('BaseAddCommand', () => {
filesystem.write('.nbxrc', { git: { user: 'aaa', email: 'bbb' } }); filesystem.write('.nbxrc', { git: { user: 'aaa', email: 'bbb' } });
await system.run('git init'); await system.run('git init');
await system.run('git add .nbxrc'); await system.run('git add .nbxrc');
await system.run('pwd');
try {
await system.run('git config user.email "you@example.com"');
await system.run('git config user.name "Your Name"');
} catch {}
await system.run('git commit -m "initial commit"'); await system.run('git commit -m "initial commit"');
await system.run('git status'); await system.run('git status');
await RunCommand.run(['initGit']); await RunCommand.run(['initGit']);
@ -252,6 +259,10 @@ describe('BaseAddCommand', () => {
filesystem.write('.nbxrc', { git: { user: 'aaa', email: 'bbb' } }); filesystem.write('.nbxrc', { git: { user: 'aaa', email: 'bbb' } });
await system.run('touch test.md'); await system.run('touch test.md');
await system.run('git add * .nbxrc'); await system.run('git add * .nbxrc');
try {
await system.run('git config user.email "you@example.com"');
await system.run('git config user.name "Your Name"');
} catch {}
await system.run('git commit -m "test"'); await system.run('git commit -m "test"');
const before = await system.run('git status -s'); const before = await system.run('git status -s');

5
src/utls/base-add-command.ts

@ -1,7 +1,10 @@
import { BaseCommand } from './base-command'; import { BaseCommand } from './base-command';
import { add, commit, config as gitConfig, statusMatrix } from 'isomorphic-git';
import { add, plugins, commit, config as gitConfig, statusMatrix } from 'isomorphic-git';
import * as latestVersion from 'latest-version'; import * as latestVersion from 'latest-version';
import { filesystem, system } from 'gluegun'; import { filesystem, system } from 'gluegun';
import * as fs from 'fs';
plugins.set('fs', fs);
export abstract class BaseAddCommand extends BaseCommand { export abstract class BaseAddCommand extends BaseCommand {
static flags = { static flags = {

Loading…
Cancel
Save