mirror of https://github.com/CIRCL/PyCIRCLean
Working version of file-by-file testing
parent
3f9be48cbd
commit
871eea7a68
|
@ -2,11 +2,10 @@
|
||||||
# -*- coding: utf-8 -*-
|
# -*- coding: utf-8 -*-
|
||||||
|
|
||||||
import os
|
import os
|
||||||
import shutil
|
|
||||||
|
|
||||||
import pytest
|
import pytest
|
||||||
|
|
||||||
from tests.utils import save_logs, SampleFile
|
from tests.utils import SampleFile
|
||||||
try:
|
try:
|
||||||
from bin.filecheck import KittenGroomerFileCheck, File, GroomerLogger
|
from bin.filecheck import KittenGroomerFileCheck, File, GroomerLogger
|
||||||
NODEPS = False
|
NODEPS = False
|
||||||
|
@ -33,21 +32,25 @@ def gather_sample_files():
|
||||||
|
|
||||||
|
|
||||||
def list_files(dir_path):
|
def list_files(dir_path):
|
||||||
|
"""List all files in `dir_path`, ignoring .expect files."""
|
||||||
full_dir_path = os.path.abspath(dir_path)
|
full_dir_path = os.path.abspath(dir_path)
|
||||||
files = []
|
files = []
|
||||||
for file_path in os.listdir(full_dir_path):
|
for file_path in os.listdir(full_dir_path):
|
||||||
full_file_path = os.path.join(full_dir_path, file_path)
|
full_file_path = os.path.join(full_dir_path, file_path)
|
||||||
_, ext = os.path.splitext(full_file_path)
|
_, ext = os.path.splitext(full_file_path)
|
||||||
if os.path.isfile(full_file_path) and ext is not '.expect':
|
if os.path.isfile(full_file_path) and not ext.endswith('.expect'):
|
||||||
files.append(full_file_path)
|
files.append(full_file_path)
|
||||||
return files
|
return files
|
||||||
|
|
||||||
|
|
||||||
def construct_sample_files(file_paths, expect_dangerous):
|
def construct_sample_files(file_paths, expect_dangerous):
|
||||||
|
"""Construct a list of a sample files from list `file_paths`."""
|
||||||
complex_exts = {'.gif', '.jpg', '.png', '.svg', '.rar', '.zip'}
|
complex_exts = {'.gif', '.jpg', '.png', '.svg', '.rar', '.zip'}
|
||||||
files = []
|
files = []
|
||||||
for path in file_paths:
|
for path in file_paths:
|
||||||
newfile = SampleFile(path, expect_dangerous)
|
newfile = SampleFile(path, expect_dangerous)
|
||||||
|
if newfile.has_expect_file:
|
||||||
|
newfile.parse_expect()
|
||||||
_, extension = os.path.splitext(path)
|
_, extension = os.path.splitext(path)
|
||||||
if extension in complex_exts:
|
if extension in complex_exts:
|
||||||
newfile.groomer_needed = True
|
newfile.groomer_needed = True
|
||||||
|
@ -57,48 +60,37 @@ def construct_sample_files(file_paths, expect_dangerous):
|
||||||
return files
|
return files
|
||||||
|
|
||||||
|
|
||||||
def filename(argvalue):
|
def get_filename(sample_file):
|
||||||
return os.path.basname(argvalue)
|
return os.path.basename(sample_file.path)
|
||||||
|
|
||||||
|
|
||||||
@parametrize(argnames="sample_file", argvalues=gather_sample_files())
|
@parametrize(
|
||||||
def test_sample_files(sample_file):
|
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
|
||||||
if not sample_file.groomer_needed:
|
if not sample_file.groomer_needed:
|
||||||
file = File(sample_file.path, '', GroomerLogger)
|
file = File(sample_file.path, tmpdir.strpath, GroomerLogger)
|
||||||
file.check()
|
file.check()
|
||||||
assert file.is_dangerous is sample_file.expect_dangerous
|
assert file.is_dangerous is sample_file.expect_dangerous
|
||||||
|
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
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.fixture
|
||||||
|
def dest_dir(tmpdir_factory):
|
||||||
|
return tmpdir_factory.mktemp('dest')
|
||||||
|
|
||||||
|
|
||||||
@fixture
|
@fixture
|
||||||
def valid_groomer():
|
def groomer(tmpdir):
|
||||||
src_path = os.path.join(os.getcwd(), 'tests/normal')
|
dummy_src_path = os.getcwd()
|
||||||
dst_path = make_dst_dir_path(src_path)
|
dest_dir = tmpdir.strpath
|
||||||
return KittenGroomerFileCheck(src_path, dst_path, debug=True)
|
return KittenGroomerFileCheck(dummy_src_path, dest_dir, debug=True)
|
||||||
|
|
||||||
|
|
||||||
@fixture
|
|
||||||
def invalid_groomer():
|
|
||||||
src_path = os.path.join(os.getcwd(), 'tests/dangerous')
|
|
||||||
dst_path = make_dst_dir_path(src_path)
|
|
||||||
return KittenGroomerFileCheck(src_path, dst_path, debug=True)
|
|
||||||
|
|
||||||
|
|
||||||
def make_dst_dir_path(src_dir_path):
|
|
||||||
dst_path = src_dir_path + '_dst'
|
|
||||||
shutil.rmtree(dst_path, ignore_errors=True)
|
|
||||||
os.makedirs(dst_path, exist_ok=True)
|
|
||||||
return dst_path
|
|
||||||
|
|
||||||
|
|
||||||
@skip
|
|
||||||
def test_filecheck_src_valid(valid_groomer):
|
|
||||||
valid_groomer.run()
|
|
||||||
test_description = "filecheck_valid"
|
|
||||||
save_logs(valid_groomer, test_description)
|
|
||||||
|
|
||||||
|
|
||||||
@skip
|
|
||||||
def test_filecheck_src_invalid(invalid_groomer):
|
|
||||||
invalid_groomer.run()
|
|
||||||
test_description = "filecheck_invalid"
|
|
||||||
save_logs(invalid_groomer, test_description)
|
|
||||||
|
|
|
@ -1,6 +1,9 @@
|
||||||
import os
|
import os
|
||||||
|
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
|
|
||||||
|
import yaml
|
||||||
|
|
||||||
|
|
||||||
def save_logs(groomer, test_description):
|
def save_logs(groomer, test_description):
|
||||||
divider = ('=' * 10 + '{}' + '=' * 10 + '\n')
|
divider = ('=' * 10 + '{}' + '=' * 10 + '\n')
|
||||||
|
@ -33,6 +36,17 @@ class SampleFile():
|
||||||
self.path = path
|
self.path = path
|
||||||
self.expect_dangerous = expect_dangerous
|
self.expect_dangerous = expect_dangerous
|
||||||
|
|
||||||
def parse_expect(self, expect_path):
|
@property
|
||||||
# parse expect here, add to own params
|
def expect_path(self):
|
||||||
pass
|
return self.path + '.expect'
|
||||||
|
|
||||||
|
@property
|
||||||
|
def has_expect_file(self):
|
||||||
|
return os.path.isfile(self.expect_path)
|
||||||
|
|
||||||
|
def parse_expect(self):
|
||||||
|
with open(self.expect_path, 'r') as expect_file:
|
||||||
|
self.expect_dict = yaml.safe_load(expect_file)
|
||||||
|
self.expect_dangerous = self.expect_dict['expect_dangerous']
|
||||||
|
self.groomer_needed = self.expect_dict['groomer_needed']
|
||||||
|
self.expected_mimetype = self.expect_dict['expected_mimetype']
|
||||||
|
|
Loading…
Reference in New Issue