Add subreddit CLI option #118

Merged
Vylpes merged 2 commits from feature/75-cli-subreddit into develop 2023-12-01 18:15:19 +00:00
2 changed files with 61 additions and 1 deletions
Showing only changes of commit 5f66a10f26 - Show all commits

View file

@ -1,6 +1,7 @@
import { Command } from "commander"; import { Command } from "commander";
import randomBunny from "./index"; import randomBunny from "./index";
import ICliOptions from "./contracts/ICliOptions"; import ICliOptions from "./contracts/ICliOptions";
import { exit } from "process";
const program = new Command(); const program = new Command();
@ -30,10 +31,12 @@ randomBunny(options.subreddit)
outputLines.push(`Upvotes = ${result.Ups}`); outputLines.push(`Upvotes = ${result.Ups}`);
outputLines.push(`Url = ${result.Url}`); outputLines.push(`Url = ${result.Url}`);
console.log(outputLines.join("\n") + "\n"); console.log(outputLines.join("\n"));
exit(0);
} else { } else {
const error = response.Error!; const error = response.Error!;
console.error(error.Message, error.Code); console.error(error.Message, error.Code);
exit(1);
} }
}); });

View file

@ -1,6 +1,31 @@
import { exec } from "child_process"; import { exec } from "child_process";
import path from "path"; import path from "path";
describe('default', () => {
test('GIVEN no options are supplied, EXPECT standard output', async () => {
const result = await cli([], '.');
const keys = result.stdout.split('\n')
.flatMap(x => x.split(' = ')[0])
.filter(x => x && x.length > 0);
const values = result.stdout.split('\n')
.flatMap(x => x.split(' = ')[1])
.filter(x => x && x.length > 0);
expect(result.code).toBe(0);
expect(keys).toStrictEqual(['Archived', 'Downvotes', 'Hidden', 'Permalink', 'Subreddit', 'Subreddit Subscribers', 'Title', 'Upvotes', 'Url']);
expect(values.length).toBe(9);
}, 5000);
test('GIVEN an error occurs, EXPECT error output', async () => {
const result = await cli(['-s', 'textonly'], '.');
expect(result.code).toBe(1);
expect(result.stderr).toBeDefined();
}, 5000);
});
describe('version', () => { describe('version', () => {
test('GIVEN -V flag is supplied, EXPECT version returned', async () => { test('GIVEN -V flag is supplied, EXPECT version returned', async () => {
const result = await cli(['-V'], '.'); const result = await cli(['-V'], '.');
@ -31,6 +56,38 @@ describe('help', () => {
expect(result.code).toBe(0); expect(result.code).toBe(0);
expect(result.stdout.split('\n')[0]).toBe('Usage: random-bunny [options]'); expect(result.stdout.split('\n')[0]).toBe('Usage: random-bunny [options]');
}); });
});
describe('subreddit', () => {
test('GIVEN -s is not supplied, EXPECT subreddit to be defaulted', async () => {
const result = await cli([], '.');
const subreddit = result.stdout.split('\n')
.find(x => x && x.length > 0 && x.split(' = ')[0] == 'Subreddit')!
.split(' = ')[1];
expect(subreddit).toBe('Rabbits');
}, 5000);
test('GIVEN -s is supplied, EXPECT subreddit to be changed', async () => {
const result = await cli(['-s', 'horses'], '.');
const subreddit = result.stdout.split('\n')
.find(x => x && x.length > 0 && x.split(' = ')[0] == 'Subreddit')!
.split(' = ')[1];
expect(subreddit).toBe('Horses');
}, 5000);
test('GIVEN --subreddit is supplied, EXPECT subreddit to be changed', async () => {
const result = await cli(['--subreddit', 'horses'], '.');
const subreddit = result.stdout.split('\n')
.find(x => x && x.length > 0 && x.split(' = ')[0] == 'Subreddit')!
.split(' = ')[1];
expect(subreddit).toBe('Horses');
}, 5000);
}) })
function cli(args: string[], cwd: string): Promise<cliResult> { function cli(args: string[], cwd: string): Promise<cliResult> {