scripts: unit-test: Derive package name from meson.build

The implementation for package name derivation implemented in
BuildSystem.__init__() simply takes the basename of the directory
containing the source code if another name isn't supplied.

For meson builds we can make a smarter choice, and the result is that
we can lift the requirement that the directory be named after the
project. This is helpful for some workflows where the CI container is
run locally.

By introspecting the build configuration we can extract the meson
project name. Defining the package name as the project name is useful
for meson command invocations. For example, the project name can be used
to specify constraints on build variables (`-D<project>:werror=false`)
and test setups (`--setup=<project>:valgrind`), preventing configuration
from incorrectly propagating into other (sub)projects.

Override the package name once the build configuration is available.

Change-Id: I52dc03253a54589cba0ff2fae5ddba2a74d458ba
Signed-off-by: Andrew Jeffery <andrew@codeconstruct.com.au>
diff --git a/scripts/unit-test.py b/scripts/unit-test.py
index d841834..700e117 100755
--- a/scripts/unit-test.py
+++ b/scripts/unit-test.py
@@ -8,6 +8,7 @@
 """
 
 import argparse
+import json
 import multiprocessing
 import os
 import platform
@@ -804,6 +805,14 @@
 
 
 class Meson(BuildSystem):
+    @staticmethod
+    def _project_name(path):
+        doc = subprocess.check_output(
+            ["meson", "introspect", "--projectinfo", path],
+            stderr=subprocess.STDOUT,
+        ).decode("utf-8")
+        return json.loads(doc)["descriptive_name"]
+
     def __init__(self, package=None, path=None):
         super(Meson, self).__init__(package, path)
 
@@ -942,6 +951,8 @@
             shutil.rmtree("build", ignore_errors=True)
             check_call_cmd("meson", "setup", "build", *meson_flags)
 
+        self.package = Meson._project_name("build")
+
     def build(self):
         check_call_cmd("ninja", "-C", "build")