lib.ExecApp: Add class
ExecApp is a ready-made base class for applications that operate through an ExecContext: it adds the --interactive, --verbose and --target options, exposes interactive, verbose and exec_context properties, and closes the exec context when the async context manager exits. The code for that has lived in jw.pkg.App code before, which now inherits from ExecApp. A fix along the way: __aexit__() closes the exec context and then chains to super().__aexit__(), so App.close() runs when the async context manager exits. Before the change, exiting the async context left the app unclosed; close() ran only on the run() path. Add a unit test that builds an ExecApp with a root command and asserts that close() runs on context exit and that the exec options are registered. The exec options are now registered before App's own options, which moves them up in the rendered --help output. Update the golden file of the help integration test to match. Signed-off-by: Jan Lindemann <jan@janware.com> Assisted-by: unsloth/Qwen3.8-27B-GGUF:Q4_K_M with pi.dev v0.84.2
This commit is contained in:
parent
eec9325b46
commit
1385b1a4ba
5 changed files with 167 additions and 77 deletions
|
|
@ -1,10 +1,11 @@
|
|||
============= Running: jw-pkg.py -t ../../../.. --log-level info --help
|
||||
usage: jw-pkg.py [--log-flags LOG_FLAGS] [--log-level LOG_LEVEL]
|
||||
[--log-file LOG_FILE] [--backtrace]
|
||||
[--write-profile WRITE_PROFILE] [-t TOPDIR]
|
||||
[--topdir-format TOPDIR_FORMAT] [-p PREFIX]
|
||||
[--distro-id DISTRO_ID] [--interactive {true,false,auto}]
|
||||
[--verbose] [--target TARGET] [--pkg-filter PKG_FILTER] [-h]
|
||||
[--write-profile WRITE_PROFILE]
|
||||
[--interactive {true,false,auto}] [--verbose]
|
||||
[--target TARGET] [-t TOPDIR] [--topdir-format TOPDIR_FORMAT]
|
||||
[-p PREFIX] [--distro-id DISTRO_ID] [--pkg-filter PKG_FILTER]
|
||||
[-h]
|
||||
...
|
||||
|
||||
jw-pkg swiss army knife
|
||||
|
|
@ -18,6 +19,12 @@ options:
|
|||
--backtrace Show exception backtraces (default: False)
|
||||
--write-profile WRITE_PROFILE
|
||||
Profile code and store output to file (default: None)
|
||||
--interactive {true,false,auto}
|
||||
Wait for user input or try to proceed unattended
|
||||
(default: true)
|
||||
--verbose Be verbose on stderr about what's being done on the
|
||||
distro level (default: False)
|
||||
--target TARGET Run commands on this host (default: local)
|
||||
-t, --topdir TOPDIR Project Path (default: None)
|
||||
--topdir-format TOPDIR_FORMAT
|
||||
Output references to topdir as one of "make:<var-
|
||||
|
|
@ -28,12 +35,6 @@ options:
|
|||
--distro-id DISTRO_ID
|
||||
Distribution ID (default is taken from /etc/os-
|
||||
release) (default: None)
|
||||
--interactive {true,false,auto}
|
||||
Wait for user input or try to proceed unattended
|
||||
(default: true)
|
||||
--verbose Be verbose on stderr about what's being done on the
|
||||
distro level (default: False)
|
||||
--target TARGET Run commands on this host (default: local)
|
||||
--pkg-filter PKG_FILTER
|
||||
Default filter for all distribution package-related
|
||||
operations (default: None)
|
||||
|
|
|
|||
7
test/unit/python/jw/pkg/lib/ExecApp/Makefile
Normal file
7
test/unit/python/jw/pkg/lib/ExecApp/Makefile
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
TOPDIR = ../../../../../../..
|
||||
|
||||
include $(TOPDIR)/make/proj.mk
|
||||
include $(JWBDIR)/make/py-run.mk
|
||||
|
||||
all:
|
||||
test: run
|
||||
57
test/unit/python/jw/pkg/lib/ExecApp/test.py
Normal file
57
test/unit/python/jw/pkg/lib/ExecApp/test.py
Normal file
|
|
@ -0,0 +1,57 @@
|
|||
import asyncio
|
||||
|
||||
import sys
|
||||
|
||||
from jw.pkg.lib.Cmd import Cmd
|
||||
from jw.pkg.lib.ExecApp import ExecApp
|
||||
|
||||
# -- A minimal root command so the app can be built without a command tree.
|
||||
|
||||
class RootCmd(Cmd):
|
||||
|
||||
def __init__(self, parent):
|
||||
super().__init__(parent, 'root', 'Root command')
|
||||
|
||||
async def _run(self, args):
|
||||
pass
|
||||
|
||||
# -- Counts the close() calls made by App.__aexit__() when the async context
|
||||
# manager exits.
|
||||
|
||||
class RecordingExecApp(ExecApp):
|
||||
|
||||
closed = 0
|
||||
|
||||
def close(self):
|
||||
RecordingExecApp.closed += 1
|
||||
super().close()
|
||||
|
||||
# -- App.__init__ builds the parser from sys.argv, so point it at a clean
|
||||
# command line while constructing the app.
|
||||
|
||||
saved_argv = sys.argv
|
||||
sys.argv = ['jw-pkg-test']
|
||||
try:
|
||||
app = RecordingExecApp(description = 'ExecApp test', root = RootCmd)
|
||||
finally:
|
||||
sys.argv = saved_argv
|
||||
|
||||
# -- ExecApp registers the exec-related options on the top-level parser.
|
||||
|
||||
opts = [
|
||||
o for a in app.parser._actions for o in getattr(a, 'option_strings', ())
|
||||
]
|
||||
for opt in ('--interactive', '--verbose', '--target'):
|
||||
assert opt in opts, f'{opt} must be registered by ExecApp'
|
||||
|
||||
# -- Exiting the async context must close the app, through the
|
||||
# App.__aexit__() that ExecApp.__aexit__() chains to.
|
||||
|
||||
async def exit_context():
|
||||
async with app:
|
||||
pass
|
||||
|
||||
asyncio.run(exit_context())
|
||||
assert RecordingExecApp.closed == 1, 'exiting the app context must close the app'
|
||||
|
||||
print('All ExecApp tests passed')
|
||||
Loading…
Reference in a new issue