]> git.ipfire.org Git - thirdparty/httpx.git/commitdiff
Use relative tests directory references (#1052)
authorFlorimond Manca <florimond.manca@gmail.com>
Thu, 16 Jul 2020 14:28:31 +0000 (16:28 +0200)
committerGitHub <noreply@github.com>
Thu, 16 Jul 2020 14:28:31 +0000 (16:28 +0200)
* Use relative tests directory references

* Use absolute TESTS_DIR

Co-authored-by: Tom Christie <tom@tomchristie.com>
tests/client/test_auth.py
tests/common.py [new file with mode: 0644]
tests/fixtures/.netrc [moved from tests/.netrc with 100% similarity]
tests/test_utils.py

index 13184a06bac75d4d3cfd9648e2ac80395aca5a4c..29b6f20bf146be4bf08c182e52c6ee30703f9aae 100644 (file)
@@ -18,6 +18,8 @@ from httpx import (
 )
 from httpx._content_streams import ContentStream, JSONStream
 
+from ..common import FIXTURES_DIR
+
 
 def get_header_value(headers, key, default=None):
     lookup = key.encode("ascii").lower()
@@ -233,7 +235,7 @@ async def test_custom_auth() -> None:
 
 @pytest.mark.asyncio
 async def test_netrc_auth() -> None:
-    os.environ["NETRC"] = "tests/.netrc"
+    os.environ["NETRC"] = str(FIXTURES_DIR / ".netrc")
     url = "http://netrcexample.org"
 
     client = AsyncClient(transport=AsyncMockTransport())
@@ -247,7 +249,7 @@ async def test_netrc_auth() -> None:
 
 @pytest.mark.asyncio
 async def test_auth_header_has_priority_over_netrc() -> None:
-    os.environ["NETRC"] = "tests/.netrc"
+    os.environ["NETRC"] = str(FIXTURES_DIR / ".netrc")
     url = "http://netrcexample.org"
 
     client = AsyncClient(transport=AsyncMockTransport())
@@ -259,7 +261,7 @@ async def test_auth_header_has_priority_over_netrc() -> None:
 
 @pytest.mark.asyncio
 async def test_trust_env_auth() -> None:
-    os.environ["NETRC"] = "tests/.netrc"
+    os.environ["NETRC"] = str(FIXTURES_DIR / ".netrc")
     url = "http://netrcexample.org"
 
     client = AsyncClient(transport=AsyncMockTransport(), trust_env=False)
diff --git a/tests/common.py b/tests/common.py
new file mode 100644 (file)
index 0000000..064c25a
--- /dev/null
@@ -0,0 +1,4 @@
+import pathlib
+
+TESTS_DIR = pathlib.Path(__file__).parent
+FIXTURES_DIR = TESTS_DIR / "fixtures"
similarity index 100%
rename from tests/.netrc
rename to tests/fixtures/.netrc
index 334bff9a2d8bc20b2ab55a9cab4d709b694f8ce1..70fc1d622941119a1e178203c9b5cdc4d0d44566 100644 (file)
@@ -16,6 +16,8 @@ from httpx._utils import (
 )
 from tests.utils import override_log_level
 
+from .common import FIXTURES_DIR, TESTS_DIR
+
 
 @pytest.mark.parametrize(
     "encoding",
@@ -54,12 +56,12 @@ def test_guess_by_bom(encoding, expected):
 
 
 def test_bad_get_netrc_login():
-    netrc_info = NetRCInfo(["tests/does-not-exist"])
+    netrc_info = NetRCInfo([str(FIXTURES_DIR / "does-not-exist")])
     assert netrc_info.get_credentials("netrcexample.org") is None
 
 
 def test_get_netrc_login():
-    netrc_info = NetRCInfo(["tests/.netrc"])
+    netrc_info = NetRCInfo([str(FIXTURES_DIR / ".netrc")])
     expected_credentials = (
         "example-username",
         "example-password",
@@ -68,7 +70,7 @@ def test_get_netrc_login():
 
 
 def test_get_netrc_unknown():
-    netrc_info = NetRCInfo(["tests/.netrc"])
+    netrc_info = NetRCInfo([str(FIXTURES_DIR / ".netrc")])
     assert netrc_info.get_credentials("nonexistant.org") is None
 
 
@@ -137,14 +139,16 @@ def test_get_ssl_cert_file():
     # Two environments is not set.
     assert get_ca_bundle_from_env() is None
 
-    os.environ["SSL_CERT_DIR"] = "tests/"
+    os.environ["SSL_CERT_DIR"] = str(TESTS_DIR)
     # SSL_CERT_DIR is correctly set, SSL_CERT_FILE is not set.
-    assert get_ca_bundle_from_env() == "tests"
+    ca_bundle = get_ca_bundle_from_env()
+    assert ca_bundle is not None and ca_bundle.endswith("tests")
 
     del os.environ["SSL_CERT_DIR"]
-    os.environ["SSL_CERT_FILE"] = "tests/test_utils.py"
+    os.environ["SSL_CERT_FILE"] = str(TESTS_DIR / "test_utils.py")
     # SSL_CERT_FILE is correctly set, SSL_CERT_DIR is not set.
-    assert get_ca_bundle_from_env() == "tests/test_utils.py"
+    ca_bundle = get_ca_bundle_from_env()
+    assert ca_bundle is not None and ca_bundle.endswith("tests/test_utils.py")
 
     os.environ["SSL_CERT_FILE"] = "wrongfile"
     # SSL_CERT_FILE is set with wrong file,  SSL_CERT_DIR is not set.
@@ -155,14 +159,16 @@ def test_get_ssl_cert_file():
     # SSL_CERT_DIR is set with wrong path,  SSL_CERT_FILE is not set.
     assert get_ca_bundle_from_env() is None
 
-    os.environ["SSL_CERT_DIR"] = "tests/"
-    os.environ["SSL_CERT_FILE"] = "tests/test_utils.py"
+    os.environ["SSL_CERT_DIR"] = str(TESTS_DIR)
+    os.environ["SSL_CERT_FILE"] = str(TESTS_DIR / "test_utils.py")
     # Two environments is correctly set.
-    assert get_ca_bundle_from_env() == "tests/test_utils.py"
+    ca_bundle = get_ca_bundle_from_env()
+    assert ca_bundle is not None and ca_bundle.endswith("tests/test_utils.py")
 
     os.environ["SSL_CERT_FILE"] = "wrongfile"
     # Two environments is set but SSL_CERT_FILE is not a file.
-    assert get_ca_bundle_from_env() == "tests"
+    ca_bundle = get_ca_bundle_from_env()
+    assert ca_bundle is not None and ca_bundle.endswith("tests")
 
     os.environ["SSL_CERT_DIR"] = "wrongpath"
     # Two environments is set but both are not correct.