1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365
|
# test_merge_drivers.py -- Tests for merge driver support
# Copyright (C) 2025 Jelmer Vernooij <jelmer@jelmer.uk>
#
# Dulwich is dual-licensed under the Apache License, Version 2.0 and the GNU
# General Public License as published by the Free Software Foundation; version 2.0
# or (at your option) any later version. You can redistribute it and/or
# modify it under the terms of either of these two licenses.
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# You should have received a copy of the licenses; if not, see
# <http://www.gnu.org/licenses/> for a copy of the GNU General Public License
# and <http://www.apache.org/licenses/LICENSE-2.0> for a copy of the Apache
# License, Version 2.0.
#
"""Tests for merge driver support."""
import importlib.util
import sys
import unittest
from dulwich.attrs import GitAttributes, Pattern
from dulwich.config import ConfigDict
from dulwich.merge import merge_blobs
from dulwich.merge_drivers import (
MergeDriverRegistry,
ProcessMergeDriver,
get_merge_driver_registry,
)
from dulwich.objects import Blob
from . import DependencyMissing
class _TestMergeDriver:
"""Test merge driver implementation."""
def __init__(self, name: str = "test"):
self.name = name
self.called = False
self.last_args = None
def merge(
self,
ancestor: bytes,
ours: bytes,
theirs: bytes,
path: str | None = None,
marker_size: int = 7,
) -> tuple[bytes, bool]:
"""Test merge implementation."""
self.called = True
self.last_args = {
"ancestor": ancestor,
"ours": ours,
"theirs": theirs,
"path": path,
"marker_size": marker_size,
}
# Simple test merge: combine all three versions
result = b"TEST MERGE OUTPUT\n"
result += b"Ancestor: " + ancestor + b"\n"
result += b"Ours: " + ours + b"\n"
result += b"Theirs: " + theirs + b"\n"
# Return success if all three are different
success = ancestor != ours and ancestor != theirs
return result, success
class MergeDriverRegistryTests(unittest.TestCase):
"""Tests for MergeDriverRegistry."""
def test_register_driver(self):
"""Test registering a merge driver."""
registry = MergeDriverRegistry()
driver = _TestMergeDriver("mydriver")
registry.register_driver("mydriver", driver)
retrieved = registry.get_driver("mydriver")
self.assertIs(retrieved, driver)
def test_register_factory(self):
"""Test registering a merge driver factory."""
registry = MergeDriverRegistry()
def create_driver():
return _TestMergeDriver("factory_driver")
registry.register_factory("factory", create_driver)
driver = registry.get_driver("factory")
self.assertIsInstance(driver, _TestMergeDriver)
self.assertEqual(driver.name, "factory_driver")
# Second call should return the same instance
driver2 = registry.get_driver("factory")
self.assertIs(driver2, driver)
def test_get_nonexistent_driver(self):
"""Test getting a non-existent driver returns None."""
registry = MergeDriverRegistry()
driver = registry.get_driver("nonexistent")
self.assertIsNone(driver)
def test_create_from_config(self):
"""Test creating a merge driver from configuration."""
config = ConfigDict()
config.set((b"merge", b"xmlmerge"), b"driver", b"xmlmerge %O %A %B")
registry = MergeDriverRegistry(config)
driver = registry.get_driver("xmlmerge")
self.assertIsInstance(driver, ProcessMergeDriver)
self.assertEqual(driver.name, "xmlmerge")
self.assertEqual(driver.command, "xmlmerge %O %A %B")
class ProcessMergeDriverTests(unittest.TestCase):
"""Tests for ProcessMergeDriver."""
def test_merge_with_echo(self):
"""Test merge driver using echo command."""
# Use a simple echo command that writes to the output file
command = "echo merged > %A"
driver = ProcessMergeDriver(command, "echo_driver")
ancestor = b"ancestor content"
ours = b"our content"
theirs = b"their content"
result, success = driver.merge(ancestor, ours, theirs, "test.txt", 7)
# Expect different line endings on Windows vs Unix
if sys.platform == "win32":
expected = b"merged \r\n"
else:
expected = b"merged\n"
self.assertEqual(result, expected)
self.assertTrue(success) # echo returns 0
def test_merge_with_cat(self):
"""Test merge driver using cat command."""
# Cat all three files together
command = "cat %O %B >> %A"
driver = ProcessMergeDriver(command, "cat_driver")
ancestor = b"ancestor\n"
ours = b"ours\n"
theirs = b"theirs\n"
result, success = driver.merge(ancestor, ours, theirs)
self.assertEqual(result, b"ours\nancestor\ntheirs\n")
self.assertTrue(success)
def test_merge_with_failure(self):
"""Test merge driver that fails."""
# Use false command which always returns 1
command = "false"
driver = ProcessMergeDriver(command, "fail_driver")
result, success = driver.merge(b"a", b"b", b"c")
# Should return original content on failure
self.assertEqual(result, b"b")
self.assertFalse(success)
def test_merge_with_markers(self):
"""Test merge driver with conflict marker size."""
# Echo the marker size
command = "echo marker size: %L > %A"
driver = ProcessMergeDriver(command, "marker_driver")
result, success = driver.merge(b"a", b"b", b"c", marker_size=15)
# Expect different line endings on Windows vs Unix
if sys.platform == "win32":
expected = b"marker size: 15 \r\n"
else:
expected = b"marker size: 15\n"
self.assertEqual(result, expected)
self.assertTrue(success)
def test_merge_with_path(self):
"""Test merge driver with file path."""
# Echo the path
command = "echo path: %P > %A"
driver = ProcessMergeDriver(command, "path_driver")
result, success = driver.merge(b"a", b"b", b"c", path="dir/file.xml")
# Expect different line endings on Windows vs Unix
if sys.platform == "win32":
expected = b"path: dir/file.xml \r\n"
else:
expected = b"path: dir/file.xml\n"
self.assertEqual(result, expected)
self.assertTrue(success)
class MergeBlobsWithDriversTests(unittest.TestCase):
"""Tests for merge_blobs with merge drivers."""
def setUp(self):
"""Set up test fixtures."""
# Check if merge3 module is available
if importlib.util.find_spec("merge3") is None:
raise DependencyMissing("merge3")
# Reset global registry
global _merge_driver_registry
from dulwich import merge_drivers
merge_drivers._merge_driver_registry = None
def test_merge_blobs_without_driver(self):
"""Test merge_blobs without any merge driver."""
base = Blob.from_string(b"base\ncontent\n")
ours = Blob.from_string(b"base\nour change\n")
theirs = Blob.from_string(b"base\ntheir change\n")
result, has_conflicts = merge_blobs(base, ours, theirs)
# Should use default merge and have conflicts
self.assertTrue(has_conflicts)
self.assertIn(b"<<<<<<< ours", result)
self.assertIn(b">>>>>>> theirs", result)
def test_merge_blobs_with_text_driver(self):
"""Test merge_blobs with 'text' merge driver (default)."""
base = Blob.from_string(b"base\ncontent\n")
ours = Blob.from_string(b"base\nour change\n")
theirs = Blob.from_string(b"base\ntheir change\n")
# Set up gitattributes
patterns = [(Pattern(b"*.txt"), {b"merge": b"text"})]
gitattributes = GitAttributes(patterns)
result, has_conflicts = merge_blobs(
base, ours, theirs, b"file.txt", gitattributes
)
# Should use default merge (text is the default)
self.assertTrue(has_conflicts)
self.assertIn(b"<<<<<<< ours", result)
def test_merge_blobs_with_custom_driver(self):
"""Test merge_blobs with custom merge driver."""
# Register a test driver
registry = get_merge_driver_registry()
test_driver = _TestMergeDriver("custom")
registry.register_driver("custom", test_driver)
base = Blob.from_string(b"base content")
ours = Blob.from_string(b"our content")
theirs = Blob.from_string(b"their content")
# Set up gitattributes
patterns = [(Pattern(b"*.xml"), {b"merge": b"custom"})]
gitattributes = GitAttributes(patterns)
result, has_conflicts = merge_blobs(
base, ours, theirs, b"file.xml", gitattributes
)
# Check that our test driver was called
self.assertTrue(test_driver.called)
self.assertEqual(test_driver.last_args["ancestor"], b"base content")
self.assertEqual(test_driver.last_args["ours"], b"our content")
self.assertEqual(test_driver.last_args["theirs"], b"their content")
self.assertEqual(test_driver.last_args["path"], "file.xml")
# Check result
self.assertIn(b"TEST MERGE OUTPUT", result)
self.assertFalse(
has_conflicts
) # Our test driver returns success=True when all differ, so had_conflicts=False
def test_merge_blobs_with_process_driver(self):
"""Test merge_blobs with process-based merge driver."""
# Set up config with merge driver
config = ConfigDict()
config.set((b"merge", b"union"), b"driver", b"echo process merge worked > %A")
base = Blob.from_string(b"base")
ours = Blob.from_string(b"ours")
theirs = Blob.from_string(b"theirs")
# Set up gitattributes
patterns = [(Pattern(b"*.list"), {b"merge": b"union"})]
gitattributes = GitAttributes(patterns)
result, has_conflicts = merge_blobs(
base, ours, theirs, b"file.list", gitattributes, config
)
# Check that the process driver was executed
# Expect different line endings on Windows vs Unix
if sys.platform == "win32":
expected = b"process merge worked \r\n"
else:
expected = b"process merge worked\n"
self.assertEqual(result, expected)
self.assertFalse(has_conflicts) # echo returns 0
def test_merge_blobs_driver_not_found(self):
"""Test merge_blobs when specified driver is not found."""
base = Blob.from_string(b"base")
ours = Blob.from_string(b"ours")
theirs = Blob.from_string(b"theirs")
# Set up gitattributes with non-existent driver
patterns = [(Pattern(b"*.dat"), {b"merge": b"nonexistent"})]
gitattributes = GitAttributes(patterns)
result, has_conflicts = merge_blobs(
base, ours, theirs, b"file.dat", gitattributes
)
# Should fall back to default merge
self.assertTrue(has_conflicts)
self.assertIn(b"<<<<<<< ours", result)
class GlobalRegistryTests(unittest.TestCase):
"""Tests for global merge driver registry."""
def setUp(self):
"""Reset global registry before each test."""
global _merge_driver_registry
from dulwich import merge_drivers
merge_drivers._merge_driver_registry = None
def test_get_merge_driver_registry_singleton(self):
"""Test that get_merge_driver_registry returns singleton."""
registry1 = get_merge_driver_registry()
registry2 = get_merge_driver_registry()
self.assertIs(registry1, registry2)
def test_get_merge_driver_registry_with_config(self):
"""Test updating config on existing registry."""
# Get registry without config
registry = get_merge_driver_registry()
self.assertIsNone(registry._config)
# Get with config
config = ConfigDict()
registry2 = get_merge_driver_registry(config)
self.assertIs(registry2, registry)
self.assertIsNotNone(registry._config)
if __name__ == "__main__":
unittest.main()
|