def test_get_file_hash_without_interfering_with_each_other( from_line, pypi_repository): """ The PyPIRepository._get_file_hash() used to call unpack_url(), when generating the hash. Unpacking both packages to the same directory will then fail. E.g. matplotlib-2.0.2.tar.gz has a directory named LICENSE, but many other packages have a file named LICENSE. See GH-512 and GH-544. """ assert (pypi_repository._get_file_hash( Link( "https://files.pythonhosted.org/packages/" "f5/f0/9da3ef24ea7eb0ccd12430a261b66eca36b924aeef06e17147f9f9d7d310/" "matplotlib-2.0.2.tar.gz") ) == "sha256:0ffbc44faa34a8b1704bc108c451ecf87988f900ef7ce757b8e2e84383121ff1" ) assert (pypi_repository._get_file_hash( Link( "https://files.pythonhosted.org/packages/" "a1/32/e3d6c3a8b5461b903651dd6ce958ed03c093d2e00128e3f33ea69f1d7965/" "cffi-1.9.1.tar.gz") ) == "sha256:563e0bd53fda03c151573217b3a49b3abad8813de9dd0632e10090f6190fdaf8" )
def test_open_local_or_remote_file__directory(tmpdir): """ Test the `open_local_or_remote_file` raises a ValueError for a given `Link` to a directory. """ link = Link(path_to_url(tmpdir.strpath)) session = Session() with pytest.raises(ValueError, match="Cannot open directory for read"): with open_local_or_remote_file(link, session): pass # pragma: no cover
def test_open_local_or_remote_file__local_file(tmp_path, content, content_length): """ Test the `open_local_or_remote_file` returns a context manager to a FileStream for a given `Link` to a local file. """ local_file_path = tmp_path / "foo.txt" local_file_path.write_bytes(content) link = Link(local_file_path.as_uri()) session = Session() with open_local_or_remote_file(link, session) as file_stream: assert file_stream.stream.read() == content assert file_stream.size == content_length
def test_open_local_or_remote_file__remote_file(tmp_path, content, content_length, expected_content_length): """ Test the `open_local_or_remote_file` returns a context manager to a FileStream for a given `Link` to a remote file. """ link = Link("https://example.com/foo.txt") session = Session() response_file_path = tmp_path / "foo.txt" response_file_path.write_bytes(content) mock_response = mock.Mock() mock_response.raw = response_file_path.open("rb") mock_response.headers = {"content-length": content_length} with mock.patch.object(session, "get", return_value=mock_response): with open_local_or_remote_file(link, session) as file_stream: assert file_stream.stream.read() == content assert file_stream.size == expected_content_length mock_response.close.assert_called_once()