Skip to content

Commit bc91a39

Browse files
author
Lukas Puehringer
committed
Test that image modifications are adopted
Test a recent change in `add_pep_image` that allows modifying images. Signed-off-by: Lukas Puehringer <lukas.puehringer@nyu.edu>
1 parent fa602ec commit bc91a39

File tree

1 file changed

+41
-1
lines changed

1 file changed

+41
-1
lines changed

peps/tests/test_converters.py

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
1+
import os
2+
import filecmp
3+
import shutil
4+
15
from django.test import TestCase, override_settings
26
from django.core.exceptions import ImproperlyConfigured
37
from django.test.utils import captured_stdout
8+
from django.conf import settings
49

5-
from peps.converters import get_pep0_page, get_pep_page, add_pep_image
10+
from peps.converters import get_pep0_page, get_pep_page, add_pep_image, pep_url
611

712
from . import FAKE_PEP_REPO
813

@@ -43,6 +48,41 @@ def test_add_image_not_found(self):
4348
r"Image Path '(.*)/path/that/does/not/exist(.*)' does not exist, skipping"
4449
)
4550

51+
def test_add_image_update(self):
52+
"""Test that image modifications are adopted. """
53+
pep = '3001'
54+
pep_img_name = 'pep-3001-1.png'
55+
pep_img_path_src = os.path.join(FAKE_PEP_REPO, pep_img_name)
56+
pep_img_path_src_backup = pep_img_path_src + ".backup"
57+
pep_img_path_dst = os.path.join(
58+
settings.MEDIA_ROOT, pep_url(pep), pep_img_name)
59+
60+
# We need to remove a prior uploaded file to fix an inconsistency
61+
# between db and filesystem.
62+
# TODO: This should be dealt with in page.model or converters module
63+
if os.path.exists(pep_img_path_dst):
64+
os.remove(pep_img_path_dst)
65+
66+
# Create a pep page and "upload" a file
67+
get_pep_page(FAKE_PEP_REPO, pep)
68+
image = add_pep_image(FAKE_PEP_REPO, pep, pep_img_name)
69+
70+
# The source and destination files should be the same
71+
self.assertTrue(filecmp.cmp(pep_img_path_src, pep_img_path_dst))
72+
73+
# Create a backup, modify the source file and "re-upload"
74+
shutil.copyfile(pep_img_path_src, pep_img_path_src_backup)
75+
with open(pep_img_path_src, "ab") as f:
76+
f.write(b"TEST")
77+
image = add_pep_image(FAKE_PEP_REPO, pep, pep_img_name)
78+
79+
# Again, modified src and re-uploaded dst files should be the same
80+
self.assertTrue(filecmp.cmp(pep_img_path_src, pep_img_path_dst))
81+
82+
# Restore source file
83+
shutil.move(pep_img_path_src_backup, pep_img_path_src)
84+
85+
4686
def test_html_do_not_prettify(self):
4787
pep = get_pep_page(FAKE_PEP_REPO, '3001')
4888
self.assertEqual(

0 commit comments

Comments
 (0)