All of lore.kernel.org
 help / color / mirror / Atom feed
From: "Chris Laplante" <chris.laplante@agilent.com>
To: <bitbake-devel@lists.openembedded.org>
Cc: Chris Laplante <chris.laplante@agilent.com>
Subject: [PATCH v3 07/10] tests/arg_parser: add test for lazy loading of bb.ui modules
Date: Sun, 8 Nov 2020 17:10:56 -0500	[thread overview]
Message-ID: <20201108221059.16854-8-chris.laplante@agilent.com> (raw)
In-Reply-To: <20201108221059.16854-1-chris.laplante@agilent.com>

Ensures bb.ui modules are loaded lazily during construction
of the 'bitbake' argument parser.

Signed-off-by: Chris Laplante <chris.laplante@agilent.com>
---
 lib/bb/main.py             |  5 ++++-
 lib/bb/tests/arg_parser.py | 18 +++++++++++++++---
 2 files changed, 19 insertions(+), 4 deletions(-)

diff --git a/lib/bb/main.py b/lib/bb/main.py
index ccb844bb..f8a19bc3 100755
--- a/lib/bb/main.py
+++ b/lib/bb/main.py
@@ -98,12 +98,15 @@ class LazyUiChoices:
     """
     def __init__(self):
         # map module name => imported module
-        self._modules = {}
+        self._modules = None
 
     def _lazy_load_modules(self):
         if not self._modules:
             self._modules = {module.__name__.split(".")[-1]: module for module in load_extension_modules(bb.ui, "main")}
 
+    @property
+    def has_loaded_modules(self): return self._modules is not None
+
     def __iter__(self):
         self._lazy_load_modules()
         yield from self._modules.keys()
diff --git a/lib/bb/tests/arg_parser.py b/lib/bb/tests/arg_parser.py
index fd46fe8c..be2d86fa 100644
--- a/lib/bb/tests/arg_parser.py
+++ b/lib/bb/tests/arg_parser.py
@@ -7,12 +7,11 @@
 # SPDX-License-Identifier: MIT
 #
 
+import collections
 import shlex
 import unittest
-import collections
-
 
-from bb.main import create_bitbake_parser, BitBakeConfigParameters
+from bb.main import create_bitbake_parser, BitBakeConfigParameters, LazyUiChoices
 
 ParseResult = collections.namedtuple("ParseResult", ("options", "targets"))
 
@@ -41,3 +40,16 @@ class ArgParserTests(unittest.TestCase):
             res = self._parse_helper(arg_str)
             self.assertListEqual(res.targets, ["world"])
             self.assertListEqual(res.options.dump_signatures, ["none"])
+
+    def test_lazy_module_loading(self):
+        # This test ensures that the bb.ui modules weren't loaded immediately upon creation of the arg parser
+
+        # Find the UI action
+        ui_actions = [action for action in self._parser._actions if action.dest == "ui"]
+        if len(ui_actions) > 1:
+            self.fail("Found more than one 'ui' action in the argument parser?")
+        self.assertEqual(len(ui_actions), 1, "Didn't find the 'ui' action in the argument parser?")
+
+        ui_action = ui_actions[0]
+        self.assertIs(type(ui_action.type), LazyUiChoices, "'ui' action has wrong type")
+        self.assertFalse(ui_action.type.has_loaded_modules, "Creation of the arg parser loaded the bb.ui modules")
-- 
2.17.1


  parent reply	other threads:[~2020-11-08 22:12 UTC|newest]

Thread overview: 22+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-11-08 22:10 [PATCH v3 00/10] optparse => argparse Chris Laplante
2020-11-08 22:10 ` [PATCH v3 01/10] cookerdata: tweak to avoid mutable default argument Chris Laplante
2020-11-08 22:10 ` [PATCH v3 02/10] tests/arg_parser: add stub for testing arg parsing Chris Laplante
2020-11-08 22:10 ` [PATCH v3 03/10] tests/arg_parser: add test for 'bitbake -S none world' Chris Laplante
2020-11-08 22:10 ` [PATCH v3 04/10] main: migrate from optparse to argparse Chris Laplante
2020-11-08 22:10 ` [PATCH v3 05/10] main: group --help options to make them easier to read Chris Laplante
2020-11-08 22:10 ` [PATCH v3 06/10] main: rename list_extension_modules => load_extension_modules to clarify its function Chris Laplante
2020-11-08 22:10 ` Chris Laplante [this message]
2020-11-08 22:10 ` [PATCH v3 08/10] main: fix parsing of intermixed arguments Chris Laplante
2020-11-08 22:10 ` [PATCH v3 09/10] tinfoil: use knotty module itself as default 'ui' arg to keep up with changes in setup_bitbake Chris Laplante
2020-11-08 22:10 ` [PATCH v3 10/10] tests/arg_parser: add test for default TinfoilConfigParameters 'ui' Chris Laplante
2020-11-10 21:33 ` [bitbake-devel] [PATCH v3 00/10] optparse => argparse Richard Purdie
2020-11-10 21:36   ` Chris Laplante
2020-11-10 21:38 ` Richard Purdie
2020-11-10 21:40   ` Chris Laplante
2020-11-18 16:37   ` Chris Laplante
2020-11-18 16:44     ` Richard Purdie
2020-11-18 17:07       ` Chris Laplante
2020-11-18 17:24         ` Richard Purdie
2020-11-18 21:01           ` Chris Laplante
2020-11-20 11:17             ` Richard Purdie
     [not found]       ` <1648A9226B93C86E.28066@lists.openembedded.org>
2020-11-18 17:08         ` Chris Laplante

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20201108221059.16854-8-chris.laplante@agilent.com \
    --to=chris.laplante@agilent.com \
    --cc=bitbake-devel@lists.openembedded.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.