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
|
|
|
|
|
2016-12-22 02:41:46 +01:00
|
|
|
import pytest
|
|
|
|
|
2017-07-06 21:41:38 +02:00
|
|
|
from tests.utils import SampleFile
|
2016-12-24 22:46:22 +01:00
|
|
|
try:
|
2017-07-06 00:09:10 +02:00
|
|
|
from bin.filecheck import KittenGroomerFileCheck, File, GroomerLogger
|
2016-12-24 22:46:22 +01:00
|
|
|
NODEPS = False
|
|
|
|
except ImportError:
|
|
|
|
NODEPS = True
|
|
|
|
|
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
|
|
|
|
pytestmark = pytest.mark.skipif(NODEPS, reason="Dependencies aren't installed")
|
|
|
|
|
|
|
|
|
|
|
|
NORMAL_FILES_PATH = 'tests/normal/'
|
|
|
|
DANGEROUS_FILES_PATH = 'tests/dangerous/'
|
|
|
|
|
|
|
|
|
|
|
|
def gather_sample_files():
|
|
|
|
normal_paths = list_files(NORMAL_FILES_PATH)
|
|
|
|
dangerous_paths = list_files(DANGEROUS_FILES_PATH)
|
|
|
|
normal_files = construct_sample_files(normal_paths, expect_dangerous=False)
|
|
|
|
dangerous_files = construct_sample_files(dangerous_paths, expect_dangerous=True)
|
|
|
|
return normal_files + dangerous_files
|
|
|
|
|
|
|
|
|
|
|
|
def list_files(dir_path):
|
2017-07-06 21:41:38 +02:00
|
|
|
"""List all files in `dir_path`, ignoring .expect files."""
|
2017-07-06 00:09:10 +02:00
|
|
|
full_dir_path = os.path.abspath(dir_path)
|
|
|
|
files = []
|
|
|
|
for file_path in os.listdir(full_dir_path):
|
|
|
|
full_file_path = os.path.join(full_dir_path, file_path)
|
|
|
|
_, ext = os.path.splitext(full_file_path)
|
2017-07-06 21:41:38 +02:00
|
|
|
if os.path.isfile(full_file_path) and not ext.endswith('.expect'):
|
2017-07-06 00:09:10 +02:00
|
|
|
files.append(full_file_path)
|
|
|
|
return files
|
|
|
|
|
|
|
|
|
|
|
|
def construct_sample_files(file_paths, expect_dangerous):
|
2017-07-06 21:41:38 +02:00
|
|
|
"""Construct a list of a sample files from list `file_paths`."""
|
2017-07-06 00:09:10 +02:00
|
|
|
complex_exts = {'.gif', '.jpg', '.png', '.svg', '.rar', '.zip'}
|
|
|
|
files = []
|
|
|
|
for path in file_paths:
|
|
|
|
newfile = SampleFile(path, expect_dangerous)
|
2017-07-06 21:41:38 +02:00
|
|
|
if newfile.has_expect_file:
|
|
|
|
newfile.parse_expect()
|
2017-07-06 00:09:10 +02:00
|
|
|
_, extension = os.path.splitext(path)
|
|
|
|
if extension in complex_exts:
|
|
|
|
newfile.groomer_needed = True
|
|
|
|
else:
|
|
|
|
newfile.groomer_needed = False
|
|
|
|
files.append(newfile)
|
|
|
|
return files
|
|
|
|
|
|
|
|
|
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-06 21:41:38 +02:00
|
|
|
@parametrize(
|
|
|
|
argnames="sample_file",
|
|
|
|
argvalues=gather_sample_files(),
|
|
|
|
ids=get_filename)
|
|
|
|
def test_sample_files(sample_file, groomer, tmpdir):
|
|
|
|
# make groomer (from ? to tmpdir)
|
|
|
|
# make file (from file.strpath to tmpdir)
|
|
|
|
# run groomer.process_file on it
|
|
|
|
# do asserts
|
2017-07-06 00:09:10 +02:00
|
|
|
if not sample_file.groomer_needed:
|
2017-07-06 21:41:38 +02:00
|
|
|
file = File(sample_file.path, tmpdir.strpath, GroomerLogger)
|
2017-07-06 00:09:10 +02:00
|
|
|
file.check()
|
|
|
|
assert file.is_dangerous is sample_file.expect_dangerous
|
2017-07-06 21:41:38 +02:00
|
|
|
if sample_file.groomer_needed:
|
|
|
|
pass
|
|
|
|
# TODO: make a groomer and process the sample file here
|
|
|
|
if sample_file.has_expect_file:
|
|
|
|
assert file.mimetype == sample_file.expected_mimetype
|
2017-07-06 00:09:10 +02:00
|
|
|
|
|
|
|
|
2017-07-06 21:41:38 +02:00
|
|
|
@pytest.fixture
|
|
|
|
def dest_dir(tmpdir_factory):
|
|
|
|
return tmpdir_factory.mktemp('dest')
|
2017-07-06 00:09:10 +02:00
|
|
|
|
|
|
|
|
|
|
|
@fixture
|
2017-07-06 21:41:38 +02:00
|
|
|
def groomer(tmpdir):
|
|
|
|
dummy_src_path = os.getcwd()
|
|
|
|
dest_dir = tmpdir.strpath
|
|
|
|
return KittenGroomerFileCheck(dummy_src_path, dest_dir, debug=True)
|