Skip to content

Commit

Permalink
fix unit tests + create Vagrant to test nest-cli in a nice environment
Browse files Browse the repository at this point in the history
  • Loading branch information
thomrick committed Nov 19, 2017
1 parent 09a6b22 commit 62850f5
Show file tree
Hide file tree
Showing 6 changed files with 100 additions and 101 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
node_modules/
.idea/
.vagrant/
node_modules/
bin/
.nyc_output/
coverage/
4 changes: 4 additions & 0 deletions Vagrantfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Vagrant.configure("2") do |config|
config.vm.box = "debian/jessie64"
config.vm.provision :shell, path: "bootstrap.sh"
end
6 changes: 6 additions & 0 deletions bootstrap.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#!/usr/bin/env bash
apt-get update
apt-get install -y curl
apt-get install -y git
curl -sL https://deb.nodesource.com/setup_8.x | sudo -E bash -
apt-get install -y nodejs
2 changes: 1 addition & 1 deletion src/core/assets/generators/tests/asset.generator.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ describe('AssetGenerator', () => {
throw new Error('should not be here');
})
.catch(error => {
expect(error.message).to.be.equal('Unhandled error. (error message)');
expect(error.message).to.be.equal('Unhandled "error" event. (error message)');
});
});
});
Expand Down
138 changes: 62 additions & 76 deletions src/lib/handlers/info-command.handler.ts
Original file line number Diff line number Diff line change
@@ -1,88 +1,74 @@
import { exec } from 'child_process';
import {exec} from 'child_process';
import * as fs from 'fs';
import * as nodemon from 'nodemon';
import * as path from 'path';
import { Logger } from '../../common/logger/interfaces/logger.interface';
import { GenerateCommandArguments } from '../../common/program/interfaces/command.aguments.interface';
import { CommandHandler } from '../../common/program/interfaces/command.handler.interface';
import { GenerateCommandOptions } from '../../common/program/interfaces/command.options.interface';
import { ConfigurationService } from '../../core/configuration/configuration.service';
import { ColorService } from '../../core/logger/color.service';
import { LoggerService } from '../../core/logger/logger.service';
import { nestWords } from '../../core/utils/nest-ascii';
import {Logger} from '../../common/logger/interfaces/logger.interface';
import {GenerateCommandArguments} from '../../common/program/interfaces/command.aguments.interface';
import {CommandHandler} from '../../common/program/interfaces/command.handler.interface';
import {GenerateCommandOptions} from '../../common/program/interfaces/command.options.interface';
import {ColorService} from '../../core/logger/color.service';
import {LoggerService} from '../../core/logger/logger.service';
import {nestWords} from '../../core/utils/nest-ascii';
import osName = require('os-name');


