Skip to content

Commit

Permalink
[#709] Add support for pytest (#710)
Browse files Browse the repository at this point in the history
* Initial pytest conversion.

* Fix sudo run.

* Run elevated tess on Windows.

* Fix docstring.

* Cleanup readme.

* Fix tests.

* Enable more pytest tests.
  • Loading branch information
adiroiban authored Dec 9, 2024
1 parent 2985c14 commit d8847a8
Show file tree
Hide file tree
Showing 13 changed files with 142 additions and 59 deletions.
50 changes: 45 additions & 5 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -62,12 +62,24 @@ jobs:
with:
limit-access-to-actor: true

- name: Test
- name: Test pynose
run: |
./pythia.sh test_ci2
env:
CHEVAH_BUILD: build-py3-ț

- name: Test pytest
run: |
./pythia.sh pytest
env:
CHEVAH_BUILD: build-py3-ț

- name: Test pytest elevated
run: |
sudo -E ./pythia.sh pytest src/chevah_compat/tests/elevated/
env:
CHEVAH_BUILD: build-py3-ț

- name: Move build back to to ascii
run: mv build-py3-ț build-py

Expand Down Expand Up @@ -107,7 +119,15 @@ jobs:
- name: Test
run: ./pythia.sh test_ci2
env:
CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }}
CODECOV_TOKEN: enabled

- name: Test pytest
run: |
./pythia.sh pytest
- name: Test pytest elevated
run: |
sudo -E ./pythia.sh pytest src/chevah_compat/tests/elevated/
- name: Publish cov
run: ./pythia.sh codecov_publish
Expand Down Expand Up @@ -151,15 +171,15 @@ jobs:
run: ./pythia.sh test_ci2
env:
CHEVAH_BUILD: build-py3-ț
CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }}
CODECOV_TOKEN: enabled

- name: Move build back to to ascii
run: mv build-py3-ț build-py3

- name: Publish cov
run: ./pythia.sh codecov_publish
env:
CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }}
CODECOV_TOKEN: enabled

- name: Deps
run: |
Expand All @@ -175,7 +195,15 @@ jobs:
- name: Test
run: ./pythia.sh test_ci2
env:
CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }}
CODECOV_TOKEN: enabled

- name: Test pytest
run: |
./pythia.sh pytest
- name: Test pytest elevated
run: |
sudo -E ./pythia.sh pytest src/chevah_compat/tests/elevated/
- name: Publish cov
run: ./pythia.sh codecov_publish
Expand Down Expand Up @@ -229,6 +257,18 @@ jobs:
CODECOV_TOKEN: enabled
USER: runneradmin

- name: Test pytest
run: |
./pythia.sh pytest
env:
USER: runneradmin

- name: Test pytest elevated
run: |
./pythia.sh pytest src/chevah_compat/tests/elevated/
env:
USER: runneradmin

- name: Public coverage
if: ${{ !cancelled() }}
run: ./pythia.sh codecov_publish
Expand Down
23 changes: 3 additions & 20 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,32 +4,15 @@ chevah-compat
.. image:: https://codecov.io/gh/chevah/compat/branch/master/graph/badge.svg
:target: https://codecov.io/gh/chevah/compat

.. image:: https://github.com/chevah/compat/workflows/Lint/badge.svg
:target: https://github.com/chevah/compat/actions/workflows/lint.yml
.. image:: https://github.com/chevah/compat/workflows/ci/badge.svg
:target: https://github.com/chevah/compat/actions/workflows/ci.yml

.. image:: https://github.com/chevah/compat/workflows/Bare/badge.svg
:target: https://github.com/chevah/compat/actions/workflows/bare.yml

.. image:: https://github.com/chevah/compat/workflows/Docker/badge.svg
:target: https://github.com/chevah/compat/actions/workflows/docker.yml


Chevah OS Compatibility Layer.

Depends on:

* pam - for pam authentication
* arpy - for loading libraries on AIX.

Only Python 2.7 is supported for now.
Chevah OS Compatibility Layer for Python 3.

Unified interface for working with operating system accounts on Unix
and Windows, see IOSUsers.

Support for starting Unix daemons.

Importing this module on Windows populates sys.argv with Unicode values.

The unified interface for working with operating system file systems provides:

* Unicode
Expand Down
14 changes: 14 additions & 0 deletions pavement.py
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,20 @@ def test_ci2(args):
return call_task('test_python', args=args)


@task
@consume_args
def pytest(args):
"""
Run tests using pytest.
"""
import sys

from pytest import console_main

sys.argv = ['pytest'] + args
sys.exit(console_main())


@task
@consume_args
def lint(args):
Expand Down
3 changes: 2 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ build-backend = "setuptools.build_meta"

