tests: add some basic unit tests
authorAndrea Righi <andrea.righi@canonical.com>
Fri, 24 Nov 2023 10:06:48 +0000 (11:06 +0100)
committerAndrea Righi <andrea.righi@canonical.com>
Mon, 11 Dec 2023 10:00:49 +0000 (11:00 +0100)
Signed-off-by: Andrea Righi <andrea.righi@canonical.com>
tests/__init__.py [new file with mode: 0644]
tests/data/annotations.load.1 [new file with mode: 0644]
tests/data/annotations.load.1.result [new file with mode: 0644]
tests/data/annotations.todo-note.1 [new file with mode: 0644]
tests/data/annotations.todo-note.1.result [new file with mode: 0644]
tests/data/annotations.todo-note.main [new file with mode: 0644]
tests/data/config.todo-note.1 [new file with mode: 0644]
tests/test_load_annotations.py [new file with mode: 0644]
tests/test_todo_note.py [new file with mode: 0644]
tests/utils.py [new file with mode: 0644]

diff --git a/tests/__init__.py b/tests/__init__.py
new file mode 100644 (file)
index 0000000..e69de29
diff --git a/tests/data/annotations.load.1 b/tests/data/annotations.load.1
new file mode 100644 (file)
index 0000000..6116f10
--- /dev/null
@@ -0,0 +1,7 @@
+# Menu: HEADER
+# FORMAT: 4
+# ARCH: amd64 armhf
+# FLAVOUR: amd64-foo amd64-bar armhf-foo armhf-bar
+
+CONFIG_A_1   policy<{'amd64': '1', 'armhf': '1'}> note<Config A_1>
+CONFIG_B_2   policy<{'amd64': '2', 'armhf': '2'}> note<Config B_2>
diff --git a/tests/data/annotations.load.1.result b/tests/data/annotations.load.1.result
new file mode 100644 (file)
index 0000000..d106391
--- /dev/null
@@ -0,0 +1,14 @@
+{
+  "arch": ["amd64", "armhf"],
+  "flavour": ["amd64-foo", "amd64-bar", "armhf-foo", "armhf-bar"],
+  "config": {
+    "CONFIG_A_1": {
+      "policy": {"amd64": "1", "armhf": "1"},
+      "note": "'Config A_1'"
+    },
+    "CONFIG_B_2": {
+      "policy": {"amd64": "2", "armhf": "2"},
+      "note": "'Config B_2'"
+    }
+  }
+}
diff --git a/tests/data/annotations.todo-note.1 b/tests/data/annotations.todo-note.1
new file mode 100644 (file)
index 0000000..aea0146
--- /dev/null
@@ -0,0 +1,9 @@
+# Menu: HEADER
+# FORMAT: 4
+# ARCH: amd64 arm64
+# FLAVOUR: amd64-gcp arm64-gcp
+# FLAVOUR_DEP: {'amd64-gcp': 'amd64-generic', 'arm64-gcp': 'arm64-generic'}
+
+include "annotations.todo-note.main"
+
+CONFIG_ACCESSIBILITY                            policy<{'amd64': 'y', 'arm64': 'y'}>
diff --git a/tests/data/annotations.todo-note.1.result b/tests/data/annotations.todo-note.1.result
new file mode 100644 (file)
index 0000000..09fbb5e
--- /dev/null
@@ -0,0 +1,27 @@
+{
+    "arch": [
+        "amd64",
+        "arm64"
+    ],
+    "flavour": [
+        "amd64-gcp",
+        "arm64-gcp"
+    ],
+    "flavour_dep": {
+        "amd64-gcp": "amd64-generic",
+        "arm64-gcp": "arm64-generic"
+    },
+    "include": [
+        "annotations.todo-note.main"
+    ],
+    "config": {
+        "CONFIG_ACCESSIBILITY": {
+            "policy": {
+                "amd64": "y",
+                "arm64": "y",
+                "amd64-gcp": "n"
+            },
+            "note": "TODO: update note"
+        }
+    }
+}
diff --git a/tests/data/annotations.todo-note.main b/tests/data/annotations.todo-note.main
new file mode 100644 (file)
index 0000000..e295841
--- /dev/null
@@ -0,0 +1,7 @@
+# Menu: HEADER
+# FORMAT: 4
+# ARCH: amd64 arm64 armhf ppc64el riscv64 s390x
+# FLAVOUR: amd64-generic arm64-generic arm64-generic-64k armhf-generic armhf-generic-lpae ppc64el-generic riscv64-generic s390x-generic
+
+CONFIG_ACCESSIBILITY                            policy<{'amd64': 'y', 'arm64': 'y'}>
+CONFIG_ACCESSIBILITY                            note<'this config must be enabled'>
diff --git a/tests/data/config.todo-note.1 b/tests/data/config.todo-note.1
new file mode 100644 (file)
index 0000000..9047179
--- /dev/null
@@ -0,0 +1 @@
+# CONFIG_ACCESSIBILITY is not set
diff --git a/tests/test_load_annotations.py b/tests/test_load_annotations.py
new file mode 100644 (file)
index 0000000..b39cdb5
--- /dev/null
@@ -0,0 +1,14 @@
+import unittest
+
+from tests import utils
+
+from kconfig.annotations import Annotation
+
+
+class TestLoadAnnotations(unittest.TestCase):
+    def test_load(self):
+        for d in ("annotations.load.1",):
+            f = "tests/data/" + d
+            a = Annotation(f)
+            r = utils.load_json(f + ".result")
+            self.assertEqual(utils.to_dict(a), r)
diff --git a/tests/test_todo_note.py b/tests/test_todo_note.py
new file mode 100644 (file)
index 0000000..fef971c
--- /dev/null
@@ -0,0 +1,16 @@
+import unittest
+import json
+
+from tests import utils
+
+from kconfig.annotations import Annotation, KConfig
+
+
+class TestTodoNote(unittest.TestCase):
+    def test_todo(self):
+        a = Annotation("tests/data/annotations.todo-note.1")
+        c = KConfig("tests/data/config.todo-note.1")
+        a.update(c, arch="amd64", flavour="gcp")
+        print(json.dumps(utils.to_dict(a)))
+        r = utils.load_json("tests/data/annotations.todo-note.1.result")
+        self.assertEqual(utils.to_dict(a), r)
diff --git a/tests/utils.py b/tests/utils.py
new file mode 100644 (file)
index 0000000..16c4c9d
--- /dev/null
@@ -0,0 +1,30 @@
+import json
+
+
+def to_dict(a):
+    """Return relevant data from an annotatios object as a dict"""
+    data = {}
+
+    if a.arch:
+        data["arch"] = a.arch
+    if a.flavour:
+        data["flavour"] = a.flavour
+    if a.flavour_dep:
+        data["flavour_dep"] = a.flavour_dep
+    if a.include:
+        data["include"] = a.include
+    if a.config:
+        config = dict(a.config)
+        for _, val in config.items():
+            # Weed out internal "oneline" keys
+            if "oneline" in val:
+                del val["oneline"]
+        data["config"] = config
+
+    return data
+
+
+def load_json(d):
+    """Return JSON file content"""
+    with open(d, encoding="utf-8") as fh:
+        return json.load(fh)