|
| 1 | +# Copyright 2025 Leander Stephen Desouza |
| 2 | +# Licensed under the Apache License, Version 2.0 |
| 3 | + |
| 4 | +"""Integration tests for colcon mixin.""" |
| 5 | + |
| 6 | +import os |
| 7 | +from pathlib import Path |
| 8 | +import shutil |
| 9 | +import subprocess |
| 10 | +import sys |
| 11 | +import tempfile |
| 12 | + |
| 13 | +MIXIN_IDENTIFIER = 'demo' |
| 14 | + |
| 15 | +if sys.platform == 'win32': |
| 16 | + HOME = 'USERPROFILE' |
| 17 | +else: |
| 18 | + HOME = 'HOME' |
| 19 | + |
| 20 | + |
| 21 | +class TestMixinSubverbs: |
| 22 | + """Test the colcon mixin subverbs.""" |
| 23 | + |
| 24 | + def setup_method(self): |
| 25 | + """Set up test environment.""" |
| 26 | + test_index_path = Path(__file__).parent / 'index.yaml' |
| 27 | + self.test_url = test_index_path.absolute().as_uri() |
| 28 | + |
| 29 | + self.original_home = os.environ.get(HOME, '') |
| 30 | + |
| 31 | + # Override HOME to point to the temporary directory |
| 32 | + self.temp_dir = tempfile.mkdtemp(suffix='_colcon_mixin_test') |
| 33 | + os.environ[HOME] = self.temp_dir |
| 34 | + |
| 35 | + def teardown_method(self): |
| 36 | + """Clean up test environment.""" |
| 37 | + # Restore original home |
| 38 | + os.environ[HOME] = self.original_home |
| 39 | + shutil.rmtree(self.temp_dir, ignore_errors=True) |
| 40 | + |
| 41 | + def run_colcon_command(self, args): |
| 42 | + """Run a colcon mixin command and return the result.""" |
| 43 | + cmd = [sys.executable, '-m', 'colcon', 'mixin'] + args |
| 44 | + result = subprocess.run( |
| 45 | + cmd, |
| 46 | + capture_output=True, |
| 47 | + text=True, |
| 48 | + cwd=Path(__file__).parent.parent, |
| 49 | + check=False |
| 50 | + ) |
| 51 | + return result |
| 52 | + |
| 53 | + def test_add_success(self): |
| 54 | + """Test successfully adding a mixin repository.""" |
| 55 | + result = self.run_colcon_command( |
| 56 | + ['add', MIXIN_IDENTIFIER, self.test_url]) |
| 57 | + |
| 58 | + assert result.returncode == 0 |
| 59 | + assert result.stderr == '' |
| 60 | + |
| 61 | + def test_add_repository_invalid_url(self): |
| 62 | + """Test adding a repository with an invalid URL format.""" |
| 63 | + result = self.run_colcon_command( |
| 64 | + ['add', MIXIN_IDENTIFIER, 'invalid-url']) |
| 65 | + assert result.returncode != 0 |
| 66 | + assert "must contain '://'" in result.stderr |
| 67 | + |
| 68 | + def test_add_repository_invalid_name(self): |
| 69 | + """Test adding a repository with an invalid name.""" |
| 70 | + # Forward slash test |
| 71 | + result1 = self.run_colcon_command( |
| 72 | + ['add', f'{MIXIN_IDENTIFIER}/invalid', self.test_url]) |
| 73 | + assert result1.returncode != 0 |
| 74 | + assert "must not contain '/'" in result1.stderr |
| 75 | + |
| 76 | + # Backslash test |
| 77 | + result2 = self.run_colcon_command( |
| 78 | + ['add', f'{MIXIN_IDENTIFIER}\\invalid', self.test_url]) |
| 79 | + assert result2.returncode != 0 |
| 80 | + assert "must not contain '\\'" in result2.stderr |
| 81 | + |
| 82 | + def test_add_repository_duplicate_name(self): |
| 83 | + """Test adding a repository with a name that already exists.""" |
| 84 | + result1 = self.run_colcon_command( |
| 85 | + ['add', MIXIN_IDENTIFIER, self.test_url]) |
| 86 | + assert result1.returncode == 0 |
| 87 | + |
| 88 | + result2 = self.run_colcon_command( |
| 89 | + ['add', MIXIN_IDENTIFIER, self.test_url]) |
| 90 | + assert result2.returncode != 0 |
| 91 | + assert ("A repository with the name 'demo' " |
| 92 | + 'already exists') in result2.stderr |
| 93 | + |
| 94 | + def test_update_mixin(self): |
| 95 | + """Test updating a mixin repository.""" |
| 96 | + result1 = self.run_colcon_command( |
| 97 | + ['add', MIXIN_IDENTIFIER, self.test_url]) |
| 98 | + assert result1.returncode == 0 |
| 99 | + |
| 100 | + result2 = self.run_colcon_command(['update', MIXIN_IDENTIFIER]) |
| 101 | + assert result2.returncode == 0 |
| 102 | + assert f'fetching {MIXIN_IDENTIFIER}:' in result2.stdout |
| 103 | + assert 'build-type.mixin' in result2.stdout |
| 104 | + assert 'coverage.mixin' in result2.stdout |
| 105 | + |
| 106 | + def test_show_mixins(self): |
| 107 | + """Test showing available mixins against expected files.""" |
| 108 | + self.run_colcon_command(['add', MIXIN_IDENTIFIER, self.test_url]) |
| 109 | + self.run_colcon_command(['update', MIXIN_IDENTIFIER]) |
| 110 | + |
| 111 | + expected_demo_mixin = \ |
| 112 | + Path(__file__).parent / 'expected_mixins' / 'demo.txt' |
| 113 | + with expected_demo_mixin.open('r') as f: |
| 114 | + expected_content = f.read().strip() |
| 115 | + |
| 116 | + result = self.run_colcon_command(['show']) |
| 117 | + assert result.returncode == 0 |
| 118 | + assert result.stdout.strip() == expected_content |
| 119 | + |
| 120 | + def test_remove_repository_success(self): |
| 121 | + """Test successfully removing a mixin repository.""" |
| 122 | + result1 = self.run_colcon_command( |
| 123 | + ['add', MIXIN_IDENTIFIER, self.test_url]) |
| 124 | + assert result1.returncode == 0 |
| 125 | + |
| 126 | + result2 = self.run_colcon_command(['remove', MIXIN_IDENTIFIER]) |
| 127 | + assert result2.returncode == 0 |
| 128 | + |
| 129 | + def test_remove_repository_not_exists(self): |
| 130 | + """Test removing a repository that doesn't exist.""" |
| 131 | + result = self.run_colcon_command(['remove', MIXIN_IDENTIFIER]) |
| 132 | + assert result.returncode != 0 |
| 133 | + assert (f"A repository with the name '{MIXIN_IDENTIFIER}' " |
| 134 | + "doesn't exist") in result.stderr |
0 commit comments