Skip to content

Commit f7747fa

Browse files
author
Saurabh Kumar
committed
add tests
1 parent faf9492 commit f7747fa

9 files changed

Lines changed: 84 additions & 50 deletions

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@
33
build/
44
dist/
55
.env
6+
__pycache__

.travis-requirements.txt

Lines changed: 0 additions & 1 deletion
This file was deleted.

.travis.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ python:
44
- '3.3'
55
- pypy
66
install:
7-
- pip install -q -r .travis-requirements.txt
7+
- pip install -q -r requirements.txt
88
- pip install --editable .
99
before_script: flake8
10+
script: py.test

README.md

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -37,15 +37,18 @@ This is a first pass, will likely change.
3737

3838
Put the `dotenv.py` file in the same folder on your server as the `.env` file and `manage.py`. Then you can add a config task to your local fabfile:
3939
```
40+
from fabric.api import task, local, env
41+
42+
env.dotenv_path = '/etc/project_name/.env'
43+
4044
@task
4145
def config(action=None,key=None,value=None):
42-
command = env.django_path + "dotenv.py "
43-
command += env.django_path + ".env "
44-
command += action + " " if action else ""
45-
command += key + " " if key else ""
46-
command += value + " " if value else ""
47-
python(command)
48-
46+
command = 'dotenv'
47+
command += ' -f %s' % env.dotenv_path)
48+
command += action if action else " "
49+
command += key if key else " "
50+
command += value if value else ""
51+
run(command)
4952
```
5053

5154
Usage is designed to mirror the heroku config api very closely.

dotenv.py

Lines changed: 46 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
import warnings
44
from collections import OrderedDict
55

6+
import click
7+
68

79
def load_dotenv(dotenv_path):
810
"""
@@ -110,54 +112,58 @@ def flatten_and_write(dotenv_path, dotenv_as_dict):
110112
f.write('%s="%s"\r\n' % (k, v))
111113
return True
112114

113-
if __name__ == "__main__":
114-
import argparse
115-
parser = argparse.ArgumentParser()
116-
parser.add_argument("file_path", help="the absolute path of the .env file you want to use")
117-
parser.add_argument("action", help="what you want to do with the .env file (get, set, unset)", nargs='?')
118-
parser.add_argument("key", help="the environment key you want to set", nargs='?')
119-
parser.add_argument("value", help="the value you want to set 'key' to", nargs='?')
120-
parser.add_argument("--force", help="force writing even if the file at the given path doesn't end in .env")
121-
args = parser.parse_args()
122-
123-
if not os.path.exists(args.file_path):
124-
warnings.warn("there doesn't appear to be a file at %s" % args.file_path)
125-
exit(1)
126-
if not args.force:
127-
if not args.file_path.endswith(".env"):
128-
warnings.warn("the file %s doesn't appear to be a .env file, use --force to proceed" % args.file_path)
129-
exit(1)
130115

131-
if not args.action:
132-
with open(args.file_path) as f:
133-
print(f.read())
134-
exit(0)
135-
elif args.action == "get":
136-
stored_value = get_key(args.file_path, args.key)
137-
if stored_value is not None:
138-
print(args.key)
139-
print(stored_value)
140-
else:
116+
@click.command()
117+
@click.argument('action', type=click.Choice(['get', 'set', 'unset']), required=False)
118+
@click.argument('key', required=False)
119+
@click.argument('value', required=False)
120+
@click.option('--force', is_flag=True)
121+
@click.option('-f', '--file', default='.env', type=click.Path(exists=True))
122+
def cli(file, action, key, value, force):
123+
124+
if not action:
125+
dotenv_as_dict = parse_dotenv(file)
126+
for k, v in dotenv_as_dict:
127+
click.echo("%s=%s" % (k, v))
128+
129+
if action == 'get':
130+
stored_value = get_key(file, key)
131+
if stored_value:
132+
click.echo("%s=%s" % (key, stored_value))
141133
exit(1)
142-
elif args.action == "set":
143-
success, key, value = set_key(args.file_path, args.key, args.value)
144-
if success is not None:
145-
print("%s: %s" % (key, value))
146134
else:
135+
click.echo("%s doesn't seems to have been set yet.")
136+
exit(0)
137+
138+
elif action == 'set':
139+
if not value:
140+
click.echo("Error: value is missing.")
141+
exit(0)
142+
success, key, value = set_key(file, key, value)
143+
if success:
144+
click.echo("%s=%s" % (key, value))
147145
exit(1)
148-
elif args.action == "unset":
149-
success, key = unset_key(args.file_path, args.key)
150-
if success is not None:
151-
print("unset %s" % key)
152146
else:
147+
exit(0)
148+
149+
elif action == 'unset':
150+
success, key = unset_key(file, key)
151+
if success:
152+
click.echo("Successfully removed %s" % key)
153153
exit(1)
154+
else:
155+
exit(0)
156+
154157
# Need to investigate if this can actually work or if the scope of the new environ variables
155158
# Expires when python exits
156-
#
157-
# elif args.action == "load":
158-
# success = load_dotenv(args.file_path)
159+
160+
# elif action == "load":
161+
# success = load_dotenv(file)
159162
# if success != None:
160-
# print("loaded %s into environment" % args.file_path)
163+
# click.echo("loaded %s into environment" % file)
161164
# else:
162165
# exit(1)
163-
exit(0)
166+
167+
168+
if __name__ == "__main__":
169+
cli()

requirements.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
flake8
2+
pytest
3+
sh

setup.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,13 @@
88
author_email="ted@sittingaround.com",
99
url="http://github.com/tedtieken/django-dotenv-rw",
1010
py_modules=['dotenv'],
11-
scripts=['dotenv.py'],
11+
install_requires=[
12+
'click>=3.0',
13+
],
14+
entry_points='''
15+
[console_scripts]
16+
dotenv=dotenv:cli
17+
''',
1218
classifiers=[
1319
'Development Status :: 3 - Alpha',
1420
'Environment :: Web Environment',

tests/__init__.py

Whitespace-only changes.

tests/test_basic.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
from os.path import dirname, join
2+
3+
import sh
4+
import dotenv
5+
6+
here = dirname(__file__)
7+
dotenv_path = join(here, '.env')
8+
9+
10+
def test_read_write():
11+
sh.touch(dotenv_path)
12+
success, key_to_set, value_to_set = dotenv.set_key(dotenv_path, 'HELLO', 'WORLD')
13+
stored_value = dotenv.get_key(dotenv_path, 'HELLO')
14+
assert stored_value == 'WORLD'
15+
sh.rm(dotenv_path)

0 commit comments

Comments
 (0)