|
| 1 | +# Copyright (c) 2018 Nordic Semiconductor ASA |
| 2 | +# |
| 3 | +# SPDX-License-Identifier: Apache-2.0 |
| 4 | + |
| 5 | +import configparser |
| 6 | +import copy |
| 7 | +import os |
| 8 | +from argparse import Namespace |
| 9 | +from pathlib import Path |
| 10 | + |
| 11 | +import pytest |
| 12 | +from build import Build |
| 13 | + |
| 14 | +ROOT = Path(Path.cwd().anchor) |
| 15 | +TEST_CWD = ROOT / 'path' / 'to' / 'my' / 'current' / 'cwd' |
| 16 | +TEST_CWD_RELATIVE_TO_ROOT = TEST_CWD.relative_to(ROOT) |
| 17 | + |
| 18 | + |
| 19 | +def setup_test_build(monkeypatch, test_args=None): |
| 20 | + # mock configparser read method to keep tests independent from actual configs |
| 21 | + monkeypatch.setattr(configparser.ConfigParser, 'read', lambda self, filenames: None) |
| 22 | + # mock os.makedirs to be independent from actual filesystem |
| 23 | + monkeypatch.setattr('os.makedirs', lambda *a, **kw: None) |
| 24 | + # mock west_topdir so that tests run independent from user machine |
| 25 | + monkeypatch.setattr('build.west_topdir', lambda *a, **kw: str(west_topdir)) |
| 26 | + # mock os.getcwd and Path.cwd to use TEST_CWD |
| 27 | + monkeypatch.setattr('os.getcwd', lambda *a, **kw: str(TEST_CWD)) |
| 28 | + monkeypatch.setattr('pathlib.Path.cwd', lambda *a, **kw: TEST_CWD) |
| 29 | + # mock os.environ to ignore all environment variables during test |
| 30 | + monkeypatch.setattr('os.environ', {}) |
| 31 | + |
| 32 | + # set up Build |
| 33 | + b = Build() |
| 34 | + |
| 35 | + # apply test args |
| 36 | + b.args = copy.copy(DEFAULT_TEST_ARGS) |
| 37 | + if test_args: |
| 38 | + for k, v in vars(test_args).items(): |
| 39 | + setattr(b.args, k, v) |
| 40 | + |
| 41 | + return b |
| 42 | + |
| 43 | + |
| 44 | +# Use a hardcoded west_topdir to ensure that the tests run independently |
| 45 | +# from the actual user machine |
| 46 | +west_topdir = ROOT / 'any' / 'west' / 'workspace' |
| 47 | + |
| 48 | +DEFAULT_TEST_ARGS = Namespace( |
| 49 | + board=None, source_dir=west_topdir / 'subdir' / 'project' / 'app', build_dir=None |
| 50 | +) |
| 51 | + |
| 52 | +TEST_CASES_GET_DIR_FMT_CONTEXT = [ |
| 53 | + # (test_args, source_dir, expected) |
| 54 | + # fallback to cwd in default case |
| 55 | + ( |
| 56 | + {}, |
| 57 | + None, |
| 58 | + { |
| 59 | + 'west_topdir': str(west_topdir), |
| 60 | + 'app': TEST_CWD.name, |
| 61 | + 'source_dir': str(TEST_CWD), |
| 62 | + 'source_dir_ws': str(TEST_CWD_RELATIVE_TO_ROOT), |
| 63 | + }, |
| 64 | + ), |
| 65 | + # check for correct source_dir and source_dir_ws (if inside west_topdir) |
| 66 | + ( |
| 67 | + {}, |
| 68 | + west_topdir / 'my' / 'project', |
| 69 | + { |
| 70 | + 'west_topdir': str(west_topdir), |
| 71 | + 'app': 'project', |
| 72 | + 'source_dir': str(west_topdir / 'my' / 'project'), |
| 73 | + 'source_dir_ws': str(Path('my') / 'project'), |
| 74 | + }, |
| 75 | + ), |
| 76 | + # check for correct source_dir and source_dir_ws (if outside west_topdir) |
| 77 | + ( |
| 78 | + {}, |
| 79 | + ROOT / 'path' / 'to' / 'my-project', |
| 80 | + { |
| 81 | + 'west_topdir': str(west_topdir), |
| 82 | + 'app': 'my-project', |
| 83 | + 'source_dir': str(ROOT / 'path' / 'to' / 'my-project'), |
| 84 | + 'source_dir_ws': str(Path('path') / 'to' / 'my-project'), |
| 85 | + }, |
| 86 | + ), |
| 87 | + # check for correct board |
| 88 | + ( |
| 89 | + Namespace(board='native_sim'), |
| 90 | + None, |
| 91 | + { |
| 92 | + 'board': 'native_sim', |
| 93 | + 'west_topdir': str(west_topdir), |
| 94 | + 'app': TEST_CWD.name, |
| 95 | + 'source_dir': str(TEST_CWD), |
| 96 | + 'source_dir_ws': str(TEST_CWD_RELATIVE_TO_ROOT), |
| 97 | + }, |
| 98 | + ), |
| 99 | +] |
| 100 | + |
| 101 | + |
| 102 | +@pytest.mark.parametrize('test_case', TEST_CASES_GET_DIR_FMT_CONTEXT) |
| 103 | +def test_get_dir_fmt_context(monkeypatch, test_case): |
| 104 | + # extract data from the test case |
| 105 | + test_args, source_dir, expected = test_case |
| 106 | + |
| 107 | + # set up and run _get_dir_fmt_context |
| 108 | + b = setup_test_build(monkeypatch, test_args) |
| 109 | + b.args.source_dir = source_dir |
| 110 | + actual = b._get_dir_fmt_context() |
| 111 | + assert expected == actual |
| 112 | + |
| 113 | + |
| 114 | +TEST_CASES_BUILD_DIR = [ |
| 115 | + # (config_build, test_args, expected) |
| 116 | + # default build directory if no args and dir-fmt are specified |
| 117 | + ({}, None, Path('build')), |
| 118 | + # build_dir from args should always be preferred (if it is specified) |
| 119 | + ({}, Namespace(build_dir='from-args'), Path('from-args')), |
| 120 | + ({'dir-fmt': 'from-dir-fmt'}, Namespace(build_dir='from-args'), Path('from-args')), |
| 121 | + # build_dir is determined by resolving dir-fmt format string |
| 122 | + # must be able to resolve a simple string |
| 123 | + ({'dir-fmt': 'from-dir-fmt'}, None, 'from-dir-fmt'), |
| 124 | + # must be able to resolve west_topdir |
| 125 | + ({'dir-fmt': '{west_topdir}/build'}, None, west_topdir / 'build'), |
| 126 | + # must be able to resolve app |
| 127 | + ({'dir-fmt': '{app}'}, None, 'app'), |
| 128 | + # must be able to resolve source_dir (when it is inside west workspace) |
| 129 | + ( |
| 130 | + {'dir-fmt': '{source_dir}'}, |
| 131 | + None, |
| 132 | + # source_dir resolves to relative path (relative to cwd), so build |
| 133 | + # directory is depending on cwd and ends up outside 'build' |
| 134 | + os.path.relpath(DEFAULT_TEST_ARGS.source_dir, TEST_CWD), |
| 135 | + ), |
| 136 | + # source_dir dir is outside west workspace, so it is absolute path |
| 137 | + ( |
| 138 | + {'dir-fmt': '{source_dir}'}, |
| 139 | + Namespace(source_dir=ROOT / 'outside' / 'living' / 'app'), |
| 140 | + os.path.relpath(ROOT / 'outside' / 'living' / 'app', TEST_CWD), |
| 141 | + ), |
| 142 | + # must be able to resolve source_dir_ws (when source_dir is inside west workspace) |
| 143 | + ({'dir-fmt': '{source_dir_ws}'}, None, Path('subdir') / 'project' / 'app'), |
| 144 | + # must be able to resolve source_dir_ws (when source_dir is outside west workspace) |
| 145 | + ( |
| 146 | + {'dir-fmt': 'build/{source_dir_ws}'}, |
| 147 | + Namespace(source_dir=ROOT / 'outside' / 'living' / 'app'), |
| 148 | + Path('build') / 'outside' / 'living' / 'app', |
| 149 | + ), |
| 150 | + # must be able to resolve board (must be specified) |
| 151 | + ({'dir-fmt': '{board}'}, Namespace(board='native_sim'), 'native_sim'), |
| 152 | +] |
| 153 | + |
| 154 | + |
| 155 | +@pytest.mark.parametrize('test_case', TEST_CASES_BUILD_DIR) |
| 156 | +def test_dir_fmt(monkeypatch, test_case): |
| 157 | + # extract data from the test case |
| 158 | + config_build, test_args, expected = test_case |
| 159 | + |
| 160 | + # apply given config_build |
| 161 | + config = configparser.ConfigParser() |
| 162 | + config.add_section("build") |
| 163 | + for k, v in config_build.items(): |
| 164 | + config.set('build', k, v) |
| 165 | + monkeypatch.setattr("build_helpers.config", config) |
| 166 | + monkeypatch.setattr("build.config", config) |
| 167 | + |
| 168 | + # set up and run _setup_build_dir |
| 169 | + b = setup_test_build(monkeypatch, test_args) |
| 170 | + b._setup_build_dir() |
| 171 | + |
| 172 | + # check for expected build-dir |
| 173 | + assert os.path.abspath(expected) == b.build_dir |
0 commit comments