File size: 1,754 Bytes
a402b9b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Test dumper.apply_source_patches() integration with source_patcher."""

from pathlib import Path
from types import ModuleType

import yaml

from sglang.srt.debug_utils.dumper import DumperConfig, _Dumper
from sglang.test.ci.ci_register import register_cpu_ci

register_cpu_ci(est_time=10, suite="default", nightly=True)

SAMPLE_MODULE_NAME = "_source_patcher_test_fixtures.sample_module"


class TestDumperApplySourcePatches:
    def test_no_config_is_noop(self) -> None:
        config = DumperConfig(source_patcher_config=None)
        d = _Dumper(config=config)
        d.apply_source_patches()

    def test_patches_applied_from_yaml(
        self, sample_module: ModuleType, tmp_path: Path
    ) -> None:
        cls = sample_module.SampleClass
        obj = cls()
        assert obj.greet("world") == "hello world"

        original_code = cls.greet.__code__

        patch_config = {
            "patches": [
                {
                    "target": f"{SAMPLE_MODULE_NAME}.SampleClass.greet",
                    "edits": [
                        {
                            "match": 'greeting = f"hello {name}"',
                            "replacement": 'greeting = f"dumper_patched {name}"',
                        }
                    ],
                }
            ]
        }

        config_path = tmp_path / "patch_config.yaml"
        config_path.write_text(yaml.dump(patch_config))

        config = DumperConfig(source_patcher_config=str(config_path))
        d = _Dumper(config=config)

        try:
            d.apply_source_patches()
            assert obj.greet("world") == "dumper_patched world"
        finally:
            cls.greet.__code__ = original_code

        assert obj.greet("world") == "hello world"