diff --git a/shallow_backup/config.py b/shallow_backup/config.py index 862db71b..62368a31 100644 --- a/shallow_backup/config.py +++ b/shallow_backup/config.py @@ -7,8 +7,12 @@ def get_config_path(): - xdg_config_home = environ.get('XDG_CONFIG_HOME') or path.join(path.expanduser('~'), '.config') - return path.join(xdg_config_home, "shallow-backup", "shallow-backup.conf") + test_config_path = environ.get('SHALLOW_BACKUP_TEST_CONFIG_PATH', None) + if test_config_path: + return test_config_path + else: + xdg_config_home = environ.get('XDG_CONFIG_HOME') or path.join(path.expanduser('~'), '.config') + return path.join(xdg_config_home, "shallow-backup", "shallow-backup.conf") def get_config(): diff --git a/shallow_backup/git_wrapper.py b/shallow_backup/git_wrapper.py index 79724735..adfee7ef 100644 --- a/shallow_backup/git_wrapper.py +++ b/shallow_backup/git_wrapper.py @@ -134,4 +134,3 @@ def move_git_repo(source_path, dest_path): print_blue_bold("Moving git repo to new location.") except FileNotFoundError: pass - diff --git a/tests/test_backups.py b/tests/test_backups.py index 28ad03a2..75ff78ea 100644 --- a/tests/test_backups.py +++ b/tests/test_backups.py @@ -1,29 +1,11 @@ import os import sys import shutil +from .test_utils import BACKUP_DEST_DIR, FAKE_HOME_DIR, DIRS, DOTFILES, DOTFOLDERS, setup_env_vars, unset_env_vars, create_config_for_test sys.path.insert(0, "../shallow_backup") from shallow_backup.backup import backup_dotfiles -from shallow_backup.config import safe_create_config -BACKUP_DIR = 'shallow-backup-test-backups-dir' -FAKE_HOME_DIR = 'shallow-backup-test-backups-src-dir' -TEST_TEXT_CONTENT = 'THIS IS TEST CONTENT FOR THE DOTFILE' -DIRS = [BACKUP_DIR, FAKE_HOME_DIR] -DOTFILES = [ - os.path.join(FAKE_HOME_DIR, ".bashrc"), - os.path.join(FAKE_HOME_DIR, ".bash_profile"), - os.path.join(FAKE_HOME_DIR, ".gitconfig"), - os.path.join(FAKE_HOME_DIR, ".profile"), - os.path.join(FAKE_HOME_DIR, ".pypirc"), - os.path.join(FAKE_HOME_DIR, ".shallow-backup"), - os.path.join(FAKE_HOME_DIR, ".vimrc"), - os.path.join(FAKE_HOME_DIR, ".zshrc") -] - -DOTFOLDERS = [ - os.path.join(FAKE_HOME_DIR, ".ssh"), - os.path.join(FAKE_HOME_DIR, ".vim") -] +TEST_TEXT_CONTENT = 'THIS IS TEST CONTENT FOR THE DOTFILES' class TestBackupMethods: @@ -33,7 +15,8 @@ class TestBackupMethods: @staticmethod def setup_method(): - safe_create_config() + setup_env_vars() + create_config_for_test() for directory in DIRS: try: os.mkdir(directory) @@ -43,6 +26,7 @@ def setup_method(): # Create all dotfiles and dotfolders for file in DOTFILES: + print(f"Creating {file}") with open(file, "w+") as f: f.write(TEST_TEXT_CONTENT) @@ -55,18 +39,21 @@ def setup_method(): @staticmethod def teardown_method(): for folder in DIRS: + print(f"Removing {folder}") shutil.rmtree(folder) + unset_env_vars() def test_backup_dotfiles(self): """ Test backing up dotfiles and dotfolders. """ - dotfiles_path = os.path.join(BACKUP_DIR, "dotfiles") - backup_dotfiles(dotfiles_path, home_path=FAKE_HOME_DIR, skip=True) - assert os.path.isdir(dotfiles_path) + backup_dest_path = os.path.join(BACKUP_DEST_DIR, "dotfiles") + backup_dotfiles(backup_dest_path, home_path=FAKE_HOME_DIR, skip=True) + assert os.path.isdir(backup_dest_path) for path in DOTFILES: - print("DOTFILES DIRECTORY CONTENTS:", os.listdir(dotfiles_path)) - print(path + " being backed up.") + print(f"BACKUP DESTINATION DIRECTORY ({backup_dest_path}) CONTENTS:", os.listdir(backup_dest_path)) + print(path + " should already be backed up.") print("CWD:", os.getcwd()) - backed_up_dot = os.path.join(dotfiles_path, os.path.split(path)[-1]) + backed_up_dot = os.path.join(backup_dest_path, os.path.split(path)[-1]) + print(f"Backed up dot: {backed_up_dot}") assert os.path.isfile(backed_up_dot) diff --git a/tests/test_copies.py b/tests/test_copies.py index 3bf8dccb..f23153ff 100644 --- a/tests/test_copies.py +++ b/tests/test_copies.py @@ -2,13 +2,11 @@ import sys import pytest import shutil +from .test_utils import setup_env_vars, unset_env_vars, BACKUP_DEST_DIR, FAKE_HOME_DIR, DIRS sys.path.insert(0, "../shallow_backup") from shallow_backup.utils import copy_dir_if_valid -DIR_TO_BACKUP = 'shallow-backup-test-copy-dir' -BACKUP_DIR = 'shallow-backup-test-copy-backup-dir' -TEST_TEXT_FILE = 'test-file.txt' -DIRS = [DIR_TO_BACKUP, BACKUP_DIR] +TEST_TEXT_FILE = os.path.join(FAKE_HOME_DIR, 'test-file.txt') class TestCopyMethods: @@ -18,37 +16,39 @@ class TestCopyMethods: @staticmethod def setup_method(): - for directory in DIRS: - try: - os.mkdir(directory) - except FileExistsError: - shutil.rmtree(directory) - os.mkdir(directory) - f = open(TEST_TEXT_FILE, "w+") - f.close() + setup_env_vars() + try: + os.mkdir(FAKE_HOME_DIR) + except FileExistsError: + shutil.rmtree(FAKE_HOME_DIR) + os.mkdir(FAKE_HOME_DIR) + print(f"Created {TEST_TEXT_FILE}") + open(TEST_TEXT_FILE, "w+").close() @staticmethod def teardown_method(): for directory in DIRS: - shutil.rmtree(directory) - os.remove(TEST_TEXT_FILE) + if os.path.isdir(directory): + shutil.rmtree(directory) + unset_env_vars() def test_copy_dir(self): """ Test that copying a directory works as expected """ # TODO: Test that all subfiles and folders are copied. - test_dir = 'test' - test_path = os.path.join(DIR_TO_BACKUP, test_dir) + test_dir = 'subdir-to-copy' + test_path = os.path.join(FAKE_HOME_DIR, test_dir) os.mkdir(test_path) - copy_dir_if_valid(test_path, BACKUP_DIR) + copy_dir_if_valid(FAKE_HOME_DIR, BACKUP_DEST_DIR) assert os.path.isdir(test_path) - assert os.path.isdir(os.path.join(BACKUP_DIR, test_dir)) + assert os.path.isfile(os.path.join(BACKUP_DEST_DIR, os.path.split(TEST_TEXT_FILE)[1])) + assert os.path.isdir(os.path.join(BACKUP_DEST_DIR, test_dir)) @pytest.mark.parametrize('invalid', {".Trash", ".npm", ".cache", ".rvm"}) def test_copy_dir_invalid(self, invalid): """ Test that attempting to copy an invalid directory fails """ - copy_dir_if_valid(invalid, DIR_TO_BACKUP) - assert not os.path.isdir(os.path.join(BACKUP_DIR, invalid)) + copy_dir_if_valid(invalid, FAKE_HOME_DIR) + assert not os.path.isdir(os.path.join(BACKUP_DEST_DIR, invalid)) diff --git a/tests/test_git_folder_moving.py b/tests/test_git_folder_moving.py index 185a3397..b334bfda 100644 --- a/tests/test_git_folder_moving.py +++ b/tests/test_git_folder_moving.py @@ -1,13 +1,9 @@ import os import sys import shutil +from .test_utils import BACKUP_DEST_DIR, FAKE_HOME_DIR, DIRS, setup_env_vars, create_config_for_test sys.path.insert(0, "../shallow_backup") from shallow_backup.git_wrapper import move_git_repo, safe_git_init, safe_create_gitignore -from shallow_backup.config import safe_create_config - -OLD_BACKUP_DIR = 'shallow-backup-test-git-old-backup-dir' -NEW_BACKUP_DIR = 'shallow-backup-test-git-new-backup-backup-dir' -DIRS = [OLD_BACKUP_DIR, NEW_BACKUP_DIR] class TestGitFolderCopying: @@ -17,7 +13,8 @@ class TestGitFolderCopying: @staticmethod def setup_method(): - safe_create_config() + setup_env_vars() + create_config_for_test() for directory in DIRS: try: os.mkdir(directory) @@ -34,10 +31,10 @@ def test_copy_git_folder(self): """ Test copying the .git folder and .gitignore from an old directory to a new one """ - safe_git_init(OLD_BACKUP_DIR) - safe_create_gitignore(OLD_BACKUP_DIR) - move_git_repo(OLD_BACKUP_DIR, NEW_BACKUP_DIR) - assert os.path.isdir(os.path.join(NEW_BACKUP_DIR, '.git/')) - assert os.path.isfile(os.path.join(NEW_BACKUP_DIR, '.gitignore')) - assert not os.path.isdir(os.path.join(OLD_BACKUP_DIR, '.git/')) - assert not os.path.isfile(os.path.join(OLD_BACKUP_DIR, '.gitignore')) + safe_git_init(FAKE_HOME_DIR) + safe_create_gitignore(FAKE_HOME_DIR) + move_git_repo(FAKE_HOME_DIR, BACKUP_DEST_DIR) + assert os.path.isdir(os.path.join(BACKUP_DEST_DIR, '.git/')) + assert os.path.isfile(os.path.join(BACKUP_DEST_DIR, '.gitignore')) + assert not os.path.isdir(os.path.join(FAKE_HOME_DIR, '.git/')) + assert not os.path.isfile(os.path.join(FAKE_HOME_DIR, '.gitignore')) diff --git a/tests/test_utils.py b/tests/test_utils.py new file mode 100644 index 00000000..b9231f34 --- /dev/null +++ b/tests/test_utils.py @@ -0,0 +1,44 @@ +import os +import sys +sys.path.insert(0, "../shallow_backup") +from shallow_backup.config import safe_create_config + + +def setup_env_vars(): + os.environ["SHALLOW_BACKUP_TEST_DEST_DIR"] = "/tmp/shallow-backup-test-dest-dir" + os.environ["SHALLOW_BACKUP_TEST_SOURCE_DIR"] = "/tmp/shallow-backup-test-source-dir" + os.environ["SHALLOW_BACKUP_TEST_CONFIG_PATH"] = "/tmp/shallow-backup.conf" + + +def unset_env_vars(): + del os.environ["SHALLOW_BACKUP_TEST_DEST_DIR"] + del os.environ["SHALLOW_BACKUP_TEST_SOURCE_DIR"] + del os.environ["SHALLOW_BACKUP_TEST_CONFIG_PATH"] + + +def create_config_for_test(): + config_file = os.environ["SHALLOW_BACKUP_TEST_CONFIG_PATH"] + if os.path.isfile(config_file): + os.remove(config_file) + safe_create_config() + + +setup_env_vars() +BACKUP_DEST_DIR = os.environ.get("SHALLOW_BACKUP_TEST_DEST_DIR") +FAKE_HOME_DIR = os.environ.get("SHALLOW_BACKUP_TEST_SOURCE_DIR") +DIRS = [BACKUP_DEST_DIR, FAKE_HOME_DIR] + +DOTFILES = [ + os.path.join(FAKE_HOME_DIR, ".bashrc"), + os.path.join(FAKE_HOME_DIR, ".bash_profile"), + os.path.join(FAKE_HOME_DIR, ".gitconfig"), + os.path.join(FAKE_HOME_DIR, ".profile"), + os.path.join(FAKE_HOME_DIR, ".pypirc"), + os.path.join(FAKE_HOME_DIR, ".vimrc"), + os.path.join(FAKE_HOME_DIR, ".zshrc") +] + +DOTFOLDERS = [ + os.path.join(FAKE_HOME_DIR, ".ssh"), + os.path.join(FAKE_HOME_DIR, ".vim") +]