forked from nestjs/nest-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix unit tests + create Vagrant to test nest-cli in a nice environment
- Loading branch information
thomrick
committed
Nov 19, 2017
1 parent
09a6b22
commit 62850f5
Showing
6 changed files
with
100 additions
and
101 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
node_modules/ | ||
.idea/ | ||
.vagrant/ | ||
node_modules/ | ||
bin/ | ||
.nyc_output/ | ||
coverage/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
}); | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
}); | ||
}); |