tests: path tweaks

The tests are written to assume you run them from the test directory.
Relax the assumption with a quick hack.

Signed-off-by: Martí Bolívar <marti.bolivar@nordicsemi.no>
This commit is contained in:
Martí Bolívar 2021-03-17 22:35:36 -07:00
parent c75772855a
commit 7322777065

View file

@ -1,6 +1,7 @@
# Copyright (c) 2019 Nordic Semiconductor ASA
# SPDX-License-Identifier: BSD-3-Clause
import contextlib
import io
from logging import WARNING
import os
@ -22,6 +23,18 @@ from devicetree import edtlib
# bindings. The tests mostly use string comparisons via the various __repr__()
# methods.
HERE = os.path.dirname(__file__)
@contextlib.contextmanager
def from_here():
# Convenience hack to minimize diff from zephyr.
cwd = os.getcwd()
try:
os.chdir(HERE)
yield
finally:
os.chdir(cwd)
def hpath(filename):
'''Convert 'filename' to the host path syntax.'''
return os.fspath(Path(filename))
@ -29,7 +42,7 @@ def hpath(filename):
def test_warnings(caplog):
'''Tests for situations that should cause warnings.'''
edtlib.EDT("test.dts", ["test-bindings"])
with from_here(): edtlib.EDT("test.dts", ["test-bindings"])
enums_hpath = hpath('test-bindings/enums.yaml')
expected_warnings = [
@ -45,6 +58,7 @@ def test_warnings(caplog):
def test_interrupts():
'''Tests for the interrupts property.'''
with from_here():
edt = edtlib.EDT("test.dts", ["test-bindings"])
filenames = {i: hpath(f'test-bindings/interrupt-{i}-cell.yaml')
for i in range(1, 4)}
@ -66,6 +80,7 @@ def test_interrupts():
def test_reg():
'''Tests for the regs property'''
with from_here():
edt = edtlib.EDT("test.dts", ["test-bindings"])
assert str(edt.get_node("/reg-zero-address-cells/node").regs) == \
@ -82,6 +97,7 @@ def test_reg():
def test_pinctrl():
'''Test 'pinctrl-<index>'.'''
with from_here():
edt = edtlib.EDT("test.dts", ["test-bindings"])
assert str(edt.get_node("/pinctrl/dev").pinctrls) == \
@ -89,6 +105,7 @@ def test_pinctrl():
def test_hierarchy():
'''Test Node.parent and Node.children'''
with from_here():
edt = edtlib.EDT("test.dts", ["test-bindings"])
assert edt.get_node("/").parent is None
@ -106,6 +123,7 @@ def test_hierarchy():
def test_include():
'''Test 'include:' and the legacy 'inherits: !include ...' in bindings'''
with from_here():
edt = edtlib.EDT("test.dts", ["test-bindings"])
assert str(edt.get_node("/binding-include").description) == \
@ -116,6 +134,7 @@ def test_include():
def test_bus():
'''Test 'bus:' and 'on-bus:' in bindings'''
with from_here():
edt = edtlib.EDT("test.dts", ["test-bindings"])
assert edt.get_node("/buses/foo-bus").bus == "foo"
@ -159,6 +178,7 @@ def test_bus():
def test_child_binding():
'''Test 'child-binding:' in bindings'''
with from_here():
edt = edtlib.EDT("test.dts", ["test-bindings"])
child1 = edt.get_node("/child-binding/child-1")
child2 = edt.get_node("/child-binding/child-2")
@ -176,6 +196,7 @@ def test_child_binding():
assert str(grandchild.description) == "grandchild node"
assert str(grandchild.props) == "OrderedDict([('grandchild-prop', <Property, name: grandchild-prop, type: int, value: 2>)])"
with from_here():
binding_file = Path("test-bindings/child-binding.yaml").resolve()
top = edtlib.Binding(binding_file, {})
child = top.child_binding
@ -184,6 +205,7 @@ def test_child_binding():
assert top.compatible == 'top-binding'
assert child.compatible is None
with from_here():
binding_file = Path("test-bindings/child-binding-with-compat.yaml").resolve()
top = edtlib.Binding(binding_file, {})
child = top.child_binding
@ -194,6 +216,7 @@ def test_child_binding():
def test_props():
'''Test Node.props (derived from DT and 'properties:' in the binding)'''
with from_here():
edt = edtlib.EDT("test.dts", ["test-bindings"])
filenames = {i: hpath(f'test-bindings/phandle-array-controller-{i}.yaml')
for i in range(0, 4)}
@ -242,6 +265,7 @@ def test_props():
def test_nexus():
'''Test <prefix>-map via gpio-map (the most common case).'''
with from_here():
edt = edtlib.EDT("test.dts", ["test-bindings"])
filename = hpath('test-bindings/gpio-dst.yaml')
@ -250,6 +274,7 @@ def test_nexus():
def test_prop_defaults():
'''Test property default values given in bindings'''
with from_here():
edt = edtlib.EDT("test.dts", ["test-bindings"])
assert str(edt.get_node("/defaults").props) == \
@ -258,6 +283,7 @@ def test_prop_defaults():
def test_prop_enums():
'''test properties with enum: in the binding'''
with from_here():
edt = edtlib.EDT("test.dts", ["test-bindings"])
props = edt.get_node('/enums').props
int_enum = props['int-enum']
@ -295,10 +321,12 @@ def test_prop_enums():
def test_binding_inference():
'''Test inferred bindings for special zephyr-specific nodes.'''
warnings = io.StringIO()
with from_here():
edt = edtlib.EDT("test.dts", ["test-bindings"], warnings)
assert str(edt.get_node("/zephyr,user").props) == r"OrderedDict()"
with from_here():
edt = edtlib.EDT("test.dts", ["test-bindings"], warnings,
infer_binding_for_paths=["/zephyr,user"])
filenames = {i: hpath(f'test-bindings/phandle-array-controller-{i}.yaml')
@ -309,6 +337,7 @@ def test_binding_inference():
def test_multi_bindings():
'''Test having multiple directories with bindings'''
with from_here():
edt = edtlib.EDT("test-multidir.dts", ["test-bindings", "test-bindings-2"])
assert str(edt.get_node("/in-dir-1").binding_path) == \
@ -319,6 +348,7 @@ def test_multi_bindings():
def test_dependencies():
''''Test dependency relations'''
with from_here():
edt = edtlib.EDT("test-multidir.dts", ["test-bindings", "test-bindings-2"])
assert edt.get_node("/").dep_ordinal == 0