[PATCH v2] Start conversion tests to using pytest.

Amar Takhar amar at rtems.org
Mon Mar 16 04:21:29 UTC 2020


error.py:
    - Check for exceptions and error messages.

path.py:
    - Test params and setting module variables.

macros.py:
    - Test class with setup and shared module access.

host.py:
    - Check host specific settings using simple regex for formatting.
    - String check on exception.
---
 rtemstoolkit/conftest.py          | 37 +++++++++++++++
 rtemstoolkit/darwin.py            |  5 --
 rtemstoolkit/error.py             | 10 ----
 rtemstoolkit/freebsd.py           |  5 --
 rtemstoolkit/host.py              | 14 ------
 rtemstoolkit/linux.py             |  5 --
 rtemstoolkit/macros.py            | 24 ----------
 rtemstoolkit/netbsd.py            |  5 --
 rtemstoolkit/path.py              | 17 -------
 rtemstoolkit/solaris.py           |  5 --
 rtemstoolkit/tests/__init__.py    |  0
 rtemstoolkit/tests/test_error.py  | 39 +++++++++++++++
 rtemstoolkit/tests/test_host.py   | 64 +++++++++++++++++++++++++
 rtemstoolkit/tests/test_macros.py | 66 ++++++++++++++++++++++++++
 rtemstoolkit/tests/test_path.py   | 79 +++++++++++++++++++++++++++++++
 rtemstoolkit/windows.py           |  5 --
 16 files changed, 285 insertions(+), 95 deletions(-)
 create mode 100644 rtemstoolkit/conftest.py
 create mode 100644 rtemstoolkit/tests/__init__.py
 create mode 100644 rtemstoolkit/tests/test_error.py
 create mode 100644 rtemstoolkit/tests/test_host.py
 create mode 100644 rtemstoolkit/tests/test_macros.py
 create mode 100644 rtemstoolkit/tests/test_path.py

diff --git a/rtemstoolkit/conftest.py b/rtemstoolkit/conftest.py
new file mode 100644
index 0000000..3e1ec80
--- /dev/null
+++ b/rtemstoolkit/conftest.py
@@ -0,0 +1,37 @@
+# SPDX-License-Identifier: BSD-2-Clause
+
+# RTEMS Tools Project (http://www.rtems.org/)
+# Copyright (C) 2020 Amar Takhar <amar at rtems.org>
+#
+# Redistribution and use in source and binary forms, with or without
+# modification, are permitted provided that the following conditions
+# are met:
+# 1. Redistributions of source code must retain the above copyright
+#    notice, this list of conditions and the following disclaimer.
+# 2. Redistributions in binary form must reproduce the above copyright
+#    notice, this list of conditions and the following disclaimer in the
+#    documentation and/or other materials provided with the distribution.
+#
+# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
+# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+# POSSIBILITY OF SUCH DAMAGE.
+
+
+import pytest
+from sys import path
+import os
+
+
+# Always find the parent directory of rtemstoolkit/ no matter where
+# pytest is run.
+def pytest_runtest_setup(item):
+    path_toolkit = os.path.dirname(os.path.abspath(__file__))
+    path.append(os.path.dirname(path_toolkit))
diff --git a/rtemstoolkit/darwin.py b/rtemstoolkit/darwin.py
index e780d1d..b9e17cf 100644
--- a/rtemstoolkit/darwin.py
+++ b/rtemstoolkit/darwin.py
@@ -76,8 +76,3 @@ def overrides():
     defines['_build_arch']   = defines['_host_arch']
 
     return defines
-
-if __name__ == '__main__':
-    import pprint
-    pprint.pprint(cpus())
-    pprint.pprint(overrides())
diff --git a/rtemstoolkit/error.py b/rtemstoolkit/error.py
index e3a43c1..697b0a6 100644
--- a/rtemstoolkit/error.py
+++ b/rtemstoolkit/error.py
@@ -55,13 +55,3 @@ class exit(error):
     """Raise for to exit."""
     def __init__(self):
         pass
-
-if __name__ == '__main__':
-    try:
-        raise general('a general error')
-    except general as gerr:
-        print('caught:', gerr)
-    try:
-        raise internal('an internal error')
-    except internal as ierr:
-        print('caught:', ierr)
diff --git a/rtemstoolkit/freebsd.py b/rtemstoolkit/freebsd.py
index ed16140..6e40c34 100644
--- a/rtemstoolkit/freebsd.py
+++ b/rtemstoolkit/freebsd.py
@@ -97,8 +97,3 @@ def overrides():
             break
 
     return defines
