Skip to content

Commit

Permalink
test(seeds): new environment config (#851)
Browse files Browse the repository at this point in the history
  • Loading branch information
RomainLanz committed Aug 26, 2022
1 parent 170f2a8 commit 11ae8b6
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 3 deletions.
4 changes: 4 additions & 0 deletions adonis-typings/seeder.ts
Expand Up @@ -14,7 +14,11 @@ declare module '@ioc:Adonis/Lucid/Seeder' {
* Shape of seeder class
*/
export type SeederConstructorContract = {
/**
* @deprecated
*/
developmentOnly: boolean
environment: string[]
new (client: QueryClientContract): {
client: QueryClientContract
run(): Promise<void>
Expand Down
4 changes: 4 additions & 0 deletions src/BaseSeeder/index.ts
Expand Up @@ -10,7 +10,11 @@
import { QueryClientContract } from '@ioc:Adonis/Lucid/Database'

export class BaseSeeder {
/**
* @deprecated
*/
public static developmentOnly: boolean
public static environment: string[]
constructor(public client: QueryClientContract) {}

public async run() {}
Expand Down
12 changes: 9 additions & 3 deletions src/SeedsRunner/index.ts
Expand Up @@ -57,11 +57,17 @@ export class SeedsRunner {
file: file,
}

if ('developmentOnly' in Source) {
this.app.logger.warn(`Seeder "${file.name}" is using the deprecated flag "developmentOnly".`)
}

/**
* Ignore when running in non-development environment and seeder is development
* only
* Ignore when when the node environement is not the same as the seeder configuration.
*/
if (Source.developmentOnly && !this.app.inDev) {
if (
(Source.developmentOnly && !this.app.inDev) ||
(Source.environment && !Source.environment.includes(this.app.nodeEnvironment))
) {
seeder.status = 'ignored'
return seeder
}
Expand Down
58 changes: 58 additions & 0 deletions test/seeds/seeds-runner.spec.ts
Expand Up @@ -96,4 +96,62 @@ test.group('Seeds Runner', (group) => {
delete process.env.NODE_ENV
await db.manager.closeAll()
})

test('mark file as ignored when "environment = production" and not running in production mode', async ({
assert,
}) => {
process.env.NODE_ENV = 'development'

const app = await setupApplication()
const db = getDb(app)
const runner = new SeedsRunner(db, app)

await fs.add(
'database/seeders/User.ts',
`export default class FooSeeder {
public static invoked = false
public static environment = ['production']
run () {
(this.constructor as any).invoked = true
}
}`
)

const files = await runner.getList()
const report = await runner.run(files[0])
assert.equal(report.status, 'ignored')

delete process.env.NODE_ENV
await db.manager.closeAll()
})

test('mark file as ignored when "environment = development" and not running in development mode', async ({
assert,
}) => {
process.env.NODE_ENV = 'production'

const app = await setupApplication()
const db = getDb(app)
const runner = new SeedsRunner(db, app)

await fs.add(
'database/seeders/User.ts',
`export default class FooSeeder {
public static invoked = false
public static environment = ['development']
run () {
(this.constructor as any).invoked = true
}
}`
)

const files = await runner.getList()
const report = await runner.run(files[0])
assert.equal(report.status, 'ignored')

delete process.env.NODE_ENV
await db.manager.closeAll()
})
})

0 comments on commit 11ae8b6

Please sign in to comment.