From a39ea731c810627341980f4e28b0b5afa55c1c08 Mon Sep 17 00:00:00 2001 From: Jim Porter Date: Thu, 29 Feb 2024 10:35:08 -0800 Subject: [PATCH] Add `set-default --allow-undefined`; see #210 --- CHANGES.md | 4 ++++ README.md | 4 ++++ mike/commands.py | 7 ++++--- mike/driver.py | 6 +++++- test/integration/test_set_default.py | 6 ++++++ test/unit/test_commands.py | 6 ++++++ 6 files changed, 29 insertions(+), 4 deletions(-) diff --git a/CHANGES.md b/CHANGES.md index 2fc63a2..d1242d0 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -2,6 +2,10 @@ ## v2.1.0 (in progress) +### New features +- When calling `set-default`, you can now pass `--allow-undefined` to set the + default to a version that doesn't exist yet + ### Bug fixes - When loading an MkDocs config, mike now runs the `startup` and `shutdown` events diff --git a/README.md b/README.md index 47547b5..153e71a 100644 --- a/README.md +++ b/README.md @@ -217,6 +217,10 @@ version of the docs: mike set-default [identifier] ``` +Normally, this command will return an error if `identifier` doesn't exist. If +you want to set the default to a version that doesn't exist yet, you can pass +`--allow-undefined`. + If you want to use a different template from the default, you can pass `-T`/`--template`; this takes a path to a [Jinja][jinja] template that accepts an `{{href}}` variable. (Note that this page *always* uses a redirect, no matter diff --git a/mike/commands.py b/mike/commands.py index 0dbefb7..8240c97 100644 --- a/mike/commands.py +++ b/mike/commands.py @@ -272,8 +272,9 @@ def retitle(identifier, title, *, branch='gh-pages', message=None, commit.add_file(versions_to_file_info(all_versions, deploy_prefix)) -def set_default(identifier, template=None, *, branch='gh-pages', message=None, - allow_empty=False, deploy_prefix=''): +def set_default(identifier, template=None, allow_undefined=False, *, + branch='gh-pages', message=None, allow_empty=False, + deploy_prefix=''): if message is None: message = ( 'Set default version to {doc_identifier}{deploy_prefix} with ' + @@ -285,7 +286,7 @@ def set_default(identifier, template=None, *, branch='gh-pages', message=None, ) all_versions = list_versions(branch, deploy_prefix) - if not all_versions.find(identifier): + if not allow_undefined and not all_versions.find(identifier): raise ValueError('identifier {!r} does not exist'.format(identifier)) t = _redirect_template(template) diff --git a/mike/driver.py b/mike/driver.py index 5656451..d41675e 100644 --- a/mike/driver.py +++ b/mike/driver.py @@ -299,7 +299,8 @@ def set_default(parser, args): check_remote_status(args, strict=True) with handle_empty_commit(): commands.set_default(args.identifier, args.template, - branch=args.branch, message=args.message, + args.allow_undefined, branch=args.branch, + message=args.message, allow_empty=args.allow_empty, deploy_prefix=args.deploy_prefix) if args.push: @@ -424,6 +425,9 @@ def main(): set_default_p.set_defaults(func=set_default) set_default_p.add_argument('-T', '--template', complete='file', help='template file to use') + set_default_p.add_argument('--allow-undefined', action='store_true', + help=('allow setting undefined versions as ' + + 'default')) add_git_arguments(set_default_p) set_default_p.add_argument('identifier', metavar='IDENTIFIER', help='version or alias to set as default') diff --git a/test/integration/test_set_default.py b/test/integration/test_set_default.py index d99b327..9edb5f7 100644 --- a/test/integration/test_set_default.py +++ b/test/integration/test_set_default.py @@ -43,6 +43,12 @@ def test_set_default(self): check_call_silent(['git', 'checkout', 'gh-pages']) self._test_default() + def test_allow_undefined(self): + self._deploy() + assertPopen(['mike', 'set-default', '2.0', '--allow-undefined']) + check_call_silent(['git', 'checkout', 'gh-pages']) + self._test_default(match_redir('2.0/')) + def test_custom_template(self): self._deploy() assertPopen(['mike', 'set-default', '1.0', '-T', diff --git a/test/unit/test_commands.py b/test/unit/test_commands.py index d20d200..bd99fbf 100644 --- a/test/unit/test_commands.py +++ b/test/unit/test_commands.py @@ -752,6 +752,12 @@ def test_set_default(self): check_call_silent(['git', 'checkout', 'gh-pages']) self._test_default() + def test_allow_undefined(self): + self._deploy() + commands.set_default('2.0', allow_undefined=True) + check_call_silent(['git', 'checkout', 'gh-pages']) + self._test_default(match_redir('2.0/')) + def test_custom_template(self): self._deploy() with mock.patch('builtins.open',