-
-if __name__ == '__main__':
-    import pprint
-    pprint.pprint(cpus())
-    pprint.pprint(overrides())
diff --git a/rtemstoolkit/host.py b/rtemstoolkit/host.py
index 01aae07..67b006a 100644
--- a/rtemstoolkit/host.py
+++ b/rtemstoolkit/host.py
@@ -103,17 +103,3 @@ def label(mode = 'all'):
     if mode == 'all':
         return '%s (%s)' % (compact, extended)
     raise error.general('invalid platform mode: %s' % (mode))
-
-if __name__ == '__main__':
-    import pprint
-    print('Python\'s OS name: %s' % (os.name))
-    load()
-    print('Name      : %s' % (name))
-    if is_windows:
-        status = 'Yes'
-    else:
-        status = 'No'
-    print('Windows   : %s' % (status))
-    print('CPUs      : %d' % (cpus()))
-    print('Overrides :')
-    pprint.pprint(overrides())
diff --git a/rtemstoolkit/linux.py b/rtemstoolkit/linux.py
index 21f6f97..15b8528 100644
--- a/rtemstoolkit/linux.py
+++ b/rtemstoolkit/linux.py
@@ -134,8 +134,3 @@ def overrides():
     defines['_build_arch']   = defines['_host_arch']
 
     return defines
-
-if __name__ == '__main__':
-    import pprint
-    pprint.pprint(cpus())
-    pprint.pprint(overrides())
diff --git a/rtemstoolkit/macros.py b/rtemstoolkit/macros.py
index d8012e1..708107f 100644
--- a/rtemstoolkit/macros.py
+++ b/rtemstoolkit/macros.py
@@ -534,27 +534,3 @@ class macros:
 
     def unlock_read_map(self):
         self.read_map_locked = False
-
-if __name__ == "__main__":
-    import copy
-    import sys
-    print(inspect.getfile(macros))
-    m = macros()
-    d = copy.copy(m)
-    m['test1'] = 'something'
-    if d.has_key('test1'):
-        print('error: copy failed.')
-        sys.exit(1)
-    m.parse("[test]\n" \
-            "test1: none, undefine, ''\n" \
-            "name:  none, override, 'pink'\n")
-    print('set test:', m.set_read_map('test'))
-    if m['name'] != 'pink':
-        print('error: override failed. name is %s' % (m['name']))
-        sys.exit(1)
-    if m.has_key('test1'):
-        print('error: map undefine failed.')
-        sys.exit(1)
-    print('unset test:', m.unset_read_map('test'))
-    print(m)
-    print(m.keys())
diff --git a/rtemstoolkit/netbsd.py b/rtemstoolkit/netbsd.py
index 1d9a11f..eab9353 100644
--- a/rtemstoolkit/netbsd.py
+++ b/rtemstoolkit/netbsd.py
@@ -90,8 +90,3 @@ def overrides():
             break
 
     return defines
-
-if __name__ == '__main__':
-    import pprint
-    pprint.pprint(cpus())
-    pprint.pprint(overrides())
diff --git a/rtemstoolkit/path.py b/rtemstoolkit/path.py
index b15164b..7b5f64b 100644
--- a/rtemstoolkit/path.py
+++ b/rtemstoolkit/path.py
@@ -430,20 +430,3 @@ def get_humanize_size(path, depth = -1):
             return "%5.3f%sB" % (size, unit)
         size /= 1024.0
     return "%.3f%sB" % (size, 'Y')
-
-if __name__ == '__main__':
-    print(host('/a/b/c/d-e-f'))
-    print(host('//a/b//c/d-e-f'))
-    print(shell('/w/x/y/z'))
-    print(basename('/as/sd/df/fg/me.txt'))
-    print(dirname('/as/sd/df/fg/me.txt'))
-    print(join('/d', 'g', '/tyty/fgfg'))
-    windows = True
-    print(host('/a/b/c/d-e-f'))
-    print(host('//a/b//c/d-e-f'))
-    print(shell('/w/x/y/z'))
-    print(shell('w:/x/y/z'))
-    print(basename('x:/sd/df/fg/me.txt'))
-    print(dirname('x:/sd/df/fg/me.txt'))
-    print(join('s:/d/', '/g', '/tyty/fgfg'))
-    print(join('s:/d/e\\f/g', '/h', '/tyty/zxzx', '\\mm\\nn/p'))
diff --git a/rtemstoolkit/solaris.py b/rtemstoolkit/solaris.py
index 79ddd75..1b265da 100644
--- a/rtemstoolkit/solaris.py
+++ b/rtemstoolkit/solaris.py
@@ -83,8 +83,3 @@ def overrides():
     defines['_build_arch']   = defines['_host_arch']
 
     return defines
