gh-21 Add checksum assertions to ensure that FileSystem.file_process does not modify file

This commit is contained in:
Jaisen Mathai 2015-12-09 20:22:56 -08:00
parent 3a916aa589
commit 41c9446a9a
1 changed files with 36 additions and 4 deletions

View File

@ -2,6 +2,7 @@
import os import os
import sys import sys
import hashlib
import random import random
import re import re
import shutil import shutil
@ -170,9 +171,14 @@ def test_process_file_plain():
media = Photo(origin) media = Photo(origin)
destination = filesystem.process_file(origin, temporary_folder, media, allowDuplicate=True) destination = filesystem.process_file(origin, temporary_folder, media, allowDuplicate=True)
origin_checksum = checksum(origin)
destination_checksum = checksum(destination)
shutil.rmtree(folder) shutil.rmtree(folder)
shutil.rmtree(os.path.dirname(os.path.dirname(destination))) shutil.rmtree(os.path.dirname(os.path.dirname(destination)))
assert origin_checksum is not None
assert origin_checksum == destination_checksum
assert '2015-12-Dec/Unknown Location/2015-12-05_00-59-26-plain.jpg' in destination assert '2015-12-Dec/Unknown Location/2015-12-05_00-59-26-plain.jpg' in destination
def test_process_file_with_title(): def test_process_file_with_title():
@ -186,9 +192,14 @@ def test_process_file_with_title():
media = Photo(origin) media = Photo(origin)
destination = filesystem.process_file(origin, temporary_folder, media, allowDuplicate=True) destination = filesystem.process_file(origin, temporary_folder, media, allowDuplicate=True)
origin_checksum = checksum(origin)
destination_checksum = checksum(destination)
shutil.rmtree(folder) shutil.rmtree(folder)
shutil.rmtree(os.path.dirname(os.path.dirname(destination))) shutil.rmtree(os.path.dirname(os.path.dirname(destination)))
assert origin_checksum is not None
assert origin_checksum == destination_checksum
assert '2015-12-Dec/Unknown Location/2015-12-05_00-59-26-plain-some-title.jpg' in destination assert '2015-12-Dec/Unknown Location/2015-12-05_00-59-26-plain-some-title.jpg' in destination
def test_process_file_with_location(): def test_process_file_with_location():
@ -202,9 +213,14 @@ def test_process_file_with_location():
media = Photo(origin) media = Photo(origin)
destination = filesystem.process_file(origin, temporary_folder, media, allowDuplicate=True) destination = filesystem.process_file(origin, temporary_folder, media, allowDuplicate=True)
origin_checksum = checksum(origin)
destination_checksum = checksum(destination)
shutil.rmtree(folder) shutil.rmtree(folder)
shutil.rmtree(os.path.dirname(os.path.dirname(destination))) shutil.rmtree(os.path.dirname(os.path.dirname(destination)))
assert origin_checksum is not None
assert origin_checksum == destination_checksum
assert '2015-12-Dec/Sunnyvale/2015-12-05_00-59-26-plain.jpg' in destination assert '2015-12-Dec/Sunnyvale/2015-12-05_00-59-26-plain.jpg' in destination
def test_process_file_with_location_and_title(): def test_process_file_with_location_and_title():
@ -218,11 +234,31 @@ def test_process_file_with_location_and_title():
media = Photo(origin) media = Photo(origin)
destination = filesystem.process_file(origin, temporary_folder, media, allowDuplicate=True) destination = filesystem.process_file(origin, temporary_folder, media, allowDuplicate=True)
origin_checksum = checksum(origin)
destination_checksum = checksum(destination)
shutil.rmtree(folder) shutil.rmtree(folder)
shutil.rmtree(os.path.dirname(os.path.dirname(destination))) shutil.rmtree(os.path.dirname(os.path.dirname(destination)))
assert origin_checksum is not None
assert origin_checksum == destination_checksum
assert '2015-12-Dec/Sunnyvale/2015-12-05_00-59-26-plain-some-title.jpg' in destination assert '2015-12-Dec/Sunnyvale/2015-12-05_00-59-26-plain-some-title.jpg' in destination
def checksum(file_path, blocksize=65536):
hasher = hashlib.sha256()
with open(file_path, 'r') as f:
buf = f.read(blocksize)
while len(buf) > 0:
hasher.update(buf)
buf = f.read(blocksize)
return hasher.hexdigest()
return None
def get_file(name):
current_folder = os.path.dirname(os.path.realpath(__file__))
return '%s/files/%s' % (current_folder, name)
def populate_folder(number_of_files): def populate_folder(number_of_files):
folder = '%s/%s' % (tempfile.gettempdir(), random_string(10)) folder = '%s/%s' % (tempfile.gettempdir(), random_string(10))
os.makedirs(folder) os.makedirs(folder)
@ -235,9 +271,5 @@ def populate_folder(number_of_files):
return folder return folder
def get_file(name):
current_folder = os.path.dirname(os.path.realpath(__file__))
return '%s/files/%s' % (current_folder, name)
def random_string(length): def random_string(length):
return ''.join(random.SystemRandom().choice(string.ascii_uppercase + string.digits) for _ in range(length)) return ''.join(random.SystemRandom().choice(string.ascii_uppercase + string.digits) for _ in range(length))