Skip to content

Instantly share code, notes, and snippets.

@kumar303
Last active January 20, 2017 20:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kumar303/4a9886e0c3a72e48ad785318ca567d60 to your computer and use it in GitHub Desktop.
Save kumar303/4a9886e0c3a72e48ad785318ca567d60 to your computer and use it in GitHub Desktop.
diff --git a/src/config.js b/src/config.js
new file mode 100644
index 0000000..226fd0c
--- /dev/null
+++ b/src/config.js
@@ -0,0 +1,6 @@
+export function applyConfigToArgv({argv, configObject}) {
+ // Replace argv with config file option values.
+ // This is of course only a naiive implementation that is missing
+ // many things.
+ return {...argv, ...configObject};
+}
diff --git a/tests/unit/test.config.js b/tests/unit/test.config.js
new file mode 100644
index 0000000..35b2097
--- /dev/null
+++ b/tests/unit/test.config.js
@@ -0,0 +1,70 @@
+import {assert} from 'chai';
+import {describe, it} from 'mocha';
+import sinon from 'sinon';
+
+import {fake} from './helpers';
+import {Program} from '../../src/program';
+import {applyConfigToArgv} from '../../src/config';
+
+function makeArgv({
+ userCmd = [],
+ command = 'fakecommand',
+ commandDesc = 'this is a fake command',
+ commandExecutor = sinon.stub(),
+ commandOpt,
+ globalOpt,
+}) {
+ const program = new Program(userCmd)
+ if (globalOpt) {
+ program.setGlobalOptions(globalOpt);
+ }
+ if (commandOpt) {
+ program.command(command, commandDesc, commandExecutor, commandOpt);
+ }
+
+ return program.yargs.exitProcess(false).argv;
+}
+
+describe('config', () => {
+ describe('applyConfigToArgv', () => {
+ it('overrides a string option value with a configured value', () => {
+
+ const argv = makeArgv({
+ globalOpt: {
+ 'source-dir': {
+ requiresArg: true,
+ type: 'string',
+ demand: false,
+ },
+ },
+ });
+
+ const configObject = {
+ sourceDir: '/configured/source/dir',
+ };
+ const newArgv = applyConfigToArgv({argv, configObject});
+ assert.strictEqual(newArgv.sourceDir, configObject.sourceDir);
+ });
+
+ it('preserves a string option value on the command line', () => {
+ const cmdLineSrcDir = '/user/specified/source/dir/';
+
+ const argv = makeArgv({
+ userCmd: ['fakecommand', '--source-dir', cmdLineSrcDir],
+ globalOpt: {
+ 'source-dir': {
+ requiresArg: true,
+ type: 'string',
+ demand: false,
+ },
+ },
+ });
+
+ const configObject = {
+ sourceDir: '/configured/source/dir',
+ };
+ const newArgv = applyConfigToArgv({argv, configObject});
+ assert.strictEqual(newArgv.sourceDir, cmdLineSrcDir);
+ });
+ });
+});
@kumar303
Copy link
Author

Apply with patch -p1 -r . < scratch-config-file-for-sj.diff

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment