1#!/usr/bin/env python3
2# Copyright (c) 2024 Intel Corporation
3#
4# SPDX-License-Identifier: Apache-2.0
5"""
6Blackbox tests for twister's command line functions related to disable features.
7"""
8
9import pytest
10from unittest import mock
11import os
12import sys
13import re
14
15# pylint: disable=no-name-in-module
16from conftest import TEST_DATA, suite_filename_mock
17from twisterlib.testplan import TestPlan
18from twisterlib.twister_main import main as twister_main
19
20
21@mock.patch.object(TestPlan, 'TESTSUITE_FILENAME', suite_filename_mock)
22class TestDisable:
23    TESTDATA_1 = [
24        (
25            os.path.join(TEST_DATA, 'tests', 'dummy', 'agnostic'),
26            ['qemu_x86'],
27            '--disable-suite-name-check',
28            [r"Expected suite names:\[['\w+'\[,\s]*\]", r"Detected suite names:\[['\w+'\[,\s]*\]"],
29            True
30        ),
31        (
32            os.path.join(TEST_DATA, 'tests', 'dummy', 'agnostic'),
33            ['qemu_x86'],
34            '-v',
35            [r"Expected suite names:\[['(\w+)'[, ]*]+", r"Detected suite names:\[['(\w+)'[, ]*]+"],
36            False
37        ),
38    ]
39    TESTDATA_2 = [
40        (
41            os.path.join(TEST_DATA, 'tests', 'always_warning'),
42            ['qemu_x86'],
43            '--disable-warnings-as-errors',
44            0
45        ),
46        (
47            os.path.join(TEST_DATA, 'tests', 'always_warning'),
48            ['qemu_x86'],
49            '-v',
50            1
51        ),
52    ]
53
54
55    @pytest.mark.parametrize(
56        'test_path, test_platforms, flag, expected, expected_none',
57        TESTDATA_1,
58        ids=[
59            'disable-suite-name-check',
60            'suite-name-check'
61        ],
62    )
63
64    def test_disable_suite_name_check(self, capfd, out_path, test_path, test_platforms, flag, expected, expected_none):
65        args = ['-i', '--outdir', out_path, '-T', test_path] + \
66               [flag] + \
67               ['-vv', '-ll', 'DEBUG'] + \
68               [val for pair in zip(
69                   ['-p'] * len(test_platforms), test_platforms
70               ) for val in pair]
71
72
73        return_value = twister_main(args)
74
75        out, err = capfd.readouterr()
76        sys.stdout.write(out)
77        sys.stderr.write(err)
78
79        assert return_value == 0
80        if expected_none:
81            assert re.search(expected[0], err) is None, f"Not expected string in log: {expected[0]}"
82            assert re.search(expected[1], err) is None, f"Not expected: {expected[1]}"
83        else:
84            assert re.search(expected[0], err) is not None, f"Expected string in log: {expected[0]}"
85            assert re.search(expected[1], err) is not None, f"Expected string in log: {expected[1]}"
86
87
88    @pytest.mark.parametrize(
89        'test_path, test_platforms, flag, expected_exit_code',
90        TESTDATA_2,
91        ids=[
92            'disable-warnings-as-errors',
93            'warnings-as-errors'
94        ],
95    )
96
97    def test_disable_warnings_as_errors(self, capfd, out_path, test_path, test_platforms, flag, expected_exit_code):
98        args = ['-i', '--outdir', out_path, '-T', test_path] + \
99               [flag] + \
100               ['-vv'] + \
101               ['--build-only'] + \
102               [val for pair in zip(
103                   ['-p'] * len(test_platforms), test_platforms
104               ) for val in pair]
105
106
107        return_value = twister_main(args)
108
109        out, err = capfd.readouterr()
110        sys.stdout.write(out)
111        sys.stderr.write(err)
112
113        assert return_value == expected_exit_code, \
114            f"Twister return not expected ({expected_exit_code}) exit code: ({return_value})"
115