[project]
name = "chevah-compat"
version = "1.2.2"
version = "1.3.0"
maintainers = [
{ name = "Adi Roiban", email = "[email protected]" },
]
Expand Down Expand Up @@ -44,6 +44,7 @@ dev = [
"incremental",
"service-identity==24.2.0",
"pynose == 1.5.3",
"pytest ~= 8.3",
"coverage ~= 7.5",
"diff_cover == 9.2.0",
"codecov == 2.1.12",
Expand Down
10 changes: 10 additions & 0 deletions pytest.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
[pytest]
minversion = 6.0
addopts = -ra -q -v --capture=no
testpaths =
src/chevah_compat/tests/normal

filterwarnings =
ignore: 'crypt' is deprecated and slated for removal
ignore: 'spwd' is deprecated and slated for removal

5 changes: 5 additions & 0 deletions release-notes.rst
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
Release notes for chevah.compat
===============================

1.3.0 - 2024-12-08
------------------

* Add support for running tests with `pytest`.


1.2.2 - 2024-11-03
------------------
Expand Down
9 changes: 6 additions & 3 deletions src/chevah_compat/administration.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,14 +102,16 @@ def _getUnixGroup(self, name):

# Try to get the group in list of all groups.
group_found = False
for iterator in range(1000):
for iterator in range(100):
if group_found:
break
for group in grp.getgrall():
if group[0] == name:
if group.gr_name == name:
group_found = True
break
time.sleep(0.1)
if not group_found:
print(f'Waiting for group "{name}"...')
time.sleep(0.1)

if not group_found:
raise AssertionError(f'Failed to get group from all: {name}')
Expand All @@ -123,6 +125,7 @@ def _getUnixGroup(self, name):
except KeyError:
# Group not ready yet.
pass
print(f'Waiting for group "{name}" in API...')
time.sleep(0.1)

raise AssertionError(
Expand Down
4 changes: 2 additions & 2 deletions src/chevah_compat/testing/assertion.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ def assertIsEmpty(self, target):
"""
Raise AssertionError if target is not empty.
"""
if isinstance(target, collections.Iterable):
if isinstance(target, collections.abc.Iterable):
iterator = iter(target)
try:
next(iterator)
Expand All @@ -167,7 +167,7 @@ def assertIsNotEmpty(self, target):
"""
Raise AssertionError if target is empty.
"""
if isinstance(target, collections.Iterable):
if isinstance(target, collections.abc.Iterable):
try:
self.assertIsEmpty(target)
except AssertionError:
Expand Down
2 changes: 2 additions & 0 deletions src/chevah_compat/testing/mockup.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ def _sanitize_name_windows(candidate):


class SanitizeNameMixin:
__test__ = False

@classmethod
def sanitizeName(cls, name):
"""
Expand Down
6 changes: 2 additions & 4 deletions src/chevah_compat/testing/testcase.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,9 +92,7 @@ def _caller_success_member(self):
"""
Return true if last test run was successful.
"""
if self._outcome.result.errors:
return False
return True
return self._outcome.success

def tearDown(self):
try:
Expand Down Expand Up @@ -993,7 +991,7 @@ def tearDown(self):
threads = threading.enumerate()
if len(threads) > 1:
for thread in threads:
thread_name = thread.getName()
thread_name = thread.name
if self._isExceptedThread(thread_name):
continue
self._teardown_errors.append(
Expand Down
22 changes: 22 additions & 0 deletions src/chevah_compat/tests/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# Copyright (c) 2024 Adi Roiban.
# See LICENSE for details.
"""
Fixtures for pytest run mode.
"""

import pytest

from chevah_compat.tests import setup_package, teardown_package


@pytest.fixture(scope='session', autouse=True)
def setup_package_pytest():
"""
Just wraps pynose package setup to pytest fixtuers.
Mare sure the name does not start with `pytest` as this is reserved
for the pytest framework.
"""
setup_package()
yield
teardown_package()
22 changes: 22 additions & 0 deletions src/chevah_compat/tests/elevated/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# Copyright (c) 2024 Adi Roiban.
# See LICENSE for details.
"""
Fixtures for pytest run mode.
"""

import pytest

from chevah_compat.tests.elevated import setup_package, teardown_package


@pytest.fixture(scope='session', autouse=True)
def setup_package_elevated_pytest():
"""
Just wraps pynose elevated package setup to pytest fixtures.
Mare sure the name does not start with `pytest` as this is reserved
for the pytest framework.
"""
setup_package()
yield
teardown_package()
31 changes: 7 additions & 24 deletions src/chevah_compat/tests/normal/testing/test_testcase.py
Original file line number Diff line number Diff line change
Expand Up @@ -727,37 +727,20 @@ def test_onCapability(self):
'is True.',
)

@conditionals.onAdminPrivileges(True)
def test_onAdminPrivileges_present(self):
"""
Run test only on machines that execute the tests with administrator
privileges.
"""
if self.os_version in ['nt-5.1', 'nt-5.2']:
raise AssertionError(
'Windows XP and 2003 BS does not run as administrator',
)

if self.ci_name in [self.CI.TRAVIS, self.CI.GITHUB]:
raise AssertionError(
'Travis and GitHub does not run as administrator',
)

@conditionals.onAdminPrivileges(False)
def test_onAdminPrivileges_missing(self):
"""
Run test on build slaves that do not have administrator privileges.
Runs test in environments that don't have administrator privileges.
"""
if (
self.TEST_LANGUAGE == 'FR'
or self.os_name
or self.ci_name not in [self.CI.BUILDBOT, self.CI.LOCAL]
):
# Not available on Windows XP and 2003 and non-buildbot runs.
if self.TEST_LANGUAGE == 'FR' or self.ci_name in [
self.CI.LOCAL,
self.CI.GITHUB,
]:
# We expect to local run not to have admin.
return

raise AssertionError(
f'"{self.hostname}" is running with administrator privileges',
f'"{self.hostname}" is running with non-administrator privileges',
)

def test_cleanup_test_segments_file(self):
Expand Down

0 comments on commit d8847a8

Please sign in to comment.