Skip to content
This repository has been archived by the owner on Jan 24, 2024. It is now read-only.

Commit

Permalink
feat: add support for adding test providers
Browse files Browse the repository at this point in the history
  • Loading branch information
thetutlage committed Apr 14, 2022
1 parent 0637f9e commit ccebb85
Show file tree
Hide file tree
Showing 6 changed files with 155 additions and 1 deletion.
6 changes: 6 additions & 0 deletions instructions.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,12 @@
"items": {
"type": "string"
}
},
"testProviders": {
"type": "array",
"items": {
"type": "string"
}
}
}
}
Expand Down
1 change: 1 addition & 0 deletions src/Contracts/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ export type PackageInstructionsBlock = {
providers?: string[]
aliases?: { [key: string]: string }
aceProviders?: string[]
testProviders?: string[]
}

/**
Expand Down
25 changes: 25 additions & 0 deletions src/Files/Special/AdonisRc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,20 @@ export class AdonisRcFile extends JsonFile {
*/
private aceProviders: any[] = []

/**
* Storing a local copy of testProviders for concatenating
* new entries.
*/
private testProviders: any[] = []

constructor(basePath: string) {
super(basePath, '.adonisrc.json')
this.preloads = this.get('preloads', [])
this.metaFiles = this.get('metaFiles', [])
this.commands = this.get('commands', [])
this.providers = this.get('providers', [])
this.aceProviders = this.get('aceProviders', [])
this.testProviders = this.get('testProviders', [])
}

/**
Expand Down Expand Up @@ -82,6 +89,10 @@ export class AdonisRcFile extends JsonFile {
key = 'aceProviders'
}

if (body.key.startsWith('testProviders')) {
key = 'testProviders'
}

if (!key) {
return
}
Expand Down Expand Up @@ -225,4 +236,18 @@ export class AdonisRcFile extends JsonFile {
this.aceProviders[entryIndex] = provider
this.set(`aceProviders[${entryIndex}]`, provider)
}

/**
* Add new providers to the test providers array
*/
public addTestProvider(provider: string) {
let entryIndex = this.testProviders.findIndex((command) => {
return command === provider
})

entryIndex = entryIndex === -1 ? this.testProviders.length : entryIndex

this.testProviders[entryIndex] = provider
this.set(`testProviders[${entryIndex}]`, provider)
}
}
11 changes: 10 additions & 1 deletion src/Tasks/Instructions/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ export class Instructions {
/**
* Return early when not providers are mentioned
*/
if (!instructions.providers && !instructions.aceProviders) {
if (!instructions.providers && !instructions.aceProviders && !instructions.testProviders) {
return
}

Expand All @@ -273,6 +273,10 @@ export class Instructions {
instructions.aceProviders.forEach((provider) => adonisRcFile.addAceProvider(provider))
}

if (instructions.testProviders) {
instructions.testProviders.forEach((provider) => adonisRcFile.addTestProvider(provider))
}

adonisRcFile.commit()

if (instructions.providers) {
Expand All @@ -284,6 +288,11 @@ export class Instructions {
const suffix = this.getSuffix(this.formatArray(instructions.aceProviders), 'aceProviders')
this.logger.action('update').succeeded(`.adonisrc.json ${suffix}`)
}

if (instructions.testProviders) {
const suffix = this.getSuffix(this.formatArray(instructions.testProviders), 'testProviders')
this.logger.action('update').succeeded(`.adonisrc.json ${suffix}`)
}
}

/**
Expand Down
91 changes: 91 additions & 0 deletions test/adonis-rc-file.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -762,4 +762,95 @@ test.group('AdonisRc file', (group) => {
aceProviders: ['@adonisjs/core'],
})
})

test('add provider to test providers array', async ({ assert }) => {
const rcfile = new AdonisRcFile(fs.basePath)
rcfile.addTestProvider('./providers/App')
rcfile.commit()

const contents = await fs.get('.adonisrc.json')
assert.deepEqual(JSON.parse(contents), {
testProviders: ['./providers/App'],
})
})

test('add multiple providers to test providers array', async ({ assert }) => {
const rcfile = new AdonisRcFile(fs.basePath)
rcfile.addTestProvider('@adonisjs/core')
rcfile.addTestProvider('@adonisjs/fold')
rcfile.commit()

const contents = await fs.get('.adonisrc.json')
assert.deepEqual(JSON.parse(contents), {
testProviders: ['@adonisjs/core', '@adonisjs/fold'],
})
})

test('update provider path inside test providers array', async ({ assert }) => {
await fs.add(
'.adonisrc.json',
JSON.stringify(
{
testProviders: ['@adonisjs/core', '@adonisjs/fold'],
},
null,
2
)
)

const rcfile = new AdonisRcFile(fs.basePath)
rcfile.addTestProvider('@adonisjs/core')
rcfile.commit()

const contents = await fs.get('.adonisrc.json')
assert.deepEqual(JSON.parse(contents), {
testProviders: ['@adonisjs/core', '@adonisjs/fold'],
})
})

test('update provider path inside test providers array by adding new provider', async ({
assert,
}) => {
await fs.add(
'.adonisrc.json',
JSON.stringify(
{
testProviders: ['@adonisjs/core'],
},
null,
2
)
)

const rcfile = new AdonisRcFile(fs.basePath)
rcfile.addTestProvider('@adonisjs/fold')
rcfile.commit()

const contents = await fs.get('.adonisrc.json')
assert.deepEqual(JSON.parse(contents), {
testProviders: ['@adonisjs/core', '@adonisjs/fold'],
})
})

test('remove provider path from test providers array on rollback', async ({ assert }) => {
await fs.add(
'.adonisrc.json',
JSON.stringify(
{
testProviders: ['@adonisjs/core'],
},
null,
2
)
)

const rcfile = new AdonisRcFile(fs.basePath)
rcfile.addTestProvider('@adonisjs/fold')
rcfile.rollback()

const contents = await fs.get('.adonisrc.json')
assert.deepEqual(JSON.parse(contents), {
testProviders: ['@adonisjs/core'],
})
})
})
22 changes: 22 additions & 0 deletions test/execute-instructions.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -444,4 +444,26 @@ test.group('Execute instructions', (group) => {
],
})
})

test('define test providers inside .adonisrc.json file', async ({ assert }) => {
await fs.add(
'node_modules/@fake/app/package.json',
JSON.stringify({
name: '@fake/app',
version: '1.0.0',
adonisjs: {
testProviders: ['@fake/app'],
},
})
)

const application = new Application(fs.basePath, 'web', {})
const completed = await new Instructions('@fake/app', fs.basePath, application, true).execute()
assert.isTrue(completed)

const rcContents = await fs.fsExtra.readJSON(join(fs.basePath, '.adonisrc.json'))
assert.deepEqual(rcContents, {
testProviders: ['@fake/app'],
})
})
})

0 comments on commit ccebb85

Please sign in to comment.