From c953a461d3512fbd1af989d4daf092c3c95cf130 Mon Sep 17 00:00:00 2001 From: Anders Ingemann Date: Tue, 28 May 2024 18:31:24 +0200 Subject: [PATCH] Make docopt library version available as a variable for sourcing --- README.adoc | 3 ++- docopt_sh/parser.py | 2 +- tests/test_scriptpatching.py | 2 +- 3 files changed, 4 insertions(+), 3 deletions(-) diff --git a/README.adoc b/README.adoc index a46b2a1..1f2a690 100644 --- a/README.adoc +++ b/README.adoc @@ -264,7 +264,8 @@ automatically glean the previously used parameters from your script and re-apply them. `--library` can be any valid bash expression, meaning you can use things like -`"$(dirname "$0")"`. +`"$(dirname "$0")/docopt-lib-$v.sh"` (the `$v` is the version of docopt +the parser AST was generated with). On every invocation docopt checks that the library version and the version of the dynamic part in the script match. The parser exits with an error if that is diff --git a/docopt_sh/parser.py b/docopt_sh/parser.py index c225e9f..ca17499 100644 --- a/docopt_sh/parser.py +++ b/docopt_sh/parser.py @@ -48,7 +48,7 @@ def generate(self, script): ) if self.parameters.library_path: - library = indent(f'''source {self.parameters.library_path} '{__version__}' || {{ + library = indent(f'''local v='{__version__}'; source {self.parameters.library_path} "$v" || {{ ret=$? printf -- "exit %d\\n" "$ret" exit "$ret" diff --git a/tests/test_scriptpatching.py b/tests/test_scriptpatching.py index 7b90653..ac60316 100644 --- a/tests/test_scriptpatching.py +++ b/tests/test_scriptpatching.py @@ -224,7 +224,7 @@ def test_library_version(monkeypatch, capsys, bash): invoke_docopt(monkeypatch, program_params=['--library', library.name, script.name]) with open(script.name, 'r') as h: contents = h.read() - contents = re.sub(r"source (\S+) '([^']+)'", r"source \1 '0.0.0'", contents) + contents = re.sub(r'source (\S+) "([^"]+)"', r"source \1 '0.0.0'", contents) with open(script.name, 'w') as h: h.write(contents) code, out, err = run(bash, 'ship', 'new', 'Olympia')