Skip to content

Commit

Permalink
Show a warning if a config file explicitly specified on the command l…
Browse files Browse the repository at this point in the history
…ine is missing. Move all test config files into one directory.
  • Loading branch information
misterfifths committed Jun 10, 2017
1 parent 3f8fab2 commit 12544a3
Show file tree
Hide file tree
Showing 24 changed files with 42 additions and 19 deletions.
3 changes: 1 addition & 2 deletions cmdline.js
Expand Up @@ -27,8 +27,7 @@ const globalOpts = [
names: ['config-file', 'c'],
helpArg: 'FILE',
help: 'Load the given config file. The default is the JUTIL_CONFIG_PATH environmental variable or ~/.jutil/config. Specify --no-config-file to use the default configuration.',
type: 'string',
'default': process.env.JUTIL_CONFIG_PATH || '~/.jutil/config'
type: 'string'
},
{
name: 'no-config-file',
Expand Down
29 changes: 24 additions & 5 deletions jutil.js
Expand Up @@ -157,7 +157,7 @@ cmdline.parseCommandLine({

function runCommand(commandDesc, opts)
{
let config = loadConfig(defaultConfig, opts.no_config_file ? undefined : opts.config_file),
let config = loadConfig(defaultConfig, opts),
runtimeSettings = makeRuntimeSettings(commandDesc, config, opts),
res = commandDesc.handler(runtimeSettings, config, opts);

Expand Down Expand Up @@ -362,11 +362,25 @@ function outputObject(obj, runtimeSettings, config)

//// Configuration file handling

function loadConfig(defaultConfig, configPath)
function loadConfig(defaultConfig, opts)
{
let config = {},
userConfig;

userConfig,
warnAboutMissingConfig = false,
configPath;

if(!opts.no_config_file) {
if(opts.config_file) {
// We only warn about a config file missing if they explicitly specified
// it on the command line.
warnAboutMissingConfig = true;
configPath = opts.config_file;
}
else {
configPath = process.env.JUTIL_CONFIG_PATH || '~/.jutil/config';
}
}

utils.shallowCopy(defaultConfig, config);

if(!configPath)
Expand All @@ -387,8 +401,13 @@ function loadConfig(defaultConfig, configPath)
}
catch(exc) {
// It's fine if we didn't find a config file; we'll use the defaults
if(exc.code == 'ENOENT')
if(exc.code == 'ENOENT') {
if(warnAboutMissingConfig) {
console.warn('Warning: unable to load configuration file "' + configPath + '"');
}

return config;
}
else {
console.error('Error loading configuration file: ' + exc);
process.exit(1);
Expand Down
14 changes: 11 additions & 3 deletions tests/config-files.tush.md
Expand Up @@ -3,7 +3,7 @@
Exercising some edge cases and obscure uses of config files. This test has its own batch of configuration files. Let's move those into place:

```sh
$ cp -r "$FIXTURE_DIR"/config-tests/* .
$ cp -r "$FIXTURE_DIR"/config-files/* .
```

## Errors
Expand All @@ -24,11 +24,19 @@ $ echo '{}' | jutil -c invalid-config
@ Warning: config file must assign to the global "config" var; ignoring the file
```

Nonexistent configuration files are ignored silently, and the default configuration is used:
Nonexistent configuration files from an environmental variable are ignored silently, and the default configuration is used:

```sh
$ echo '{}' | JUTIL_CONFIG_PATH="$(pwd)/nonexistent-config" jutil
| {}
```

But nonexistent configuration files explicitly passed on the command line result in a warning:

```sh
$ echo '{}' | jutil -c nonexistent-config
| {}
@ Warning: unable to load configuration file "nonexistent-config"
```

## Type Warnings
Expand Down Expand Up @@ -133,6 +141,6 @@ $ echo '{ "hashme": "hello" }' | jutil -c module-dirs-config --no-module-dir 're
You can also use `--no-config-file` to completely disable the loading of a configuration file, even if one is specified in your environment.
```sh
$ echo '{"b": 1, "a": 2}' | JUTIL_CONFIG_PATH="$FIXTURE_DIR/sort_and_pretty_print_config" jutil --no-config-file
$ echo '{"b": 1, "a": 2}' | JUTIL_CONFIG_PATH="$(pwd)/sort_and_pretty_print_config" jutil --no-config-file
| {"b":1,"a":2}
```
3 changes: 2 additions & 1 deletion tests/core.tush.md
Expand Up @@ -8,6 +8,7 @@ For convenience, we're going to copy all the [support files](fixtures) for the t

```sh
$ cp -r "$FIXTURE_DIR"/* .
$ cp -r "$FIXTURE_DIR"/config-files/* .
```

## Input
Expand Down Expand Up @@ -109,7 +110,7 @@ $ echo '{"b": 1, "a": 2}' | jutil -c sort_and_pretty_print_config
You can also set a configuration file via environmental variable:
```sh
$ echo '{"b": 1, "a": 2}' | JUTIL_CONFIG_PATH="$FIXTURE_DIR/sort_and_pretty_print_config" jutil
$ echo '{"b": 1, "a": 2}' | JUTIL_CONFIG_PATH="$(pwd)/sort_and_pretty_print_config" jutil
| {
| "a": 2,
| "b": 1
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
4 changes: 0 additions & 4 deletions tests/fixtures/sort_and_pretty_print_config

This file was deleted.

8 changes: 4 additions & 4 deletions tests/smart-output.tush.md
Expand Up @@ -20,13 +20,13 @@ $ echo '[1]' | jutil --force-smart
If it detects that the output is too big to fit on one screen, it automatically pipes the output to your pager. It uses the `PAGER` environmental variable, or `less` by default. We have a custom pager, `pager-with-side-effects`, that prints a header before the output:

```sh
$ echo '[1]' | PAGER="${FIXTURE_DIR}/pager-with-side-effects" jutil --force-smart
$ echo '[1]' | PAGER="$(pwd)/pager-with-side-effects" jutil --force-smart
| --PAGING--
| [
| 1
| ]

$ echo '[]' | PAGER="${FIXTURE_DIR}/pager-with-side-effects" jutil --force-smart
$ echo '[]' | PAGER="$(pwd)/pager-with-side-effects" jutil --force-smart
| []
```

Expand All @@ -39,11 +39,11 @@ $ echo '[1]' | PAGER=nonsense jutil --force-smart
| ]
@ /bin/sh: nonsense: command not found

$ echo '[1]' | PAGER="${FIXTURE_DIR}" jutil --force-smart
$ echo '[1]' | PAGER="$(pwd)" jutil --force-smart
| [
| 1
| ]
@ /bin/sh: /Users/tclem/dev/jutil/tests/fixtures: is a directory
@ /bin/sh: $(pwd): is a directory
```

If `PAGER` exits with a non-zero status code, no special output is produced, but the status code is passed through:
Expand Down

0 comments on commit 12544a3

Please sign in to comment.