Skip to content

Commit a39ea73

Browse files
committed
Add set-default --allow-undefined; see #210
1 parent 9f56762 commit a39ea73

File tree

6 files changed

+29
-4
lines changed

6 files changed

+29
-4
lines changed

CHANGES.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
## v2.1.0 (in progress)
44

5+
### New features
6+
- When calling `set-default`, you can now pass `--allow-undefined` to set the
7+
default to a version that doesn't exist yet
8+
59
### Bug fixes
610
- When loading an MkDocs config, mike now runs the `startup` and `shutdown`
711
events

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,10 @@ version of the docs:
217217
mike set-default [identifier]
218218
```
219219

220+
Normally, this command will return an error if `identifier` doesn't exist. If
221+
you want to set the default to a version that doesn't exist yet, you can pass
222+
`--allow-undefined`.
223+
220224
If you want to use a different template from the default, you can pass
221225
`-T`/`--template`; this takes a path to a [Jinja][jinja] template that accepts
222226
an `{{href}}` variable. (Note that this page *always* uses a redirect, no matter

mike/commands.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -272,8 +272,9 @@ def retitle(identifier, title, *, branch='gh-pages', message=None,
272272
commit.add_file(versions_to_file_info(all_versions, deploy_prefix))
273273

274274

275-
def set_default(identifier, template=None, *, branch='gh-pages', message=None,
276-
allow_empty=False, deploy_prefix=''):
275+
def set_default(identifier, template=None, allow_undefined=False, *,
276+
branch='gh-pages', message=None, allow_empty=False,
277+
deploy_prefix=''):
277278
if message is None:
278279
message = (
279280
'Set default version to {doc_identifier}{deploy_prefix} with ' +
@@ -285,7 +286,7 @@ def set_default(identifier, template=None, *, branch='gh-pages', message=None,
285286
)
286287

287288
all_versions = list_versions(branch, deploy_prefix)
288-
if not all_versions.find(identifier):
289+
if not allow_undefined and not all_versions.find(identifier):
289290
raise ValueError('identifier {!r} does not exist'.format(identifier))
290291

291292
t = _redirect_template(template)

mike/driver.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -299,7 +299,8 @@ def set_default(parser, args):
299299
check_remote_status(args, strict=True)
300300
with handle_empty_commit():
301301
commands.set_default(args.identifier, args.template,
302-
branch=args.branch, message=args.message,
302+
args.allow_undefined, branch=args.branch,
303+
message=args.message,
303304
allow_empty=args.allow_empty,
304305
deploy_prefix=args.deploy_prefix)
305306
if args.push:
@@ -424,6 +425,9 @@ def main():
424425
set_default_p.set_defaults(func=set_default)
425426
set_default_p.add_argument('-T', '--template', complete='file',
426427
help='template file to use')
428+
set_default_p.add_argument('--allow-undefined', action='store_true',
429+
help=('allow setting undefined versions as ' +
430+
'default'))
427431
add_git_arguments(set_default_p)
428432
set_default_p.add_argument('identifier', metavar='IDENTIFIER',
429433
help='version or alias to set as default')

test/integration/test_set_default.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,12 @@ def test_set_default(self):
4343
check_call_silent(['git', 'checkout', 'gh-pages'])
4444
self._test_default()
4545

46+
def test_allow_undefined(self):
47+
self._deploy()
48+
assertPopen(['mike', 'set-default', '2.0', '--allow-undefined'])
49+
check_call_silent(['git', 'checkout', 'gh-pages'])
50+
self._test_default(match_redir('2.0/'))
51+
4652
def test_custom_template(self):
4753
self._deploy()
4854
assertPopen(['mike', 'set-default', '1.0', '-T',

test/unit/test_commands.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -752,6 +752,12 @@ def test_set_default(self):
752752
check_call_silent(['git', 'checkout', 'gh-pages'])
753753
self._test_default()
754754

755+
def test_allow_undefined(self):
756+
self._deploy()
757+
commands.set_default('2.0', allow_undefined=True)
758+
check_call_silent(['git', 'checkout', 'gh-pages'])
759+
self._test_default(match_redir('2.0/'))
760+
755761
def test_custom_template(self):
756762
self._deploy()
757763
with mock.patch('builtins.open',

0 commit comments

Comments
 (0)