Skip to content

Commit

Permalink
add envy reset feature, along with a (messy) test..
Browse files Browse the repository at this point in the history
  • Loading branch information
shaunvxc committed Jul 13, 2016
1 parent 2e90eff commit 9722a91
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 5 deletions.
2 changes: 1 addition & 1 deletion envy/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
VERSION = '0.1.0'
from .application import get_active_venv, get_package_name, get_envy_path, original_backed_up, get_venv_full_package_path, get_file_path, sync, clean
from .application import get_active_venv, get_package_name, get_envy_path, original_backed_up, get_venv_full_package_path, get_file_path, sync, clean, reset
from .decorators import is_active_venv, in_python_package, validate_env, validate_pkg
24 changes: 24 additions & 0 deletions envy/application.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,17 @@ def clean(args):
restore_environment(args.package)


@validate_env
def reset(args):
if args.all:
for package in os.listdir(get_envy_base() + "/{}".format(get_active_venv())):
reset_environment(package)
else:
# needs to be args.package instead of args.package[0] here-- as we can also pass --all, making package technically
# an optional argument, and hence we use nargs='?' instead of nargs=1.
reset_environment(args.package)


@validate_env
def diff(args):
pkg_name_given_in_arg = args.path[0].split('/')[0]
Expand Down Expand Up @@ -144,6 +155,14 @@ def restore_environment(package_name):
print ("removing .envie")
shutil.rmtree(get_envy_path(package_name))

def reset_environment(package_name):
if not os.path.isdir(get_envy_path(package_name)):
print ("uh oh..no recorded backup in {}, so nothing to reset".format(get_envy_path(package_name)))
return

print("dropping saved envie for {}".format(package_name))
shutil.rmtree(get_envy_path(package_name))


def copytree(src, dst):
if not os.path.exists(dst):
Expand Down Expand Up @@ -189,6 +208,11 @@ def prepare_parser():
parser_diff.set_defaults(func=diff)
parser_diff.add_argument('path', nargs=1)

parser_reset = subparsers.add_parser('reset', help='reset the persisted backup copy of the provided package ')
parser_reset.set_defaults(func=reset)
parser_reset.add_argument('package', nargs='?')
parser_reset.add_argument('--all', action='store_true', help='clean all backed up environments')

return parser


Expand Down
22 changes: 18 additions & 4 deletions tests/envy_acceptance_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,13 @@
import envy
import pkg_resources

from envy import get_active_venv, get_package_name, get_envy_path, original_backed_up, get_venv_full_package_path, sync, clean
from envy import get_active_venv, get_package_name, get_envy_path, original_backed_up, get_venv_full_package_path, sync, clean, reset
from envy.decorators import is_active_venv, in_python_package

from mock import MagicMock, PropertyMock
from mock import patch
import argparse

## TODO: Add test for deeply nested files

base = os.getcwd()

def setup_test(f):
Expand Down Expand Up @@ -90,6 +88,22 @@ def test_sync_file_from_inner_dir_and_clean(mock_os):
args.all = False
clean(args)


assert os.path.isdir('./tests/testsrc/someuser/.envies/someenv/some_package') == False
assert os.path.isfile('./tests/testsrc/someuser/.virtualenvs/someenv/lib/python2.7/site-packages/some_package/test.py') == False

@patch('os.getcwd', return_value='{}/tests/testsrc/someuser/src/some_package2/some_package2'.format(base))
def test_sync_and_reset(mock_os):
with patch('envy.decorators.is_active_venv', return_value=True):
with patch('envy.application.get_active_venv', return_value="someenv"):
with patch('envy.decorators.in_python_package', return_value=True):
with patch('envy.application.get_envy_base', return_value="{}/tests/testsrc/someuser/.envies/".format(base)):
with patch('envy.application.get_envy_path', return_value="{}/tests/testsrc/someuser/.envies/someenv/some_package2".format(base)):
with patch('envy.application.get_venv_full_package_path', return_value='{}/tests/testsrc/someuser/.virtualenvs/someenv/lib/python2.7/site-packages/some_package2'.format(base)):
with patch('os.path.expanduser', return_value="{}/tests/testsrc/someuser/".format(base)):
args = argparse.Namespace()
args.package = ['some_package2']
args.all = False
os.makedirs('{}/tests/testsrc/someuser/.envies/someenv/some_package2'.format(base))
assert os.path.isdir('./tests/testsrc/someuser/.envies/someenv/some_package2') == True
reset(args)
assert os.path.isdir('./tests/testsrc/someuser/.envies/someenv/some_package2') == False

0 comments on commit 9722a91

Please sign in to comment.