-
-if __name__ == '__main__':
-    import pprint
-    pprint.pprint(cpus())
-    pprint.pprint(overrides())
diff --git a/rtemstoolkit/tests/__init__.py b/rtemstoolkit/tests/__init__.py
new file mode 100644
index 0000000..e69de29
diff --git a/rtemstoolkit/tests/test_error.py b/rtemstoolkit/tests/test_error.py
new file mode 100644
index 0000000..0702ced
--- /dev/null
+++ b/rtemstoolkit/tests/test_error.py
@@ -0,0 +1,39 @@
+# SPDX-License-Identifier: BSD-2-Clause
+
+# RTEMS Tools Project (http://www.rtems.org/)
+# Copyright (C) 2020 Amar Takhar <amar at rtems.org>
+#
+# Redistribution and use in source and binary forms, with or without
+# modification, are permitted provided that the following conditions
+# are met:
+# 1. Redistributions of source code must retain the above copyright
+#    notice, this list of conditions and the following disclaimer.
+# 2. Redistributions in binary form must reproduce the above copyright
+#    notice, this list of conditions and the following disclaimer in the
+#    documentation and/or other materials provided with the distribution.
+#
+# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
+# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+# POSSIBILITY OF SUCH DAMAGE.
+
+
+import pytest
+from rtemstoolkit.error import general, internal
+
+
+def test_general():
+    with pytest.raises(general, match=r"^error: test_general$"):
+        raise general("test_general")
+
+
+def test_internal():
+    with pytest.raises(internal, match=r"^internal error: test_internal$"):
+        raise internal("test_internal")
diff --git a/rtemstoolkit/tests/test_host.py b/rtemstoolkit/tests/test_host.py
new file mode 100644
index 0000000..fb15a24
--- /dev/null
+++ b/rtemstoolkit/tests/test_host.py
@@ -0,0 +1,64 @@
+# SPDX-License-Identifier: BSD-2-Clause
+
+# RTEMS Tools Project (http://www.rtems.org/)
+# Copyright (C) 2020 Amar Takhar <amar at rtems.org>
+#
+# Redistribution and use in source and binary forms, with or without
+# modification, are permitted provided that the following conditions
+# are met:
+# 1. Redistributions of source code must retain the above copyright
+#    notice, this list of conditions and the following disclaimer.
+# 2. Redistributions in binary form must reproduce the above copyright
+#    notice, this list of conditions and the following disclaimer in the
+#    documentation and/or other materials provided with the distribution.
+#
+# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
+# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+# POSSIBILITY OF SUCH DAMAGE.
+
+
+import pytest
+import re
+from rtemstoolkit import host
+
+
+def test_cpu():
+    assert type(host.cpus()) is int
+
+
+def test_overrides():
+    assert type(host.overrides()) is dict
+
+
+def test_label_system():
+    assert host.label(mode="system") is not None
+
+
+def test_label_compact():
+    assert re.match("^[a-zA-Z]*-.*-.*$", host.label(mode="compact"))
+
+
+def test_label_extended():
+    assert re.match("^[a-zA-Z]* .*$", host.label(mode="extended"))
+
+
+def test_label_extended():
+    assert re.match("^[a-zA-Z]* .*$", host.label(mode="extended"))
+
+
+def test_label_all():
+    assert re.match("^[a-zA-Z]*-.*-.* \\(.*\\)$", host.label(mode="all"))
+
+
+def test_label_error():
+    from rtemstoolkit.error import general
+    with pytest.raises(general, match=r"^error: invalid platform mode: _nonexistent_$"):
+        assert host.label(mode="_nonexistent_")
diff --git a/rtemstoolkit/tests/test_macros.py b/rtemstoolkit/tests/test_macros.py
new file mode 100644
index 0000000..45d4abd
--- /dev/null
+++ b/rtemstoolkit/tests/test_macros.py
@@ -0,0 +1,66 @@
+# SPDX-License-Identifier: BSD-2-Clause
+
+# RTEMS Tools Project (http://www.rtems.org/)
+# Copyright (C) 2020 Amar Takhar <amar at rtems.org>
+#
+# Redistribution and use in source and binary forms, with or without
+# modification, are permitted provided that the following conditions
+# are met:
+# 1. Redistributions of source code must retain the above copyright
+#    notice, this list of conditions and the following disclaimer.
+# 2. Redistributions in binary form must reproduce the above copyright
+#    notice, this list of conditions and the following disclaimer in the
+#    documentation and/or other materials provided with the distribution.
+#
+# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
+# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+# POSSIBILITY OF SUCH DAMAGE.
+
+
+import pytest
+from rtemstoolkit.macros import macros
+
+
+class TestClass:
+    @classmethod
+    def setup_class(cls):
+        import copy
+        import sys
+        cls.m = macros()
+        cls.d = copy.copy(cls.m)
+
+
+    def test_copy_failure(self):
+        self.m['test1'] = 'something'
+        assert "test1" not in self.d
+
+
+    def test_set_tests(self):
+        assert self.m.parse("[test]\n" \
+                "test1: none, undefine, ''\n" \
+                "name:  none, override, 'pink'\n") is None
+
+        assert self.m.set_read_map("test")
+
+
+    def test_check_override(self):
+        if self.m["name"] != "pink":
+            raise Exception("error: override failed. name is %s" % self.m["name"])
+
+
+    def test_undefine(self):
+        assert "test1" not in self.m
+
+
+    def test_unset(self):
+        assert "name" in self.m
+        assert self.m.unset_read_map("test")
+        assert "name" not in self.m
diff --git a/rtemstoolkit/tests/test_path.py b/rtemstoolkit/tests/test_path.py
new file mode 100644
index 0000000..ee0ae24
--- /dev/null
+++ b/rtemstoolkit/tests/test_path.py
@@ -0,0 +1,79 @@
+# SPDX-License-Identifier: BSD-2-Clause
+
+# RTEMS Tools Project (http://www.rtems.org/)
+# Copyright (C) 2020 Amar Takhar <amar at rtems.org>
+#
+# Redistribution and use in source and binary forms, with or without
+# modification, are permitted provided that the following conditions
+# are met:
+# 1. Redistributions of source code must retain the above copyright
+#    notice, this list of conditions and the following disclaimer.
+# 2. Redistributions in binary form must reproduce the above copyright
+#    notice, this list of conditions and the following disclaimer in the
+#    documentation and/or other materials provided with the distribution.
+#
+# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
+# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+# POSSIBILITY OF SUCH DAMAGE.
+
+
+import pytest
+from rtemstoolkit import path
+
+
+rtems_windows = pytest.mark.parametrize("windows", [False, True], \
+                ids=["Windows=False", "Windows=True"])
+
+
+ at rtems_windows
+def test_host(windows):
+    path.windows = windows
+
+    if windows:
+        path_str = "a:\\b\\c\\d-e-f"
+    else:
+        path_str = "/a/b/c/d-e-f"
+
+    assert path.host('//a/b/c/d-e-f') == path_str
+
+
+ at rtems_windows
+def test_shell(windows):
+    path.windows = windows
+    assert path.shell('/w/x/y/z') == "/w/x/y/z"
+
+
+ at rtems_windows
+def test_basename(windows):
+    path.windows = windows
+    assert path.basename('/as/sd/df/fg/me.txt') == "me.txt"
+
+    if windows:
+        assert path.basename('x:/sd/df/fg/me.txt') == "me.txt"
+
+
+ at rtems_windows
+def test_dirname(windows):
+    path.windows = windows
+    assert path.dirname('/as/sd/df/fg/me.txt') == "/as/sd/df/fg"
+
+    if windows:
+        assert path.dirname('x:/sd/df/fg/me.txt') == "/x/sd/df/fg"
+
+
+ at rtems_windows
+def test_join(windows):
+    path.windows = windows
+    assert path.join('/d', 'g', '/tyty/fgfg') == "/d/g/tyty/fgfg"
+
+    if windows:
+        assert path.join('s:/d/e\\f/g', '/h', '/tyty/zxzx', '\\mm\\nn/p') == "/s/d/e/f/g/h/tyty/zxzx/mm/nn/p"
+        assert path.join('s:/d/', '/g', '/tyty/fgfg') == "/s/d/g/tyty/fgfg"
diff --git a/rtemstoolkit/windows.py b/rtemstoolkit/windows.py
index 802263b..585cddf 100644
--- a/rtemstoolkit/windows.py
+++ b/rtemstoolkit/windows.py
@@ -132,8 +132,3 @@ def overrides():
         '___setup_shell': ('exe',     'required', '%{__sh}')
         }
     return defines
-
-if __name__ == '__main__':
-    import pprint
-    pprint.pprint(cpus())
-    pprint.pprint(overrides())
-- 
2.25.0



More information about the devel mailing list