Skip to content

Commit 0629b9b

Browse files
Apply suggestions from code review (1)
* `_exclude_files_from_build` becomes a static method * Replace ternary expression with simple `if/else` statement Co-Authored-By: Graham Hammond <[email protected]>
1 parent 14f3d0a commit 0629b9b

File tree

2 files changed

+23
-22
lines changed

2 files changed

+23
-22
lines changed

tools/test/build_api/build_api_test.py

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
from mock import patch, MagicMock
2121
from tools.build_api import prepare_toolchain, build_project, build_library
2222
from tools.regions import merge_region_list
23-
from tools.resources import Resources
23+
from tools.resources import Resources, FileType
2424
from tools.toolchains import TOOLCHAINS
2525
from tools.notifier.mock import MockNotifier
2626
from tools.config import Region, Config, ConfigException
@@ -108,14 +108,7 @@ def test_compile_legacy_sources_always_complete_build(self, *_):
108108
assert any('percent' in msg and msg['percent'] == 100.0
109109
for msg in notify.messages if msg)
110110

111-
@patch('tools.toolchains.arm.ARM_STD.parse_dependencies',
112-
return_value=["foo"])
113-
@patch('tools.toolchains.mbedToolchain.need_update',
114-
side_effect=[i % 2 for i in range(3000)])
115-
@patch('os.mkdir')
116-
@patch('tools.toolchains.mbedToolchain.dump_build_profile')
117-
@patch('tools.utils.run_cmd', return_value=(b'', b'', 0))
118-
def test_dirs_exclusion_from_build(self, *_):
111+
def test_dirs_exclusion_from_file_to_compile(self, *_):
119112
"""Test that dirs can be excluded from the build."""
120113
notify = MockNotifier()
121114
toolchain = prepare_toolchain(self.src_paths, self.build_path, self.target,
@@ -127,19 +120,26 @@ def test_dirs_exclusion_from_build(self, *_):
127120
toolchain.RESPONSE_FILES=False
128121
toolchain.config_processed = True
129122
toolchain.config_file = "junk"
130-
exclude_dirs = ['platform/','rtos/', 'targets/']
123+
exclude_dirs = ['platform/','drivers/', 'targets/']
124+
exclude_dirs2 = []
131125

132-
toolchain.compile_legacy_sources(
133-
res, inc_dirs=None, exclude_dirs = exclude_dirs
126+
files_to_compile = (
127+
res.get_file_refs(FileType.ASM_SRC) +
128+
res.get_file_refs(FileType.C_SRC) +
129+
res.get_file_refs(FileType.CPP_SRC)
130+
)
131+
compilation_queue = toolchain._exclude_files_from_build(
132+
files_to_compile, exclude_dirs
134133
)
134+
print(len(compilation_queue))
135+
136+
assert(len(compilation_queue) < len(files_to_compile))
135137
assert all(
136-
exclude_dir not in msg
137-
for msg in notify.messages
138-
if msg
138+
exclude_dir not in file_to_compile.path
139+
for file_to_compile in compilation_queue
139140
for exclude_dir in exclude_dirs
140141
)
141142

142-
143143
@patch('tools.build_api.Config')
144144
def test_prepare_toolchain_app_config(self, mock_config_init):
145145
"""

tools/toolchains/mbed_toolchain.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -413,7 +413,8 @@ def compile_sources(self, resources, inc_dirs=None):
413413
"""Compile source files."""
414414
return self._compile_sources(resources, inc_dirs=inc_dirs)
415415

416-
def _exclude_files_from_build(self, files_to_compile, exclude_dirs):
416+
@staticmethod
417+
def _exclude_files_from_build(files_to_compile, exclude_dirs):
417418
"""Remove files from dirs to be excluded for the build."""
418419
return [
419420
file_to_compile
@@ -431,11 +432,11 @@ def _compile_sources(self, resources, inc_dirs=None, exclude_dirs=None):
431432
resources.get_file_refs(FileType.C_SRC) +
432433
resources.get_file_refs(FileType.CPP_SRC)
433434
)
434-
compilation_queue = (
435-
files_to_compile
436-
if not exclude_dirs
437-
else self._exclude_files_from_build(files_to_compile, exclude_dirs)
438-
)
435+
if exclude_dirs:
436+
compilation_queue = self._exclude_files_from_build(files_to_compile, exclude_dirs)
437+
else:
438+
compilation_queue = files_to_compile
439+
439440
self.to_be_compiled = len(compilation_queue)
440441
self.compiled = 0
441442

0 commit comments

Comments
 (0)