Skip to content

Commit

Permalink
Subfolder dotfiles case fix with the added test case
Browse files Browse the repository at this point in the history
  • Loading branch information
heysupratim committed Dec 13, 2019
1 parent 5501f5f commit 6bdf4d4
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 4 deletions.
13 changes: 12 additions & 1 deletion shallow_backup/reinstall.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from .printing import *
from .compatibility import *
from .config import get_config
from pathlib import Path
from shutil import copytree, copyfile, copy

# NOTE: Naming convention is like this since the CLI flags would otherwise
Expand All @@ -17,12 +18,22 @@ def reinstall_dots_sb(dots_path,home_path=os.path.expanduser("~")):
"""
empty_backup_dir_check(dots_path, 'dotfile')
print_section_header("REINSTALLING DOTFILES", Fore.BLUE)
parent = Path(dots_path)

for file in get_abs_path_subfiles(dots_path):
if os.path.isdir(file):
copytree(file, home_path, symlinks=True)
else:
copy(file, home_path)
son=Path(os.path.dirname(file))
destination=""
if parent in son.parents:
folderLevel = son.relative_to(parent)
destination = os.path.join(home_path,folderLevel)
if not os.path.exists(os.path.join(home_path,folderLevel)):
os.makedirs(os.path.join(home_path,folderLevel))
else:
destination = home_path
copy(file,destination)
print_section_header("DOTFILE REINSTALLATION COMPLETED", Fore.BLUE)


Expand Down
14 changes: 11 additions & 3 deletions tests/test_reinstall_dotfiles.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,27 +32,35 @@ def setup_method():
os.mkdir(DOTFILES_PATH)

# SAMPLE SUBFOLDER IN DOTFILES PATH
print(os.path.join(DOTFILES_PATH, "testfolder/"))
try:
os.mkdir(os.path.join(DOTFILES_PATH, "testfolder/"))
except FileExistsError:
shutil.rmtree(os.path.join(DOTFILES_PATH, "testfolder/"))
os.mkdir(os.path.join(DOTFILES_PATH, "testfolder/"))

# SAMPLE DOTFILE TO REINSTALL
# SAMPLE DOTFILES TO REINSTALL
file = os.path.join(DOTFILES_PATH, ".testrc")
print(f"Creating {file}")
with open(file, "w+") as f:
f.write(TEST_TEXT_CONTENT)

file = os.path.join(DOTFILES_PATH, "testfolder/.testsubfolder_rc")
print(f"Creating {file}")
with open(file, "w+") as f:
f.write(TEST_TEXT_CONTENT)

@staticmethod
def teardown_method():
for directory in DIRS:
shutil.rmtree(directory)

def test_reinstall_dotfiles(self):
"""
Test resintalling dotfile to fake home dir
Test resintalling dotfiles to fake home dir
"""
reinstall_dots_sb(DOTFILES_PATH,home_path=FAKE_HOME_DIR)
assert os.path.isfile(os.path.join(FAKE_HOME_DIR, '.testrc'))
assert os.path.isdir(os.path.join(FAKE_HOME_DIR, 'testfolder/'))
print(os.path.join(FAKE_HOME_DIR, 'testfolder/'))
assert os.path.isdir(os.path.join(FAKE_HOME_DIR, 'testfolder/'))
assert os.path.isfile(os.path.join(FAKE_HOME_DIR, 'testfolder/.testsubfolder_rc'))

0 comments on commit 6bdf4d4

Please sign in to comment.