Skip to content

Unable to apply hunk to file without trailing newline #132

Open
@bnavigator

Description

Dephell needs this fix to work with bowler: dephell/dephell#474

Otherwise:

========================================= test session starts =========================================
platform linux -- Python 3.8.5, pytest-6.0.1, py-1.9.0, pluggy-0.13.1
rootdir: /home/abuild/rpmbuild/BUILD/dephell-0.8.3, configfile: setup.cfg
plugins: requests-mock-1.8.0
collected 1 item                                                                                      

tests/test_commands/test_vendor_import.py F
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> captured log >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
WARNING  dephell.controllers._graph:_graph.py:82 empty root passed
ERROR    bowler.tool:tool.py:361 failed to apply patch hunk: Failed to apply with offset at 0
Traceback (most recent call last):
  File "/usr/lib/python3.8/site-packages/bowler/tool.py", line 359, in apply_hunks
    new_data = apply_single_file(data, accepted_hunks)
  File "/usr/lib/python3.8/site-packages/moreorless/patch.py", line 18, in apply_single_file
    return "".join(_apply_hunks(lines, hunks, allow_offsets))
  File "/usr/lib/python3.8/site-packages/moreorless/patch.py", line 114, in _apply_hunks
    raise PatchException(f"Failed to apply with offset at {cur_line}")
moreorless.patch.PatchException: Failed to apply with offset at 0
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> traceback >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

temp_path = PosixPath('/tmp/pytest-of-abuild/pytest-16/test_patch_imports0')

    @pytest.mark.skipif(IS_WINDOWS, reason='unsupported on windows')
    def test_patch_imports(temp_path: Path):
        (temp_path / 'project').mkdir()
        (temp_path / 'project' / '__init__.py').write_text('import requests\nimport django')
        (temp_path / 'project' / 'vendor' / 'requests').mkdir(parents=True)
        (temp_path / 'project' / 'vendor' / 'requests' / '__init__.py').touch()
    
        config = Config()
        config.attach(dict(project=str(temp_path)))
        package = PackageRoot(name='project', path=temp_path)
        root = RootDependency(raw_name='project', package=package)
        resolver = Resolver(
            graph=Graph(root),
            mutator=Mutator(),
        )
        command = VendorImportCommand(argv=[], config=config)
        command._patch_imports(
            resolver=resolver,
            output_path=temp_path / 'project' / 'vendor',
        )
    
        expected = 'import project.vendor.requests as requests\nimport django'
>       assert (temp_path / 'project' / '__init__.py').read_text() == expected
E       AssertionError: assert 'import reque...import django' == 'import proje...import django'
E         - import project.vendor.requests as requests
E         + import requests
E           import django

The hunk is:

> /usr/lib/python3.8/site-packages/bowler/tool.py(359)apply_hunks()
-> new_data = apply_single_file(data, accepted_hunks)
(Pdb) data
'import requests\nimport django'
(Pdb) accepted_hunks
'--- /tmp/pytest-of-abuild/pytest-18/test_patch_imports0/project/__init__.py\n+++ /tmp/pytest-of-abuild/pytest-18/test_patch_imports0/project/__init__.py\n@@ -1,2 +1,2 @@\n-import requests\n+import project.vendor.requests as requests\n import django\n'
(Pdb) print(accepted_hunks)
--- /tmp/pytest-of-abuild/pytest-18/test_patch_imports0/project/__init__.py
+++ /tmp/pytest-of-abuild/pytest-18/test_patch_imports0/project/__init__.py
@@ -1,2 +1,2 @@
-import requests
+import project.vendor.requests as requests
 import django

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions