This commit is contained in:
94
tests/cli.test.ts
Normal file
94
tests/cli.test.ts
Normal file
@@ -0,0 +1,94 @@
|
||||
import * as path from 'path';
|
||||
import * as fs from 'fs';
|
||||
import { execSync } from 'child_process';
|
||||
|
||||
describe('CLI', () => {
|
||||
const fixturesDir = path.join(__dirname, 'fixtures');
|
||||
const cliPath = path.join(__dirname, '..', 'dist', 'index.js');
|
||||
|
||||
describe('--help flag', () => {
|
||||
test('shows help', () => {
|
||||
const output = execSync(
|
||||
`node ${cliPath} --help`,
|
||||
{ encoding: 'utf-8' }
|
||||
);
|
||||
|
||||
expect(output).toContain('Usage');
|
||||
expect(output).toContain('validate');
|
||||
expect(output).toContain('generate');
|
||||
expect(output).toContain('migrate');
|
||||
});
|
||||
});
|
||||
|
||||
describe('--version flag', () => {
|
||||
test('shows version', () => {
|
||||
const output = execSync(
|
||||
`node ${cliPath} --version`,
|
||||
{ encoding: 'utf-8' }
|
||||
);
|
||||
|
||||
expect(output).toMatch(/^\d+\.\d+\.\d+/);
|
||||
});
|
||||
});
|
||||
|
||||
describe('validate command', () => {
|
||||
test('validates valid .env file', () => {
|
||||
const envPath = path.join(fixturesDir, '.env');
|
||||
const schemaPath = path.join(fixturesDir, 'schema.json');
|
||||
|
||||
const output = execSync(
|
||||
`node ${cliPath} validate -e ${envPath} -s ${schemaPath}`,
|
||||
{ encoding: 'utf-8' }
|
||||
);
|
||||
|
||||
expect(output).toContain('Validation passed');
|
||||
});
|
||||
});
|
||||
|
||||
describe('generate command', () => {
|
||||
test('generates TypeScript interface', () => {
|
||||
const envPath = path.join(fixturesDir, '.env');
|
||||
|
||||
const output = execSync(
|
||||
`node ${cliPath} generate -e ${envPath}`,
|
||||
{ encoding: 'utf-8' }
|
||||
);
|
||||
|
||||
expect(output).toContain('export interface EnvVariables');
|
||||
expect(output).toContain('PORT: number;');
|
||||
});
|
||||
|
||||
test('writes output to file', () => {
|
||||
const envPath = path.join(fixturesDir, '.env');
|
||||
const outputFile = path.join(__dirname, 'output.ts');
|
||||
|
||||
execSync(
|
||||
`node ${cliPath} generate -e ${envPath} -o ${outputFile}`,
|
||||
{ encoding: 'utf-8' }
|
||||
);
|
||||
|
||||
const generated = fs.readFileSync(outputFile, 'utf-8');
|
||||
expect(generated).toContain('export interface EnvVariables');
|
||||
|
||||
fs.unlinkSync(outputFile);
|
||||
});
|
||||
});
|
||||
|
||||
describe('migrate command', () => {
|
||||
test('generates schema.json', () => {
|
||||
const envPath = path.join(fixturesDir, '.env');
|
||||
const outputFile = path.join(__dirname, 'schema_output.json');
|
||||
|
||||
execSync(
|
||||
`node ${cliPath} migrate -e ${envPath} -o ${outputFile}`,
|
||||
{ encoding: 'utf-8' }
|
||||
);
|
||||
|
||||
const schema = JSON.parse(fs.readFileSync(outputFile, 'utf-8'));
|
||||
expect(schema.variables).toBeDefined();
|
||||
expect(schema.variables.PORT).toBeDefined();
|
||||
|
||||
fs.unlinkSync(outputFile);
|
||||
});
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user