Skip to content

Commit

Permalink
fix: Created option tests and fixed a few bugs along the way (#15)
Browse files Browse the repository at this point in the history
  • Loading branch information
jeremyoverman authored Oct 28, 2017
1 parent 1971637 commit 0071bc6
Show file tree
Hide file tree
Showing 3 changed files with 135 additions and 14 deletions.
30 changes: 16 additions & 14 deletions src/options.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { join } from 'path';
import { join, resolve } from 'path';
import { readdir } from 'fs';
import { IActionSetup } from './action';

Expand All @@ -23,47 +23,49 @@ export interface IOptionParams {
export class Options {
setup: IActionSetup;
path: string;
args: string[];

constructor (path: string, setup: IActionSetup) {
this.path = join(path, 'options');
this.setup = setup;
this.args = process.argv;
}

getParams (params: IOptionParam[], start: number) {
getParams (params: IOptionParam[]) {
let final: IOptionParams = {};

params.forEach((param, idx) => {
let name = param.name;
let value = process.argv.splice(start + idx, 1)[0];
let value = this.args.shift();

final[name] = value;
if (value) final[name] = value;
});

return final;
}

parseOptions () {
process.argv.forEach((arg, idx) => {
let match = arg.match(OPTION_REGEX);
if (!match) return;
parseOptions (): IActionSetup {
if (!this.args.length) return this.setup;

let match = this.args[0].match(OPTION_REGEX);
this.args.shift();

if (match) {
let opt = match[1];
this.setup.flags.push(opt);

process.argv.splice(idx, 1);

let option = this.getOption(opt);
let params = this.getParams(option.params, idx);
let params = this.getParams(option.params);

this.setup = option.run(params);
});
}

return this.setup;
return this.parseOptions();
}

getOption (opt: string): Option {
let module_exists: boolean;
let file = join(this.path, opt + '.js');
let file = resolve(join(this.path, opt + '.js'));

try {
require.resolve(file);
Expand Down
102 changes: 102 additions & 0 deletions src/spec/options.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
import * as options from '../options';
import * as action from '../action';
import TestOption from './test-action/options/test';

describe('the options module', () => {
let test_action: string;
let setup: action.IActionSetup;

beforeEach(() => {
test_action = './built/spec/test-action';
setup = {
flags: []
};
});

describe('the parseOptions function', () => {
let option: options.Option;
let runSpy: jasmine.Spy;
let getOptionSpy: jasmine.Spy;
let _options: options.Options;

beforeEach(() => {
option = new options.Option(setup);
option.params = [
{ name: 'firstParam', description: 'First Param' },
{ name: 'secondParam', description: 'Second Param' }
]

runSpy = spyOn(options.Option.prototype, 'run').and.returnValue(setup);
_options = new options.Options(test_action, setup);

getOptionSpy = spyOn(_options, 'getOption').and.returnValue(option);
});

it('should ignore non-options args', () => {
let _options = new options.Options(test_action, setup);
_options.args = ['arg1', 'arg2'];

let new_setup = _options.parseOptions();

expect(new_setup.flags).toEqual([]);
});

it('adds all options to the setup.flags', () => {
let _options = new options.Options(test_action, setup);
_options.args = ['--option1', '--option2'];

let new_setup = _options.parseOptions();

expect(new_setup.flags).toEqual(['option1', 'option2']);
});

it('sends params to the run function', () => {
_options.args = ['--option1', 'param1', 'param2'];
_options.parseOptions();

expect(runSpy).toHaveBeenCalledWith({
firstParam: 'param1',
secondParam: 'param2'
});
});

it('should only pass the first param', () => {
_options.args = ['--option1', 'param1'];
_options.parseOptions();

expect(runSpy).toHaveBeenCalledWith({
firstParam: 'param1',
});
});
});

describe('the getOption method', () => {
it('should return the default option class if no file is found', () => {
let _options = new options.Options(test_action, setup);

let option = _options.getOption('fake-file');

expect(option.params.length).toEqual(0);
});

it('should load an options file if it exists', () => {
let _options = new options.Options(test_action, setup);

let option = _options.getOption('test');
let testOption = new TestOption(setup);

expect(option.params.length).toEqual(2);
});
});

describe('the Option class', () => {
describe('the run method', () => {
it('should return the setup passed in', () => {
let option = new options.Option(setup);
let new_setup = option.run({});

expect(new_setup).toEqual(setup);
});
});
});
});
17 changes: 17 additions & 0 deletions src/spec/test-action/options/test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { Option, IOptionParams } from '../../../options'
import { IActionSetup } from '../../../action';

export default class TestOption extends Option {
constructor (setup: IActionSetup) {
super(setup);

this.params = [
{ name: 'firstParam', description: 'First Param' },
{ name: 'secondParam', description: 'Second Param' }
];
}

run (params: IOptionParams): IActionSetup {
return this.setup;
}
}

0 comments on commit 0071bc6

Please sign in to comment.