2016-12-05 17:19:39 +01:00
|
|
|
#!/usr/bin/env python
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
2016-12-09 02:12:08 +01:00
|
|
|
import os
|
|
|
|
import sys
|
|
|
|
|
2016-12-05 17:19:39 +01:00
|
|
|
import pytest
|
|
|
|
|
2016-12-09 22:13:54 +01:00
|
|
|
from kittengroomer import FileBase, KittenGroomerBase
|
|
|
|
from kittengroomer.helpers import ImplementationRequired
|
2016-12-05 17:19:39 +01:00
|
|
|
|
2016-12-09 02:12:08 +01:00
|
|
|
PY3 = sys.version_info.major == 3
|
|
|
|
skip = pytest.mark.skip
|
|
|
|
xfail = pytest.mark.xfail
|
|
|
|
fixture = pytest.fixture
|
2016-12-05 17:19:39 +01:00
|
|
|
|
|
|
|
|
2016-12-09 22:13:54 +01:00
|
|
|
# FileBase
|
2016-12-05 17:19:39 +01:00
|
|
|
|
2016-12-09 02:12:08 +01:00
|
|
|
class TestFileBase:
|
2016-12-05 17:19:39 +01:00
|
|
|
|
2016-12-09 02:12:08 +01:00
|
|
|
@fixture
|
|
|
|
def source_file(self):
|
2016-12-24 22:46:22 +01:00
|
|
|
return 'tests/src_valid/blah.conf'
|
2016-12-09 02:12:08 +01:00
|
|
|
|
|
|
|
@fixture
|
|
|
|
def dest_file(self):
|
|
|
|
return 'tests/dst/blah.conf'
|
|
|
|
|
|
|
|
@fixture
|
|
|
|
def generic_conf_file(self, source_file, dest_file):
|
|
|
|
return FileBase(source_file, dest_file)
|
|
|
|
|
|
|
|
@fixture
|
|
|
|
def symlink(self, tmpdir):
|
|
|
|
file_path = tmpdir.join('test.txt')
|
|
|
|
file_path.write('testing')
|
|
|
|
file_path = file_path.strpath
|
|
|
|
symlink_path = tmpdir.join('symlinked.txt')
|
|
|
|
symlink_path = symlink_path.strpath
|
2016-12-09 22:13:54 +01:00
|
|
|
os.symlink(file_path, symlink_path)
|
2016-12-09 02:12:08 +01:00
|
|
|
return FileBase(symlink_path, symlink_path)
|
|
|
|
|
|
|
|
@fixture
|
|
|
|
def temp_file(self, tmpdir):
|
|
|
|
file_path = tmpdir.join('test.txt')
|
|
|
|
file_path.write('testing')
|
|
|
|
file_path = file_path.strpath
|
|
|
|
return FileBase(file_path, file_path)
|
|
|
|
|
|
|
|
@fixture
|
|
|
|
def temp_file_no_ext(self, tmpdir):
|
|
|
|
file_path = tmpdir.join('test')
|
|
|
|
file_path.write('testing')
|
|
|
|
file_path = file_path.strpath
|
|
|
|
return FileBase(file_path, file_path)
|
|
|
|
|
|
|
|
@fixture
|
|
|
|
def file_marked_dangerous(self, generic_conf_file):
|
|
|
|
generic_conf_file.make_dangerous()
|
2016-12-09 22:13:54 +01:00
|
|
|
return generic_conf_file
|
2016-12-09 02:12:08 +01:00
|
|
|
|
|
|
|
@fixture
|
|
|
|
def file_marked_unknown(self, generic_conf_file):
|
|
|
|
generic_conf_file.make_unknown()
|
2016-12-09 22:13:54 +01:00
|
|
|
return generic_conf_file
|
2016-12-09 02:12:08 +01:00
|
|
|
|
|
|
|
@fixture
|
|
|
|
def file_marked_binary(self, generic_conf_file):
|
|
|
|
generic_conf_file.mark_binary()
|
2016-12-09 22:13:54 +01:00
|
|
|
return generic_conf_file
|
2016-12-09 02:12:08 +01:00
|
|
|
|
|
|
|
@fixture(params=[
|
|
|
|
FileBase.make_dangerous,
|
|
|
|
FileBase.make_unknown,
|
|
|
|
FileBase.make_binary
|
|
|
|
])
|
|
|
|
def file_marked_all_parameterized(self, request, generic_conf_file):
|
|
|
|
request.param(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
|
2016-12-09 22:13:54 +01:00
|
|
|
# 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 explicitly happened (and maybe put it in the log)
|
2016-12-09 02:12:08 +01:00
|
|
|
|
|
|
|
def test_create(self):
|
2016-12-24 22:46:22 +01:00
|
|
|
file = FileBase('tests/src_valid/blah.conf', '/tests/dst/blah.conf')
|
2016-12-09 02:12:08 +01:00
|
|
|
|
|
|
|
def test_create_broken(self, tmpdir):
|
|
|
|
with pytest.raises(TypeError):
|
|
|
|
file_no_args = FileBase()
|
|
|
|
if PY3:
|
|
|
|
with pytest.raises(FileNotFoundError):
|
|
|
|
file_empty_args = FileBase('', '')
|
|
|
|
else:
|
|
|
|
with pytest.raises(IOError):
|
|
|
|
file_empty_args = FileBase('', '')
|
|
|
|
if PY3:
|
|
|
|
with pytest.raises(IsADirectoryError):
|
|
|
|
file_directory = FileBase(tmpdir.strpath, tmpdir.strpath)
|
|
|
|
else:
|
|
|
|
with pytest.raises(IOError):
|
|
|
|
file_directory = FileBase(tmpdir.strpath, tmpdir.strpath)
|
|
|
|
# are there other cases here? path to a file that doesn't exist? permissions?
|
|
|
|
|
|
|
|
def test_init(self, generic_conf_file):
|
|
|
|
file = generic_conf_file
|
|
|
|
assert file.log_details
|
|
|
|
assert file.log_details['filepath'] == file.src_path
|
|
|
|
assert file.extension == '.conf'
|
|
|
|
copied_log = file.log_details.copy()
|
|
|
|
file.log_details = ''
|
|
|
|
# assert file.log_details == copied_log # this fails for now, we need to make log_details undeletable
|
|
|
|
# we should probably check for more extensions here
|
|
|
|
|
2016-12-22 16:12:13 +01:00
|
|
|
def test_extension_uppercase(self, tmpdir):
|
|
|
|
file_path = tmpdir.join('TEST.TXT')
|
|
|
|
file_path.write('testing')
|
|
|
|
file_path = file_path.strpath
|
|
|
|
file = FileBase(file_path, file_path)
|
|
|
|
assert file.extension == '.txt'
|
|
|
|
|
2016-12-09 02:12:08 +01:00
|
|
|
def test_mimetypes(self, generic_conf_file):
|
|
|
|
assert generic_conf_file.has_mimetype()
|
|
|
|
assert generic_conf_file.mimetype == 'text/plain'
|
|
|
|
assert generic_conf_file.main_type == 'text'
|
|
|
|
assert generic_conf_file.sub_type == 'plain'
|
2016-12-09 22:13:54 +01:00
|
|
|
# Need to test something without a mimetype
|
|
|
|
# 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
|
2016-12-09 02:12:08 +01:00
|
|
|
|
|
|
|
def test_has_extension(self, temp_file, temp_file_no_ext):
|
2016-12-09 22:13:54 +01:00
|
|
|
assert temp_file.has_extension() is True
|
|
|
|
assert temp_file_no_ext.has_extension() is False
|
|
|
|
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
|
2016-12-09 02:12:08 +01:00
|
|
|
|
|
|
|
def test_marked_dangerous(self, file_marked_all_parameterized):
|
|
|
|
file_marked_all_parameterized.make_dangerous()
|
2016-12-09 22:13:54 +01:00
|
|
|
assert file_marked_all_parameterized.is_dangerous() is True
|
2016-12-05 17:19:39 +01:00
|
|
|
# Should work regardless of weird paths??
|
2016-12-09 02:12:08 +01:00
|
|
|
# Should check file path alteration behavior as well
|
|
|
|
|
|
|
|
def test_generic_dangerous(self, generic_conf_file):
|
2016-12-09 22:13:54 +01:00
|
|
|
assert generic_conf_file.is_dangerous() is False
|
2016-12-09 02:12:08 +01:00
|
|
|
generic_conf_file.make_dangerous()
|
2016-12-09 22:13:54 +01:00
|
|
|
assert generic_conf_file.is_dangerous() is True
|
2016-12-09 02:12:08 +01:00
|
|
|
|
|
|
|
def test_has_symlink(self, tmpdir):
|
|
|
|
file_path = tmpdir.join('test.txt')
|
|
|
|
file_path.write('testing')
|
|
|
|
file_path = file_path.strpath
|
|
|
|
symlink_path = tmpdir.join('symlinked.txt')
|
|
|
|
symlink_path = symlink_path.strpath
|
|
|
|
file_symlink = os.symlink(file_path, symlink_path)
|
|
|
|
file = FileBase(file_path, file_path)
|
|
|
|
symlink = FileBase(symlink_path, symlink_path)
|
2016-12-09 22:13:54 +01:00
|
|
|
assert file.is_symlink() is False
|
|
|
|
assert symlink.is_symlink() is True
|
2016-12-09 02:12:08 +01:00
|
|
|
|
|
|
|
def test_has_symlink_fixture(self, symlink):
|
2016-12-09 22:13:54 +01:00
|
|
|
assert symlink.is_symlink() is True
|
2016-12-09 02:12:08 +01:00
|
|
|
|
|
|
|
def test_generic_make_unknown(self, generic_conf_file):
|
2016-12-09 22:13:54 +01:00
|
|
|
assert generic_conf_file.log_details.get('unknown') is None
|
2016-12-09 02:12:08 +01:00
|
|
|
generic_conf_file.make_unknown()
|
2016-12-09 22:13:54 +01:00
|
|
|
assert generic_conf_file.log_details.get('unknown') is True
|
2016-12-05 17:19:39 +01:00
|
|
|
# given a FileBase object with no marking, should do the right things
|
|
|
|
|
2016-12-09 02:12:08 +01:00
|
|
|
def test_marked_make_unknown(self, file_marked_all_parameterized):
|
|
|
|
file = file_marked_all_parameterized
|
|
|
|
if file.log_details.get('unknown'):
|
|
|
|
file.make_unknown()
|
2016-12-09 22:13:54 +01:00
|
|
|
assert file.log_details.get('unknown') is True
|
2016-12-09 02:12:08 +01:00
|
|
|
else:
|
2016-12-09 22:13:54 +01:00
|
|
|
assert file.log_details.get('unknown') is None
|
2016-12-09 02:12:08 +01:00
|
|
|
file.make_unknown()
|
2016-12-09 22:13:54 +01:00
|
|
|
assert file.log_details.get('unknown') is None
|
2016-12-09 02:12:08 +01:00
|
|
|
# given a FileBase object with an unrecognized marking, should ???
|
2016-12-05 17:19:39 +01:00
|
|
|
|
2016-12-09 02:12:08 +01:00
|
|
|
def test_generic_make_binary(self, generic_conf_file):
|
2016-12-09 22:13:54 +01:00
|
|
|
assert generic_conf_file.log_details.get('binary') is None
|
2016-12-09 02:12:08 +01:00
|
|
|
generic_conf_file.make_binary()
|
2016-12-09 22:13:54 +01:00
|
|
|
assert generic_conf_file.log_details.get('binary') is True
|
2016-12-09 02:12:08 +01:00
|
|
|
|
|
|
|
def test_marked_make_binary(self, file_marked_all_parameterized):
|
|
|
|
file = file_marked_all_parameterized
|
|
|
|
if file.log_details.get('dangerous'):
|
|
|
|
file.make_binary()
|
2016-12-09 22:13:54 +01:00
|
|
|
assert file.log_details.get('binary') is None
|
2016-12-09 02:12:08 +01:00
|
|
|
else:
|
|
|
|
file.make_binary()
|
2016-12-09 22:13:54 +01:00
|
|
|
assert file.log_details.get('binary') is True
|
2016-12-09 02:12:08 +01:00
|
|
|
|
|
|
|
def test_force_ext_change(self, generic_conf_file):
|
|
|
|
assert generic_conf_file.has_extension()
|
|
|
|
assert generic_conf_file.extension == '.conf'
|
|
|
|
assert os.path.splitext(generic_conf_file.dst_path)[1] == '.conf'
|
|
|
|
generic_conf_file.force_ext('.txt')
|
|
|
|
assert os.path.splitext(generic_conf_file.dst_path)[1] == '.txt'
|
2016-12-09 22:13:54 +01:00
|
|
|
assert generic_conf_file.log_details.get('force_ext') is True
|
2016-12-05 17:19:39 +01:00
|
|
|
# should make a file's extension change
|
2016-12-09 02:12:08 +01:00
|
|
|
# should be able to handle weird paths
|
|
|
|
|
|
|
|
def test_force_ext_correct(self, generic_conf_file):
|
|
|
|
assert generic_conf_file.has_extension()
|
|
|
|
assert generic_conf_file.extension == '.conf'
|
|
|
|
generic_conf_file.force_ext('.conf')
|
|
|
|
assert os.path.splitext(generic_conf_file.dst_path)[1] == '.conf'
|
2016-12-09 22:13:54 +01:00
|
|
|
assert generic_conf_file.log_details.get('force_ext') is None
|
2016-12-05 17:19:39 +01:00
|
|
|
# shouldn't change a file's extension if it already is right
|
|
|
|
|
|
|
|
|
|
|
|
class TestKittenGroomerBase:
|
2016-12-09 02:12:08 +01:00
|
|
|
|
2016-12-09 22:13:54 +01:00
|
|
|
@fixture
|
|
|
|
def source_directory(self):
|
2016-12-24 22:46:22 +01:00
|
|
|
return 'tests/src_invalid'
|
2016-12-05 17:19:39 +01:00
|
|
|
|
2016-12-09 22:13:54 +01:00
|
|
|
@fixture
|
|
|
|
def dest_directory(self):
|
|
|
|
return 'tests/dst'
|
2016-12-05 17:19:39 +01:00
|
|
|
|
2016-12-09 22:13:54 +01:00
|
|
|
@fixture
|
|
|
|
def generic_groomer(self, source_directory, dest_directory):
|
|
|
|
return KittenGroomerBase(source_directory, dest_directory)
|
2016-12-05 17:19:39 +01:00
|
|
|
|
2016-12-09 22:13:54 +01:00
|
|
|
def test_create(self, generic_groomer):
|
|
|
|
assert generic_groomer
|
2016-12-05 17:19:39 +01:00
|
|
|
|
2016-12-09 22:13:54 +01:00
|
|
|
def test_instantiation(self, source_directory, dest_directory):
|
|
|
|
groomer = KittenGroomerBase(source_directory, dest_directory)
|
|
|
|
debug_groomer = KittenGroomerBase(source_directory,
|
|
|
|
dest_directory,
|
|
|
|
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_tree(self, generic_groomer):
|
|
|
|
generic_groomer.tree(generic_groomer.src_root_dir)
|
|
|
|
|
|
|
|
def test_safe_copy(self, tmpdir):
|
|
|
|
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
|
2016-12-05 17:19:39 +01:00
|
|
|
#check that it handles weird file path inputs
|
|
|
|
|
2016-12-09 22:13:54 +01:00
|
|
|
def test_safe_metadata_split(self, tmpdir):
|
|
|
|
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
|
2016-12-05 17:19:39 +01:00
|
|
|
# if metadata file already exists
|
|
|
|
# if there is no metadata to write should this work?
|
|
|
|
|
2016-12-09 22:13:54 +01:00
|
|
|
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_print_log(self, generic_groomer):
|
|
|
|
with pytest.raises(AttributeError):
|
|
|
|
generic_groomer._print_log()
|
|
|
|
# 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()
|