Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor testing #3357

Merged
merged 125 commits into from
Apr 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
125 commits
Select commit Hold shift + click to select a range
ff79a8c
init more tests
ludeeus Nov 18, 2023
7ecad1d
Update snapshots
ludeeus Nov 18, 2023
e62a92f
update fixtures
ludeeus Nov 18, 2023
9b20985
sort files
ludeeus Nov 18, 2023
ead41d5
Remove tests before running hassfest
ludeeus Nov 18, 2023
22dcec5
fix json
ludeeus Nov 18, 2023
4c93848
more test adjustments
ludeeus Nov 19, 2023
8f36831
lint
ludeeus Nov 19, 2023
98418c1
add psutil_home_assistant test requirement
ludeeus Nov 19, 2023
0d13de5
sqlalchemy
ludeeus Nov 19, 2023
d39f4a3
fnv_hash_fast
ludeeus Nov 19, 2023
4297af6
more
ludeeus Nov 19, 2023
0cf43c6
PIL
ludeeus Nov 19, 2023
92a0cb1
Pillow
ludeeus Nov 19, 2023
e80afbd
janus
ludeeus Nov 19, 2023
7ca446c
hass_frontend
ludeeus Nov 19, 2023
4212b0e
home-assistant-frontend
ludeeus Nov 19, 2023
8acd0e6
full entry for newer
ludeeus Nov 19, 2023
157d346
lint
ludeeus Nov 19, 2023
32e53c3
get entity for update test
ludeeus Nov 21, 2023
79c4edd
renames
ludeeus Nov 21, 2023
b52f37f
pin test packages
ludeeus Nov 21, 2023
fd81637
single t
ludeeus Nov 21, 2023
c12f4d0
updates
ludeeus Nov 21, 2023
7aee526
fix stuff
ludeeus Nov 21, 2023
6fb949b
store entities and ws for setup test
ludeeus Nov 22, 2023
95f16bc
remove base
ludeeus Nov 22, 2023
a5dd688
Add basic fixtures for all categories
ludeeus Nov 22, 2023
3ec5235
Use WS command to register custom repository
ludeeus Nov 22, 2023
f815918
lint
ludeeus Nov 22, 2023
fb7935b
cleanup
ludeeus Nov 22, 2023
57ef009
unused import
ludeeus Nov 22, 2023
9f47488
Test addons and core
ludeeus Nov 23, 2023
83cd871
start generate tests
ludeeus Nov 24, 2023
5d3552d
Output all calls to proxy
ludeeus Nov 25, 2023
06879e3
change messaging
ludeeus Nov 26, 2023
31f18ae
lint
ludeeus Nov 26, 2023
c146a41
remove old
ludeeus Dec 10, 2023
5cf6b73
plugin
ludeeus Dec 15, 2023
e3c0b49
lint
ludeeus Dec 15, 2023
82ed2b4
try with minor_version first
ludeeus Dec 15, 2023
fe35eaa
theme
ludeeus Dec 16, 2023
a3e077d
lint
ludeeus Dec 16, 2023
ecf0043
Setup all categories for tests
ludeeus Dec 17, 2023
7763499
lint
ludeeus Dec 17, 2023
bbf9372
Cleanup stale
ludeeus Dec 17, 2023
155ab61
Remove unnecessary test files
ludeeus Dec 17, 2023
ed81774
Remove aresponses
ludeeus Dec 17, 2023
45feb82
remove proxy_session usage
ludeeus Dec 17, 2023
4b62af5
more!
ludeeus Dec 21, 2023
e5c4131
remove backup arg
ludeeus Dec 26, 2023
72eb07d
fix
ludeeus Dec 26, 2023
f615542
lint
ludeeus Dec 28, 2023
3f9da15
Remove autouse from setup_integration fixture
ludeeus Dec 28, 2023
a848050
add entities to snapshots
ludeeus Dec 28, 2023
cb3c557
Fix HACS test manifest
ludeeus Dec 28, 2023
0dc08cd
filter calls
ludeeus Dec 28, 2023
b5bb541
replace diagnostics test
ludeeus Dec 28, 2023
05a9a34
Fix sorting
ludeeus Dec 29, 2023
13f2725
better output
ludeeus Dec 29, 2023
4e673c8
adjust resp
ludeeus Dec 29, 2023
790c8bd
ignore minor_version
ludeeus Dec 29, 2023
00f57cb
replace print in test
ludeeus Dec 29, 2023
98a0aed
add appdaemon
ludeeus Dec 29, 2023
9e7ca35
cleanup
ludeeus Dec 29, 2023
b24f61b
lint
ludeeus Dec 29, 2023
6793db3
Add test for removing repositories
ludeeus Dec 30, 2023
4bf7839
lint
ludeeus Dec 30, 2023
cc17ee8
update output
ludeeus Dec 30, 2023
e794ade
extend generate
ludeeus Dec 30, 2023
fe66ec9
More usage of common test data
ludeeus Dec 30, 2023
9a72eee
data client test
ludeeus Dec 30, 2023
06e4271
output
ludeeus Dec 30, 2023
23ff018
add python_script tests
ludeeus Dec 30, 2023
bd0f62e
typo
ludeeus Dec 30, 2023
bf22681
enable all
ludeeus Dec 30, 2023
3757124
config_flow tests
ludeeus Dec 30, 2023
39287db
remaining generate
ludeeus Dec 31, 2023
5d78f9e
use xfail
ludeeus Dec 31, 2023
9f9b517
lint
ludeeus Dec 31, 2023
a14c62f
lint
ludeeus Dec 31, 2023
bfa3f25
sort
ludeeus Dec 31, 2023
911b89f
adjust
ludeeus Dec 31, 2023
ead383d
cover bad cases
ludeeus Jan 1, 2024
08f7661
one more
ludeeus Jan 1, 2024
6d2cce5
update snapshots
ludeeus Jan 1, 2024
964ac8d
init better action testing
ludeeus Jan 2, 2024
885bd36
track loader.async_suggest_report_issue
ludeeus Jan 5, 2024
6f60e7c
lint
ludeeus Jan 7, 2024
9942ed1
Sort action results
ludeeus Jan 7, 2024
f5da2a7
lint...
ludeeus Jan 7, 2024
2bc6dcd
Guard async_suggest_report_issue
ludeeus Jan 13, 2024
ffa02eb
lint
ludeeus Jan 13, 2024
d6595da
Run dev tests against 3.12 as well
ludeeus Jan 14, 2024
6b1b33f
change names
ludeeus Jan 14, 2024
305a05f
move report check to fixture
ludeeus Jan 14, 2024
642c9cd
fix version selector
ludeeus Jan 14, 2024
7634bf6
Add setuptools to base
ludeeus Jan 14, 2024
cd76945
fix check_report_issue check
ludeeus Jan 14, 2024
75ee64b
Use set_state to bust cached propery
ludeeus Feb 17, 2024
d9f3aff
Use async_test_home_assistant test helpers from homeassistant core repo
emontnemery Apr 4, 2024
c24273b
Lint
emontnemery Apr 4, 2024
9b2df11
Update snapshots
emontnemery Apr 4, 2024
296e02f
Align with HA Core 2024.4.0
emontnemery Apr 4, 2024
b29492b
Fix test_update_repository_entity
emontnemery Apr 4, 2024
65c8fd6
Fix import
emontnemery Apr 4, 2024
f05d16c
Fix test_hacs_action_integration
emontnemery Apr 4, 2024
d4ad7ca
Disable testing dev core with Python 3.11
emontnemery Apr 4, 2024
3d38174
Allow more issues
emontnemery Apr 4, 2024
f1982a9
Use Python 3.12 run in codecov
emontnemery Apr 4, 2024
9bbac89
Enable config flow tests
emontnemery Apr 8, 2024
5846a8e
Adjust tests
emontnemery Apr 8, 2024
5a0c40c
Update generate_category_data tests
emontnemery Apr 9, 2024
0cb5c50
Clear stored responses after usage
ludeeus Apr 10, 2024
a91b531
Rename tests/z_action to tests/action
ludeeus Apr 10, 2024
78b16eb
Update snapshot
ludeeus Apr 10, 2024
9555922
Update action test
ludeeus Apr 10, 2024
72bf7c5
Remove cleanups
ludeeus Apr 10, 2024
ed569d5
use with expectation
ludeeus Apr 10, 2024
66a0e85
fix typing
ludeeus Apr 10, 2024
50cb0e5
Use python version from matrix
ludeeus Apr 10, 2024
d5a4fec
Test option flow
emontnemery Apr 10, 2024
0565405
Lint
emontnemery Apr 10, 2024
c5df365
Update snapshots
emontnemery Apr 10, 2024
81fed84
Use token constant
emontnemery Apr 10, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 1 addition & 12 deletions .coveragerc
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,11 @@
source = custom_components

