Added unit tests for KittenGroomerBase

pull/7/head
Dan Puttick 2016-12-09 16:13:54 -05:00
parent 196a9fcac1
commit 0364e038e1
3 changed files with 107 additions and 57 deletions

View File

@ -258,7 +258,7 @@ class KittenGroomer(KittenGroomerBase):
self._safe_mkdir(tmpdir) self._safe_mkdir(tmpdir)
# The magic comes from here: http://svn.ghostscript.com/ghostscript/trunk/gs/doc/Ps2pdf.htm#PDFA # The magic comes from here: http://svn.ghostscript.com/ghostscript/trunk/gs/doc/Ps2pdf.htm#PDFA
curdir = os.getcwd() curdir = os.getcwd()
os.chdir(self.ressources_path) os.chdir(self.resources_path)
gs_command = '{} -dPDFA -dQUIET -dSAFER -dBATCH -dNOPAUSE -dNOOUTERSAVE -sProcessColorModel=DeviceCMYK -sDEVICE=pdfwrite -sPDFACompatibilityPolicy=1 -sOutputFile="{}" ./PDFA_def.ps "{}"'.format( gs_command = '{} -dPDFA -dQUIET -dSAFER -dBATCH -dNOPAUSE -dNOOUTERSAVE -sProcessColorModel=DeviceCMYK -sDEVICE=pdfwrite -sPDFACompatibilityPolicy=1 -sOutputFile="{}" ./PDFA_def.ps "{}"'.format(
GS, os.path.join(curdir, tmppath), os.path.join(curdir, self.cur_file.src_path)) GS, os.path.join(curdir, tmppath), os.path.join(curdir, self.cur_file.src_path))
self._run_process(gs_command) self._run_process(gs_command)

View File

@ -262,7 +262,7 @@ class KittenGroomerBase(object):
"""Create a separate file to hold this file's metadata.""" """Create a separate file to hold this file's metadata."""
dst = self.cur_file.dst_path dst = self.cur_file.dst_path
try: try:
if os.path.exists(self.cur_file.src_path + ext): if os.path.exists(self.cur_file.src_path + ext): # should we check dst_path as well?
raise KittenGroomerError("Cannot create split metadata file for \"" + raise KittenGroomerError("Cannot create split metadata file for \"" +
self.cur_file.dst_path + "\", type '" + self.cur_file.dst_path + "\", type '" +
ext + "': File exists.") ext + "': File exists.")

View File

