Repository: MatthewFlamm/pytest-homeassistant-custom-component (language: python)

Issue:
custom_components again
I see there some other similar issues already fixed/closed (#106 or #64) about this but there's something apparently more subtle here or I'm not getting it right (it could be since I'm still far from grasping all of these frameworks and python itself)

If I got it right these issues could be not related to the fixture or so but to the fact the installation of pytest-homeassistant-custom-component creates a custom_components directory into the site-packages of the python environment and this in turn messes up the HA core loader.

This is my setup:
I'm using a forked custom_integration [blueprint](https://github.com/custom-components/integration_blueprint) repository to setup my dev environment and everything works fine (or looks like).
When I invoke the `pip install -r requirements_test.py` from the root workspace folder inside my devcontainer it nicely installs everything (I'm actually on version `pytest_homeassistant_custom_component-0.12.54`) but also creates the custom_components (sample?) folder inside the site-packages of the environment.
When HA starts the loader tries to import the custom_components root module and it obviously find it into the site-packages (I've opened an issue on HA core thinking this is wrong but they rejected this as non pertinent to the core) but that folder is far from being the one on which us, custom integration developers, are working.

Maybe I'm doing it wrong by invoking the pip install inside the container or I'm in the wrong place anyway but this is subtle and took half a day to be able to debug the HA loading process in these early stages.

The behavior was: when HA starts it doesnt find my custom integration since it is looking in a completely wrong folder (or at least a folder where no-one expects to put custom_components)

I still think this is a relatively bad behavior for the HA core and should be fixed but also I don't get why the pip install should put sample folder data inside the site-packages


Discussion / hints:
Not quite following everything here, but you rightly point out that this package is mistakenly adding a `custom_components` package, which shoudl only be used for internal testing.  We should move to a src-layout to avoid this. https://setuptools.pypa.io/en/latest/userguide/package_discovery.html#src-layout

Yep sorry I get quite verbose..but my suspect too was the custom_components folder shouldnt get there.
At any rate, this behavior is very subtle: I'm working on 2 different integrations with different instances of the same container image and 1 constantly refuses to load the correct 'custom_components' while the other works flawlessly (both have the rogue 'custom_components' folder in the site-packages)

Fix patch:
diff --git a/.github/workflows/automatic_generation.yml b/.github/workflows/automatic_generation.yml
index 0358e00..eaae0ac 100644
--- a/.github/workflows/automatic_generation.yml
+++ b/.github/workflows/automatic_generation.yml
@@ -24,6 +24,8 @@ jobs:
           python-version: '3.10'
       - name: install dependencies
         run: pip install -r requirements_generate.txt
+      - name: Install phacc for current versions
+        run: pip install -e .
       - name: execute generate package
         run: |
           export PYTHONPATH=$PYTHONPATH:$(pwd)
diff --git a/.github/workflows/generate_package.yml b/.github/workflows/generate_package.yml
index 3f94d05..bfcaa96 100644
--- a/.github/workflows/generate_package.yml
+++ b/.github/workflows/generate_package.yml
@@ -16,6 +16,8 @@ jobs:
           python-version: '3.10'
       - name: install dependencies
         run: pip install -r requirements_generate.txt
+      - name: Install phacc for current versions
+        run: pip install -e .
       - name: execute generate package
         run: |
           export PYTHONPATH=$PYTHONPATH:$(pwd)
diff --git a/generate_phacc/const.py b/generate_phacc/const.py
index 99828cf..e3ceaeb 100644
--- a/generate_phacc/const.py
+++ b/generate_phacc/const.py
@@ -1,6 +1,6 @@
 """Constants for phacc."""
 TMP_DIR = "tmp_dir"
-PACKAGE_DIR = "pytest_homeassistant_custom_component"
+PACKAGE_DIR = "src/pytest_homeassistant_custom_component"
 REQUIREMENTS_FILE = "requirements_test.txt"
 CONST_FILE = "const.py"
 
diff --git a/setup.py b/setup.py
index 7130a02..e407f78 100644
--- a/setup.py
+++ b/setup.py
@@ -17,7 +17,8 @@
     author="Matthew Flamm",
     name="pytest-homeassistant-custom-component",
     version=__version__,
-    packages=find_packages(),
+    packages=find_packages(where="src"),
+    package_dir={"": "src"},
     python_requires=">=3.10",
     install_requires=requirements,
     license="MIT license",


Test patch:
diff --git a/.github/workflows/pytest.yml b/.github/workflows/pytest.yml
index 6767edd..1c1eb17 100644
--- a/.github/workflows/pytest.yml
+++ b/.github/workflows/pytest.yml
@@ -27,6 +27,8 @@ jobs:
       run: |
         python -m pip install --upgrade pip
         pip install -r requirements_generate.txt
+    - name: Install phacc for current versions
+      run: pip install -e .
     - name: execute generate package
       run: |
         export PYTHONPATH=$PYTHONPATH:$(pwd)
diff --git a/pytest_homeassistant_custom_component/__init__.py b/src/pytest_homeassistant_custom_component/__init__.py
similarity index 100%
rename from pytest_homeassistant_custom_component/__init__.py
rename to src/pytest_homeassistant_custom_component/__init__.py
diff --git a/pytest_homeassistant_custom_component/asyncio_legacy.py b/src/pytest_homeassistant_custom_component/asyncio_legacy.py
similarity index 100%
rename from pytest_homeassistant_custom_component/asyncio_legacy.py
rename to src/pytest_homeassistant_custom_component/asyncio_legacy.py
diff --git a/pytest_homeassistant_custom_component/common.py b/src/pytest_homeassistant_custom_component/common.py
similarity index 100%
rename from pytest_homeassistant_custom_component/common.py
rename to src/pytest_homeassistant_custom_component/common.py
diff --git a/pytest_homeassistant_custom_component/components/__init__.py b/src/pytest_homeassistant_custom_component/components/__init__.py
similarity index 100%
rename from pytest_homeassistant_custom_component/components/__init__.py
rename to src/pytest_homeassistant_custom_component/components/__init__.py
diff --git a/pytest_homeassistant_custom_component/components/recorder/__init__.py b/src/pytest_homeassistant_custom_component/components/recorder/__init__.py
similarity index 100%
rename from pytest_homeassistant_custom_component/components/recorder/__init__.py
rename to src/pytest_homeassistant_custom_component/components/recorder/__init__.py
diff --git a/pytest_homeassistant_custom_component/components/recorder/common.py b/src/pytest_homeassistant_custom_component/components/recorder/common.py
similarity index 100%
rename from pytest_homeassistant_custom_component/components/recorder/common.py
rename to src/pytest_homeassistant_custom_component/components/recorder/common.py
diff --git a/pytest_homeassistant_custom_component/components/recorder/db_schema_0.py b/src/pytest_homeassistant_custom_component/components/recorder/db_schema_0.py
similarity index 100%
rename from pytest_homeassistant_custom_component/components/recorder/db_schema_0.py
rename to src/pytest_homeassistant_custom_component/components/recorder/db_schema_0.py
diff --git a/pytest_homeassistant_custom_component/const.py b/src/pytest_homeassistant_custom_component/const.py
similarity index 100%
rename from pytest_homeassistant_custom_component/const.py
rename to src/pytest_homeassistant_custom_component/const.py
diff --git a/pytest_homeassistant_custom_component/ignore_uncaught_exceptions.py b/src/pytest_homeassistant_custom_component/ignore_uncaught_exceptions.py
similarity index 100%
rename from pytest_homeassistant_custom_component/ignore_uncaught_exceptions.py
rename to src/pytest_homeassistant_custom_component/ignore_uncaught_exceptions.py
diff --git a/pytest_homeassistant_custom_component/plugins.py b/src/pytest_homeassistant_custom_component/plugins.py
similarity index 100%
rename from pytest_homeassistant_custom_component/plugins.py
rename to src/pytest_homeassistant_custom_component/plugins.py
diff --git a/pytest_homeassistant_custom_component/syrupy.py b/src/pytest_homeassistant_custom_component/syrupy.py
similarity index 100%
rename from pytest_homeassistant_custom_component/syrupy.py
rename to src/pytest_homeassistant_custom_component/syrupy.py
diff --git a/pytest_homeassistant_custom_component/test_util/__init__.py b/src/pytest_homeassistant_custom_component/test_util/__init__.py
similarity index 100%
rename from pytest_homeassistant_custom_component/test_util/__init__.py
rename to src/pytest_homeassistant_custom_component/test_util/__init__.py
diff --git a/pytest_homeassistant_custom_component/test_util/aiohttp.py b/src/pytest_homeassistant_custom_component/test_util/aiohttp.py
similarity index 100%
rename from pytest_homeassistant_custom_component/test_util/aiohttp.py
rename to src/pytest_homeassistant_custom_component/test_util/aiohttp.py
diff --git a/pytest_homeassistant_custom_component/typing.py b/src/pytest_homeassistant_custom_component/typing.py
similarity index 100%
rename from pytest_homeassistant_custom_component/typing.py
rename to src/pytest_homeassistant_custom_component/typing.py