172 lines
7.9 KiB
Python
172 lines
7.9 KiB
Python
import contextlib
|
|
import io
|
|
from pathlib import Path
|
|
import random
|
|
import string
|
|
import subprocess
|
|
import sys
|
|
import tempfile
|
|
import unittest
|
|
|
|
from fix_submodules import __version__, 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_initialized_submodule_remote(self):
|
|
source = self.root / 'source'
|
|
git('init', str(source))
|
|
git('-c', 'user.name=Test', '-c', 'user.email=test@example.com',
|
|
'-c', 'commit.gpgSign=false',
|
|
'commit', '--allow-empty', '-m', 'initial', cwd=source)
|
|
git('-c', 'protocol.file.allow=always', 'submodule', 'add',
|
|
str(source), 'checkout', cwd=self.root)
|
|
checkout = self.root / 'checkout'
|
|
git('remote', 'set-url', 'origin',
|
|
'https://proxy.dandaedre.com/gitea/Beffroi/reportist.git', cwd=checkout)
|
|
git('remote', 'add', 'origin', 'git@dandaedre.com:Beffroi/parent.git',
|
|
cwd=self.root)
|
|
config = self.root / '.git/modules/checkout/config'
|
|
before = config.read_bytes()
|
|
self.run_fix(dry_run=True)
|
|
self.assertEqual(config.read_bytes(), before)
|
|
self.run_fix()
|
|
self.assertEqual(git('remote', 'get-url', 'origin', cwd=checkout).strip(),
|
|
'git@git.dandaedre.com:Beffroi/reportist.git')
|
|
self.assertEqual(git('remote', 'get-url', 'origin', cwd=self.root).strip(),
|
|
'git@dandaedre.com:Beffroi/parent.git')
|
|
self.assertEqual(self.run_fix(), 0)
|
|
|
|
def test_nested_module_configs_and_multiple_remote_urls(self):
|
|
config = self.root / '.git/modules/parent/modules/child/config'
|
|
config.parent.mkdir(parents=True)
|
|
config.write_text(
|
|
'# Preserve settings\n[core]\n\tbare = false\n'
|
|
'[remote "origin"]\n'
|
|
'\turl = git@dandaedre.com:Beffroi/child.git\n'
|
|
'\turl = https://example.com/owner/mirror.git\n'
|
|
'\tpushurl = https://proxy.dandaedre.com/gitea/Beffroi/fork.git\n'
|
|
'\tfetch = +refs/heads/*:refs/remotes/origin/*\n'
|
|
'[submodule "nested"]\n'
|
|
'\turl = git@dandaedre.com:Beffroi/nested.git\n'
|
|
)
|
|
before = config.read_bytes()
|
|
self.run_fix(dry_run=True)
|
|
self.assertEqual(config.read_bytes(), before)
|
|
self.run_fix()
|
|
self.assertEqual(git('config', '--file', str(config), '--get-all',
|
|
'remote.origin.url').splitlines(),
|
|
['git@git.dandaedre.com:Beffroi/child.git',
|
|
'https://example.com/owner/mirror.git'])
|
|
self.assertIn('pushurl = git@git.dandaedre.com:Beffroi/fork.git', config.read_text())
|
|
self.assertEqual(read_urls(config)['submodule.nested.url'],
|
|
'git@git.dandaedre.com:Beffroi/nested.git')
|
|
self.assertIn('# Preserve settings', config.read_text())
|
|
self.assertIn('fetch = +refs/heads/*:refs/remotes/origin/*', config.read_text())
|
|
self.assertEqual(self.run_fix(), 0)
|
|
|
|
def test_linked_worktree(self):
|
|
git('-c', 'user.name=Test', '-c', 'user.email=test@example.com',
|
|
'-c', 'commit.gpgSign=false',
|
|
'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))
|
|
|
|
def test_malformed_urls_are_rejected(self):
|
|
malformed = (
|
|
None, 42, b'git@git.dandaedre.com:owner/repo.git', '', ' ',
|
|
'https://[broken/owner/repo.git',
|
|
'https://git.dandaedre.com/owner/repo.git?download=1',
|
|
'git@git.dandaedre.com:owner\\repo.git',
|
|
'git@git.dandaedre.com:owner/repo.git\nother',
|
|
'git@git.dandaedre.com:owner/../repo.git',
|
|
)
|
|
for value in malformed:
|
|
with self.subTest(value=value):
|
|
self.assertIsNone(repository_path(value))
|
|
|
|
def test_fuzzed_url_shapes_do_not_raise(self):
|
|
randomizer = random.Random(0)
|
|
alphabet = string.ascii_letters + string.digits + ':/.@?#[]\\\x00\n\t'
|
|
for _ in range(500):
|
|
value = ''.join(randomizer.choices(alphabet,
|
|
k=randomizer.randrange(0, 100)))
|
|
result = repository_path(value)
|
|
if result is not None:
|
|
self.assertGreaterEqual(len(result.split('/')), 2)
|
|
self.assertNotIn('..', result.split('/'))
|
|
|
|
def test_cli_version(self):
|
|
script = Path(__file__).resolve().parents[1] / 'fix_submodules.py'
|
|
result = subprocess.run(
|
|
[sys.executable, str(script), '--version'],
|
|
text=True, capture_output=True, check=True,
|
|
)
|
|
self.assertEqual(result.stdout.strip(), __version__)
|
|
|
|
|
|
if __name__ == '__main__':
|
|
unittest.main()
|