Skip to content

Commit

Permalink
Fix tests (#238)
Browse files Browse the repository at this point in the history
* Begin reworking tests

Progress on #237

* Respect XDG Base Directory spec (#239)

Fix #236

* Fix config creation bug

* Get all tests to pass

Fix #237

* Apparently travis VMs don't come with /tmp created already

* Wrong diagnosis of the issue
  • Loading branch information
alichtman authored Nov 15, 2019
1 parent 386b326 commit d0d7867
Show file tree
Hide file tree
Showing 6 changed files with 94 additions and 63 deletions.
8 changes: 6 additions & 2 deletions shallow_backup/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -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():
Expand Down
1 change: 0 additions & 1 deletion shallow_backup/git_wrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,4 +134,3 @@ def move_git_repo(source_path, dest_path):
print_blue_bold("Moving git repo to new location.")
except FileNotFoundError:
pass

41 changes: 14 additions & 27 deletions tests/test_backups.py
Original file line number Diff line number Diff line change
@@ -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:
Expand All @@ -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)
Expand All @@ -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)

Expand All @@ -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)
40 changes: 20 additions & 20 deletions tests/test_copies.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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))
23 changes: 10 additions & 13 deletions tests/test_git_folder_moving.py
Original file line number Diff line number Diff line change
@@ -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:
Expand All @@ -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)
Expand All @@ -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'))
44 changes: 44 additions & 0 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
@@ -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")
]

0 comments on commit d0d7867

Please sign in to comment.