@ -3,12 +3,11 @@
import os import os
import sys import sys
import tempfile
import pytest import pytest
from kittengroomer import FileBase from kittengroomer import FileBase, KittenGroomerBase
from kittengroomer import KittenGroomerBase from kittengroomer.helpers import ImplementationRequired
PY3 = sys.version_info.major == 3 PY3 = sys.version_info.major == 3
skip = pytest.mark.skip skip = pytest.mark.skip
@ -16,7 +15,7 @@ xfail = pytest.mark.xfail
fixture = pytest.fixture fixture = pytest.fixture
### FileBase # FileBase
class TestFileBase: class TestFileBase:
@ -39,7 +38,7 @@ class TestFileBase:
file_path = file_path.strpath file_path = file_path.strpath
symlink_path = tmpdir.join('symlinked.txt') symlink_path = tmpdir.join('symlinked.txt')
symlink_path = symlink_path.strpath symlink_path = symlink_path.strpath
file_symlink = os.symlink(file_path, symlink_path) os.symlink(file_path, symlink_path)
return FileBase(symlink_path, symlink_path) return FileBase(symlink_path, symlink_path)
@fixture @fixture
@ -59,17 +58,17 @@ class TestFileBase:
@fixture @fixture
def file_marked_dangerous(self, generic_conf_file): def file_marked_dangerous(self, generic_conf_file):
generic_conf_file.make_dangerous() generic_conf_file.make_dangerous()
return file return generic_conf_file
@fixture @fixture
def file_marked_unknown(self, generic_conf_file): def file_marked_unknown(self, generic_conf_file):
generic_conf_file.make_unknown() generic_conf_file.make_unknown()
return file return generic_conf_file
@fixture @fixture
def file_marked_binary(self, generic_conf_file): def file_marked_binary(self, generic_conf_file):
generic_conf_file.mark_binary() generic_conf_file.mark_binary()
return file return generic_conf_file
@fixture(params=[ @fixture(params=[
FileBase.make_dangerous, FileBase.make_dangerous,
@ -81,8 +80,8 @@ class TestFileBase:
return generic_conf_file return generic_conf_file
# What are the various things that can go wrong with file paths? We should have fixtures for them # What are the various things that can go wrong with file paths? We should have fixtures for them
# What should the object do if it's given a path that isn't a file? Currently magic throws an exception # What should FileBase do if it's given a path that isn't a file (doesn't exist or is a dir)? Currently magic throws an exception
# We should probably catch everytime that happens and tell the user what happened (and maybe put it in the log) # We should probably catch everytime that happens and tell the user explicitly happened (and maybe put it in the log)
def test_create(self): def test_create(self):
file = FileBase('tests/src_simple/blah.conf', '/tests/dst/blah.conf') file = FileBase('tests/src_simple/blah.conf', '/tests/dst/blah.conf')
@ -119,24 +118,39 @@ class TestFileBase:
assert generic_conf_file.mimetype == 'text/plain' assert generic_conf_file.mimetype == 'text/plain'
assert generic_conf_file.main_type == 'text' assert generic_conf_file.main_type == 'text'
assert generic_conf_file.sub_type == 'plain' assert generic_conf_file.sub_type == 'plain'
# FileBase(source_path) # Need to test something without a mimetype
# this is essentially testing the behavior of magic. I guess for now it's ok if we just test for some basic file types? # Need to test something that's a directory
# Need to test something that causes the unicode exception
def test_has_mimetype_no_main_type(self, generic_conf_file):
generic_conf_file.main_type = ''
assert generic_conf_file.has_mimetype() is False
def test_has_mimetype_no_sub_type(self, generic_conf_file):
generic_conf_file.sub_type = ''
assert generic_conf_file.has_mimetype() is False
def test_has_extension(self, temp_file, temp_file_no_ext): def test_has_extension(self, temp_file, temp_file_no_ext):
assert temp_file.has_extension() == True assert temp_file.has_extension() is True
assert temp_file_no_ext.has_extension() == False assert temp_file_no_ext.has_extension() is False
assert temp_file_no_ext.log_details.get('no_extension') == True assert temp_file_no_ext.log_details.get('no_extension') is True
def test_add_log_details(self, generic_conf_file):
generic_conf_file.add_log_details('test', True)
assert generic_conf_file.log_details['test'] is True
with pytest.raises(KeyError):
assert generic_conf_file.log_details['wrong'] is False
def test_marked_dangerous(self, file_marked_all_parameterized): def test_marked_dangerous(self, file_marked_all_parameterized):
file_marked_all_parameterized.make_dangerous() file_marked_all_parameterized.make_dangerous()
assert file_marked_all_parameterized.is_dangerous() == True assert file_marked_all_parameterized.is_dangerous() is True
# Should work regardless of weird paths?? # Should work regardless of weird paths??
# Should check file path alteration behavior as well # Should check file path alteration behavior as well
def test_generic_dangerous(self, generic_conf_file): def test_generic_dangerous(self, generic_conf_file):
assert generic_conf_file.is_dangerous() == False assert generic_conf_file.is_dangerous() is False
generic_conf_file.make_dangerous() generic_conf_file.make_dangerous()
assert generic_conf_file.is_dangerous() == True assert generic_conf_file.is_dangerous() is True
def test_has_symlink(self, tmpdir): def test_has_symlink(self, tmpdir):
file_path = tmpdir.join('test.txt') file_path = tmpdir.join('test.txt')
@ -147,42 +161,42 @@ class TestFileBase:
file_symlink = os.symlink(file_path, symlink_path) file_symlink = os.symlink(file_path, symlink_path)
file = FileBase(file_path, file_path) file = FileBase(file_path, file_path)
symlink = FileBase(symlink_path, symlink_path) symlink = FileBase(symlink_path, symlink_path)
assert file.is_symlink() == False assert file.is_symlink() is False
assert symlink.is_symlink() == True assert symlink.is_symlink() is True
def test_has_symlink_fixture(self, symlink): def test_has_symlink_fixture(self, symlink):
assert symlink.is_symlink() == True assert symlink.is_symlink() is True
def test_generic_make_unknown(self, generic_conf_file): def test_generic_make_unknown(self, generic_conf_file):
assert generic_conf_file.log_details.get('unknown') == None assert generic_conf_file.log_details.get('unknown') is None
generic_conf_file.make_unknown() generic_conf_file.make_unknown()
assert generic_conf_file.log_details.get('unknown') == True assert generic_conf_file.log_details.get('unknown') is True
# given a FileBase object with no marking, should do the right things # given a FileBase object with no marking, should do the right things
def test_marked_make_unknown(self, file_marked_all_parameterized): def test_marked_make_unknown(self, file_marked_all_parameterized):
file = file_marked_all_parameterized file = file_marked_all_parameterized
if file.log_details.get('unknown'): if file.log_details.get('unknown'):
file.make_unknown() file.make_unknown()
assert file.log_details.get('unknown') == True assert file.log_details.get('unknown') is True
else: else:
assert file.log_details.get('unknown') == None assert file.log_details.get('unknown') is None
file.make_unknown() file.make_unknown()
assert file.log_details.get('unknown') == None assert file.log_details.get('unknown') is None
# given a FileBase object with an unrecognized marking, should ??? # given a FileBase object with an unrecognized marking, should ???
def test_generic_make_binary(self, generic_conf_file): def test_generic_make_binary(self, generic_conf_file):
assert generic_conf_file.log_details.get('binary') == None assert generic_conf_file.log_details.get('binary') is None
generic_conf_file.make_binary() generic_conf_file.make_binary()
assert generic_conf_file.log_details.get('binary') == True assert generic_conf_file.log_details.get('binary') is True
def test_marked_make_binary(self, file_marked_all_parameterized): def test_marked_make_binary(self, file_marked_all_parameterized):
file = file_marked_all_parameterized file = file_marked_all_parameterized
if file.log_details.get('dangerous'): if file.log_details.get('dangerous'):
file.make_binary() file.make_binary()
assert file.log_details.get('binary') == None assert file.log_details.get('binary') is None
else: else:
file.make_binary() file.make_binary()
assert file.log_details.get('binary') == True assert file.log_details.get('binary') is True
def test_force_ext_change(self, generic_conf_file): def test_force_ext_change(self, generic_conf_file):
assert generic_conf_file.has_extension() assert generic_conf_file.has_extension()
@ -190,7 +204,7 @@ class TestFileBase:
assert os.path.splitext(generic_conf_file.dst_path)[1] == '.conf' assert os.path.splitext(generic_conf_file.dst_path)[1] == '.conf'
generic_conf_file.force_ext('.txt') generic_conf_file.force_ext('.txt')
assert os.path.splitext(generic_conf_file.dst_path)[1] == '.txt' assert os.path.splitext(generic_conf_file.dst_path)[1] == '.txt'
assert generic_conf_file.log_details.get('force_ext') == True assert generic_conf_file.log_details.get('force_ext') is True
# should make a file's extension change # should make a file's extension change
# should be able to handle weird paths # should be able to handle weird paths
@ -199,45 +213,81 @@ class TestFileBase:
assert generic_conf_file.extension == '.conf' assert generic_conf_file.extension == '.conf'
generic_conf_file.force_ext('.conf') generic_conf_file.force_ext('.conf')
assert os.path.splitext(generic_conf_file.dst_path)[1] == '.conf' assert os.path.splitext(generic_conf_file.dst_path)[1] == '.conf'
assert generic_conf_file.log_details.get('force_ext') == None assert generic_conf_file.log_details.get('force_ext') is None
# shouldn't change a file's extension if it already is right # shouldn't change a file's extension if it already is right
class TestKittenGroomerBase: class TestKittenGroomerBase:
@fixture @fixture
def generic_groomer(self): def source_directory(self):
return KittenGroomerBase('tests/src_complex', 'tests/dst') return 'tests/src_complex'
@fixture
def dest_directory(self):
return 'tests/dst'
@fixture
def generic_groomer(self, source_directory, dest_directory):
return KittenGroomerBase(source_directory, dest_directory)
def test_create(self, generic_groomer): def test_create(self, generic_groomer):
assert generic_groomer assert generic_groomer
def test_instantiation(self): def test_instantiation(self, source_directory, dest_directory):
# src_path and dest_path groomer = KittenGroomerBase(source_directory, dest_directory)
# what if the log file already exists? debug_groomer = KittenGroomerBase(source_directory,
# we should probably protect access to self.current_file in some way? dest_directory,
pass debug=True)
# we should maybe protect access to self.current_file in some way?
def test_computehash(self, tmpdir):
file = tmpdir.join('test.txt')
file.write('testing')
simple_groomer = KittenGroomerBase(tmpdir.strpath, tmpdir.strpath)
simple_groomer._computehash(file.strpath)
def test_computehash(self): def test_tree(self, generic_groomer):
# what are the ways this could go wrong? Should give the same hash every time? generic_groomer.tree(generic_groomer.src_root_dir)
# what is buf doing here?
pass
def test_safe_copy(self, tmpdir):
def test_safe_copy(self): file = tmpdir.join('test.txt')
file.write('testing')
testdir = tmpdir.join('testdir')
os.mkdir(testdir.strpath)
filedest = testdir.join('test.txt')
simple_groomer = KittenGroomerBase(tmpdir.strpath, testdir.strpath)
simple_groomer.cur_file = FileBase(file.strpath, filedest.strpath)
assert simple_groomer._safe_copy() is True
#check that it handles weird file path inputs #check that it handles weird file path inputs
pass
def test_safe_metadata_split(self, tmpdir):
def test_safe_metadata_split(self): file = tmpdir.join('test.txt')
file.write('testing')
simple_groomer = KittenGroomerBase(tmpdir.strpath, tmpdir.strpath)
simple_groomer.cur_file = FileBase(file.strpath, file.strpath)
metadata_file = simple_groomer._safe_metadata_split('metadata.log')
metadata_file.write('Have some metadata!')
metadata_file.close()
assert simple_groomer._safe_metadata_split('') is False
# if metadata file already exists # if metadata file already exists
# if there is no metadata to write should this work? # if there is no metadata to write should this work?
# check that returned file is writable?
pass
def test_list_all_files(self, tmpdir):
file = tmpdir.join('test.txt')
file.write('testing')
testdir = tmpdir.join('testdir')
os.mkdir(testdir.strpath)
simple_groomer = KittenGroomerBase(tmpdir.strpath, tmpdir.strpath)
files = simple_groomer._list_all_files(simple_groomer.src_root_dir)
assert file.strpath in files
assert testdir.strpath not in files
def test_list_all_files(self): def test_print_log(self, generic_groomer):
# various possible types of directories with pytest.raises(AttributeError):
# invalid directory generic_groomer._print_log()
pass # Kind of a bad test, but this should be implemented by the user anyway
def test_processdir(self, generic_groomer):
with pytest.raises(ImplementationRequired):
generic_groomer.processdir()