#!/usr/bin/python3

# This file is part of Cockpit.
#
# Copyright (C) 2020 Red Hat, Inc.
#
# Cockpit is free software; you can redistribute it and/or modify it
# under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation; either version 2.1 of the License, or
# (at your option) any later version.
#
# Cockpit is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with Cockpit; If not, see <http://www.gnu.org/licenses/>.

import unittest
import subprocess
import os

import parent
from testlib import *

dirname = os.path.dirname(__file__)

class TestTestLib(unittest.TestCase):

    def testTestListing(self):
        # Listing on check-* file
        self.assertEqual(subprocess.check_output([os.path.join(dirname, "check-example"), "-l", "TestNondestructiveExample"]).strip().decode(),
                         "TestNondestructiveExample.testOne\nTestNondestructiveExample.testTwo")
        # Filter on class
        self.assertIn("1..2\nTestNondestructiveExample.testOne\nTestNondestructiveExample.testTwo",
                      subprocess.check_output([os.path.join(dirname, "run-tests"), "-l", "TestNondestructiveExample"]).strip().decode())
        # Filter a specific test
        self.assertIn("1..1\nTestNondestructiveExample.testOne",
                      subprocess.check_output([os.path.join(dirname, "run-tests"), "-l", "TestNondestructiveExample.testOne"]).strip().decode())
        # nondestructive tests are listed together
        self.assertIn("1..3\nTestExample.testNondestructive\nTestNondestructiveExample.testOne\nTestSimple.testOne",
                      subprocess.check_output([os.path.join(dirname, "run-tests"), "-l", "TestSimple.testOne", "TestNondestructiveExample.testOne", "testNondestructive"]).strip().decode())

        self.assertIn("TestExample.testNondestructive\nTestNondestructiveExample.testOne\nTestNondestructiveExample.testTwo",
                      subprocess.check_output([os.path.join(dirname, "run-tests"), "-l"]).strip().decode())

    def testNonDestructive(self):
        self.assertEqual(subprocess.check_output([os.path.join(dirname, "run-tests"), "--nondestructive", "-l", "TestExample"]).strip().decode(),
                         "1..1\nTestExample.testNondestructive")

        # with short option and substring
        self.assertEqual(subprocess.check_output([os.path.join(dirname, "run-tests"), "-nl", "TestExamp"]).strip().decode(),
                         "1..1\nTestExample.testNondestructive")

if __name__ == '__main__':
    test_main()
