import re
from typing import Optional

from pip._internal.models.direct_url import DIRECT_URL_METADATA_NAME, DirectUrl
from tests.lib import TestPipResult


def get_created_direct_url(result: TestPipResult, pkg: str) -> Optional[DirectUrl]:
    direct_url_metadata_re = re.compile(
        pkg + r"-[\d\.]+\.dist-info." + DIRECT_URL_METADATA_NAME + r"$"
    )
    for filename in result.files_created:
        if direct_url_metadata_re.search(filename):
            direct_url_path = result.test_env.base_path / filename
            with open(direct_url_path) as f:
                return DirectUrl.from_json(f.read())
    return None

@@ -148,16 +148,48 @@
 pResult%0A
+from tests.lib.path import Path%0A
 %0A%0Adef ge
@@ -208,16 +208,21 @@
 rect_url
+_path
 (result:
@@ -259,25 +259,20 @@
 ptional%5B
-DirectUrl
+Path
 %5D:%0A    d
@@ -495,25 +495,14 @@
     
-direct_url_path =
+return
  res
@@ -535,24 +535,209 @@
 ilename%0A    
+return None%0A%0A%0Adef get_created_direct_url(result: TestPipResult, pkg: str) -%3E Optional%5BDirectUrl%5D:%0A    direct_url_path = get_created_direct_url_path(result, pkg)%0A    if direct_url_path:%0A
         with
@@ -765,20 +765,16 @@
 ) as f:%0A
-    
         

Refactor direct_url test helper