1#!/usr/bin/env python3
2# Copyright (c) 2020 Intel Corporation
3#
4# SPDX-License-Identifier: Apache-2.0
5
6'''Common fixtures for use in testing the twister tool.'''
7
8import os
9import sys
10import pytest
11
12pytest_plugins = ["pytester"]
13
14ZEPHYR_BASE = os.getenv("ZEPHYR_BASE")
15sys.path.insert(0, os.path.join(ZEPHYR_BASE, "scripts/pylib/twister"))
16sys.path.insert(0, os.path.join(ZEPHYR_BASE, "scripts"))
17from twisterlib.testplan import TestPlan
18from twisterlib.testinstance import TestInstance
19from twisterlib.environment import TwisterEnv, add_parse_arguments, parse_arguments
20
21def new_get_toolchain(*args, **kwargs):
22    return 'zephyr'
23
24TestPlan.get_toolchain = new_get_toolchain
25
26@pytest.fixture(name='test_data')
27def _test_data():
28    """ Pytest fixture to load the test data directory"""
29    data = ZEPHYR_BASE + "/scripts/tests/twister/test_data/"
30    return data
31
32@pytest.fixture(name='zephyr_base')
33def zephyr_base_directory():
34    return ZEPHYR_BASE
35
36@pytest.fixture(name='testsuites_dir')
37def testsuites_directory():
38    """ Pytest fixture to load the test data directory"""
39    return ZEPHYR_BASE + "/scripts/tests/twister/test_data/testsuites"
40
41@pytest.fixture(name='class_env')
42def tesenv_obj(test_data, testsuites_dir, tmpdir_factory):
43    """ Pytest fixture to initialize and return the class TestPlan object"""
44    parser = add_parse_arguments()
45    options = parse_arguments(parser, [])
46    env = TwisterEnv(options)
47    env.board_roots = [os.path.join(test_data, "board_config", "1_level", "2_level")]
48    env.test_roots = [os.path.join(testsuites_dir, 'tests', testsuites_dir, 'samples')]
49    env.test_config = os.path.join(test_data, "test_config.yaml")
50    env.outdir = tmpdir_factory.mktemp("sanity_out_demo")
51    return env
52
53
54@pytest.fixture(name='class_testplan')
55def testplan_obj(test_data, class_env, testsuites_dir, tmpdir_factory):
56    """ Pytest fixture to initialize and return the class TestPlan object"""
57    env = class_env
58    env.board_roots = [test_data +"board_config/1_level/2_level/"]
59    env.test_roots = [testsuites_dir + '/tests', testsuites_dir + '/samples']
60    env.outdir = tmpdir_factory.mktemp("sanity_out_demo")
61    plan = TestPlan(env)
62    plan.parse_configuration(config_file=env.test_config)
63    return plan
64
65@pytest.fixture(name='all_testsuites_dict')
66def testsuites_dict(class_testplan):
67    """ Pytest fixture to call add_testcase function of
68	Testsuite class and return the dictionary of testsuites"""
69    class_testplan.SAMPLE_FILENAME = 'test_sample_app.yaml'
70    class_testplan.TESTSUITE_FILENAME = 'test_data.yaml'
71    class_testplan.add_testsuites()
72    return class_testplan.testsuites
73
74@pytest.fixture(name='platforms_list')
75def all_platforms_list(test_data, class_testplan):
76    """ Pytest fixture to call add_configurations function of
77	Testsuite class and return the Platforms list"""
78    class_testplan.env.board_roots = [os.path.abspath(os.path.join(test_data, "board_config"))]
79    plan = TestPlan(class_testplan.env)
80    plan.parse_configuration(config_file=class_testplan.env.test_config)
81    plan.add_configurations()
82    return plan.platforms
83
84@pytest.fixture
85def instances_fixture(class_testplan, platforms_list, all_testsuites_dict, tmpdir_factory):
86    """ Pytest fixture to call add_instances function of Testsuite class
87    and return the instances dictionary"""
88    class_testplan.outdir = tmpdir_factory.mktemp("sanity_out_demo")
89    class_testplan.platforms = platforms_list
90    platform = class_testplan.get_platform("demo_board_2")
91    instance_list = []
92    for _, testcase in all_testsuites_dict.items():
93        instance = TestInstance(testcase, platform, class_testplan.outdir)
94        instance_list.append(instance)
95    class_testplan.add_instances(instance_list)
96    return class_testplan.instances
97