#!/usr/bin/env python

import argparse
import os
import sys

import pytest

import ska_pss_protest


class ProTest():
    def __init__(self, path, mark="product"):

        # Verify test type
        self.verify_type(mark)

        self.path = path
        self.mark = mark

        # Obtain path of protest
        self.src = os.path.dirname(ska_pss_protest.__file__)

        self.run()

    @staticmethod
    def verify_type(mark):

        test_types = ["product"]

        if mark not in test_types:
            raise Exception("Test type {} not found".format(mark))

    def run(self):

        # Get path to pytest.ini
        ini_path = os.path.join(self.src, "pytest.ini")

        # Set up path to PSS
        pss_path = "--path=" + self.path

        pytest_args = ["-m", self.mark, "-c", ini_path, pss_path, self.src]
        print('Running pytest', ' '.join(pytest_args))

        sys.exit(pytest.main(pytest_args))

def main():
    parser = argparse.ArgumentParser(description="Run PSS Product Tests")
    parser.add_argument('-p', '--path', help='Path to cheetah build tree', required=True)
    parser.add_argument('-m', '--mark', help='Marker of test type to execute (def=product)', required=False)
    args = parser.parse_args()

    protest = ProTest(args.path, args.mark)
    protest.run()

if __name__ == "__main__":
    main()

