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 286860a commit 28ae18b
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 13 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: 8 additions & 0 deletions tests/cli/test.bats
Original file line number Diff line number Diff line change
@@ -1,10 +1,18 @@
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)"
export 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
Expand Down

0 comments on commit 28ae18b

Please sign in to comment.