Working version of file-by-file testing

pull/16/head
Dan Puttick 2017-07-06 15:41:38 -04:00
parent 3f9be48cbd
commit 871eea7a68
2 changed files with 49 additions and 43 deletions

View File

@ -2,11 +2,10 @@
# -*- coding: utf-8 -*-
import os
import shutil
import pytest
from tests.utils import save_logs, SampleFile
from tests.utils import SampleFile
try:
from bin.filecheck import KittenGroomerFileCheck, File, GroomerLogger
NODEPS = False
@ -33,21 +32,25 @@ def gather_sample_files():
def list_files(dir_path):
"""List all files in `dir_path`, ignoring .expect files."""
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)
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)
return files
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'}
files = []
for path in file_paths:
newfile = SampleFile(path, expect_dangerous)
if newfile.has_expect_file:
newfile.parse_expect()
_, extension = os.path.splitext(path)
if extension in complex_exts:
newfile.groomer_needed = True
@ -57,48 +60,37 @@ def construct_sample_files(file_paths, expect_dangerous):
return files
def filename(argvalue):
return os.path.basname(argvalue)
def get_filename(sample_file):
return os.path.basename(sample_file.path)
@parametrize(argnames="sample_file", argvalues=gather_sample_files())
def test_sample_files(sample_file):
@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
if not sample_file.groomer_needed:
file = File(sample_file.path, '', GroomerLogger)
file = File(sample_file.path, tmpdir.strpath, GroomerLogger)
file.check()
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
def valid_groomer():
src_path = os.path.join(os.getcwd(), 'tests/normal')
dst_path = make_dst_dir_path(src_path)
return KittenGroomerFileCheck(src_path, dst_path, 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)
def groomer(tmpdir):
dummy_src_path = os.getcwd()
dest_dir = tmpdir.strpath
return KittenGroomerFileCheck(dummy_src_path, dest_dir, debug=True)

View File

@ -1,6 +1,9 @@
import os
from datetime import datetime
import yaml
def save_logs(groomer, test_description):
divider = ('=' * 10 + '{}' + '=' * 10 + '\n')
@ -33,6 +36,17 @@ class SampleFile():
self.path = path
self.expect_dangerous = expect_dangerous
def parse_expect(self, expect_path):
# parse expect here, add to own params
pass
@property
def expect_path(self):
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']