Skip to content

Commit

Permalink
Fix add_dot_path_to_config and add tests for configs
Browse files Browse the repository at this point in the history
  • Loading branch information
alichtman committed May 13, 2020
1 parent 869c4da commit 759ea2c
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 15 deletions.
9 changes: 7 additions & 2 deletions shallow_backup/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,9 +147,14 @@ def add_dot_path_to_config(backup_config: dict, file_path: str) -> dict:
abs_path = path.abspath(file_path)
if not path.exists(abs_path):
print_path_red("Invalid file path:", abs_path)
sys.exit(1)
return backup_config
else:
backup_config["dotfiles"][strip_home(abs_path)] = {"reinstall_condition": "", "backup_condition": ""}
stripped_home_path = strip_home(abs_path)
print_path_blue("Added:", stripped_home_path)
backup_config["dotfiles"][stripped_home_path] = {
"reinstall_condition": "",
"backup_condition": ""
}
return backup_config


Expand Down
4 changes: 0 additions & 4 deletions tests/test_backups.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,6 @@ def setup_method():
with open(os.path.join(directory, file_2), "w+") as f:
f.write(TEST_TEXT_CONTENT)

config = get_config()
config["dotfiles"][os.environ["SHALLOW_BACKUP_TEST_CONFIG_PATH"]]["backup_condition"] = "false"
write_config(config)

@staticmethod
def teardown_method():
clean_up_dirs_and_env_vars()
Expand Down
36 changes: 36 additions & 0 deletions tests/test_config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import os
import sys
from .testing_utility_functions import setup_dirs_and_env_vars_and_create_config, clean_up_dirs_and_env_vars, FAKE_HOME_DIR
sys.path.insert(0, "../shallow_backup")
from shallow_backup.config import get_config, add_dot_path_to_config
from shallow_backup.utils import strip_home


class TestConfigMethods:
"""Test the config methods."""

@staticmethod
def setup_method():
setup_dirs_and_env_vars_and_create_config()

@staticmethod
def teardown_method():
clean_up_dirs_and_env_vars()

def test_add_path(self):
config = get_config()
home_path = os.path.expanduser("~")
invalid_path = "some_random_nonexistent_path"
path_to_add = os.path.join(home_path, invalid_path)
new_config = add_dot_path_to_config(config, path_to_add)
assert strip_home(invalid_path) not in new_config["dotfiles"]

valid_path = "valid"
path_to_add = os.path.join(FAKE_HOME_DIR, valid_path)
os.mkdir(path_to_add)
new_config = add_dot_path_to_config(config, path_to_add)
from pprint import pprint
pprint(new_config)
stripped_home_path = strip_home(path_to_add)
assert stripped_home_path in new_config["dotfiles"]
assert isinstance(new_config["dotfiles"][stripped_home_path], dict)
9 changes: 0 additions & 9 deletions tests/test_reinstall_dotfiles.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

sys.path.insert(0, "../shallow_backup")
from shallow_backup.reinstall import reinstall_dots_sb
from shallow_backup.config import get_config, write_config

TEST_TEXT_CONTENT = 'THIS IS TEST CONTENT FOR THE DOTFILES'
DOTFILES_PATH = os.path.join(FAKE_HOME_DIR, "dotfiles/")
Expand Down Expand Up @@ -40,14 +39,6 @@ def create_git_dir(parent):

setup_dirs_and_env_vars_and_create_config()

# NOTE that these tests use the default included config. I remove the config
# from the backup list because otherwise it tries to reinstall it at the
# same path, which causes an error.

config = get_config()
config["dotfiles"][os.environ["SHALLOW_BACKUP_TEST_CONFIG_PATH"]]["reinstall_condition"] = "false"
write_config(config)

# Dotfiles / dirs to NOT reinstall
create_git_dir(DOTFILES_PATH) # Should NOT reinstall DOTFILES_PATH/.git
img_dir_should_not_reinstall = create_nested_dir(DOTFILES_PATH, "img")
Expand Down

0 comments on commit 759ea2c

Please sign in to comment.