-
Notifications
You must be signed in to change notification settings - Fork 116
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(cli): fix setting flag to false (#135)
- This fixes `webdriver-manager update --gecko=false` - This does not fix `webdriver-manager update --gecko=0`. Minimist interprets 0 as true. - Add options and programs unit tests closes #110
- Loading branch information
Showing
5 changed files
with
249 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,145 @@ | ||
import {Option} from '../../lib/cli/options'; | ||
|
||
|
||
describe('options', () => { | ||
let option: Option; | ||
|
||
describe('get number', () => { | ||
|
||
describe('for this.value not set', () => { | ||
it('should return the default value', () => { | ||
option = new Option('fake opt', 'fake description', 'number', 10); | ||
expect(option.getNumber()).toEqual(10); | ||
|
||
option = new Option('fake opt', 'fake description', 'number', 0); | ||
expect(option.getNumber()).toEqual(0); | ||
|
||
option = new Option('fake opt', 'fake description', 'number', -5); | ||
expect(option.getNumber()).toEqual(-5); | ||
}); | ||
|
||
it('should return null if the default value is not set', () => { | ||
option = new Option('fake opt', 'fake description', 'number'); | ||
expect(option.getNumber()).toBeNull(); | ||
}); | ||
}); | ||
|
||
describe('for this.value set', () => { | ||
beforeEach(() => { | ||
option = new Option('fake opt', 'fake description', 'number', -10); | ||
}); | ||
|
||
it('should return the this.value when this.value is a number', () => { | ||
option.value = 20; | ||
expect(option.getNumber()).toEqual(20); | ||
}); | ||
|
||
it('should return a number of this.value when it is a string of a number', () => { | ||
option.value = '10'; | ||
expect(option.getNumber()).toEqual(10); | ||
option.value = '0'; | ||
expect(option.getNumber()).toEqual(0); | ||
option.value = '-5'; | ||
expect(option.getNumber()).toEqual(-5); | ||
}); | ||
|
||
it('should return null if this.value is not a string or a number', () => { | ||
option.value = true; | ||
expect(option.getNumber()).toBeNull(); | ||
option.value = false; | ||
expect(option.getNumber()).toBeNull(); | ||
}); | ||
|
||
it('should return NaN if this.value is a string but is not a number', () => { | ||
option.value = 'foobar'; | ||
expect(option.getNumber()).toEqual(NaN); | ||
}); | ||
}); | ||
}); | ||
|
||
describe('get boolean', () => { | ||
describe('for this.value not set', () => { | ||
it('should return the default value', () => { | ||
option = new Option('fake opt', 'fake description', 'boolean', true); | ||
expect(option.getBoolean()).toBeTruthy(); | ||
option = new Option('fake opt', 'fake description', 'boolean', false); | ||
expect(option.getBoolean()).not.toBeTruthy(); | ||
}); | ||
|
||
it('should return false if the default value is not defined', () => { | ||
option = new Option('fake opt', 'fake description', 'boolean'); | ||
expect(option.getBoolean()).not.toBeTruthy(); | ||
}); | ||
}); | ||
|
||
describe('for this.value set', () => { | ||
beforeEach(() => { | ||
option = new Option('fake opt', 'fake description', 'boolean'); | ||
}); | ||
|
||
it('should return a boolean when this.value is a string', () => { | ||
option.value = 'true'; | ||
expect(option.getBoolean()).toBeTruthy(); | ||
option.value = 'false'; | ||
expect(option.getBoolean()).not.toBeTruthy(); | ||
}); | ||
|
||
it('should return a boolean of this.value when this.value is a number', () => { | ||
option.value = 1; | ||
expect(option.getNumber()).toBeTruthy(); | ||
option.value = 0; | ||
expect(option.getNumber()).not.toBeTruthy(); | ||
}); | ||
|
||
it('should return the boolean of this.value when this.value is a boolean', () => { | ||
option.value = true; | ||
expect(option.getNumber()).toBeNull(); | ||
option.value = false; | ||
expect(option.getNumber()).toBeNull(); | ||
}); | ||
}); | ||
}); | ||
|
||
describe('get string', () => { | ||
describe('for this.value not set', () => { | ||
it('should return the default value', () => { | ||
option = new Option('fake opt', 'fake description', 'string', 'foobar'); | ||
expect(option.getString()).toBe('foobar'); | ||
option = new Option('fake opt', 'fake description', 'string', ''); | ||
expect(option.getString()).toBe(''); | ||
}); | ||
|
||
it('should return an empty string if the default value is not defined', () => { | ||
option = new Option('fake opt', 'fake description', 'string'); | ||
expect(option.getString()).toBe(''); | ||
}); | ||
}); | ||
|
||
describe('for this.value set', () => { | ||
beforeEach(() => { | ||
option = new Option('fake opt', 'fake description', 'string', 'foo'); | ||
}); | ||
|
||
it('should return this.value when this.value is a string', () => { | ||
option.value = 'bar'; | ||
expect(option.getString()).toEqual('bar'); | ||
option.value = ''; | ||
expect(option.getString()).toEqual(''); | ||
}); | ||
|
||
it('should return the string of this.value when this.value is a number', () => { | ||
option.value = 0; | ||
expect(option.getString()).toEqual('0'); | ||
option.value = 1; | ||
expect(option.getString()).toEqual('1'); | ||
}); | ||
|
||
it('should return the string of this.value when this.value is a boolean', () => { | ||
option.value = false; | ||
expect(option.getString()).toEqual('false'); | ||
option.value = true; | ||
expect(option.getString()).toEqual('true'); | ||
}); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
import {Option, Options, Program} from '../../lib/cli'; | ||
|
||
|
||
describe('program', () => { | ||
let program: Program; | ||
|
||
beforeEach(() => { | ||
program = new Program() | ||
.command('fooCmd', 'fooDescription') | ||
.addOption(new Option('fooString1', 'fooDescription', 'string', 'foo')) | ||
.addOption(new Option('fooString1', 'fooDescription', 'string', 'foo')) | ||
.addOption(new Option('fooBoolean1', 'fooDescription', 'boolean', false)) | ||
.addOption(new Option('fooBoolean2', 'fooDescription', 'boolean', true)) | ||
.addOption(new Option('fooNumber1', 'fooDescription', 'number', 1)) | ||
.addOption(new Option('fooNumber2', 'fooDescription', 'number', 2)) | ||
.addOption(new Option('fooNumber3', 'fooDescription', 'number', 3)) | ||
}); | ||
|
||
it('should get minimist options', () => { | ||
let json = JSON.parse(JSON.stringify(program.getMinimistOptions())); | ||
expect(json.string.length).toEqual(1); | ||
expect(json.boolean.length).toEqual(2); | ||
expect(json.number.length).toEqual(3); | ||
let length = 0; | ||
for (let item in json.default) { | ||
length++; | ||
} | ||
expect(length).toEqual(6); | ||
expect(json.string[0]).toBe('fooString1'); | ||
expect(json.boolean[0]).toBe('fooBoolean1'); | ||
expect(json.boolean[1]).toBe('fooBoolean2'); | ||
expect(json.number[0]).toBe('fooNumber1'); | ||
expect(json.number[1]).toBe('fooNumber2'); | ||
expect(json.number[2]).toBe('fooNumber3'); | ||
}); | ||
|
||
it('should be able to extract the correct type and value', () => { | ||
let testString: string; | ||
let json = JSON.parse(JSON.stringify({ | ||
'_': ['fooCmd'], | ||
'fooString1': 'bar', | ||
'fooBoolean1': true, | ||
'fooBoolean2': false, | ||
'fooNumber1': 10, | ||
'fooNumber2': 20, | ||
'fooNumber3': 30 | ||
})); | ||
let callbackTest = (options: Options) => { | ||
expect(options['fooString1'].getString()).toEqual('bar'); | ||
expect(options['fooBoolean1'].getBoolean()).toEqual(true); | ||
expect(options['fooBoolean2'].getBoolean()).toEqual(false); | ||
expect(options['fooNumber1'].getNumber()).toEqual(10); | ||
expect(options['fooNumber2'].getNumber()).toEqual(20); | ||
expect(options['fooNumber3'].getNumber()).toEqual(30); | ||
} | ||
program.action(callbackTest); | ||
program.run(json); | ||
}); | ||
|
||
it('should be able to extract the mixed type and get the right type', () => { | ||
let testString: string; | ||
let json = JSON.parse(JSON.stringify({ | ||
'_': ['fooCmd'], | ||
'fooString1': 1, | ||
'fooBoolean1': 'true', | ||
'fooBoolean2': 0, | ||
'fooNumber1': '100', | ||
'fooNumber2': 'foo', | ||
'fooNumber3': true | ||
})); | ||
let callbackTest = (options: Options) => { | ||
expect(options['fooString1'].getString()).toEqual('1'); | ||
expect(options['fooBoolean1'].getBoolean()).toEqual(true); | ||
expect(options['fooBoolean2'].getBoolean()).toEqual(false); | ||
expect(options['fooNumber1'].getNumber()).toEqual(100); | ||
expect(options['fooNumber2'].getNumber()).toEqual(NaN); | ||
expect(options['fooNumber3'].getNumber()).toEqual(null); | ||
} | ||
program.action(callbackTest); | ||
program.run(json); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters