Skip to content

Commit 7b254a9

Browse files
committed
test_extension.py: add new test_extension_special_characters()
Add some test coverage for forward and backslashes + whitespace in filenames for west extensions Spurred by a discussion in the review of zephyrproject-rtos#920 which fixes zephyrproject-rtos#725 Signed-off-by: Marc Herbert <Marc.Herbert@gmail.com>
1 parent af2aa4d commit 7b254a9

1 file changed

Lines changed: 84 additions & 1 deletion

File tree

tests/test_extension_commands.py

Lines changed: 84 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,14 @@
22
#
33
# SPDX-License-Identifier: Apache-2.0
44

5+
import subprocess
56
import textwrap
7+
from pathlib import Path
68

7-
from conftest import add_commit, cmd, cmd_raises
9+
import yaml
10+
from conftest import GIT, WINDOWS, add_commit, cmd, cmd_raises, yaml_editor
11+
12+
# The west command "test-extension" comes from the "west_update_tmpdir" fixture in conftest.py
813

914

1015
def test_extension_commands_basic(west_update_tmpdir):
@@ -259,3 +264,81 @@ def do_run(self, args, unknown):
259264
assert 'first command' in ext_output
260265
ext_output = cmd('second')
261266
assert 'second command' in ext_output
267+
268+
269+
def test_extension_special_chars(west_update_tmpdir):
270+
# Detect any unexpected changes in the way we've been handling backslashes and other
271+
# special characters. Changes in how we handle such edge cases may or may not be desired
272+
# (and this test may be updated accordingly), but we never want these changes to come as
273+
# a surprise and we want to keep control over them.
274+
275+
ext_proj = 'net-tools'
276+
ext_proj_p = Path(ext_proj)
277+
278+
# Rename scripts/test.py to something strange.
279+
# The actual location is purposely different on Windows
280+
weird_ext_py = r'scripts///win subdir\\\test.py'
281+
with yaml_editor(ext_proj_p / 'scripts' / 'west-commands.yml') as cmds:
282+
assert cmds["west-commands"][0]["file"] == 'scripts/test.py'
283+
cmds["west-commands"][0]["file"] = weird_ext_py
284+
if WINDOWS:
285+
(ext_proj_p / 'scripts' / 'win subdir').mkdir()
286+
(ext_proj_p / 'scripts' / 'test.py').rename(ext_proj_p / weird_ext_py)
287+
288+
# Just for the logs
289+
subprocess.check_call([GIT, '-C', ext_proj, 'add', weird_ext_py])
290+
print(cmd('diff --manifest'))
291+
292+
# Does the extension still work
293+
ext_output = cmd('test-extension')
294+
assert 'Testing test command 1' in ext_output
295+
296+
def yaml_get_proj(mf: dict, projname: str):
297+
_l = [p for p in mf["manifest"]['projects'] if p["name"] == projname]
298+
assert len(_l) == 1
299+
return _l[0]
300+
301+
# Now also rename the project's 'scripts/west-commands.yml' to something strange
302+
weird_cmds = r'scripts///win subdir\\\w-cmds.yml'
303+
with yaml_editor('zephyr/west.yml') as _mf:
304+
_ext_p_yml = yaml_get_proj(_mf, ext_proj)
305+
assert _ext_p_yml["west-commands"] == 'scripts/west-commands.yml'
306+
_ext_p_yml["west-commands"] = weird_cmds
307+
(ext_proj_p / 'scripts' / 'west-commands.yml').rename(ext_proj_p / weird_cmds)
308+
309+
# Just for the logs
310+
subprocess.check_call([GIT, '-C', ext_proj, 'add', weird_cmds])
311+
print(cmd('diff --manifest'))
312+
313+
# Does the extension still work
314+
ext_output = cmd('test-extension')
315+
assert 'Testing test command 1' in ext_output
316+
317+
# Test how west-commands gets printed back in `west manifest --resolve`
318+
resolved_mf = cmd('manifest --resolve')
319+
resolved_mf = yaml.safe_load(resolved_mf)
320+
ext_proj_yaml = yaml_get_proj(resolved_mf, ext_proj)
321+
assert ext_proj_yaml["west-commands"] == weird_cmds
322+
323+
###### self: west-commands #####
324+
325+
# self: west-commands: follows a slightly different code path.
326+
# Move the extension away from the project and into self.
327+
Path('zephyr', 'scripts').mkdir()
328+
if WINDOWS:
329+
Path('zephyr', 'scripts', 'win subdir').mkdir()
330+
(ext_proj_p / weird_ext_py).rename(Path('zephyr', weird_ext_py))
331+
332+
(ext_proj_p / weird_cmds).rename(Path('zephyr', weird_cmds))
333+
334+
# The extension is now missing from ext_proj. That's OK, it's supported.
335+
with yaml_editor('zephyr/west.yml') as _mf:
336+
_mf["manifest"]["self"]["west-commands"] = weird_cmds
337+
338+
ext_output = cmd('test-extension')
339+
assert 'Testing test command 1' in ext_output
340+
341+
# Test how west-commands gets printed back in `west manifest --resolve`
342+
resolved_mf = cmd('manifest --resolve')
343+
resolved_mf = yaml.safe_load(resolved_mf)
344+
assert resolved_mf["manifest"]["self"]["west-commands"] == weird_cmds

0 commit comments

Comments
 (0)