"""Opt-in network test; run with unittest discover -s integration -v.""" import os from pathlib import Path import subprocess import sys import tempfile import unittest from fix_submodules import git, read_urls, repository_path SCRIPT = Path(__file__).resolve().parents[1] / 'fix_submodules.py' CLONE_URL = 'https://proxy.dandaedre.com/gitea/Ravenous/ravenous_roll20.git' class CloneTest(unittest.TestCase): def test_real_repository(self): with tempfile.TemporaryDirectory(prefix='repo-fix-ci-') as directory: root = Path(directory) / 'ravenous' subprocess.run( ['git', 'clone', '--depth', '1', '--no-recurse-submodules', CLONE_URL, str(root)], env=dict(os.environ, GIT_TERMINAL_PROMPT='0'), check=True, timeout=120, ) modules = root / '.gitmodules' config = root / '.git/config' original = read_urls(modules) paths = {key: repository_path(url) for key, url in original.items() if repository_path(url) is not None} self.assertTrue(paths, 'Expected at least one Dandaedre submodule to exercise') def run_fix(*args): subprocess.run([sys.executable, str(SCRIPT), str(root), *args], check=True, timeout=30) def snapshot(): return modules.read_bytes(), config.read_bytes() def check_urls(): public, local = read_urls(modules), read_urls(config) for key, path in paths.items(): self.assertEqual(public[key], 'https://proxy.dandaedre.com/gitea/' + path) self.assertEqual(local[key], 'git@git.dandaedre.com:' + path) for key, url in original.items(): if key not in paths: self.assertEqual(public[key], url) before = snapshot() run_fix('--dry-run') self.assertEqual(snapshot(), before, 'Dry run changed files') run_fix() check_urls() fixed = snapshot() run_fix() self.assertEqual(snapshot(), fixed, 'Second run changed files') # Reproduce Git overwriting local URLs with the CI HTTPS URLs. git('submodule', 'init', cwd=root) git('submodule', 'sync', cwd=root) for key, path in paths.items(): self.assertEqual(read_urls(config)[key], 'https://proxy.dandaedre.com/gitea/' + path) run_fix() check_urls() # Exercise legacy hostnames using real submodule names and paths. for file in (modules, config): for key, path in paths.items(): git('config', '--file', str(file), key, 'git@dandaedre.com:' + path) run_fix() check_urls() if __name__ == '__main__': unittest.main()