|
| 1 | +/* |
| 2 | + * @adonisjs/core |
| 3 | + * |
| 4 | + * (c) AdonisJS |
| 5 | + * |
| 6 | + * For the full copyright and license information, please view the LICENSE |
| 7 | + * file that was distributed with this source code. |
| 8 | + */ |
| 9 | + |
| 10 | +import { test } from '@japa/runner' |
| 11 | +import EnvAdd from '../../commands/env/add.js' |
| 12 | +import { AceFactory } from '../../factories/core/ace.js' |
| 13 | + |
| 14 | +test.group('Env Add command', () => { |
| 15 | + test('add new env variable to the different files', async ({ assert, fs }) => { |
| 16 | + await fs.createJson('tsconfig.json', {}) |
| 17 | + await fs.create('.env', '') |
| 18 | + await fs.create('.env.example', '') |
| 19 | + await fs.create( |
| 20 | + './start/env.ts', |
| 21 | + `import { Env } from '@adonisjs/core/env' |
| 22 | + export default await Env.create(new URL('../', import.meta.url), {})` |
| 23 | + ) |
| 24 | + |
| 25 | + const ace = await new AceFactory().make(fs.baseUrl) |
| 26 | + await ace.app.init() |
| 27 | + ace.ui.switchMode('raw') |
| 28 | + |
| 29 | + const command = await ace.create(EnvAdd, ['variable', 'value', '--type=string']) |
| 30 | + await command.exec() |
| 31 | + |
| 32 | + await assert.fileContains('.env', 'VARIABLE=value') |
| 33 | + await assert.fileContains('.env.example', 'VARIABLE=value') |
| 34 | + await assert.fileContains('./start/env.ts', 'VARIABLE: Env.schema.string()') |
| 35 | + }) |
| 36 | + |
| 37 | + test('convert variable to screaming snake case', async ({ assert, fs }) => { |
| 38 | + await fs.createJson('tsconfig.json', {}) |
| 39 | + await fs.create('.env', '') |
| 40 | + await fs.create('.env.example', '') |
| 41 | + await fs.create( |
| 42 | + './start/env.ts', |
| 43 | + `import { Env } from '@adonisjs/core/env' |
| 44 | + export default await Env.create(new URL('../', import.meta.url), {})` |
| 45 | + ) |
| 46 | + |
| 47 | + const ace = await new AceFactory().make(fs.baseUrl) |
| 48 | + await ace.app.init() |
| 49 | + ace.ui.switchMode('raw') |
| 50 | + |
| 51 | + const command = await ace.create(EnvAdd, ['stripe_ApiKey', 'value', '--type=string']) |
| 52 | + await command.exec() |
| 53 | + |
| 54 | + await assert.fileContains('.env', 'STRIPE_API_KEY=value') |
| 55 | + await assert.fileContains('.env.example', 'STRIPE_API_KEY=value') |
| 56 | + await assert.fileContains('./start/env.ts', 'STRIPE_API_KEY: Env.schema.string()') |
| 57 | + }) |
| 58 | + |
| 59 | + test('enum type with allowed values', async ({ assert, fs }) => { |
| 60 | + await fs.createJson('tsconfig.json', {}) |
| 61 | + await fs.create('.env', '') |
| 62 | + await fs.create('.env.example', '') |
| 63 | + await fs.create( |
| 64 | + './start/env.ts', |
| 65 | + `import { Env } from '@adonisjs/core/env' |
| 66 | + export default await Env.create(new URL('../', import.meta.url), {})` |
| 67 | + ) |
| 68 | + |
| 69 | + const ace = await new AceFactory().make(fs.baseUrl) |
| 70 | + await ace.app.init() |
| 71 | + ace.ui.switchMode('raw') |
| 72 | + |
| 73 | + const command = await ace.create(EnvAdd, [ |
| 74 | + 'variable', |
| 75 | + 'bar', |
| 76 | + '--type=enum', |
| 77 | + '--enum-values=foo', |
| 78 | + '--enum-values=bar', |
| 79 | + ]) |
| 80 | + await command.exec() |
| 81 | + |
| 82 | + await assert.fileContains('.env', 'VARIABLE=bar') |
| 83 | + await assert.fileContains('.env.example', 'VARIABLE=bar') |
| 84 | + await assert.fileContains( |
| 85 | + './start/env.ts', |
| 86 | + "VARIABLE: Env.schema.enum(['foo', 'bar'] as const)" |
| 87 | + ) |
| 88 | + }) |
| 89 | + |
| 90 | + test('prompt when nothing is passed to the command', async ({ assert, fs }) => { |
| 91 | + await fs.createJson('tsconfig.json', {}) |
| 92 | + await fs.create('.env', '') |
| 93 | + await fs.create('.env.example', '') |
| 94 | + await fs.create( |
| 95 | + './start/env.ts', |
| 96 | + `import { Env } from '@adonisjs/core/env' |
| 97 | + export default await Env.create(new URL('../', import.meta.url), {})` |
| 98 | + ) |
| 99 | + |
| 100 | + const ace = await new AceFactory().make(fs.baseUrl) |
| 101 | + await ace.app.init() |
| 102 | + ace.ui.switchMode('raw') |
| 103 | + |
| 104 | + const command = await ace.create(EnvAdd, []) |
| 105 | + |
| 106 | + command.prompt.trap('Enter the variable name').replyWith('my_variable_name') |
| 107 | + command.prompt.trap('Enter the variable value').replyWith('my_value') |
| 108 | + command.prompt.trap('Select the variable type').replyWith('string') |
| 109 | + |
| 110 | + await command.exec() |
| 111 | + |
| 112 | + await assert.fileContains('.env', 'MY_VARIABLE_NAME=my_value') |
| 113 | + await assert.fileContains('.env.example', 'MY_VARIABLE_NAME=my_value') |
| 114 | + await assert.fileContains('./start/env.ts', 'MY_VARIABLE_NAME: Env.schema.string()') |
| 115 | + }) |
| 116 | +}) |
0 commit comments