Repository: theskumar/python-dotenv
Pull request #114 (2018-06-07 10:32:33)

Feat/version
Closes: #56 

This patch adds `--version` CLI option.

Changes:

* moved the version string into `dotenv/version.py`
* made `setup.py` read the version string from above
* added tests for the new CLI option
* point `bumpversion` to the new location of the version string

Example run:
```bash
$ dotenv --help
Usage: dotenv [OPTIONS] COMMAND [ARGS]...

  This script is used to set, get or unset values from a .env file.

Options:
  -f, --file PATH                 Location of the .env file, defaults to .env
                                  file in current working directory.
  -q, --quote [always|never|auto]
                                  Whether to quote or not the variable values.
                                  Default mode is always. This does not affect
                                  parsing.
  --version                       Show the version and exit.
  --help                          Show this message and exit.
[...]
```
and the output
```bash
$ dotenv --version
dotenv, version 0.8.2
```

coveralls: 
[![Coverage Status](https://coveralls.io/builds/17368774/badge)](https://coveralls.io/builds/17368774)

Coverage increased (+0.1%) to 90.977% when pulling **bee8e204ebfddd4873048b79fbfcb99089db2d31 on venthur:feat/version** into **b20d818c9296817352f5d0d7cb7b8acd90a2548d on theskumar:master**.


Patch:
diff --git a/.bumpversion.cfg b/.bumpversion.cfg
index 94f79cc..02a26c9 100644
--- a/.bumpversion.cfg
+++ b/.bumpversion.cfg
@@ -3,5 +3,5 @@ current_version = 0.8.2
 commit = True
 tag = True
 
-[bumpversion:file:setup.py]
+[bumpversion:file:dotenv/version.py]
 
diff --git a/README.md b/README.md
index 1ec2c03..67fe928 100644
--- a/README.md
+++ b/README.md
@@ -285,7 +285,7 @@ Changelog
 -----
 
 -   Add tests for docs ([@Flimm])
--   Make 'cli' support optional. Use `pip install python-dotnev[cli]`. ([@theskumar])
+-   Make 'cli' support optional. Use `pip install python-dotenv[cli]`. ([@theskumar])
 
 0.8.0
 -----
diff --git a/dotenv/cli.py b/dotenv/cli.py
index 687be9a..4e03c12 100644
--- a/dotenv/cli.py
+++ b/dotenv/cli.py
@@ -9,6 +9,7 @@ except ImportError:
     sys.exit(1)
 
 from .main import dotenv_values, get_key, set_key, unset_key, run_command
+from .version import __version__
 
 
 @click.group()
@@ -18,6 +19,7 @@ from .main import dotenv_values, get_key, set_key, unset_key, run_command
 @click.option('-q', '--quote', default='always',
               type=click.Choice(['always', 'never', 'auto']),
               help="Whether to quote or not the variable values. Default mode is always. This does not affect parsing.")
+@click.version_option(version=__version__)
 @click.pass_context
 def cli(ctx, file, quote):
     '''This script is used to set, get or unset values from a .env file.'''
diff --git a/dotenv/main.py b/dotenv/main.py
index cdb5d9d..252a2dd 100644
--- a/dotenv/main.py
+++ b/dotenv/main.py
@@ -232,7 +232,11 @@ def find_dotenv(filename='.env', raise_error_if_not_found=False, usecwd=False):
         path = os.getcwd()
     else:
         # will work for .py files
-        frame_filename = sys._getframe().f_back.f_code.co_filename
+        frame = sys._getframe()
+        # find first frame that is outside of this file
+        while frame.f_code.co_filename == __file__:
+            frame = frame.f_back
+        frame_filename = frame.f_code.co_filename
         path = os.path.dirname(os.path.abspath(frame_filename))
 
     for dirname in _walk_to_root(path):
diff --git a/dotenv/version.py b/dotenv/version.py
new file mode 100644
index 0000000..deded32
--- /dev/null
+++ b/dotenv/version.py
@@ -0,0 +1,1 @@
+__version__ = "0.8.2"
diff --git a/setup.py b/setup.py
index bf7b1f7..1c97b92 100644
--- a/setup.py
+++ b/setup.py
@@ -13,11 +13,14 @@ except (OSError, ImportError):
     with io.open('README.md', encoding="utf-8") as f:
         long_description = f.read()
 
+meta = {}
+exec(open('./dotenv/version.py').read(), meta)
+
 setup(
     name="python-dotenv",
     description="Add .env support to your django/flask apps in development and deployments",
     long_description=long_description,
-    version="0.8.2",
+    version=meta['__version__'],
     author="Saurabh Kumar",
     author_email="me+github@saurabh-kumar.com",
     url="http://github.com/theskumar/python-dotenv",


Test patch:
diff --git a/tests/test_cli.py b/tests/test_cli.py
index ac3844d..9f08cbd 100644
--- a/tests/test_cli.py
+++ b/tests/test_cli.py
@@ -3,6 +3,7 @@ from os import environ
 from os.path import dirname, join
 
 import dotenv
+from dotenv.version import __version__
 from dotenv.cli import cli as dotenv_cli
 
 import sh
@@ -217,3 +218,10 @@ def test_run_without_cmd(cli):
 def test_run_with_invalid_cmd(cli):
     result = cli.invoke(dotenv_cli, ['run', 'i_do_not_exist'])
     assert result.exit_code != 0
+
+
+def test_run_with_version(cli):
+    result = cli.invoke(dotenv_cli, ['--version'])
+    print(vars(result))
+    assert result.exit_code == 0
+    assert result.output.strip().endswith(__version__)
diff --git a/tests/test_core.py b/tests/test_core.py
index a7fb347..1547e24 100644
--- a/tests/test_core.py
+++ b/tests/test_core.py
@@ -1,4 +1,4 @@
-# -*- coding: utf8 -*-
+# -*- coding: utf-8 -*-
 from __future__ import unicode_literals
 
 import os
@@ -107,6 +107,19 @@ def test_load_dotenv_override(cli):
         sh.rm(dotenv_path)
 
 
+def test_load_dotenv_in_current_dir():
+    # make sure were are here!
+    os.chdir(os.path.dirname(os.path.realpath(__file__)))
+    dotenv_path = '.env'
+    with open(dotenv_path, 'w') as f:
+        f.write("TOTO=bla\n")
+    assert 'TOTO' not in os.environ
+    success = load_dotenv(verbose=True)
+    assert success
+    assert os.environ['TOTO'] == 'bla'
+    sh.rm(dotenv_path)
+
+
 def test_ipython():
     tmpdir = os.path.realpath(tempfile.mkdtemp())
     os.chdir(tmpdir)