From 8512da699ec68722a36fbb6e18ab09d2bb93910b Mon Sep 17 00:00:00 2001 From: Pierre Verkest Date: Tue, 24 Sep 2024 11:45:49 +0200 Subject: [PATCH 01/10] [FIX] _find_manifest_path when no manifest found This happens if the test module is less than 5 directories deep from root. --- pytest_odoo.py | 8 +++++--- tests/__init__.py | 0 tests/test_pytest_odoo.py | 39 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 44 insertions(+), 3 deletions(-) create mode 100644 tests/__init__.py create mode 100644 tests/test_pytest_odoo.py diff --git a/pytest_odoo.py b/pytest_odoo.py index 6e27c90..4bffeca 100644 --- a/pytest_odoo.py +++ b/pytest_odoo.py @@ -233,10 +233,12 @@ def _find_manifest_path(collection_path: Path) -> Path: """Try to locate an Odoo manifest file in the collection path.""" # check if collection_path is an addon directory path = collection_path - level = 0 - while level < 5 and not (path.parent / "__manifest__.py").is_file(): + for _ in range(0, 5): + if (path.parent / "__manifest__.py").is_file(): + break path = path.parent - level += 1 + else: + return None return path.parent / "__manifest__.py" diff --git a/tests/__init__.py b/tests/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/tests/test_pytest_odoo.py b/tests/test_pytest_odoo.py new file mode 100644 index 0000000..eefcac7 --- /dev/null +++ b/tests/test_pytest_odoo.py @@ -0,0 +1,39 @@ +from unittest import TestCase +import tempfile +from contextlib import contextmanager +from pytest_odoo import _find_manifest_path +from pathlib import Path + +class TestPytestOdoo(TestCase): + + @contextmanager + def fake_module(self): + directory = tempfile.TemporaryDirectory() + try: + module_path = Path(directory.name) + manifest_path = module_path / "__manifest__.py" + manifest_path.touch() + test_path = module_path / "tests" / "test_module.py" + test_path.parent.mkdir(parents=True, exist_ok=True) + test_path.touch() + yield (module_path, manifest_path, test_path,) + finally: + directory.cleanup() + + + def test_find_manifest_path_less_than_5_directories(self): + self.assertIsNone(_find_manifest_path(Path("/some/path"))) + + def test_find_manifest_path_from_test_module(self): + with self.fake_module() as (_, manifest_path, test_path): + self.assertEqual(_find_manifest_path(test_path), manifest_path) + + def test_find_manifest_path_from_itself(self): + with self.fake_module() as (_, manifest_path, _): + self.assertEqual(_find_manifest_path(manifest_path), manifest_path) + + def test_find_manifest_path_from_brother(self): + with self.fake_module() as (module_path, manifest_path, _): + test = module_path / "test_something.py" + test.touch() + self.assertEqual(_find_manifest_path(test), manifest_path) From 0d1a58688379b928c0b6bfeac539624e6ef9110e Mon Sep 17 00:00:00 2001 From: Yannick Payot Date: Fri, 4 Oct 2024 21:20:13 +0200 Subject: [PATCH 02/10] Mock Odoo module for unit tests As Odoo is an heavy piece of software and we don't have to check it runs we can mock the import. For further unit tests, the few methods we use will have to be mocked too. --- conftest.py | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 conftest.py diff --git a/conftest.py b/conftest.py new file mode 100644 index 0000000..eadd95e --- /dev/null +++ b/conftest.py @@ -0,0 +1,5 @@ +import sys +import types + +sys.modules["odoo"] = types.ModuleType("odoo") +sys.modules["odoo.tests"] = types.ModuleType("odoo.tests") From e28a371e3da02f63c38cc68794aa835b069aa869 Mon Sep 17 00:00:00 2001 From: Yannick Payot Date: Fri, 4 Oct 2024 22:24:44 +0200 Subject: [PATCH 03/10] test pytest addon with pytest --- .github/workflows/ci.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 75c4b04..e3a3c3a 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -39,6 +39,6 @@ jobs: run: | pip install coverage pip install .[test] - coverage run -m unittest + coverage run -m pytest - name: Upload Coverage to Codecov uses: codecov/codecov-action@v3 From dee29ca9a4b3058460fce8a90a0218a265d9fe96 Mon Sep 17 00:00:00 2001 From: Yannick Payot Date: Fri, 4 Oct 2024 22:41:56 +0200 Subject: [PATCH 04/10] Test with pytester --- .github/workflows/ci.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index e3a3c3a..861147c 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -39,6 +39,6 @@ jobs: run: | pip install coverage pip install .[test] - coverage run -m pytest + coverage run -m pytest -p pytester - name: Upload Coverage to Codecov uses: codecov/codecov-action@v3 From cd26c99e564bebe87d6b44d166965c32e138db32 Mon Sep 17 00:00:00 2001 From: Yannick Vaucher Date: Sat, 5 Oct 2024 11:31:08 +0200 Subject: [PATCH 05/10] use pytest-cov --- .github/workflows/ci.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 861147c..1200997 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -37,8 +37,8 @@ jobs: pip install tox codecov tox-gh-actions wheel - name: Generate Report run: | - pip install coverage + pip install coverage pytest-cov pip install .[test] - coverage run -m pytest -p pytester + pytest --cov - name: Upload Coverage to Codecov uses: codecov/codecov-action@v3 From 00e1e79c92a00856d913356946b88b4484c7ad07 Mon Sep 17 00:00:00 2001 From: Yannick Vaucher Date: Sat, 5 Oct 2024 13:18:12 +0200 Subject: [PATCH 06/10] Use tox --- .coveragerc | 3 +++ .github/workflows/ci.yml | 4 +--- .gitignore | 21 +++++++++++++++++---- conftest.py | 5 ----- tests/mock/odoo/odoo/__init__.py | 3 +++ tests/mock/odoo/odoo/tests/__init__.py | 2 ++ tests/mock/odoo/setup.py | 12 ++++++++++++ tox.ini | 7 +++++++ 8 files changed, 45 insertions(+), 12 deletions(-) create mode 100644 .coveragerc delete mode 100644 conftest.py create mode 100644 tests/mock/odoo/odoo/__init__.py create mode 100644 tests/mock/odoo/odoo/tests/__init__.py create mode 100644 tests/mock/odoo/setup.py create mode 100644 tox.ini diff --git a/.coveragerc b/.coveragerc new file mode 100644 index 0000000..a42a5c2 --- /dev/null +++ b/.coveragerc @@ -0,0 +1,3 @@ +[run] +omit = tests/* + setup.py diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 1200997..1cb8c5b 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -37,8 +37,6 @@ jobs: pip install tox codecov tox-gh-actions wheel - name: Generate Report run: | - pip install coverage pytest-cov - pip install .[test] - pytest --cov + tox run - name: Upload Coverage to Codecov uses: codecov/codecov-action@v3 diff --git a/.gitignore b/.gitignore index 9bd8c0e..926a818 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,18 @@ -*.egg-info -*.pyc -.cache/ -dist/ +# Byte-compiled / optimized / DLL +__pycache__/ +*.py[cod] +*$py.class + +# Distribution / packaging build/ +dist/ +eggs/ +.eggs/ +*.egg-info/ +*.egg + +# Unit test / coverage reports +.tox +.coverage +.coverage.* +.cache/ diff --git a/conftest.py b/conftest.py deleted file mode 100644 index eadd95e..0000000 --- a/conftest.py +++ /dev/null @@ -1,5 +0,0 @@ -import sys -import types - -sys.modules["odoo"] = types.ModuleType("odoo") -sys.modules["odoo.tests"] = types.ModuleType("odoo.tests") diff --git a/tests/mock/odoo/odoo/__init__.py b/tests/mock/odoo/odoo/__init__.py new file mode 100644 index 0000000..f912ef4 --- /dev/null +++ b/tests/mock/odoo/odoo/__init__.py @@ -0,0 +1,3 @@ +from mock import MagicMock +registry = MagicMock() +tools = MagicMock() diff --git a/tests/mock/odoo/odoo/tests/__init__.py b/tests/mock/odoo/odoo/tests/__init__.py new file mode 100644 index 0000000..480e37c --- /dev/null +++ b/tests/mock/odoo/odoo/tests/__init__.py @@ -0,0 +1,2 @@ +from mock import MagicMock +common = MagicMock() diff --git a/tests/mock/odoo/setup.py b/tests/mock/odoo/setup.py new file mode 100644 index 0000000..fbdd278 --- /dev/null +++ b/tests/mock/odoo/setup.py @@ -0,0 +1,12 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- + +from setuptools import find_packages, setup + +setup( + name="odoo", + version="0.0.1", + packages=find_packages(), + package_dir={"odoo": "odoo"}, + install_requires="mock" +) diff --git a/tox.ini b/tox.ini new file mode 100644 index 0000000..9b98fdf --- /dev/null +++ b/tox.ini @@ -0,0 +1,7 @@ +[testenv] +deps = pytest + pytest-cov + coverage +commands = + pip install tests/mock/odoo + pytest --cov . From f366539aefaa925e3eb2d34e51995ba309db2073 Mon Sep 17 00:00:00 2001 From: Yannick Vaucher Date: Sat, 5 Oct 2024 13:44:35 +0200 Subject: [PATCH 07/10] fix coverage data collection --- tox.ini | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tox.ini b/tox.ini index 9b98fdf..2099a1b 100644 --- a/tox.ini +++ b/tox.ini @@ -4,4 +4,4 @@ deps = pytest coverage commands = pip install tests/mock/odoo - pytest --cov . + pytest --cov=pytest_odoo . From 93f0aeed57b18a730f26b3d0409c6ab2df57c4b7 Mon Sep 17 00:00:00 2001 From: Yannick Vaucher Date: Sat, 5 Oct 2024 13:51:01 +0200 Subject: [PATCH 08/10] xml report for codecov --- tox.ini | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tox.ini b/tox.ini index 2099a1b..55666df 100644 --- a/tox.ini +++ b/tox.ini @@ -4,4 +4,4 @@ deps = pytest coverage commands = pip install tests/mock/odoo - pytest --cov=pytest_odoo . + pytest --cov=pytest_odoo . --cov-report=xml From 84ddc22876327a02e7b82bffc1bff3f73e883710 Mon Sep 17 00:00:00 2001 From: Yannick Vaucher Date: Sat, 5 Oct 2024 13:55:27 +0200 Subject: [PATCH 09/10] Set token for codecov --- .github/workflows/ci.yml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 1cb8c5b..ecf124f 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -40,3 +40,5 @@ jobs: tox run - name: Upload Coverage to Codecov uses: codecov/codecov-action@v3 + with: + token: ${{ secrets.CODECOV_TOKEN }} # required From f132885d282bb1c5e8ab094c18ae886f2cc22ead Mon Sep 17 00:00:00 2001 From: Yannick Vaucher Date: Sat, 5 Oct 2024 13:58:51 +0200 Subject: [PATCH 10/10] Upgrade GH action codecov to v4 --- .github/workflows/ci.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index ecf124f..46fe72b 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -39,6 +39,6 @@ jobs: run: | tox run - name: Upload Coverage to Codecov - uses: codecov/codecov-action@v3 + uses: codecov/codecov-action@v4 with: token: ${{ secrets.CODECOV_TOKEN }} # required