Skip to content

Commit

Permalink
cli tests: Fix to override $HOME, to avoid leaking user env state
Browse files Browse the repository at this point in the history
  • Loading branch information
dbarnett committed Sep 14, 2024
1 parent b84621f commit 8041b43
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 18 deletions.
27 changes: 14 additions & 13 deletions gcalcli/argparsers.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,19 @@
from .details import DETAILS
from .printer import valid_color_name


def shorten_path(path: pathlib.Path) -> pathlib.Path:
"""Try to shorten path using special characters like ~.
Returns original path unmodified if it can't be shortened.
"""
tilde_home = pathlib.Path('~')
expanduser_len = len(tilde_home.expanduser().parts)
if path.parts[:expanduser_len] == tilde_home.expanduser().parts:
return tilde_home.joinpath(*path.parts[expanduser_len:])
return path


PROGRAM_OPTIONS = {
'--client-id': {'default': None, 'type': str, 'help': 'API client_id'},
'--client-secret': {
Expand All @@ -33,7 +46,7 @@
'is no longer supported.',
},
'--config-folder': {
'default': env.default_config_dir(),
'default': shorten_path(env.default_config_dir()),
'type': pathlib.Path,
'help': 'Optional directory used to load config files. Deprecated: '
'prefer $GCALCLI_CONFIG.',
Expand Down Expand Up @@ -314,18 +327,6 @@ class RawDescArgDefaultsHelpFormatter(
"""


def shorten_path(path: pathlib.Path) -> pathlib.Path:
"""Try to shorten path using special characters like ~.
Returns original path unmodified if it can't be shortened.
"""
tilde_home = pathlib.Path('~')
expanduser_len = len(tilde_home.expanduser().parts)
if path.parts[:expanduser_len] == tilde_home.expanduser().parts:
return tilde_home.joinpath(*path.parts[expanduser_len:])
return path


@parser_allow_deprecated(name='program')
def get_argument_parser():
config_dir = shorten_path(env.default_config_dir())
Expand Down
8 changes: 4 additions & 4 deletions tests/cli/__snapshot__/test-02-test_prints_correct_help.snap
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,19 @@ configuration:
gcalcli supports a few other configuration mechanisms in addition to
the command-line arguments listed below.

$GCALCLI_CONFIG=/some/gcalcli/config
$GCALCLI_CONFIG=~/.config/gcalcli
Path to user config directory.
Note: this path is also used to determine fallback paths to check
for cache/oauth files to be migrated into their proper data dir
paths.

/some/gcalcli/config/config.toml
~/.config/gcalcli/config.toml
A toml config file where some general-purpose settings can be
configured.
Schema:
https://raw.githubusercontent.com/insanum/gcalcli/HEAD/data/config-schema.json

gcalclirc @ /some/gcalcli/config/gcalclirc
gcalclirc @ ~/.config/gcalcli/gcalclirc
A flag file listing additional command-line args to always pass,
one per line.
Note: Use this sparingly and prefer other configuration mechanisms
Expand Down Expand Up @@ -73,7 +73,7 @@ options:
--config-folder CONFIG_FOLDER
Optional directory used to load config files.
Deprecated: prefer $GCALCLI_CONFIG. (default:
/some/gcalcli/config)
~/.config/gcalcli)
--noincluderc Whether to include ~/.gcalclirc when using
configFolder (default: True)
--calendar CALENDAR Which calendars to use, in the format
Expand Down
10 changes: 9 additions & 1 deletion tests/cli/test.bats
Original file line number Diff line number Diff line change
@@ -1,18 +1,26 @@
setup() {
load 'test_helper/bats-support/load'
load 'test_helper/bats-assert/load'
load 'test_helper/bats-file/load'
load 'test_helper/bats-snapshot/load'

TEST_HOME_DIR="$(temp_make)"
HOME="$TEST_HOME_DIR"
export GCALCLI_USERLESS_MODE=1
}

function teardown() {
temp_del "$TEST_HOME_DIR"
}

@test "can run" {
run gcalcli
assert_equal $status 2
assert_output --regexp 'usage: .*error:.*required: .*command'
}

@test "prints correct help" {
GCALCLI_CONFIG=/some/gcalcli/config COLUMNS=72 run gcalcli -h
COLUMNS=72 run gcalcli -h
assert_success
assert_snapshot
}
Expand Down

0 comments on commit 8041b43

Please sign in to comment.