Compare commits
8 Commits
Author | SHA1 | Date |
---|---|---|
|
56b40de56c | 2 years ago |
|
2d466826fc | 2 years ago |
|
133ed73568 | 2 years ago |
|
6bf0f19d3d | 2 years ago |
|
57caea14cb | 2 years ago |
|
2fcea26577 | 2 years ago |
|
fbff65b29d | 2 years ago |
|
818d7f8f82 | 2 years ago |
@ -0,0 +1,121 @@
|
||||
import { mockProcessExit, mockProcessStderr, mockProcessStdout } from 'jest-mock-process';
|
||||
import { makeProgram } from './binlib';
|
||||
import { Command } from 'commander';
|
||||
import mock from 'mock-fs';
|
||||
import fs from 'fs';
|
||||
|
||||
let mockStdout: jest.SpyInstance;
|
||||
let mockStderr: jest.SpyInstance;
|
||||
let mockExit: jest.SpyInstance;
|
||||
let program: Command;
|
||||
let stdinContents = '';
|
||||
|
||||
function getContents() {
|
||||
return stdinContents;
|
||||
}
|
||||
|
||||
jest.mock('./binutils.ts', () => ({
|
||||
stdinToString: jest.fn(() => getContents()),
|
||||
}));
|
||||
|
||||
beforeEach(() => {
|
||||
mockStdout = mockProcessStdout();
|
||||
mockStderr = mockProcessStderr();
|
||||
program = makeProgram();
|
||||
program.exitOverride();
|
||||
mockExit = mockProcessExit();
|
||||
mock({
|
||||
'.env': 'ALA=makota',
|
||||
'ugly.env': '########\n\n SOME = value \n\n\n ANOTHER= "value with space"',
|
||||
'first.env': 'SERVER_PORT=80\nSERVER_HOST=localhost',
|
||||
'second.env': 'SERVER_PORT=8080\nSERVER_PASSWORD=secret\nCLIENT_HOST=192.168.4.42',
|
||||
'third.env': 'CLIENT_PORT=3000',
|
||||
'broken.env': 'CLI ENT_PORT=3000',
|
||||
'read-only.env': mock.file({
|
||||
content: 'READ=only',
|
||||
mode: 0o444,
|
||||
}),
|
||||
});
|
||||
stdinContents = '';
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
mockStdout.mockRestore();
|
||||
mockStderr.mockRestore();
|
||||
mockExit.mockRestore();
|
||||
mock.restore();
|
||||
});
|
||||
|
||||
describe('Standard utils', () => {
|
||||
it('outputs help', () => {
|
||||
expect(() => {
|
||||
program.parse(['node', 'test', '--help']);
|
||||
}).toThrow();
|
||||
expect(mockStdout).toHaveBeenCalledWith(expect.stringContaining('Usage: dotenv-tool [options] [command] [paramsToSet...]'));
|
||||
});
|
||||
|
||||
it('outputs version', () => {
|
||||
expect(() => {
|
||||
program.parse(['node', 'test', '-v']);
|
||||
}).toThrow(
|
||||
expect.objectContaining({
|
||||
message: expect.stringMatching(/[0-9]+\.[0-9]+\.[0-9]+/),
|
||||
}),
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
describe('Read command', () => {
|
||||
it('reads from empty stdin', () => {
|
||||
stdinContents = '';
|
||||
program.parse(['node', 'test', 'get', 'TEST']);
|
||||
expect(mockExit).toHaveBeenCalledWith(1);
|
||||
expect(mockStderr).toBeCalledTimes(1);
|
||||
expect(mockStderr).toHaveBeenNthCalledWith(1, expect.stringMatching(/Variable TEST not found in stdin/));
|
||||
});
|
||||
|
||||
it('reads from invalid stdin', () => {
|
||||
stdinContents = 'junks';
|
||||
program.parse(['node', 'test', 'get', 'TEST']);
|
||||
expect(mockExit).toHaveBeenCalledWith(1);
|
||||
expect(mockStderr).toBeCalledTimes(2);
|
||||
expect(mockStderr).toHaveBeenNthCalledWith(1, expect.stringMatching(/Parsing stdin failed/));
|
||||
expect(mockStderr).toHaveBeenNthCalledWith(2, expect.stringMatching(/PARSING FAILED/));
|
||||
});
|
||||
|
||||
it('reads from correct stdin', () => {
|
||||
stdinContents = 'TEST=works';
|
||||
program.parse(['node', 'test', 'get', 'TEST']);
|
||||
expect(mockExit).toBeCalled();
|
||||
expect(mockStdout).toHaveBeenCalledWith('works');
|
||||
});
|
||||
|
||||
it('writes result to file', () => {
|
||||
stdinContents = 'TEST=works';
|
||||
program.parse(['node', 'test', 'get', '-o', 'test.txt', 'TEST']);
|
||||
expect(mockExit).toBeCalled();
|
||||
expect(fs.readFileSync('test.txt').toString()).toEqual('works');
|
||||
});
|
||||
|
||||
it('reads from correct file', () => {
|
||||
program.parse(['node', 'test', 'get', '-f', '.env', 'ALA']);
|
||||
expect(mockExit).toBeCalled();
|
||||
expect(mockStdout).toHaveBeenCalledWith('makota');
|
||||
});
|
||||
});
|
||||
|
||||
describe('Silent flag', () => {
|
||||
it('reads from empty stdin', () => {
|
||||
stdinContents = '';
|
||||
program.parse(['node', 'test', '-s', 'get', 'TEST']);
|
||||
expect(mockExit).toHaveBeenCalledWith(1);
|
||||
expect(mockStderr).toBeCalledTimes(0);
|
||||
});
|
||||
|
||||
it('reads from invalid stdin', () => {
|
||||
stdinContents = 'junks';
|
||||
program.parse(['node', 'test', '-s', 'get', 'TEST']);
|
||||
expect(mockExit).toHaveBeenCalledWith(1);
|
||||
expect(mockStderr).toBeCalledTimes(0);
|
||||
});
|
||||
});
|
@ -0,0 +1,121 @@
|
||||
import { mockProcessExit, mockProcessStderr, mockProcessStdout } from 'jest-mock-process';
|
||||
import { makeProgram } from './binlib';
|
||||
import { Command } from 'commander';
|
||||
import mock from 'mock-fs';
|
||||
import fs from 'fs';
|
||||
|
||||
let mockStdout: jest.SpyInstance;
|
||||
let mockStderr: jest.SpyInstance;
|
||||
let mockExit: jest.SpyInstance;
|
||||
let program: Command;
|
||||
let stdinContents = '';
|
||||
|
||||
function getContents() {
|
||||
return stdinContents;
|
||||
}
|
||||
|
||||
jest.mock('./binutils.ts', () => ({
|
||||
stdinToString: jest.fn(() => getContents()),
|
||||
}));
|
||||
|
||||
beforeEach(() => {
|
||||
mockStdout = mockProcessStdout();
|
||||
mockStderr = mockProcessStderr();
|
||||
program = makeProgram();
|
||||
program.exitOverride();
|
||||
mockExit = mockProcessExit();
|
||||
mock({
|
||||
'.env': 'ALA=makota',
|
||||
'ugly.env': '########\n\n SOME = value \n\n\n ANOTHER= "value with space"',
|
||||
'first.env': 'SERVER_PORT=80\nSERVER_HOST=localhost',
|
||||
'second.env': 'SERVER_PORT=8080\nSERVER_PASSWORD=secret\nCLIENT_HOST=192.168.4.42',
|
||||
'third.env': 'CLIENT_PORT=3000',
|
||||
'broken.env': 'CLI ENT_PORT=3000',
|
||||
'read-only.env': mock.file({
|
||||
content: 'READ=only',
|
||||
mode: 0o444,
|
||||
}),
|
||||
});
|
||||
stdinContents = '';
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
mockStdout.mockRestore();
|
||||
mockStderr.mockRestore();
|
||||
mockExit.mockRestore();
|
||||
mock.restore();
|
||||
});
|
||||
|
||||
describe('Parse command', () => {
|
||||
it('reads from empty stdin', () => {
|
||||
stdinContents = '';
|
||||
program.parse(['node', 'test', 'parse']);
|
||||
expect(mockStdout).toBeCalledTimes(2);
|
||||
expect(mockStdout).toHaveBeenNthCalledWith(1, "{\"status\":\"ok\",\"values\":{}}");
|
||||
});
|
||||
|
||||
it('reads from invalid stdin', () => {
|
||||
stdinContents = 'junks';
|
||||
program.parse(['node', 'test', 'parse']);
|
||||
expect(mockExit).toHaveBeenCalledWith(1);
|
||||
expect(mockStdout).toBeCalledTimes(2);
|
||||
expect(mockStdout).toHaveBeenNthCalledWith(1, expect.stringMatching(/\"status\":\"parse_error\"/));
|
||||
expect(mockStdout).toHaveBeenNthCalledWith(1, expect.stringMatching(/\"errorLine\":1/));
|
||||
});
|
||||
|
||||
it('reads from correct stdin', () => {
|
||||
stdinContents = 'TEST=works';
|
||||
program.parse(['node', 'test', 'parse', 'TEST']);
|
||||
expect(mockExit).toBeCalled();
|
||||
expect(mockStdout).toHaveBeenCalledWith("{\"status\":\"ok\",\"values\":{\"TEST\":\"works\"}}");
|
||||
});
|
||||
|
||||
it('writes result to file', () => {
|
||||
stdinContents = 'TEST=works';
|
||||
program.parse(['node', 'test', 'parse', '-o', 'test.txt']);
|
||||
expect(mockExit).toBeCalled();
|
||||
expect(fs.readFileSync('test.txt').toString()).toEqual("{\"status\":\"ok\",\"values\":{\"TEST\":\"works\"}}");
|
||||
});
|
||||
|
||||
it('writes error to file', () => {
|
||||
stdinContents = 'junks';
|
||||
program.parse(['node', 'test', 'parse', '-o', 'test.txt']);
|
||||
expect(mockExit).toBeCalled();
|
||||
const fileCnt = fs.readFileSync('test.txt').toString();
|
||||
expect(fileCnt).toMatch(/\"status\":\"parse_error\"/);
|
||||
expect(fileCnt).toMatch(/\"errorLine\":1/);
|
||||
});
|
||||
|
||||
it('writes error to file beautified', () => {
|
||||
stdinContents = 'junks';
|
||||
program.parse(['node', 'test', 'parse', '-b', '-o', 'test.txt']);
|
||||
expect(mockExit).toBeCalled();
|
||||
const fileCnt = fs.readFileSync('test.txt').toString();
|
||||
expect(fileCnt).toMatch(/\"status\": \"parse_error\"/);
|
||||
expect(fileCnt).toMatch(/\"errorLine\": 1/);
|
||||
});
|
||||
|
||||
it('reads from correct file', () => {
|
||||
program.parse(['node', 'test', 'parse', '-f', '.env']);
|
||||
expect(mockExit).toBeCalled();
|
||||
expect(mockStdout).toHaveBeenCalledWith("{\"status\":\"ok\",\"values\":{\"ALA\":\"makota\"}}");
|
||||
});
|
||||
|
||||
it('parses file', () => {
|
||||
program.parse(['node', 'test', 'parse', '-f', 'second.env']);
|
||||
expect(mockExit).toBeCalled();
|
||||
expect(mockStdout).toHaveBeenCalledWith("{\"status\":\"ok\",\"values\":{\"SERVER_PORT\":\"8080\",\"SERVER_PASSWORD\":\"secret\",\"CLIENT_HOST\":\"192.168.4.42\"}}");
|
||||
});
|
||||
|
||||
it('normalize json keys', () => {
|
||||
program.parse(['node', 'test', 'parse', '-n', '-f', 'second.env']);
|
||||
expect(mockExit).toBeCalled();
|
||||
expect(mockStdout).toHaveBeenCalledWith("{\"status\":\"ok\",\"values\":{\"server_port\":\"8080\",\"server_password\":\"secret\",\"client_host\":\"192.168.4.42\"}}");
|
||||
});
|
||||
|
||||
it('beautify keys', () => {
|
||||
program.parse(['node', 'test', 'parse', '-b', '-n', '-f', '.env']);
|
||||
expect(mockExit).toBeCalled();
|
||||
expect(mockStdout).toHaveBeenCalledWith("{\n \"status\": \"ok\",\n \"values\": {\n \"ala\": \"makota\"\n }\n}");
|
||||
});
|
||||
});
|
Loading…
Reference in New Issue