omit =
# omit pieces of code that rely on external API calls
# These should be mocked one day....
custom_components/hacs/helpers/get_defaults.py
custom_components/hacs/operational/setup_actions/load_hacs_repository.py
custom_components/hacs/operational/factory.py
custom_components/hacs/operational/relaod.py
custom_components/hacs/operational/remove.py
custom_components/hacs/operational/setup.py
custom_components/hacs/config_flow.py
custom_components/hacs/__init__.py

# omit tests
tests/*

# omit scripts
scripts/*
scripts/update/*

[report]
exclude_lines =
Expand Down
59 changes: 17 additions & 42 deletions .github/workflows/pytest.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,45 +13,8 @@ concurrency:
cancel-in-progress: true

jobs:
run:
name: With pytest for Python ${{ matrix.python-version }}
runs-on: ubuntu-latest
strategy:
matrix:
python-version: ["3.11"]
steps:
- name: 📥 Checkout the repository
uses: actions/[email protected]

- name: 🛠️ Set up Python
uses: actions/[email protected]
id: python
with:
python-version: ${{ matrix.python-version }}
cache: 'pip'
cache-dependency-path: |
requirements_base.txt
requirements_test.txt

- name: 📦 Install requirements
run: |
scripts/install/frontend
scripts/install/pip_packages --requirement requirements_test.txt

- name: 🏃 Run tests
env:
PYTEST: true
run: |
python3 -m pytest

- name: 📤 Upload coverage to Codecov
if: ${{ matrix.python-version == '3.11' }}
run: |
scripts/coverage
curl -sfSL https://codecov.io/bash | bash -

legacy:
name: With pytest with Home Assistant 2023.6.0
name: With pytest with Home Assistant (min. supported version)
runs-on: ubuntu-latest
steps:
- name: 📥 Checkout the repository
Expand All @@ -78,17 +41,23 @@ jobs:
run: |
python3 -m pytest

core_dev:
name: With pytest with Home Assistant dev
dev:
name: With pytest with Home Assistant (${{ matrix.homeassistant-version }}) & Python (${{ matrix.python-version }})
runs-on: ubuntu-latest
strategy:
matrix:
homeassistant-version:
- "dev"
python-version:
- "3.12"
steps:
- name: 📥 Checkout the repository
uses: actions/[email protected]

- name: 🛠️ Set up Python 3.12
- name: 🛠️ Set up Python ${{ matrix.python-version }}
uses: actions/[email protected]
with:
python-version: "3.12"
python-version: ${{ matrix.python-version }}
cache: 'pip'
cache-dependency-path: |
requirements_base.txt
Expand All @@ -105,3 +74,9 @@ jobs:
PYTEST: true
run: |
python3 -m pytest

- name: 📤 Upload coverage to Codecov
if: ${{ matrix.python-version == '3.12' }}
run: |
scripts/coverage
curl -sfSL https://codecov.io/bash | bash -
4 changes: 4 additions & 0 deletions .github/workflows/validate.yml
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@ jobs:
- name: Checkout the repository
uses: actions/[email protected]

# Test files conflics with running hassfest
- name: Remove tests
run: rm -rf tests

- name: Hassfest validation
uses: "home-assistant/actions/hassfest@master"

Expand Down
4 changes: 2 additions & 2 deletions action/action.py
Original file line number Diff line number Diff line change
Expand Up @@ -168,5 +168,5 @@ async def validate_repository(hacs, repository, category, ref=None):
except HacsException as exception:
error(exception)


asyncio.run(preflight())
if __name__ == "__main__":
asyncio.run(preflight())
3 changes: 2 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ combine_as_imports = true


[tool.pytest.ini_options]
addopts = "-rxf -x -v -l --cov=./ --cov-report=xml"
asyncio_mode="auto"
addopts = "-rf -v -l --cov=./ --cov-report=xml"
filterwarnings = [
"ignore::DeprecationWarning",
]
Expand Down
2 changes: 1 addition & 1 deletion requirements_base.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ aiohttp_cors==0.7.0
async-timeout>=4.0.3
asynctest==0.13.0
colorlog==6.7.0

setuptools==68.0.0
9 changes: 8 additions & 1 deletion requirements_test.txt
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
-r requirements_base.txt
aresponses==2.1.6
asynctest==0.13.0
fnv-hash-fast==0.5.0
home-assistant-frontend
homeassistant
janus==1.0.0
Pillow==10.1.0
psutil-home-assistant==0.0.1
pytest==7.4.3
pytest-asyncio==0.21.1
pytest-cov==4.1.0
pytest-snapshot==0.9.0
pytest-socket==0.6.0
RestrictedPython==7.0
SQLAlchemy==2.0.23
11 changes: 11 additions & 0 deletions scripts/snapshot-update
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#!/usr/bin/env bash

set -e

cd "$(dirname "$0")/.."

python3 -m \
pytest \
tests \
--snapshot-update

28 changes: 28 additions & 0 deletions tests/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import json

from awesomeversion import AwesomeVersion
from homeassistant import const, core, loader

_async_suggest_report_issue_mock_call_tracker = []

try:
_orig_async_suggest_report_issue = loader.async_suggest_report_issue
except AttributeError:
if AwesomeVersion(const.__version__) >= "2023.11.0":
raise RuntimeError("loader.async_suggest_report_issue does not exist")

@core.callback
def _fallback_async_suggest_report_issue(*args, **kwargs):
return json.dumps(kwargs)

_orig_async_suggest_report_issue = _fallback_async_suggest_report_issue


@core.callback
def async_suggest_report_issue_mock(*args, **kwargs):
result = _orig_async_suggest_report_issue(*args, **kwargs)
_async_suggest_report_issue_mock_call_tracker.append(result)
return result


loader.async_suggest_report_issue = async_suggest_report_issue_mock
73 changes: 73 additions & 0 deletions tests/action/test_hacs_action_integration.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
"""Tests for the HACS action."""
import base64
import json
import os
from unittest import mock

import pytest

from tests.common import TOKEN, MockedResponse, ResponseMocker, current_function_name
from tests.conftest import SnapshotFixture


@pytest.mark.parametrize(
"key,manifest,succeed",
(
("bad_documentation", {"documentation": None}, False),
("bad_issue_tracker", {"issue_tracker": None}, False),
("valid_manifest1", {}, True),
),
)
async def test_hacs_action_integration(
key: str,
manifest: dict[str, str | bool | None],
succeed: bool,
caplog: pytest.LogCaptureFixture,
response_mocker: ResponseMocker,
snapshots: SnapshotFixture,
):
"""Test the action."""
basemanifest = {
"domain": "example",
"version": "1.0.0",
"documentation": "https://example.com",
"codeowners": ["hacs-test-org"],
"issue_tracker": "https://example.com",
"name": "Example",
}
envpatch = {
"INPUT_GITHUB_TOKEN": TOKEN,
"INPUT_REPOSITORY": "hacs-test-org/integration-basic",
"INPUT_CATEGORY": "integration",
}

response_mocker.add(
"https://brands.home-assistant.io/domains.json",
response=MockedResponse(status=200, content={"custom": ["example"]}),
)
response_mocker.add(
f"https://api.github.com/repos/hacs-test-org/integration-basic/contents/custom_components/example/manifest.json",
response=MockedResponse(
status=200,
content={
"content": base64.b64encode(
json.dumps({**basemanifest, **manifest}).encode("ascii")
).decode("ascii")
},
keep=True,
),
)

with mock.patch.dict(os.environ, envpatch), mock.patch("builtins.exit", mock.MagicMock()):
from action.action import preflight

await preflight()

assert ("All (8) checks passed" if succeed else "1/8 checks failed") in caplog.text

splitlines = [f"<{l.rsplit(' <')[1]}" for l in caplog.text.split("\n") if " <" in l]

snapshots.assert_match(
"\n".join(splitlines[0:2] + sorted(splitlines[2:-2]) + splitlines[-2:]),
f"action/{current_function_name()}/{key}.log",
)
8 changes: 0 additions & 8 deletions tests/async_mock.py

This file was deleted.

Loading
Loading