Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,11 @@ ctp -i '[
Usage: ctp [options]

Options:
-i, --input <value> input string
-s, --stdin read input from stdin
-v, --version output the current version
-i, --input <value> input string
-s, --stdin read input from stdin
-t, --tableOptions <value> table options in JSON format
-h, --help display help for command
-h, --help display help for command
```

## License
Expand Down
31 changes: 27 additions & 4 deletions index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ import { runCLI } from './index';
import printTableFromInp from './src/service';

// Mock dependencies
jest.mock('fs');
jest.mock('./src/service');
jest.mock('commander', () => {
const mockCommand = {
option: jest.fn().mockReturnThis(),
version: jest.fn().mockReturnThis(),
parse: jest.fn().mockReturnThis(),
opts: jest.fn().mockReturnValue({}),
};
Expand All @@ -23,18 +23,36 @@ describe('CLI', () => {
mockConsoleLog = jest.spyOn(console, 'log').mockImplementation();

// Reset mocks
(fs.readFileSync as jest.Mock).mockReset();
(printTableFromInp as jest.Mock).mockReset();

// Reset Commander mock
const mockCommand = new Command();
(mockCommand.opts as jest.Mock).mockReturnValue({});
(mockCommand.version as jest.Mock).mockReset();
(mockCommand.option as jest.Mock).mockReset();
(mockCommand.parse as jest.Mock).mockReset();

// Set up default mock returns
(mockCommand.version as jest.Mock).mockReturnValue(mockCommand);
(mockCommand.option as jest.Mock).mockReturnValue(mockCommand);
(mockCommand.parse as jest.Mock).mockReturnValue(mockCommand);
});

afterEach(() => {
jest.restoreAllMocks();
});

it('should configure version command with package version', () => {
const mockCommand = new Command();
runCLI(['node', 'index.js']);

expect(mockCommand.version).toHaveBeenCalledWith(
expect.any(String),
'-v, --version',
'output the current version'
);
});

it('should handle input option', () => {
const input = '[{"id":1,"name":"John"}]';
const mockCommand = new Command();
Expand All @@ -60,12 +78,17 @@ describe('CLI', () => {
const input = '[{"id":1,"name":"John"}]';
const mockCommand = new Command();
(mockCommand.opts as jest.Mock).mockReturnValue({ stdin: true });
(fs.readFileSync as jest.Mock).mockReturnValue({ toString: () => input });

// Mock fs.readFileSync only for this test
const mockReadFileSync = jest.spyOn(fs, 'readFileSync')
.mockReturnValue(Buffer.from(input));

runCLI(['node', 'index.js', '-s']);

expect(fs.readFileSync).toHaveBeenCalledWith(0);
expect(mockReadFileSync).toHaveBeenCalledWith(0);
expect(printTableFromInp).toHaveBeenCalledWith(input, undefined);

mockReadFileSync.mockRestore();
});

it('should show error when no input option is provided', () => {
Expand Down
7 changes: 7 additions & 0 deletions index.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,20 @@
#!/usr/bin/env node
import { Command } from 'commander';
import * as fs from 'fs';
import * as path from 'path';

import printTableFromInp from './src/service';

// Read package.json to get version
const packageJson = JSON.parse(
fs.readFileSync(path.join(__dirname, 'package.json'), 'utf8')
);

export function runCLI(argv: string[] = process.argv) {
const program = new Command();

program
.version(packageJson.version, '-v, --version', 'output the current version')
.option('-i, --input <value>', 'input string')
.option('-s, --stdin', 'read input from stdin')
.option('-t, --tableOptions <value>', 'table options in JSON format')
Expand Down
Loading