Skip to content

Commit 8fe0d30

Browse files
committed
tests: stop hardcoding a forward slash in 'scripts/west-commands.yml'
Use backwards slashes on Windows to catch any future hardcoding mistake. Minor oversight found while discussing zephyrproject-rtos#920
1 parent 548b9ee commit 8fe0d30

3 files changed

Lines changed: 25 additions & 13 deletions

File tree

tests/conftest.py

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,19 @@
3131
# name.
3232
GIT_INIT_HAS_BRANCH = False
3333

34+
WINDOWS = platform.system() == 'Windows'
35+
36+
# We do NOT recommend or even advertise using backslashes on Windows because:
37+
# - they are a quoting and escape nightmare, and
38+
# - forward slashes work 99% of the time on Windows.
39+
# But:
40+
# - Backslashes should still work on Windows! So let's test them.
41+
# - This helps catch OS-independent mistakes in both production code and test code
42+
# where we hardcode forward slashes directly instead of using Python's Pathlib or os.path.
43+
_scripts_west_cmds = r'scripts\\west-commands.yml' if WINDOWS else 'scripts///west-commands.yml'
44+
3445
# If you change this, keep the docstring in repos_tmpdir() updated also.
35-
MANIFEST_TEMPLATE = '''\
46+
MANIFEST_TEMPLATE = f'''\
3647
manifest:
3748
defaults:
3849
remote: test-local
@@ -56,13 +67,11 @@
5667
- name: net-tools
5768
description: Networking tools.
5869
clone-depth: 1
59-
west-commands: scripts/west-commands.yml
70+
west-commands: '{_scripts_west_cmds}'
6071
self:
6172
path: zephyr
6273
'''
6374

64-
WINDOWS = platform.system() == 'Windows'
65-
6675
#
6776
# Contextmanager
6877
#
@@ -649,4 +658,5 @@ def check_proj_consistency(actual, expected):
649658
)
650659
assert actual.clone_depth == expected.clone_depth
651660
assert actual.revision == expected.revision
652-
assert actual.west_commands == expected.west_commands
661+
for a, e in zip(actual.west_commands, expected.west_commands, strict=True):
662+
assert PurePath(a) == PurePath(e)

tests/test_extension_commands.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
import textwrap
66

7-
from conftest import add_commit, cmd, cmd_raises
7+
from conftest import WINDOWS, add_commit, cmd, cmd_raises
88

99

1010
def test_extension_commands_basic(west_update_tmpdir):
@@ -218,11 +218,13 @@ def do_run(self, args, unknown):
218218
def test_extension_command_multiple_commands_same_file(west_update_tmpdir):
219219
# Test multiple commands defined in the same python file
220220
net_tools_path = west_update_tmpdir / 'net-tools'
221+
# Make sure we use either Pathlib or os.path
222+
ext_file = r'scripts\\multi.py' if WINDOWS else 'scripts//multi.py'
221223
add_commit(
222224
net_tools_path,
223225
'add multiple commands',
224226
files={
225-
'scripts/multi.py': textwrap.dedent('''\
227+
ext_file: textwrap.dedent('''\
226228
from west.commands import WestCommand
227229
228230
class FirstCommand(WestCommand):
@@ -241,9 +243,9 @@ def do_add_parser(self, parser_adder):
241243
def do_run(self, args, unknown):
242244
print('second command')
243245
'''),
244-
'scripts/west-commands.yml': textwrap.dedent('''\
246+
'scripts/west-commands.yml': textwrap.dedent(f'''\
245247
west-commands:
246-
- file: scripts/multi.py
248+
- file: {ext_file}
247249
commands:
248250
- name: first
249251
class: FirstCommand

tests/test_project.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -435,7 +435,7 @@ def test_manifest_freeze(west_update_tmpdir):
435435
'^ url: .*$',
436436
'^ revision: [a-f0-9]{40}$',
437437
'^ clone-depth: 1$',
438-
'^ west-commands: scripts/west-commands.yml$',
438+
r'^ west-commands: scripts[/\\]+west-commands.yml$',
439439
'^ self:$',
440440
'^ path: zephyr$',
441441
]
@@ -459,7 +459,7 @@ def test_manifest_freeze_active(west_update_tmpdir):
459459
'^ url: .*$',
460460
'^ revision: [a-f0-9]{40}$',
461461
'^ clone-depth: 1$',
462-
'^ west-commands: scripts/west-commands.yml$',
462+
r'^ west-commands: scripts[/\\]+west-commands.yml$',
463463
'^ self:$',
464464
'^ path: zephyr$',
465465
]
@@ -491,7 +491,7 @@ def test_manifest_resolve(west_update_tmpdir):
491491
'^ url: .*$',
492492
'^ revision: master$',
493493
'^ clone-depth: 1$',
494-
'^ west-commands: scripts/west-commands.yml$',
494+
r'^ west-commands: scripts[/\\]+west-commands.yml$',
495495
'^ self:$',
496496
'^ path: zephyr$',
497497
]
@@ -515,7 +515,7 @@ def test_manifest_resolve_active(west_update_tmpdir):
515515
'^ url: .*$',
516516
'^ revision: master$',
517517
'^ clone-depth: 1$',
518-
'^ west-commands: scripts/west-commands.yml$',
518+
r'^ west-commands: scripts[/\\]+west-commands.yml$',
519519
'^ self:$',
520520
'^ path: zephyr$',
521521
]

0 commit comments

Comments
 (0)