MatthewFlamm / pytest-homeassistant-custom-component

Package to automatically extract testing plugins from Home Assistant for custom component testing
MIT License
67 stars 10 forks source link

Error by simply loading the plugin: A test tried to use socket.socket #171

Closed fredck closed 9 months ago

fredck commented 10 months ago

I am encountering an error when attempting to run pytest with the homeassistant-custom-component plugin. The tests fail to run, and the "A test tried to use socket.socket" error is thrown.

Environment:

Steps to Reproduce:

  1. Set up a Python development environment in a VS Code dev container (python:3.11-bullseye).
  2. Install pytest and the homeassistant-custom-component plugin.
  3. Write a basic test case:
    def test_init():
       assert True
  4. Run pytest.

Expected Behavior: The test suite runs without any issues.

Actual Behavior: The test suite fails to start, and the following error is displayed:

CLIENT: Server listening on port 34081...
Received JSON data in run script
============================= test session starts ==============================
[...]
tests/components/lightener/test_init.py Error attempting to connect to extension communication socket[vscode-pytest]: A test tried to use socket.socket.
If you are on a Windows machine, this error may be occurring if any of your tests clear environment variables as they are required to communicate with the extension. Please reference https://docs.pytest.org/en/stable/how-to/monkeypatch.html#monkeypatching-environment-variables for the correct way to clear environment variables during testing.

Additional Information:

Any guidance or suggestions to resolve this issue would be greatly appreciated. Thank you.

fredck commented 9 months ago

To make it easy to confirm the issue, I've created this minimal VSCode project: socket-error.zip

Just open this in VSCode:

fredck commented 9 months ago

The conclusion is that the plugin is breaking the VSCode testing infrastructure somehow.

fredck commented 9 months ago

For those wasting their lives with this issue, I was able to fix it by adding the following configuration to the VSCode settings:

"python.experiments.enabled": false

For reference: https://github.com/microsoft/vscode-python/wiki/AB-Experiments

PeteRager commented 8 months ago

i continue to waste my time with this issue. I've added that setting, tried many different things and its still broke. The only thing that seems to work is commenting out the code in this modules plugin.py

#    pytest_socket.socket_allow_hosts(["127.0.0.1"])
#    pytest_socket.disable_socket(allow_unix_socket=True)