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 test filtering.
7"""
8
9import importlib
10import mock
11import os
12import pytest
13import sys
14import re
15
16# pylint: disable=no-name-in-module
17from conftest import ZEPHYR_BASE, TEST_DATA, testsuite_filename_mock
18from twisterlib.testplan import TestPlan
19
20
21class TestDevice:
22    TESTDATA_1 = [
23        (
24            1234,
25        ),
26        (
27            4321,
28        ),
29        (
30            1324,
31        )
32    ]
33
34    @classmethod
35    def setup_class(cls):
36        apath = os.path.join(ZEPHYR_BASE, 'scripts', 'twister')
37        cls.loader = importlib.machinery.SourceFileLoader('__main__', apath)
38        cls.spec = importlib.util.spec_from_loader(cls.loader.name, cls.loader)
39        cls.twister_module = importlib.util.module_from_spec(cls.spec)
40
41    @classmethod
42    def teardown_class(cls):
43        pass
44
45    @pytest.mark.parametrize(
46        'seed',
47        TESTDATA_1,
48        ids=[
49            'seed 1234',
50            'seed 4321',
51            'seed 1324'
52        ],
53    )
54
55    @mock.patch.object(TestPlan, 'TESTSUITE_FILENAME', testsuite_filename_mock)
56    def test_seed(self, capfd, out_path, seed):
57        test_platforms = ['native_sim']
58        path = os.path.join(TEST_DATA, 'tests', 'seed_native_sim')
59        args = ['--outdir', out_path, '-i', '-T', path, '-vv',] + \
60               ['--seed', f'{seed[0]}'] + \
61               [val for pair in zip(
62                   ['-p'] * len(test_platforms), test_platforms
63               ) for val in pair]
64
65        with mock.patch.object(sys, 'argv', [sys.argv[0]] + args), \
66                pytest.raises(SystemExit) as sys_exit:
67            self.loader.exec_module(self.twister_module)
68
69        out, err = capfd.readouterr()
70        sys.stdout.write(out)
71        sys.stderr.write(err)
72
73        assert str(sys_exit.value) == '1'
74
75        expected_line = r'seed_native_sim.dummy FAILED Failed \(rc=1\) \(native (\d+\.\d+)s/seed: {} <host>\)'.format(seed[0])
76        assert re.search(expected_line, err)
77