Skip to content

Commit

Permalink
Consult deploy prefix when deleting files during deploy; resolves #227
Browse files Browse the repository at this point in the history
Co-Authored-By: Jonas Fierlings <fnoegip@gmail.com>
  • Loading branch information
jimporter and PigeonF committed Jul 27, 2024
1 parent 91cf5ee commit dd9826a
Show file tree
Hide file tree
Showing 3 changed files with 123 additions and 15 deletions.
2 changes: 1 addition & 1 deletion mike/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ def deploy(cfg, version, title=None, aliases=[], update_aliases=False,
t = _redirect_template(template)

with git_utils.Commit(branch, message, allow_empty=allow_empty) as commit:
commit.delete_files([version_str] + list(info.aliases))
commit.delete_files([destdir] + alias_destdirs)

for f in git_utils.walk_real_files(cfg['site_dir']):
canonical_file = f.copy(destdir, cfg['site_dir'])
Expand Down
3 changes: 2 additions & 1 deletion test/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,8 @@ def match_redir(url):
return r'window\.location\.replace\(\s*"{}"'.format(re.escape(url))


def assertDirectory(path, contents, include_hidden=False, allow_extra=False):
def assertDirectory(path, contents, *, include_hidden=False,
allow_extra=False):
path = os.path.normpath(path)
actual = set(os.path.normpath(os.path.join(base, f))
for base, dirs, files in walk(path, include_hidden)
Expand Down
133 changes: 120 additions & 13 deletions test/unit/test_commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,8 @@ def setUp(self):
commit_files(['page.html', 'file.txt', 'dir/index.html'])

def _test_state(self, expected_message, expected_versions,
alias_type=AliasType.symlink, directory='.'):
alias_type=AliasType.symlink, directory='.', *,
allow_extra=False):
message = check_output(['git', 'log', '-1', '--pretty=%B']).rstrip()
self.assertRegex(message, expected_message)

Expand All @@ -68,7 +69,7 @@ def _test_state(self, expected_message, expected_versions,
a + '/dir/index.html'}
if this_alias_type == AliasType.copy:
files.add(a + '/file.txt')
assertDirectory(directory, files)
assertDirectory(directory, files, allow_extra=allow_extra)

with open(os.path.join(directory, 'versions.json')) as f:
self.assertEqual(list(versions.Versions.loads(f.read())),
Expand All @@ -83,6 +84,8 @@ def setUp(self):
self.cfg['site_dir'] = os.path.join(self.cfg['site_dir'], 'site')

def _mock_build(self):
if os.path.exists(self.cfg['site_dir']):
shutil.rmtree(self.cfg['site_dir'])
copytree(self.stage, self.cfg['site_dir'])

def _test_deploy(self, expected_message=None,
Expand Down Expand Up @@ -227,6 +230,21 @@ def test_deploy_prefix(self):
versions.VersionInfo('1.0', aliases=['latest'])
], directory='prefix')

def test_mixed_deploy_prefixes(self):
with commands.deploy(self.cfg, '1.0', aliases=['latest']):
self._mock_build()
with commands.deploy(self.cfg, '1.0', aliases=['latest'],
deploy_prefix='prefix'):
self._mock_build()

check_call_silent(['git', 'checkout', 'gh-pages'])
self._test_deploy(expected_versions=[
versions.VersionInfo('1.0', aliases=['latest'])
], expected_message='.*', allow_extra=True)
self._test_deploy(expected_versions=[
versions.VersionInfo('1.0', aliases=['latest'])
], directory='prefix')

def test_overwrite_version(self):
with git_utils.Commit('gh-pages', 'add versions.json') as commit:
commit.add_file(git_utils.FileInfo(
Expand Down Expand Up @@ -357,6 +375,18 @@ def test_deploy_prefix(self):
check_call_silent(['git', 'checkout', 'gh-pages'])
self._test_delete(directory='prefix')

def test_mixed_deploy_prefixes(self):
self._deploy()
self._deploy(deploy_prefix='prefix')
commands.delete(['1.0'], deploy_prefix='prefix')
check_call_silent(['git', 'checkout', 'gh-pages'])

self._test_delete(expected_versions=[
versions.VersionInfo('2.0'),
versions.VersionInfo('1.0', aliases=['stable']),
], expected_message='.*', allow_extra=True)
self._test_delete(directory='prefix')

def test_deploy_prefix_delete_all(self):
self._deploy(deploy_prefix='prefix')
commands.delete(all=True, deploy_prefix='prefix')
Expand All @@ -367,6 +397,22 @@ def test_deploy_prefix_delete_all(self):
r'^Removed everything in prefix with mike \S+$')
assertDirectory('prefix', set())

def test_mixed_deploy_prefixes_delete_all(self):
self._deploy()
self._deploy(deploy_prefix='prefix')
commands.delete(all=True, deploy_prefix='prefix')
check_call_silent(['git', 'checkout', 'gh-pages'])

self._test_delete(expected_versions=[
versions.VersionInfo('2.0'),
versions.VersionInfo('1.0', aliases=['stable']),
], expected_message='.*', allow_extra=True)

message = check_output(['git', 'log', '-1', '--pretty=%B']).rstrip()
self.assertRegex(message,
r'^Removed everything in prefix with mike \S+$')
assertDirectory('prefix', set())

def test_delete_invalid(self):
self._deploy()
self.assertRaises(ValueError, commands.delete)
Expand Down Expand Up @@ -510,6 +556,16 @@ def test_deploy_prefix(self):
check_call_silent(['git', 'checkout', 'gh-pages'])
self._test_alias(directory='prefix')

def test_mixed_deploy_prefixes(self):
self._deploy()
self._deploy(deploy_prefix='prefix')
commands.alias(self.cfg, '1.0', ['greatest'], deploy_prefix='prefix')
check_call_silent(['git', 'checkout', 'gh-pages'])

self._test_alias(expected_aliases=[], expected_message='.*',
allow_extra=True)
self._test_alias(directory='prefix')

def test_alias_invalid_version(self):
self._deploy()
self.assertRaises(ValueError, commands.alias, self.cfg, '2.0',
Expand Down Expand Up @@ -568,6 +624,20 @@ def test_deploy_prefix(self):
{'hidden': True}
)

def test_mixed_deploy_prefixes(self):
self._commit_versions(versions.VersionInfo(
'1.0', properties={'foo': True}
))
self._commit_versions(versions.VersionInfo(
'1.0', properties={'bar': True}
), deploy_prefix='prefix')

self.assertEqual(commands.get_property('1.0', ''), {'foo': True})
self.assertEqual(
commands.get_property('1.0', '', deploy_prefix='prefix'),
{'bar': True}
)

def test_invalid_version(self):
self._commit_versions(versions.VersionInfo('1.0'))
with self.assertRaises(ValueError):
Expand Down Expand Up @@ -652,6 +722,23 @@ def test_deploy_prefix(self):
versions.VersionInfo('1.0', properties={'foo': {'bar': True}}),
], directory='prefix')

