PyCIRCLean/tests/test_kittengroomer.py

206 lines
6.9 KiB
Python
Raw Normal View History

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
2016-12-05 17:19:39 +01:00
import pytest
from kittengroomer import FileBase, KittenGroomerBase
2016-12-05 17:19:39 +01:00
2016-12-09 02:12:08 +01:00
skip = pytest.mark.skip
xfail = pytest.mark.xfail
fixture = pytest.fixture
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 symlink_file(self, tmpdir):
2016-12-09 02:12:08 +01:00
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):
pass
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
2016-12-09 02:12:08 +01:00
def test_init_identify_filename(self):
"""Init should identify the filename correctly for src_path."""
pass
def test_init_identify_extension(self):
"""Init should identify the extension for src_path."""
pass
def test_init_file_doesnt_exist(self):
"""Init should raise an exception if the file doesn't exist."""
pass
def test_init_srcpath_is_directory(self):
"""Init should raise an exception if given a path to a directory."""
def test_init_symlink(self):
"""Init should properly identify symlinks."""
pass
2016-12-09 02:12:08 +01:00
def test_create_broken(self, tmpdir):
with pytest.raises(TypeError):
FileBase()
2017-01-19 23:06:34 +01:00
with pytest.raises(FileNotFoundError):
FileBase('', '')
2017-01-19 23:06:34 +01:00
with pytest.raises(IsADirectoryError):
FileBase(tmpdir.strpath, tmpdir.strpath)
# TODO: are there other cases here? path to a file that doesn't exist? permissions?
2016-12-09 02:12:08 +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'
def test_mimetypes(self, temp_file):
assert temp_file.mimetype == 'text/plain'
assert temp_file.maintype == 'text'
assert temp_file.subtype == 'plain'
assert temp_file.has_mimetype
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
@skip
2016-12-09 22:13:54 +01:00
def test_has_mimetype_no_main_type(self, generic_conf_file):
generic_conf_file.maintype = ''
2017-03-10 19:13:38 +01:00
assert generic_conf_file.has_mimetype is False
2016-12-09 22:13:54 +01:00
@skip
2016-12-09 22:13:54 +01:00
def test_has_mimetype_no_sub_type(self, generic_conf_file):
generic_conf_file.sub_type = ''
2017-03-10 19:13:38 +01:00
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):
2017-03-10 19:13:38 +01:00
assert temp_file.has_extension is True
print(temp_file_no_ext.extension)
2017-03-10 19:13:38 +01:00
assert temp_file_no_ext.has_extension is False
2016-12-09 22:13:54 +01:00
def test_set_property(self, temp_file):
temp_file.set_property('test', True)
assert temp_file.get_property('test') is True
# TODO: split asserts into two tests
assert temp_file.get_property('wrong') is None
2016-12-09 02:12:08 +01:00
@skip
2016-12-09 02:12:08 +01:00
def test_marked_dangerous(self, file_marked_all_parameterized):
file_marked_all_parameterized.make_dangerous()
2017-03-10 19:13:38 +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
@xfail
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
os.symlink(file_path, symlink_path)
2016-12-09 02:12:08 +01:00
file = FileBase(file_path, file_path)
symlink = FileBase(symlink_path, symlink_path)
2017-03-10 19:13:38 +01:00
assert file.is_symlink is False
assert symlink.is_symlink is True
2016-12-09 02:12:08 +01:00
@xfail
def test_has_symlink_fixture(self, symlink_file):
2017-03-10 19:13:38 +01:00
assert symlink_file.is_symlink is True
2016-12-09 02:12:08 +01:00
@skip
2016-12-09 02:12:08 +01:00
def test_force_ext_change(self, generic_conf_file):
2017-03-10 19:13:38 +01:00
assert generic_conf_file.has_extension
assert generic_conf_file.get_property('extension') == '.conf'
2016-12-09 02:12:08 +01:00
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'
assert generic_conf_file.get_property('extension') == '.txt'
2016-12-09 02:12:08 +01:00
# should be able to handle weird paths
@skip
2016-12-09 02:12:08 +01:00
def test_force_ext_correct(self, generic_conf_file):
2017-03-10 19:13:38 +01:00
assert generic_conf_file.has_extension
assert generic_conf_file.get_property('extension') == '.conf'
2016-12-09 02:12:08 +01:00
generic_conf_file.force_ext('.conf')
assert os.path.splitext(generic_conf_file.dst_path)[1] == '.conf'
assert generic_conf_file.get_property('force_ext') is None
2016-12-05 17:19:39 +01:00
# shouldn't change a file's extension if it already is right
@xfail
2017-02-16 23:27:00 +01:00
def test_create_metadata_file(self, temp_file):
metadata_file_path = temp_file.create_metadata_file('.metadata.txt')
with open(metadata_file_path, 'w+') as metadata_file:
metadata_file.write('Have some metadata!')
# Shouldn't be able to make a metadata file with no extension
assert temp_file.create_metadata_file('') is False
# if metadata file already exists
# if there is no metadata to write should this work?
def test_safe_copy(self):
pass
2017-03-10 19:13:38 +01:00
2016-12-05 17:19:39 +01:00
@skip
2016-12-05 17:19:39 +01:00
class TestKittenGroomerBase:
2016-12-09 02:12:08 +01:00
2016-12-09 22:13:54 +01:00
@fixture
def source_directory(self):
return 'tests/dangerous'
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):
KittenGroomerBase(source_directory, dest_directory)
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)
2017-03-21 00:39:37 +01:00
files = simple_groomer.list_all_files(simple_groomer.src_root_path)
2016-12-09 22:13:54 +01:00
assert file.strpath in files
assert testdir.strpath not in files