2016-12-22 02:41:46 +01:00
|
|
|
#!/usr/bin/env python
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
2016-12-24 22:46:22 +01:00
|
|
|
import os
|
2020-01-13 16:04:51 +01:00
|
|
|
from pathlib import Path
|
2016-12-24 22:46:22 +01:00
|
|
|
|
2020-01-13 16:04:51 +01:00
|
|
|
import pytest # type: ignore
|
2017-07-07 00:00:55 +02:00
|
|
|
import yaml
|
2016-12-22 02:41:46 +01:00
|
|
|
|
2016-12-24 22:46:22 +01:00
|
|
|
try:
|
2017-08-11 00:12:30 +02:00
|
|
|
from filecheck.filecheck import KittenGroomerFileCheck, File
|
2016-12-24 22:46:22 +01:00
|
|
|
NODEPS = False
|
|
|
|
except ImportError:
|
|
|
|
NODEPS = True
|
2017-07-07 00:00:55 +02:00
|
|
|
pytestmark = pytest.mark.skipif(NODEPS, reason="Dependencies aren't installed")
|
2016-12-24 22:46:22 +01:00
|
|
|
|
2017-07-06 00:09:10 +02:00
|
|
|
|
2017-04-10 13:18:51 +02:00
|
|
|
fixture = pytest.fixture
|
|
|
|
skip = pytest.mark.skip
|
2017-07-06 00:09:10 +02:00
|
|
|
parametrize = pytest.mark.parametrize
|
|
|
|
|
|
|
|
|
2020-01-13 16:04:51 +01:00
|
|
|
NORMAL_FILES_PATH = Path('tests/normal/')
|
|
|
|
DANGEROUS_FILES_PATH = Path('tests/dangerous/')
|
|
|
|
UNCATEGORIZED_FILES_PATH = Path('tests/uncategorized')
|
|
|
|
CATALOG_PATH = Path('tests/file_catalog.yaml')
|
2017-07-07 00:00:55 +02:00
|
|
|
|
|
|
|
|
|
|
|
class SampleFile():
|
2017-07-07 22:06:48 +02:00
|
|
|
def __init__(self, path, exp_dangerous):
|
2020-01-13 16:04:51 +01:00
|
|
|
self.path = Path(path)
|
|
|
|
self.filename = self.path.name
|
2017-07-07 22:06:48 +02:00
|
|
|
self.exp_dangerous = exp_dangerous
|
2017-07-06 00:09:10 +02:00
|
|
|
|
|
|
|
|
|
|
|
def gather_sample_files():
|
2017-07-07 22:06:48 +02:00
|
|
|
file_catalog = read_file_catalog()
|
|
|
|
normal_catalog = file_catalog['normal']
|
|
|
|
dangerous_catalog = file_catalog['dangerous']
|
|
|
|
sample_files = create_sample_files(
|
|
|
|
normal_catalog,
|
|
|
|
NORMAL_FILES_PATH,
|
|
|
|
exp_dangerous=False
|
|
|
|
)
|
|
|
|
sample_files.extend(create_sample_files(
|
|
|
|
dangerous_catalog,
|
|
|
|
DANGEROUS_FILES_PATH,
|
|
|
|
exp_dangerous=True
|
|
|
|
))
|
|
|
|
return sample_files
|
|
|
|
|
|
|
|
|
|
|
|
def read_file_catalog():
|
|
|
|
with open(os.path.abspath(CATALOG_PATH)) as catalog_file:
|
|
|
|
catalog_dict = yaml.safe_load(catalog_file)
|
|
|
|
return catalog_dict
|
|
|
|
|
|
|
|
|
|
|
|
def create_sample_files(file_catalog, dir_path, exp_dangerous):
|
|
|
|
sample_files = []
|
|
|
|
dir_files = set_of_files(dir_path)
|
|
|
|
# Sorted to make the test cases occur in a consistent order, doesn't have to be
|
|
|
|
for filename, file_dict in sorted(file_catalog.items()):
|
|
|
|
full_path = os.path.abspath(os.path.join(dir_path, filename))
|
|
|
|
try:
|
|
|
|
dir_files.remove(full_path)
|
|
|
|
newfile = SampleFile(full_path, exp_dangerous)
|
|
|
|
newfile.xfail = file_dict.get('xfail', False)
|
|
|
|
sample_files.append(newfile)
|
|
|
|
except KeyError:
|
|
|
|
raise FileNotFoundError("{} could not be found".format(filename))
|
|
|
|
for file_path in dir_files:
|
|
|
|
newfile = SampleFile(file_path, exp_dangerous)
|
|
|
|
newfile.xfail = False
|
|
|
|
sample_files.append(newfile)
|
|
|
|
return sample_files
|
|
|
|
|
|
|
|
|
|
|
|
def set_of_files(dir_path):
|
|
|
|
"""Set of all full file paths in `dir_path`."""
|
2017-07-06 00:09:10 +02:00
|
|
|
full_dir_path = os.path.abspath(dir_path)
|
2017-07-07 22:06:48 +02:00
|
|
|
file_paths = set()
|
|
|
|
for path in os.listdir(full_dir_path):
|
|
|
|
full_path = os.path.join(full_dir_path, path)
|
|
|
|
if os.path.isfile(full_path):
|
|
|
|
file_paths.add(full_path)
|
|
|
|
return file_paths
|
2017-07-06 00:09:10 +02:00
|
|
|
|
|
|
|
|
2017-07-06 21:41:38 +02:00
|
|
|
def get_filename(sample_file):
|
|
|
|
return os.path.basename(sample_file.path)
|
2017-07-06 00:09:10 +02:00
|
|
|
|
|
|
|
|
2017-07-14 23:52:21 +02:00
|
|
|
@fixture(scope='module')
|
2020-01-13 16:04:51 +01:00
|
|
|
def src_dir_path(tmp_path_factory):
|
|
|
|
return tmp_path_factory.mktemp('src')
|
2017-07-14 23:52:21 +02:00
|
|
|
|
2017-07-19 18:32:36 +02:00
|
|
|
|
2017-07-13 23:00:41 +02:00
|
|
|
@fixture(scope='module')
|
2020-01-13 16:04:51 +01:00
|
|
|
def dest_dir_path(tmp_path_factory):
|
|
|
|
return tmp_path_factory.mktemp('dest')
|
2017-07-07 00:00:55 +02:00
|
|
|
|
|
|
|
|
|
|
|
@fixture
|
|
|
|
def groomer(dest_dir_path):
|
|
|
|
dummy_src_path = os.getcwd()
|
|
|
|
return KittenGroomerFileCheck(dummy_src_path, dest_dir_path, debug=True)
|
|
|
|
|
|
|
|
|
2017-07-06 21:41:38 +02:00
|
|
|
@parametrize(
|
|
|
|
argnames="sample_file",
|
|
|
|
argvalues=gather_sample_files(),
|
|
|
|
ids=get_filename)
|
2017-07-27 19:27:12 +02:00
|
|
|
def test_sample_files(sample_file, groomer, dest_dir_path):
|
2017-07-07 22:06:48 +02:00
|
|
|
if sample_file.xfail:
|
|
|
|
pytest.xfail(reason='Marked xfail in file catalog')
|
2020-01-13 16:04:51 +01:00
|
|
|
file_dest_path = dest_dir_path / sample_file.filename
|
2017-07-27 19:27:12 +02:00
|
|
|
file = File(sample_file.path, file_dest_path)
|
2017-07-07 00:00:55 +02:00
|
|
|
groomer.process_file(file)
|
2017-07-19 18:32:36 +02:00
|
|
|
print(file.description_string)
|
|
|
|
print(file.mimetype)
|
2017-07-07 22:06:48 +02:00
|
|
|
assert file.is_dangerous == sample_file.exp_dangerous
|
2017-07-07 22:34:10 +02:00
|
|
|
|
|
|
|
|
|
|
|
def test_uncategorized(tmpdir):
|
|
|
|
src_path = os.path.abspath(UNCATEGORIZED_FILES_PATH)
|
|
|
|
dst_path = tmpdir.strpath
|
|
|
|
groomer = KittenGroomerFileCheck(src_path, dst_path, debug=True)
|
|
|
|
groomer.run()
|