Skip to content

Commit

Permalink
refactor: fix failing tests
Browse files Browse the repository at this point in the history
  • Loading branch information
thetutlage committed Nov 11, 2019
1 parent fa4410b commit 1911dc2
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 90 deletions.
24 changes: 17 additions & 7 deletions src/Ignitor/Ace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,16 @@ export class Ace {
constructor (private _ignitor: Ignitor) {
}

/**
* Exit process conditionally
*/
private _exitProcess (signal) {
if (process.env.NODE_ENV === 'testing') {
return
}
process.exit(signal)
}

/**
* Prints the ascii logo
*/
Expand Down Expand Up @@ -49,7 +59,7 @@ export class Ace {
console.log(`Framework version: ${this._ignitor.application.adonisVersion?.version || 'NA'}`)
console.log(`App version: ${this._ignitor.application.version?.version || 'NA'}`)
console.log('')
process.exit(0)
this._exitProcess(0)
}, {})
}

Expand All @@ -60,7 +70,7 @@ export class Ace {
this._dumpAsciiLogo()
kernel.printHelp(command)
console.log('')
process.exit(0)
this._exitProcess(0)
}

/**
Expand All @@ -84,10 +94,10 @@ export class Ace {
*/
command.logger.create('ace-manifest.json')

process.exit(0)
this._exitProcess(0)
} catch (error) {
await new ErrorHandler(this._ignitor.application).handleError(error)
process.exit(1)
this._exitProcess(1)
}
}

Expand Down Expand Up @@ -149,7 +159,7 @@ export class Ace {
*/
if (argv[0] === 'generate:manifest') {
class Noop extends BaseCommand {}
this._generateManifest(manifest, new Noop())
await this._generateManifest(manifest, new Noop())
return
}

Expand All @@ -162,7 +172,7 @@ export class Ace {
await this._bootstrap(command)
} catch (error) {
await new ErrorHandler(this._ignitor.application).handleError(error)
process.exit(1)
this._exitProcess(1)
}
})

