repo-fix/tests/test_fix_submodules.py

83 lines
3.7 KiB
Python

import contextlib
import io
from pathlib import Path
import tempfile
import unittest
from fix_submodules import fix, git, read_urls, repository_path
class FixTests(unittest.TestCase):
def setUp(self):
directory = tempfile.TemporaryDirectory()
self.addCleanup(directory.cleanup)
self.root = Path(directory.name)
git('init', str(self.root))
self.modules = self.root / '.gitmodules'
self.config = self.root / '.git/config'
self.modules.write_text(
'# Keep this comment\n[submodule "reportist"]\n'
'\tpath = reportist\n'
'\turl = git@dandaedre.com:Beffroi/reportist.git\n'
'[submodule "other"]\n\tpath = other\n'
'\turl = https://example.com/owner/other.git\n'
)
git('config', '--file', str(self.config), 'submodule.reportist.active', 'true')
def run_fix(self, **kwargs):
with contextlib.redirect_stdout(io.StringIO()):
return fix(self.root, **kwargs)
def test_dry_run_apply_and_repeat(self):
before = (self.modules.read_bytes(), self.config.read_bytes())
self.assertEqual(self.run_fix(dry_run=True), 2)
self.assertEqual(before, (self.modules.read_bytes(), self.config.read_bytes()))
self.assertEqual(self.run_fix(), 2)
self.assertEqual(read_urls(self.modules)['submodule.reportist.url'],
'https://proxy.dandaedre.com/gitea/Beffroi/reportist.git')
self.assertEqual(read_urls(self.config)['submodule.reportist.url'],
'git@git.dandaedre.com:Beffroi/reportist.git')
self.assertIn('# Keep this comment', self.modules.read_text())
self.assertIn('active = true', self.config.read_text())
self.assertEqual(read_urls(self.modules)['submodule.other.url'],
'https://example.com/owner/other.git')
self.assertEqual(self.run_fix(), 0)
def test_existing_local_path(self):
for url in ('https://proxy.dandaedre.com/gitea/Beffroi/fork.git',
'git@dandaedre.com:Beffroi/fork.git'):
with self.subTest(url=url):
git('config', '--file', str(self.config), 'submodule.reportist.url', url)
self.run_fix()
self.assertEqual(read_urls(self.config)['submodule.reportist.url'],
'git@git.dandaedre.com:Beffroi/fork.git')
def test_missing_gitmodules(self):
self.modules.unlink()
self.assertEqual(self.run_fix(), 0)
def test_linked_worktree(self):
git('-c', 'user.name=Test', '-c', 'user.email=test@example.com',
'commit', '--allow-empty', '-m', 'initial', cwd=self.root)
linked = self.root / 'linked'
git('worktree', 'add', '-b', 'linked', str(linked), cwd=self.root)
(linked / '.gitmodules').write_bytes(self.modules.read_bytes())
with contextlib.redirect_stdout(io.StringIO()):
self.assertEqual(fix(linked), 2)
self.assertEqual(read_urls(self.config)['submodule.reportist.url'],
'git@git.dandaedre.com:Beffroi/reportist.git')
def test_url_forms(self):
for url in ('git@git.dandaedre.com:Beffroi/reportist.git',
'ssh://git@dandaedre.com/Beffroi/reportist.git',
'https://proxy.dandaedre.com/gitea/Beffroi/reportist.git',
'git://git.dandaedre.com/Beffroi/reportist.git'):
self.assertEqual(repository_path(url), 'Beffroi/reportist.git')
for url in ('../other.git', 'git@example.com:owner/repo.git',
'https://dandaedre.com.evil.example/owner/repo.git'):
self.assertIsNone(repository_path(url))
if __name__ == '__main__':
unittest.main()