Skip to content

Commit

Permalink
Allow new versions to replace existing aliases when `--update-aliases…
Browse files Browse the repository at this point in the history
…` is set
  • Loading branch information
Wolf2323 authored and jimporter committed Nov 2, 2023
1 parent 6de61fa commit 0481f92
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 6 deletions.
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -120,9 +120,11 @@ mike deploy [version] [alias]...
If `[version]` already exists, this command will *also* update all of the
pre-existing aliases for it. Normally, if an alias specified on the command line
is already associated with another version, this will return an error. If you
*do* want to move an alias from another version to this version (e.g. when
*do* want to move an alias from another version to this version (including
when the new version itself was previously an alias), you can pass
`-u`/`--update-aliases` to allow this. For example, this can be useful when
releasing a new version and updating the `latest` alias to point to this new
version), you can pass `-u`/`--update-aliases` to allow this.
version.

By default, each alias creates a symbolic link to the base directory of the real
version of the docs; to create a copy of the docs for each alias, you can pass
Expand Down
12 changes: 10 additions & 2 deletions mike/versions.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,15 @@ def find(self, identifier, strict=False):

def _ensure_unique_aliases(self, version, aliases, update_aliases=False):
removed_aliases = []

# Check whether `version` is already defined as an alias.
key = self.find(version)
if key and len(key) == 2:
if not update_aliases:
raise ValueError('version {!r} already exists'.format(version))
removed_aliases.append(key)

# Check whether any `aliases` are already defined.
for i in aliases:
key = self.find(i)
if key and key[0] != version:
Expand All @@ -150,6 +159,7 @@ def _ensure_unique_aliases(self, version, aliases, update_aliases=False):
.format(i, str(key[0]))
)
removed_aliases.append(key)

return removed_aliases

def add(self, version, title=None, aliases=[], update_aliases=False):
Expand All @@ -161,8 +171,6 @@ def add(self, version, title=None, aliases=[], update_aliases=False):
if v in self._data:
self._data[v].update(title, aliases)
else:
if self.find(version):
raise ValueError('version {!r} already exists'.format(version))
self._data[v] = VersionInfo(version, title, aliases)

# Remove aliases from old versions that we've moved to this version.
Expand Down
9 changes: 9 additions & 0 deletions test/integration/test_deploy.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,15 @@ def test_update_aliases(self):
versions.VersionInfo('1.0'),
])

def test_update_aliases_with_version(self):
assertPopen(['mike', 'deploy', '1.0b1', '1.0'])
assertPopen(['mike', 'deploy', '1.0', 'latest', '-u'])
check_call_silent(['git', 'checkout', 'gh-pages'])
self._test_deploy(expected_versions=[
versions.VersionInfo('1.0', aliases=['latest']),
versions.VersionInfo('1.0b1'),
])

def test_from_subdir(self):
os.mkdir('sub')
with pushd('sub'):
Expand Down
8 changes: 6 additions & 2 deletions test/unit/test_versions.py
Original file line number Diff line number Diff line change
Expand Up @@ -354,11 +354,15 @@ def test_add_overwrite_version_with_alias(self):
def test_add_overwrite_alias_with_version(self):
versions = Versions()
versions.add('1.0b1', aliases=['1.0'])

msg = r"version '1\.0' already exists"
with self.assertRaisesRegex(ValueError, msg):
versions.add('1.0')
with self.assertRaisesRegex(ValueError, msg):
versions.add('1.0', update_aliases=True)
versions.add('1.0', update_aliases=True)
self.assertEqual(list(versions), [
VersionInfo('1.0'),
VersionInfo('1.0b1'),
])

def test_add_invalid(self):
versions = Versions()
Expand Down

0 comments on commit 0481f92

Please sign in to comment.