Skip to content

Commit

Permalink
Start testing cli
Browse files Browse the repository at this point in the history
  • Loading branch information
jbn committed Nov 8, 2017
1 parent fffae5b commit d5992d0
Show file tree
Hide file tree
Showing 12 changed files with 123 additions and 11 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Expand Up @@ -3,3 +3,5 @@ dist/
*.pyc
*.ipynb
*.egg-info
cli_tests/outputs
.coverage
2 changes: 1 addition & 1 deletion .travis.yml
Expand Up @@ -11,5 +11,5 @@ install:
- "pip install coveralls nose"
script:
- "coverage run --source=pathsjson setup.py nosetests"
- "./cli_tests.sh"
- "./test_cli.sh"
after_success: coveralls
5 changes: 0 additions & 5 deletions cli_tests.sh

This file was deleted.

29 changes: 29 additions & 0 deletions cli_tests/full_env/.paths.json
@@ -0,0 +1,29 @@
{
"clean_dir": [
"$data_dir",
"clean"
],
"raw_dir": [
"$data_dir",
"raw"
],
"latest_data": [
"$raw_dir",
"$$VERSION",
"data.csv"
],
"test_dir": [
"$data_dir",
"tests"
],
"__ENV": {
"VERSION": "1.0.0"
},
"data_dir": [
"data"
],
"codebook_dir": [
"$clean_dir",
"codebooks"
]
}
1 change: 1 addition & 0 deletions cli_tests/test_1.expected.txt
@@ -0,0 +1 @@
No `.paths.json` file found!
6 changes: 6 additions & 0 deletions cli_tests/test_2.expected.txt
@@ -0,0 +1,6 @@
export clean_dir="data/clean"
export raw_dir="data/raw"
export latest_data="data/raw/1.0.0/data.csv"
export test_dir="data/tests"
export data_dir="data"
export codebook_dir="data/clean/codebooks"
6 changes: 6 additions & 0 deletions cli_tests/test_3.expected.txt
@@ -0,0 +1,6 @@
clean_dir?=data/clean
raw_dir?=data/raw
latest_data?=data/raw/1.0.0/data.csv
test_dir?=data/tests
data_dir?=data
codebook_dir?=data/clean/codebooks
15 changes: 13 additions & 2 deletions pathsjson/cli.py
Expand Up @@ -5,7 +5,7 @@
from pathsjson.impl import *


def main(args=None):
def _main(args=None):
parser = argparse.ArgumentParser()

parser.add_argument('--print-global-path',
Expand Down Expand Up @@ -61,5 +61,16 @@ def main(args=None):
json.dump({"__ENV": {}}, fp, indent=" ")
sys.exit()


parser.print_help()


def main(args=None):
try:
_main(args)
except IOError as e:
print(e)
sys.exit(1)


if __name__ == '__main__':
main(sys.argv[1:])
11 changes: 9 additions & 2 deletions pathsjson/impl.py
Expand Up @@ -178,8 +178,11 @@ def create_user_globals_file(overwrite=False):

def patch_with_user_globals(data, skip_noexist=True):
file_path = get_user_globals_path()
if not skip_noexist and not os.path.exists(file_path):
raise IOError("User globals missing at: ".format(file_path))
if not os.path.exists(file_path):
if skip_noexist:
return data
else:
raise IOError("User globals missing at: ".format(file_path))

with open(file_path) as fp:
global_data = json.load(fp, object_pairs_hook=OrderedDict)
Expand Down Expand Up @@ -246,9 +249,13 @@ def __init__(self, file_path=None, src_dir=None, target_name=".paths.json",
self._enable_env_overrides = enable_env_overrides
self._enable_user_global_overrides = enable_user_global_overrides
self._validate = validate

self.reload()

def reload(self):
"""
Reload the path definitions.
"""
file_path = self._file_path
enable_env_overrides = self._enable_env_overrides
enable_user_global_overrides = self._enable_user_global_overrides
Expand Down
47 changes: 47 additions & 0 deletions test_cli.sh
@@ -0,0 +1,47 @@
#!/bin/bash

###############################################################################
# setUp
###############################################################################

set -ev # Fail on first non-zero return value.
rm -rf cli_tests/outputs && mkdir cli_tests/outputs

###############################################################################
# Test that automagic importing works.
###############################################################################

PWD=tests/examples/twitter python -c "from pathsjson.automagic import PATHS;"

###############################################################################
# 1. Test without any ancestral .paths.json
###############################################################################

(coverage run --source=pathsjson --append -m pathsjson.cli --shell-exports \
> cli_tests/outputs/test_1.txt) || true

cmp -b cli_tests/outputs/test_1.txt cli_tests/test_1.expected.txt

###############################################################################
# 2. Test --shell-exports
###############################################################################

(PWD=cli_tests/full_env
coverage run --source=pathsjson --append -m pathsjson.cli --shell-exports \
> cli_tests/outputs/test_2.txt) || true

diff cli_tests/outputs/test_2.txt cli_tests/test_2.expected.txt

###############################################################################
# 3. Test --make-exports
###############################################################################

(PWD=cli_tests/full_env \
coverage run --source=pathsjson --append -m pathsjson.cli --make-exports \
> cli_tests/outputs/test_3.txt) || true
diff cli_tests/outputs/test_3.txt cli_tests/test_3.expected.txt

###############################################################################
# tearDown
###############################################################################
rm -rf cli_tests/outputs
4 changes: 4 additions & 0 deletions tests/fixtures/paths_with_args.json
@@ -0,0 +1,4 @@
{
"__ENV": { "VERSION": "1.0.0" },
"special": ["root", "$$VERSION", "$$user_name"]
}
6 changes: 5 additions & 1 deletion tests/test_pathsjson.py
Expand Up @@ -52,7 +52,11 @@ def delete_and_replace(path):
with open(path, "w") as fp:
fp.write(src)
else:
yield
try:
yield
finally:
if os.path.exists(path):
os.unlink(path)

###############################################################################

Expand Down

0 comments on commit d5992d0

Please sign in to comment.