Skip to content

Commit

Permalink
Add set-default --allow-undefined; see #210
Browse files Browse the repository at this point in the history
  • Loading branch information
jimporter committed Feb 29, 2024
1 parent 9f56762 commit a39ea73
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 4 deletions.
4 changes: 4 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 4 additions & 3 deletions mike/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 ' +
Expand All @@ -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)
Expand Down
6 changes: 5 additions & 1 deletion mike/driver.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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')
Expand Down
6 changes: 6 additions & 0 deletions test/integration/test_set_default.py
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
6 changes: 6 additions & 0 deletions test/unit/test_commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down

0 comments on commit a39ea73

Please sign in to comment.