def test_mixed_deploy_prefixes(self):
self._commit_versions(versions.VersionInfo(
'1.0', properties={'foo': {'bar': True}}
))
self._commit_versions(versions.VersionInfo('1.0'),
deploy_prefix='prefix')
commands.set_properties('1.0', [('zoo.goat', True)],
deploy_prefix='prefix')

check_call_silent(['git', 'checkout', 'gh-pages'])
self._test_set_properties([
versions.VersionInfo('1.0', properties={'foo': {'bar': True}}),
])
self._test_set_properties([
versions.VersionInfo('1.0', properties={'zoo': {'goat': True}}),
], directory='prefix')

def test_invalid_version(self):
self._commit_versions(versions.VersionInfo('1.0'))
with self.assertRaises(ValueError):
Expand All @@ -670,25 +757,26 @@ def _deploy(self, branch='gh-pages', deploy_prefix=''):
deploy_prefix=deploy_prefix):
pass

def _test_retitle(self, expected_message=None, directory='.'):
def _test_retitle(self, expected_message=None,
expected_version=versions.VersionInfo('1.0', '1.0.1'),
directory='.', *, allow_extra=False):
message = check_output(['git', 'log', '-1', '--pretty=%B']).rstrip()
if expected_message:
self.assertEqual(message, expected_message)
else:
self.assertRegex(
message,
r'^Set title of \S+ to 1\.0\.1( in .*)? with mike \S+$'
if not expected_message:
expected_message = (
r'^Set title of {} to {}( in .*)? with mike \S+$'
.format(re.escape(str(expected_version.version)),
re.escape(expected_version.title))
)
self.assertRegex(message, expected_message)

assertDirectory(directory, {
'versions.json',
'1.0',
'1.0/file.txt'
})
}, allow_extra=allow_extra)
with open(os.path.join(directory, 'versions.json')) as f:
self.assertEqual(list(versions.Versions.loads(f.read())), [
versions.VersionInfo('1.0', '1.0.1'),
])
self.assertEqual(list(versions.Versions.loads(f.read())),
[expected_version])

def test_retitle(self):
self._deploy()
Expand All @@ -714,6 +802,16 @@ def test_deploy_prefix(self):
check_call_silent(['git', 'checkout', 'gh-pages'])
self._test_retitle(directory='prefix')

def test_mixed_deploy_prefixes(self):
self._deploy()
self._deploy(deploy_prefix='prefix')
commands.retitle('1.0', '1.0.1', deploy_prefix='prefix')
check_call_silent(['git', 'checkout', 'gh-pages'])

self._test_retitle(expected_version=versions.VersionInfo('1.0'),
expected_message='.*', allow_extra=True)
self._test_retitle(directory='prefix')

def test_retitle_invalid(self):
self._deploy()
self.assertRaises(ValueError, commands.retitle, '2.0', '2.0.2')
Expand Down Expand Up @@ -785,6 +883,15 @@ def test_deploy_prefix(self):
check_call_silent(['git', 'checkout', 'gh-pages'])
self._test_default(directory='prefix')

def test_mixed_deploy_prefixes(self):
self._deploy()
self._deploy(deploy_prefix='prefix')
commands.set_default('1.0', deploy_prefix='prefix')
check_call_silent(['git', 'checkout', 'gh-pages'])

self.assertFalse(os.path.exists('./index.html'))
self._test_default(directory='prefix')

def test_set_invalid_default(self):
self._deploy()
self.assertRaises(ValueError, commands.set_default, '2.0')
Expand Down

0 comments on commit dd9826a

Please sign in to comment.