Expand All @@ -178,7 +188,7 @@ export class Ace {
await kernel.handle(argv)
} catch (error) {
handleError(error)
process.exit(1)
this._exitProcess(1)
}
}
}
2 changes: 1 addition & 1 deletion src/Ignitor/Bootstrapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ export class Bootstrapper {
global[Symbol.for('ioc.call')] = ioc.call.bind(ioc)

const adonisCorePkgFile = findPkg(join(__dirname, '..', '..')).next().value
const appPkgFile = findPkg(this._application.appRoot).next().value
const appPkgFile = findPkg(this._appRoot).next().value

const pkgFile = {
name: appPkgFile.name,
Expand Down
24 changes: 17 additions & 7 deletions src/Ignitor/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,16 @@ export class Ignitor {
this._errorHandler = new ErrorHandler(this.application)
}

/**
* Exit process conditionally
*/
private _exitProcess (signal) {
if (process.env.NODE_ENV === 'testing') {
return
}
process.exit(signal)
}

/**
* Executes the ready hooks when the application is ready.
*
Expand Down Expand Up @@ -132,19 +142,19 @@ export class Ignitor {
process.on('SIGINT', async () => {
try {
await this.close()
process.exit(0)
this._exitProcess(0)
} catch (error) {
process.exit(1)
this._exitProcess(1)
}
})
}

process.on('SIGTERM', async () => {
try {
await this.close()
process.exit(0)
this._exitProcess(0)
} catch (error) {
process.exit(1)
this._exitProcess(1)
}
})
}
Expand Down Expand Up @@ -245,7 +255,7 @@ export class Ignitor {
await this._httpServer.start()
} catch (error) {
await this._errorHandler.handleError(error)
process.exit(1)
this._exitProcess(1)
}
}

Expand Down Expand Up @@ -286,10 +296,10 @@ export class Ignitor {
await Promise.race([this._prepareShutDown(), new Promise((resolve) => {
setTimeout(resolve, waitTimeout)
})])
process.exit(0)
this._exitProcess(0)
} catch (error) {
await this._errorHandler.handleError(error)
process.exit(1)
this._exitProcess(1)
}
}
}
5 changes: 5 additions & 0 deletions test/ignitor-boostrapper.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,18 @@ test.group('Ignitor | Setup', (group) => {
process.env.ENV_SILENT = 'true'
})

group.beforeEach(() => {
process.env.NODE_ENV = 'testing'
})

group.after(async () => {
await fs.cleanup()
delete process.env.ENV_SILENT
delete process.env.APP_KEY
})

group.afterEach(async () => {
delete process.env.NODE_ENV
await fs.cleanup()
})

Expand Down
97 changes: 22 additions & 75 deletions test/ignitor.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,18 @@ test.group('Ignitor | App Provider', (group) => {
process.env.ENV_SILENT = 'true'
})

group.beforeEach(() => {
process.env.NODE_ENV = 'testing'
})

group.after(async () => {
await fs.cleanup()
delete process.env.ENV_SILENT
delete process.env.APP_KEY
})

group.afterEach(async () => {
delete process.env.NODE_ENV
await fs.cleanup()
})

Expand Down Expand Up @@ -104,13 +109,18 @@ test.group('Ignitor | Http', (group) => {
process.env.ENV_SILENT = 'true'
})

group.beforeEach(() => {
process.env.NODE_ENV = 'testing'
})

group.after(async () => {
await fs.cleanup()
delete process.env.ENV_SILENT
delete process.env.APP_KEY
})

group.afterEach(async () => {
delete process.env.NODE_ENV
await fs.cleanup()
})

Expand Down Expand Up @@ -315,7 +325,12 @@ test.group('Ignitor | Ace', (group) => {
await fs.cleanup()
})

group.beforeEach(() => {
process.env.NODE_ENV = 'testing'
})

group.afterEach(async () => {
delete process.env.NODE_ENV
await fs.cleanup()
})

Expand All @@ -326,15 +341,15 @@ test.group('Ignitor | Ace', (group) => {
await fs.add('ace-manifest.json', JSON.stringify({
foo: {
commandName: 'foo',
commandPath: 'fooCommand',
commandPath: './fooCommand',
},
}))

await fs.add('fooCommand.ts', `export default class FooCommand {
public static args = []
public static flags = []
public static boot () {
public static $boot () {
}
public handle () {
Expand All @@ -350,7 +365,7 @@ test.group('Ignitor | Ace', (group) => {
await fs.add('ace-manifest.json', JSON.stringify({
foo: {
commandName: 'foo',
commandPath: 'foo.ts',
commandPath: './foo.ts',
settings: {
loadApp: true,
},
Expand All @@ -361,7 +376,7 @@ test.group('Ignitor | Ace', (group) => {
public static args = []
public static flags = []
public static boot () {
public static $boot () {
}
public handle () {
Expand All @@ -386,17 +401,6 @@ test.group('Ignitor | Ace', (group) => {
})

test('generate manifest file', async (assert) => {
const ignitor = new Ignitor(fs.basePath)
await fs.fsExtra.ensureDir(join(fs.basePath, 'config'))

await fs.add('providers/AppProvider.ts', `
import { join } from 'path'
export default class AppProvider {
public commands = [join(__dirname, '..', 'commands', 'Foo.ts')]
}
`)

await fs.add('commands/Foo.ts', `
export default class Foo {
public static commandName = 'foo'
Expand All @@ -405,7 +409,7 @@ test.group('Ignitor | Ace', (group) => {
public static args = []
public static flags = []
public static boot () {
public static $boot () {
}
public handle () {
Expand All @@ -415,82 +419,25 @@ test.group('Ignitor | Ace', (group) => {

await fs.add(`start/app.ts`, `export const providers = [
'${join(__dirname, '../providers/AppProvider.ts')}',
'${join(fs.basePath, 'providers/AppProvider.ts')}',
]`)

await fs.add(`config/app.ts`, `export const appKey = '${SECRET}'`)

await fs.add('.adonisrc.json', JSON.stringify({
autoloads: {
'App': './app',
},
commands: ['./commands/Foo'],
}))

await ignitor.handleAceCommand(['generate:manifest'])
assert.isTrue(ignitor.bootstraped)

const manifestFile = await fs.get('ace-manifest.json')
assert.deepEqual(JSON.parse(manifestFile), {
foo: {
settings: {},
commandPath: join(fs.basePath, 'commands/Foo'),
commandName: 'foo',
description: 'Print foo',
args: [],
flags: [],
},
})
})

test('load commands from start/app file', async (assert) => {
const ignitor = new Ignitor(fs.basePath)

await fs.fsExtra.ensureDir(join(fs.basePath, 'config'))

await fs.add('commands/Foo.ts', `
export default class Foo {
public static commandName = 'foo'
public static description = 'Print foo'
public static args = []
public static flags = []
public static boot () {
}
public handle () {
}
}
`)

await fs.add(`start/app.ts`, `
import { join } from 'path'
export const providers = [
'${join(__dirname, '../providers/AppProvider.ts')}',
]
export const commands = [
join(__dirname, '..', 'commands', 'Foo.ts')
]
`)

await fs.add(`config/app.ts`, `export const appKey = '${SECRET}'`)

await fs.add('.adonisrc.json', JSON.stringify({
autoloads: {
'App': './app',
},
}))

await ignitor.handleAceCommand(['generate:manifest'])
assert.isTrue(ignitor.bootstraped)

const manifestFile = await fs.get('ace-manifest.json')
assert.deepEqual(JSON.parse(manifestFile), {
foo: {
settings: {},
commandPath: join(fs.basePath, 'commands/Foo'),
commandPath: './commands/Foo',
commandName: 'foo',
description: 'Print foo',
args: [],
Expand Down

0 comments on commit 1911dc2

Please sign in to comment.