import pytest


def pytest_addoption(parser):
    parser.addoption("--sp", action="store", dest="sample_path", help="Path where samples are located")


@pytest.fixture
def samples_path(request):
    return request.config.getoption("--sp")

import q
import os
import yaml
import pytest


def pytest_collect_file(parent, path):
    if path.basename == "test.yml":
        return YamlFile(path, parent)


class YamlFile(pytest.File):
    def collect(self):
        spec = yaml.safe_load(self.fspath.open())
        for platform, archs in spec["Output Files"].items():
            for arch, filename in archs.items():
                yield FLOSSTest(self, platform, arch, filename, spec)


def extract_strings(sample_path):
    # TODO: implement me
    return []


class FLOSSTest(pytest.Item):
    def __init__(self, path, platform, arch, filename, spec):
        name = "{name:s}::{platform:s}::{arch:s}".format(
                name=spec["Test Name"],
                platform=platform,
                arch=arch)
        super(FLOSSTest, self).__init__(name, path)
        self.spec = spec
        self.platform = platform
        self.arch = arch
        self.filename = filename

    def runtest(self):
        spec_path = self.location[0]
        test_dir = os.path.dirname(spec_path)
        test_path = os.path.join(test_dir, self.filename)

        expected_strings = set(self.spec["Decoded strings"])
        found_strings = set(extract_strings(test_path))

        # TODO: enable this
        # assert expected_strings < found_strings

    def reportinfo(self):
        return self.fspath, 0, "usecase: %s" % self.name

add py.test conf hook to process test yaml files.