Skip to content

Commit

Permalink
Merge pull request #176 from AndreyNikiforov/bug/chinese-chars
Browse files Browse the repository at this point in the history
add Chinese base path test; refactor tests for multi platform support
  • Loading branch information
AndreyNikiforov authored Oct 20, 2020
2 parents eac1ba1 + 122b43e commit 5f05dc9
Show file tree
Hide file tree
Showing 6 changed files with 285 additions and 200 deletions.
2 changes: 1 addition & 1 deletion icloudpd/autodelete.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ def autodelete_photos(icloud, folder_structure, directory):
download_dir = os.path.join(directory, date_path)

for size in [None, "original", "medium", "thumb"]:
path = local_download_path(media, size, download_dir)
path = os.path.normpath(local_download_path(media, size, download_dir))
if os.path.exists(path):
logger.info("Deleting %s!", path)
os.remove(path)
4 changes: 2 additions & 2 deletions icloudpd/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -344,7 +344,7 @@ def photos_exception_handler(ex, retries):
if not skip_videos:
video_suffix = " or video" if photos_count == 1 else " and videos"
logger.info(
"Downloading %s %s photo%s%s to %s/ ...",
"Downloading %s %s photo%s%s to %s ...",
photos_count_str,
size,
plural_suffix,
Expand Down Expand Up @@ -401,7 +401,7 @@ def download_photo(counter, photo):
created_date = datetime.datetime.fromtimestamp(0)
date_path = folder_structure.format(created_date)

download_dir = os.path.join(directory, date_path)
download_dir = os.path.normpath(os.path.join(directory, date_path))

if not os.path.exists(download_dir):
try:
Expand Down
37 changes: 19 additions & 18 deletions tests/test_autodelete_photos.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,22 +19,23 @@ def inject_fixtures(self, caplog):
self._caplog = caplog

def test_autodelete_photos(self):
if os.path.exists("tests/fixtures/Photos"):
shutil.rmtree("tests/fixtures/Photos")
os.makedirs("tests/fixtures/Photos")
base_dir = os.path.normpath("tests/fixtures/Photos")
if os.path.exists(base_dir):
shutil.rmtree(base_dir)
os.makedirs(base_dir)

# create some empty files that should be deleted
os.makedirs("tests/fixtures/Photos/2018/07/30/")
open("tests/fixtures/Photos/2018/07/30/IMG_7406.MOV", "a").close()
os.makedirs("tests/fixtures/Photos/2018/07/26/")
open("tests/fixtures/Photos/2018/07/26/IMG_7383.PNG", "a").close()
os.makedirs("tests/fixtures/Photos/2018/07/12/")
open("tests/fixtures/Photos/2018/07/12/IMG_7190.JPG", "a").close()
open("tests/fixtures/Photos/2018/07/12/IMG_7190-medium.JPG", "a").close()
os.makedirs(os.path.join(base_dir, "2018/07/30/"))
open(os.path.join(base_dir, "2018/07/30/IMG_7406.MOV"), "a").close()
os.makedirs(os.path.join(base_dir, "2018/07/26/"))
open(os.path.join(base_dir, "2018/07/26/IMG_7383.PNG"), "a").close()
os.makedirs(os.path.join(base_dir, "2018/07/12/"))
open(os.path.join(base_dir, "2018/07/12/IMG_7190.JPG"), "a").close()
open(os.path.join(base_dir, "2018/07/12/IMG_7190-medium.JPG"), "a").close()

# Should not be deleted
open("tests/fixtures/Photos/2018/07/30/IMG_7407.JPG", "a").close()
open("tests/fixtures/Photos/2018/07/30/IMG_7407-original.JPG", "a").close()
open(os.path.join(base_dir, "2018/07/30/IMG_7407.JPG"), "a").close()
open(os.path.join(base_dir, "2018/07/30/IMG_7407-original.JPG"), "a").close()

with vcr.use_cassette("tests/vcr_cassettes/autodelete_photos.yml"):
# Pass fixed client ID via environment variable
Expand All @@ -52,12 +53,12 @@ def test_autodelete_photos(self):
"--skip-videos",
"--auto-delete",
"-d",
"tests/fixtures/Photos",
base_dir,
],
)
self.assertIn("DEBUG Looking up all photos from album All Photos...", self._caplog.text)
self.assertIn(
"INFO Downloading 0 original photos to tests/fixtures/Photos/ ...",
f"INFO Downloading 0 original photos to {base_dir} ...",
self._caplog.text,
)
self.assertIn(
Expand All @@ -74,19 +75,19 @@ def test_autodelete_photos(self):
)

self.assertIn(
"INFO Deleting tests/fixtures/Photos/2018/07/30/IMG_7406.MOV",
f"INFO Deleting {os.path.join(base_dir, os.path.normpath('2018/07/30/IMG_7406.MOV'))}",
self._caplog.text,
)
self.assertIn(
"INFO Deleting tests/fixtures/Photos/2018/07/26/IMG_7383.PNG",
f"INFO Deleting {os.path.join(base_dir, os.path.normpath('2018/07/26/IMG_7383.PNG'))}",
self._caplog.text,
)
self.assertIn(
"INFO Deleting tests/fixtures/Photos/2018/07/12/IMG_7190.JPG",
f"INFO Deleting {os.path.join(base_dir, os.path.normpath('2018/07/12/IMG_7190.JPG'))}",
self._caplog.text,
)
self.assertIn(
"INFO Deleting tests/fixtures/Photos/2018/07/12/IMG_7190-medium.JPG",
f"INFO Deleting {os.path.join(base_dir, os.path.normpath('2018/07/12/IMG_7190-medium.JPG'))}",
self._caplog.text,
)

Expand Down
Loading

0 comments on commit 5f05dc9

Please sign in to comment.