Skip to content

Commit

Permalink
feat: setup env validations
Browse files Browse the repository at this point in the history
  • Loading branch information
thetutlage committed Aug 21, 2023
1 parent 4891134 commit 0a6ec4f
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 6 deletions.
10 changes: 10 additions & 0 deletions configure.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,16 @@ export async function configure(command: Configure) {
REDIS_PASSWORD: '',
})

/**
* Validate environment variables
*/
await command.defineEnvValidations({
variables: {
REDIS_HOST: `Env.schema.string({ format: 'host' })`,
REDIS_PORT: 'Env.schema.number()',
},
})

/**
* Add provider to rc file
*/
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
"sync-labels": "github-label-sync --labels .github/labels.json adonisjs/redis"
},
"devDependencies": {
"@adonisjs/assembler": "^6.1.3-18",
"@adonisjs/core": "^6.1.5-18",
"@adonisjs/eslint-config": "^1.1.8",
"@adonisjs/prettier-config": "^1.1.8",
Expand Down
23 changes: 17 additions & 6 deletions tests/configure.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,23 +57,34 @@ test.group('Configure', (group) => {
await assert.fileContains('config/redis.ts', `declare module '@adonisjs/redis/types'`)
})

test('add redis_provider to the rc file', async ({ assert }) => {
const { command } = await setupConfigureCommand()
test('add redis_provider to the rc file', async ({ fs, assert }) => {
await fs.createJson('tsconfig.json', {})
await fs.create('adonisrc.ts', `export default defineConfig({})`)

const { command } = await setupConfigureCommand()
await command.exec()

await assert.fileExists('.adonisrc.json')
await assert.fileContains('.adonisrc.json', '"@adonisjs/redis/redis_provider"')
await assert.fileExists('adonisrc.ts')
await assert.fileContains(
'adonisrc.ts',
`providers: [() => import('@adonisjs/redis/redis_provider')]`
)
})

test('add env variables for the selected drivers', async ({ assert, fs }) => {
const { command } = await setupConfigureCommand()

await fs.createJson('tsconfig.json', {})
await fs.create('.env', '')
await fs.create('start/env.ts', `export default Env.create(new URL('./'), {})`)
await fs.create('adonisrc.ts', `export default defineConfig({})`)

const { command } = await setupConfigureCommand()
await command.exec()

await assert.fileContains('.env', 'REDIS_HOST=127.0.0.1')
await assert.fileContains('.env', 'REDIS_PORT=6379')
await assert.fileContains('.env', 'REDIS_PASSWORD=')

await assert.fileContains('start/env.ts', `REDIS_HOST: Env.schema.string({ format: 'host' })`)
await assert.fileContains('start/env.ts', 'REDIS_PORT: Env.schema.number()')
})
})

0 comments on commit 0a6ec4f

Please sign in to comment.