def test_as_pep440_requirement_dir() -> None: direct_url = DirectUrl( url="file:///home/user/project", info=DirInfo(editable=False), ) direct_url.validate() assert (direct_url_as_pep440_direct_reference( direct_url, "pkg") == "pkg @ file:///home/user/project")
def test_to_json(): direct_url = DirectUrl( url="file:///home/user/archive.tgz", info=ArchiveInfo(), ) direct_url.validate() assert direct_url.to_json() == ( '{"archive_info": {}, "url": "file:///home/user/archive.tgz"}')
def test_as_pep440_requirement_editable_dir() -> None: # direct_url_as_pep440_direct_reference behaves the same # irrespective of the editable flag. It's the responsibility of # callers to render it as editable direct_url = DirectUrl( url="file:///home/user/project", info=DirInfo(editable=True), ) direct_url.validate() assert (direct_url_as_pep440_direct_reference( direct_url, "pkg") == "pkg @ file:///home/user/project")
def test_as_pep440_requirement_vcs(): direct_url = DirectUrl( url="https:///g.c/u/p.git", info=VcsInfo(vcs="git", commit_id="1b8c5bc61a86f377fea47b4276c8c8a5842d2220")) direct_url.validate() assert (direct_url_as_pep440_direct_reference( direct_url, "pkg") == "pkg @ git+https:///g.c/u/p.git" "@1b8c5bc61a86f377fea47b4276c8c8a5842d2220") direct_url.subdirectory = "subdir" direct_url.validate() assert (direct_url_as_pep440_direct_reference( direct_url, "pkg") == "pkg @ git+https:///g.c/u/p.git" "@1b8c5bc61a86f377fea47b4276c8c8a5842d2220#subdirectory=subdir")
def direct_url_as_pep440_direct_reference(direct_url: DirectUrl, name: str) -> str: """Convert a DirectUrl to a pip requirement string.""" direct_url.validate() # if invalid, this is a pip bug requirement = name + " @ " fragments = [] if isinstance(direct_url.info, VcsInfo): requirement += "{}+{}@{}".format(direct_url.info.vcs, direct_url.url, direct_url.info.commit_id) elif isinstance(direct_url.info, ArchiveInfo): requirement += direct_url.url if direct_url.info.hash: fragments.append(direct_url.info.hash) else: assert isinstance(direct_url.info, DirInfo) requirement += direct_url.url if direct_url.subdirectory: fragments.append("subdirectory=" + direct_url.subdirectory) if fragments: requirement += "#" + "&".join(fragments) return requirement
def test_as_pep440_requirement_archive(): direct_url = DirectUrl( url="file:///home/user/archive.tgz", info=ArchiveInfo(), ) direct_url.validate() assert (direct_url_as_pep440_direct_reference( direct_url, "pkg") == "pkg @ file:///home/user/archive.tgz") direct_url.subdirectory = "subdir" direct_url.validate() assert (direct_url_as_pep440_direct_reference( direct_url, "pkg") == "pkg @ file:///home/user/archive.tgz#subdirectory=subdir") direct_url.info.hash = "sha1=1b8c5bc61a86f377fea47b4276c8c8a5842d2220" direct_url.validate() assert ( direct_url_as_pep440_direct_reference( direct_url, "pkg") == "pkg @ file:///home/user/archive.tgz" "#sha1=1b8c5bc61a86f377fea47b4276c8c8a5842d2220&subdirectory=subdir")