export class InfoCommandHandler implements CommandHandler {
private logger: Logger;

private logger: Logger;

public execute(
args: GenerateCommandArguments,
options: GenerateCommandOptions,
logger: Logger
): Promise<void> {
LoggerService.setLogger(logger);
this.logger = logger;
logger.debug(ColorService.blue('[DEBUG]'), 'execute serve command');
return ConfigurationService.load()
.then(async () => {
const nodeVersion = process.version + ' ' + ((<any>process).release.lts || '');
const nestVersion = await this.getNestVersion();
const cliVersion = await this.getCliVersion();
const npmVersion = await this.getNpmVersion();
logger.info(ColorService.yellow(nestWords));
logger.info(ColorService.green('[System Information]'))
logger.info(this.concatenate('OS Version', osName()));
logger.info(this.concatenate('NodeJS Version', nodeVersion));
logger.info(this.concatenate('NPM Version', npmVersion));
logger.info(ColorService.green('[Nest Information]'))
logger.info(this.concatenate('NestJS Version', nestVersion));
logger.info(this.concatenate('Nest CLI Version', cliVersion));
logger.info('');
})
}

private concatenate(property: string, value: string): string {
return property + ' ' + ColorService.blue(`${value}`);
}
public async execute(args: GenerateCommandArguments, options: GenerateCommandOptions, logger: Logger): Promise<void> {
LoggerService.setLogger(logger);
this.logger = logger;
logger.debug(ColorService.blue('[DEBUG]'), 'execute serve command');
const nodeVersion = process.version + ' ' + ((<any>process).release.lts || '');
const nestVersion = await this.getNestVersion();
const cliVersion = await this.getCliVersion();
const npmVersion = await this.getNpmVersion();
logger.info(ColorService.yellow(nestWords));
logger.info(ColorService.green('[System Information]'));
logger.info(this.concatenate('OS Version', osName()));
logger.info(this.concatenate('NodeJS Version', nodeVersion));
logger.info(this.concatenate('NPM Version', npmVersion));
logger.info(ColorService.green('[Nest Information]'));
logger.info(this.concatenate('NestJS Version', nestVersion));
logger.info(this.concatenate('Nest CLI Version', cliVersion));
logger.info('');
}

private async getNestVersion(): Promise<string> {
return await this._getVersionOf(path.join(process.cwd(), 'node_modules/@nestjs/core/package.json'));
}
private concatenate(property: string, value: string): string {
return `${ property } ${ ColorService.blue(value) }`;
}

private async getCliVersion(): Promise<string> {
return await this._getVersionOf(path.resolve('./package.json'));
}
private async getNestVersion(): Promise<string> {
return await this._getVersionOf(path.join(process.cwd(), 'node_modules/@nestjs/core/package.json'));
}

private getNpmVersion(): Promise<string> {
return new Promise((resolve, reject) => {
const child = exec('npm -v')
child.stdout.on('data', (data) => {
resolve(data.toString());
});
child.stderr.on('data', (err) => {
return this.logger.error(ColorService.red('[ERROR]'), 'There was an error reading the current NPM version.')
});
})
}
private async getCliVersion(): Promise<string> {
return await this._getVersionOf(path.resolve('./package.json'));
}

private _getVersionOf(file: string): Promise<string> {
return new Promise((resolve, reject) => {
fs.readFile(file, 'utf-8', (err, data) => {
if (err) {
this.logger.error(ColorService.red('[ERROR]'), 'There was an error reading package.json.');
return reject();
}
private getNpmVersion(): Promise<string> {
return new Promise((resolve, reject) => {
const child = exec('npm -v');
child.stdout.on('data', (data) => resolve(data.toString()));
child.stderr.on('error', (err) =>
this.logger.error(ColorService.red('[ERROR]'), 'There was an error reading the current NPM version.')
);
});
}

const p = JSON.parse(data);
if (p && p.version) {
return resolve(p.version);
} else {
this.logger.error(ColorService.red('[ERROR]'), 'This project does not depend on @nestjs/core. That is an error.');
return reject();
}
})
});
}
private _getVersionOf(file: string): Promise<string> {
return new Promise((resolve, reject) => {
fs.readFile(file, 'utf-8', (err, data) => {
if (err !== undefined && err !== null) {
this.logger.error(ColorService.red('[ERROR]'), 'There was an error reading package.json.');
return reject(err);
}
const p = JSON.parse(data);
if (p && p.version) {
return resolve(p.version);
} else {
this.logger.error(ColorService.red('[ERROR]'), 'This project does not depend on @nestjs/core. That is an error.');
return reject();
}
});
});
}
}
48 changes: 25 additions & 23 deletions src/lib/handlers/tests/info-command.handler.spec.ts
Original file line number Diff line number Diff line change
@@ -1,33 +1,35 @@
import * as sinon from 'sinon';
import { CommandHandler } from '../../../common/program/interfaces/command.handler.interface';
import { ConfigurationService } from '../../../core/configuration/configuration.service';
import { InfoCommandHandler } from '../info-command.handler';
import * as fs from 'fs';
import {CommandHandler} from '../../../common/program/interfaces/command.handler.interface';
import {InfoCommandHandler} from '../info-command.handler';
import * as path from 'path';


describe('InfoCommandHandler', () => {
let sandbox: sinon.SinonSandbox;
beforeEach(() => sandbox = sinon.sandbox.create());
afterEach(() => sandbox.restore());
let sandbox: sinon.SinonSandbox;
beforeEach(() => sandbox = sinon.sandbox.create());
afterEach(() => sandbox.restore());

let handler: CommandHandler;
beforeEach(() => {
handler = new InfoCommandHandler();
});
let handler: CommandHandler;
beforeEach(() => handler = new InfoCommandHandler());

let loadStub: sinon.SinonStub;
let getPropertyStub: sinon.SinonStub;
beforeEach(() => {
loadStub = sandbox.stub(ConfigurationService, 'load').callsFake(() => Promise.resolve());
getPropertyStub = sandbox.stub(ConfigurationService, 'getProperty').callsFake(() => 'ts')
let readFileStub: sinon.SinonStub;
beforeEach(() => {
readFileStub = sandbox.stub(fs, 'readFile').callsFake((filename, encoding, callback) => {
if (filename === path.join(process.cwd(), 'node_modules/@nestjs/core/package.json')) {
callback(undefined, JSON.stringify({ version: 'version' }));
} else if (filename === path.resolve('./package.json')) {
callback(undefined, JSON.stringify({ version: 'version' }));
} else {
callback(new Error('Should not be there'));
}
});
});

describe('#execute()', () => {
it('should run the command handler', () => {
return handler.execute({}, {}, console)
.then(() => {
sinon.assert.calledOnce(loadStub);
});
});

describe('#execute()', () => {
it('should run the command handler', async () => {
await handler.execute({}, {}, console);
sinon.assert.called(readFileStub);
});
});
});

0 comments on commit 62850f5

Please sign in to comment.