Repository: miketheman/pytest-socket (language: python)

Issue:
Don't block unix sockets
This is a pretty dumb implementation and I'm happy to rework it to be a bit safer/defensive, just wanted to test if it worked first. It fixes issues with `asyncio` by check if it's an `AF_UNIX` domain socket being requested and allowing that, otherwise it blocks as normal.

should help with

https://github.com/miketheman/pytest-socket/issues/47
https://github.com/miketheman/pytest-socket/issues/6

I've tested it with both the starlette example and the `asynctest` example and both work with this patch

```
import asynctest

class TestClass(asynctest.TestCase):
    async def test_something(self):
        pass
```


Discussion / hints:
Tested on five Django projects and it works like a charm 👍 
I don't need to patch django via env var anymore, thanks for your work!


Excellent, thanks for taking this on! I think this was recently enabled by converting this to a class, so yay!

I'm currently moving, so haven't had a change to check it out more thoroughly, but could you consider adding your test case to the tests so the functionality is exercised by CI?

Also, had you considered making this a configuration flag like --allow-unix-sockets so that the behavior can be controlled by the user? This way it's opt-in, and not a change of the current default behavior.
happy to do both! just wanted to throw this out there before investing too much time in it
Windows failure looks like an unrelated failure on setup. Not the absence of AF_UNIX that I was hoping to test
@miketheman, no pressure, just a ping hoping you find some time to take a look at this.
I also reran the test suite and the failing Windows test passed, so that's something we should keep an eye on - nothing worse than a flaky test!
Code Climate has analyzed commit 197d3e35 and detected **0 issues** on this pull request.

View more on [Code Climate](https://codeclimate.com/github/miketheman/pytest-socket/pull/54).

Thanks for sticking with it!
I’ll try to get to a release next week.

Fix patch:
diff --git a/pyproject.toml b/pyproject.toml
index 8f952d9..a8c94b2 100644
--- a/pyproject.toml
+++ b/pyproject.toml
@@ -35,6 +35,8 @@ pytest = "^6.0"
 pytest-cov = "^2.8.1"
 pytest-httpbin = "^1.0"
 pytest-flake8 = "^1.0.4"
+asynctest = "^0.13.0"
+starlette = "^0.14.2"
 
 [tool.poetry.plugins.pytest11]
 socket = 'pytest_socket'


Test patch:
diff --git a/tests/test_async.py b/tests/test_async.py
new file mode 100644
index 0000000..963ba73
--- /dev/null
+++ b/tests/test_async.py
@@ -0,0 +1,56 @@
+import socket
+
+import pytest
+
+
+unix_sockets_only = pytest.mark.skipif(
+    not hasattr(socket, "AF_UNIX"),
+    reason="Skip any platform that does not support AF_UNIX",
+)
+
+
+@unix_sockets_only
+def test_asynctest(testdir):
+    testdir.makepyfile("""
+        import asynctest
+
+
+        class AnExampleWithTestCaseAndCoroutines(asynctest.TestCase):
+            async def a_coroutine(self):
+                return "I worked"
+
+            async def test_that_a_coroutine_runs(self):
+                self.assertIn("worked", await self.a_coroutine())
+
+            async def test_inet_is_blocked(self):
+                socket.socket(socket.AF_INET, socket.SOCK_STREAM)
+    """)
+    result = testdir.runpytest("--verbose", "--disable-socket", "--allow-unix-socket")
+    result.assert_outcomes(passed=1, skipped=0, failed=1)
+
+
+@unix_sockets_only
+def test_starlette(testdir):
+    testdir.makepyfile("""
+        from pytest_socket import disable_socket
+        from starlette.responses import HTMLResponse
+        from starlette.testclient import TestClient
+
+
+        def pytest_runtest_setup():
+            disable_socket()
+
+
+        async def app(scope, receive, send):
+            assert scope['type'] == 'http'
+            response = HTMLResponse('<html><body>Hello, world!</body></html>')
+            await response(scope, receive, send)
+
+
+        def test_app():
+            client = TestClient(app)
+            response = client.get('/')
+            assert response.status_code == 200
+    """)
+    result = testdir.runpytest("--verbose", "--disable-socket", "--allow-unix-socket")
+    result.assert_outcomes(passed=1, skipped=0, failed=0)