PyCIRCLean/tests/test_kittengroomer.py

228 lines
7.1 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
2017-07-13 23:01:02 +02:00
import unittest.mock as mock
2016-12-09 02:12:08 +01:00
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:
2017-07-14 20:06:55 +02:00
# Fixtures
2016-12-05 17:19:39 +01:00
2017-07-13 23:01:02 +02:00
@fixture(scope='class')
def dest_dir_path(self, tmpdir_factory):
return tmpdir_factory.mktemp('dest').strpath
@fixture(scope='class')
def src_dir_path(self, tmpdir_factory):
return tmpdir_factory.mktemp('src').strpath
2016-12-09 02:12:08 +01:00
@fixture
2017-07-13 23:36:43 +02:00
def symlink_file_path(self, tmpdir, tmpfile_path):
symlink_path = tmpdir.join('symlinked')
2016-12-09 02:12:08 +01:00
symlink_path = symlink_path.strpath
2017-07-13 23:36:43 +02:00
os.symlink(tmpfile_path, symlink_path)
return symlink_path
2016-12-09 02:12:08 +01:00
@fixture
2017-07-13 23:36:43 +02:00
def tmpfile_path(self, tmpdir):
file_path = tmpdir.join('test.txt')
2016-12-09 02:12:08 +01:00
file_path.write('testing')
2017-07-13 23:36:43 +02:00
return file_path.strpath
2016-12-09 02:12:08 +01:00
@fixture
2017-07-13 23:36:43 +02:00
def tmpfile(self, src_dir_path, dst_dir_path):
file_path = os.path.join(src_dir_path, 'test.txt')
2016-12-09 02:12:08 +01:00
file_path.write('testing')
2017-07-13 23:36:43 +02:00
return FileBase(file_path, dst_dir_path)
2016-12-09 02:12:08 +01:00
@fixture
def file_marked_dangerous(self):
pass
2016-12-09 02:12:08 +01:00
2017-07-14 20:06:55 +02:00
# Constructor behavior
2017-07-13 23:01:02 +02:00
@mock.patch('kittengroomer.helpers.magic')
2017-07-13 23:36:43 +02:00
def test_init_identify_filename(self, mock_libmagic):
"""Init should identify the filename correctly for src_path."""
2017-07-13 23:01:02 +02:00
src_path = 'src/test.txt'
dst_path = 'dst/test.txt'
file = FileBase(src_path, dst_path)
assert file.filename == 'test.txt'
2017-07-13 23:36:43 +02:00
@mock.patch('kittengroomer.helpers.magic')
def test_init_identify_extension(self, mock_libmagic):
"""Init should identify the extension for src_path."""
2017-07-13 23:36:43 +02:00
src_path = 'src/test.txt'
dst_path = 'dst/test.txt'
file = FileBase(src_path, dst_path)
assert file.extension == '.txt'
2017-07-13 23:36:43 +02:00
@mock.patch('kittengroomer.helpers.magic')
def test_init_uppercase_extension(self, mock_libmagic):
2017-07-13 18:56:45 +02:00
"""Init should coerce uppercase extension to lowercase"""
2017-07-13 23:36:43 +02:00
src_path = 'src/TEST.TXT'
dst_path = 'dst/TEST.TXT'
file = FileBase(src_path, dst_path)
assert file.extension == '.txt'
2017-07-13 18:56:45 +02:00
2017-07-14 20:06:55 +02:00
@mock.patch('kittengroomer.helpers.magic')
def test_has_extension_true(self, mock_libmagic):
"""If the file has an extension, has_extension should == True."""
src_path = 'src/test.txt'
dst_path = 'dst/test.txt'
file = FileBase(src_path, dst_path)
assert file.has_extension is True
@mock.patch('kittengroomer.helpers.magic')
def test_has_extension_false(self, mock_libmagic):
"""If the file has no extension, has_extensions should == False."""
src_path = 'src/test'
dst_path = 'dst/test'
file = FileBase(src_path, dst_path)
assert file.has_extension is False
def test_init_file_doesnt_exist(self):
"""Init should raise an exception if the file doesn't exist."""
2017-07-13 18:56:45 +02:00
with pytest.raises(FileNotFoundError):
FileBase('', '')
2017-07-13 18:56:45 +02:00
def test_init_srcpath_is_directory(self, tmpdir):
"""Init should raise an exception if given a path to a directory."""
2017-07-13 18:56:45 +02:00
with pytest.raises(IsADirectoryError):
FileBase(tmpdir.strpath, tmpdir.strpath)
2017-07-13 23:36:43 +02:00
@mock.patch('kittengroomer.helpers.magic')
def test_init_symlink(self, mock_libmagic, symlink_file_path):
"""Init should properly identify symlinks."""
2017-07-13 23:36:43 +02:00
file = FileBase(symlink_file_path, '')
assert file.mimetype == 'inode/symlink'
2016-12-09 02:12:08 +01:00
2017-07-13 23:36:43 +02:00
@mock.patch('kittengroomer.helpers.magic')
def test_is_symlink_attribute(self, mock_libmagic, symlink_file_path):
2017-07-13 18:56:45 +02:00
"""If a file is a symlink, is_symlink should return True."""
2017-07-13 23:36:43 +02:00
file = FileBase(symlink_file_path, '')
assert file.is_symlink is True
2016-12-09 02:12:08 +01:00
2017-07-14 20:06:55 +02:00
def test_init_mimetype_attribute_assigned_correctly(self):
2017-07-13 18:56:45 +02:00
"""When libmagic returns a given mimetype, the mimetype should be
assigned properly."""
2017-07-13 23:36:43 +02:00
with mock.patch('kittengroomer.helpers.magic.from_file',
return_value='text/plain'):
file = FileBase('', '')
file.mimetype = 'text/plain'
2017-07-13 18:56:45 +02:00
2017-07-14 20:06:55 +02:00
def test_maintype_and_subtype_attributes(self):
"""If a file has a full mimetype, maintype and subtype should ==
the appropriate values."""
2017-07-14 22:40:26 +02:00
with mock.patch('kittengroomer.helpers.magic.from_file',
return_value='text/plain'):
file = FileBase('', '')
assert file.maintype == 'text'
assert file.subtype == 'plain'
2017-07-13 18:56:45 +02:00
2017-07-14 20:06:55 +02:00
def test_has_mimetype_no_full_type(self):
"""If a file doesn't have a full mimetype has_mimetype should == False."""
2017-07-14 22:40:26 +02:00
with mock.patch('kittengroomer.helpers.magic.from_file',
return_value='data'):
file = FileBase('', '')
assert file.has_mimetype == False
2017-07-13 18:56:45 +02:00
2017-07-14 20:06:55 +02:00
# File properties
2017-07-13 18:56:45 +02:00
def get_property_doesnt_exist(self):
2017-07-14 20:06:55 +02:00
"""Trying to get a property that doesn't exist should return None."""
2017-07-13 18:56:45 +02:00
pass
def get_property_builtin(self):
2017-07-14 20:06:55 +02:00
"""Getting a default property that's been set should return that property."""
2017-07-13 18:56:45 +02:00
pass
def get_property_user_defined(self):
2017-07-14 20:06:55 +02:00
"""Getting a user defined property should return that propety."""
2017-07-13 18:56:45 +02:00
pass
2017-07-14 20:06:55 +02:00
def set_property_user_defined(self):
"""Setting a non-default property should make it available for
get_property."""
2017-07-13 18:56:45 +02:00
pass
2017-07-14 20:06:55 +02:00
def set_property_builtin(self):
"""Setting a builtin property should assign that property."""
2017-07-13 18:56:45 +02:00
pass
def test_add_new_description(self):
pass
def test_add_description_exists(self):
pass
def test_add_new_error(self):
pass
def test_add_error_exists(self):
pass
def test_normal_file_mark_dangerous(self):
pass
def test_normal_file_mark_dangerous_filename_change(self):
pass
def test_normal_file_mark_dangerous_add_description(self):
pass
def test_dangerous_file_mark_dangerous(self):
pass
2017-02-16 23:27:00 +01:00
2017-07-14 20:06:55 +02:00
# File modifiers
def test_safe_copy(self):
pass
2017-03-10 19:13:38 +01:00
2017-07-13 18:56:45 +02:00
def test_force_ext_change(self):
pass
2017-07-13 23:01:02 +02:00
def test_force_ext_correct(self):
2017-07-13 18:56:45 +02:00
pass
2017-07-13 23:01:02 +02:00
def test_create_metadata_file_new(self):
2017-07-13 18:56:45 +02:00
pass
def test_create_metadata_file_already_exists(self):
pass
2016-12-05 17:19:39 +01:00
class TestKittenGroomerBase:
2016-12-09 02:12:08 +01:00
2017-07-13 23:01:02 +02:00
@fixture(scope='class')
def src_dir_path(self, tmpdir_factory):
return tmpdir_factory.mktemp('src').strpath
2016-12-05 17:19:39 +01:00
2017-07-13 23:01:02 +02:00
@fixture(scope='class')
def dest_dir_path(self, tmpdir_factory):
return tmpdir_factory.mktemp('dest').strpath
2016-12-05 17:19:39 +01:00
2016-12-09 22:13:54 +01:00
@fixture
2017-07-13 23:01:02 +02:00
def generic_groomer(self, src_dir_path, dest_dir_path):
return KittenGroomerBase(src_dir_path, dest_dir_path)
2016-12-09 22:13:54 +01:00
2017-07-13 23:01:02 +02:00
def test_list_all_files_includes_file(self, tmpdir):
2016-12-09 22:13:54 +01:00
file = tmpdir.join('test.txt')
file.write('testing')
2017-07-13 23:01:02 +02:00
files = KittenGroomerBase.list_all_files(KittenGroomerBase, tmpdir.strpath)
assert file.strpath in files
def test_list_all_files_excludes_dir(self, tmpdir):
2016-12-09 22:13:54 +01:00
testdir = tmpdir.join('testdir')
os.mkdir(testdir.strpath)
2017-07-13 23:01:02 +02:00
files = KittenGroomerBase.list_all_files(KittenGroomerBase, tmpdir.strpath)
2016-12-09 22:13:54 +01:00
assert testdir.strpath not in files