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

Fix macos test discovery path resolution and drop python 2 #194

Merged
merged 16 commits into from
Dec 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
22 changes: 11 additions & 11 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,6 @@ name: Test

on:
pull_request:
branches:
- master
- maintenance/**
push:
branches:
- master
Expand All @@ -17,30 +14,33 @@ env:

jobs:
test:
needs: [lint]
strategy:
matrix:
python: [3.7, 3.8, 3.9, "3.10", "3.11", '3.12', 'pypy2', 'pypy3']
os: [windows-2019, ubuntu-22.04, macos-11]
python-version: ["3.7", "3.8", "3.9", "3.10", "3.11", "3.12", "pypy3.9", "pypy3.10"]
os: [windows-latest, ubuntu-latest, macos-latest]
fail-fast: false
runs-on: ${{ matrix.os }}
steps:
- uses: actions/checkout@v4
- name: Setup python
uses: actions/setup-python@v4
with:
python-version: ${{ matrix.python }}
python-version: ${{ matrix.python-version }}
- name: Install
run: |
pip install -e .
pip install -r "test_requirements.txt"
pip --version
- name: Run tests
continue-on-error: false
working-directory: .github
- name: Run tests with unittest
run: |
coverage run --branch -m unittest discover -v -t . haas
- name: Run tests with haas
run: |
python -m unittest discover haas
python -m haas haas
- name: Print coverage report
run: |
coverage report

lint:
runs-on: ubuntu-latest
steps:
Expand Down
18 changes: 11 additions & 7 deletions haas/plugins/discoverer.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
from __future__ import absolute_import, unicode_literals

from fnmatch import fnmatch
from importlib import import_module
from os import getcwd
from pathlib import Path
import logging
import os
import sys
Expand All @@ -16,7 +19,6 @@
from haas.module_import_error import ModuleImportError
from haas.suite import find_test_cases
from haas.testing import unittest
from haas.utils import get_module_by_name
from .i_discoverer_plugin import IDiscovererPlugin

logger = logging.getLogger(__name__)
Expand All @@ -41,9 +43,11 @@ def test_error(self):


def get_relpath(top_level_directory, fullpath):
normalized = os.path.normpath(fullpath)
relpath = os.path.relpath(normalized, top_level_directory)
if os.path.isabs(relpath) or relpath.startswith('..'):
top_level = Path(top_level_directory).resolve()
normalized = Path(fullpath).resolve()
try:
relpath = str(normalized.relative_to(top_level))
except ValueError:
raise ValueError('Path not within project: {0}'.format(fullpath))
return relpath

Expand Down Expand Up @@ -77,7 +81,7 @@ def find_module_by_name(full_name):
module_attributes = []
while True:
try:
module = get_module_by_name(module_name)
module = import_module(module_name)
except ImportError:
if '.' in module_name:
module_name, attribute = module_name.rsplit('.', 1)
Expand Down Expand Up @@ -365,7 +369,7 @@ def _load_from_file(self, filepath, top_level_directory):
module_name = get_module_name(top_level_directory, filepath)
logger.debug('Loading tests from %r', module_name)
try:
module = get_module_by_name(module_name)
module = import_module(module_name)
except Exception:
test = _create_import_error_test(module_name)
else:
Expand Down Expand Up @@ -421,7 +425,7 @@ def discover_filtered_tests(self, filter_name, top_level_directory=None,
"""
if top_level_directory is None:
top_level_directory = find_top_level_directory(
os.getcwd())
getcwd())

logger.debug('Discovering filtered tests: filter_name=%r, '
'top_level_directory=%r, pattern=%r', top_level_directory,
Expand Down
5 changes: 1 addition & 4 deletions haas/plugins/i_discoverer_plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,10 @@

from abc import ABCMeta, abstractmethod

from six import add_metaclass

from haas.utils import abstractclassmethod


@add_metaclass(ABCMeta)
class IDiscovererPlugin(object):
class IDiscovererPlugin(object, metaclass=ABCMeta):

@abstractclassmethod
def from_args(cls, args, arg_prefix, loader):
Expand Down
5 changes: 1 addition & 4 deletions haas/plugins/i_hook_plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,10 @@

import abc

from six import add_metaclass

from haas.utils import abstractclassmethod


@add_metaclass(abc.ABCMeta)
class IHookPlugin(object):
class IHookPlugin(object, metaclass=abc.ABCMeta):

@abc.abstractmethod
def setup(self): # pragma: no cover
Expand Down
5 changes: 1 addition & 4 deletions haas/plugins/i_result_handler_plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,10 @@

from abc import ABCMeta, abstractmethod

from six import add_metaclass

from haas.utils import abstractclassmethod


@add_metaclass(ABCMeta)
class IResultHandlerPlugin(object):
class IResultHandlerPlugin(object, metaclass=ABCMeta):

@abstractclassmethod
def from_args(cls, args, name, dest_prefix, test_count):
Expand Down
5 changes: 1 addition & 4 deletions haas/plugins/i_runner_plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,10 @@

import abc

from six import add_metaclass

from haas.utils import abstractclassmethod


@add_metaclass(abc.ABCMeta)
class IRunnerPlugin(object):
class IRunnerPlugin(object, metaclass=abc.ABCMeta):

@abstractclassmethod
def from_args(cls, args, arg_prefix):
Expand Down
4 changes: 2 additions & 2 deletions haas/plugins/parallel_runner.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
from importlib import import_module
from multiprocessing import Pool
import time

from haas.module_import_error import ModuleImportError
from haas.suite import find_test_cases
from haas.result import ResultCollector
from haas.utils import get_module_by_name
from .i_result_handler_plugin import IResultHandlerPlugin
from .runner import BaseTestRunner

Expand Down Expand Up @@ -82,7 +82,7 @@ def from_args(cls, args, arg_prefix):
initializer = None
else:
module_name, initializer_name = initializer_spec.rsplit('.', 1)
init_module = get_module_by_name(module_name)
init_module = import_module(module_name)
initializer = getattr(init_module, initializer_name)
return cls(process_count=args.processes, initializer=initializer,
maxtasksperchild=args.process_max_tasks)
Expand Down
3 changes: 2 additions & 1 deletion haas/plugins/tests/test_discoverer.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
from haas.module_import_error import ModuleImportError
from haas.suite import find_test_cases, TestSuite
from haas.utils import cd
from haas.plugins import discoverer
from ..discoverer import (
Discoverer,
filter_test_suite,
Expand Down Expand Up @@ -451,7 +452,7 @@ def test_discover_class(self):
def test_discover_no_top_level(self):
getcwd = mock.Mock()
getcwd.return_value = self.tmpdir
with mock.patch.object(os, 'getcwd', getcwd):
with mock.patch.object(discoverer, 'getcwd', getcwd):
suite = self.discoverer.discover(
'TestCase',
)
Expand Down
10 changes: 5 additions & 5 deletions haas/plugins/tests/test_result_handlers.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from datetime import datetime, timedelta
import statistics

from six.moves import StringIO
from io import StringIO

from haas.result import TestResult, TestCompletionStatus, TestDuration
from haas.testing import unittest
Expand Down Expand Up @@ -53,7 +53,7 @@ def test_output_stop_test_run_success(self, stderr):
output = stderr.getvalue()
output_start = '\n\nTest timing report\n' + handler.separator2
self.assertTrue(output.startswith(output_start))
self.assertRegexpMatches(
self.assertRegex(
output.replace('\n', ''), r'--+.*?00:10\.123 test_method \(')

@mock.patch('time.ctime')
Expand Down Expand Up @@ -234,7 +234,7 @@ def test_output_with_error_on_stop_test_run(self, stderr):
output = stderr.getvalue()
output_start = '\n\nTest timing report\n' + handler.separator2
self.assertTrue(output.startswith(output_start))
self.assertRegexpMatches(
self.assertRegex(
output.replace('\n', ''), r'--+.*?1543:00:12\.234 test_method \(')

@mock.patch('sys.stderr', new_callable=StringIO)
Expand All @@ -261,7 +261,7 @@ def test_output_with_failure_on_stop_test_run(self, stderr):
output = stderr.getvalue()
output_start = '\n\nTest timing report\n' + handler.separator2
self.assertTrue(output.startswith(output_start))
self.assertRegexpMatches(
self.assertRegex(
output.replace('\n', ''), r'--+.*?1:01:14\.567 test_method \(')

def _calculate_statistics(self, test_durations):
Expand Down Expand Up @@ -341,7 +341,7 @@ class Case(_test_cases.TestCase):
output = stderr.getvalue()
output_start = '\n\nTest timing report\n' + handler.separator2
self.assertTrue(output.startswith(output_start))
self.assertRegexpMatches(
self.assertRegex(
output.replace('\n', ''), r'--+.*?00:09\.123 test_method \(')
self.assertIn(expected_stats, output)

Expand Down
5 changes: 2 additions & 3 deletions haas/result.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@
import traceback
import warnings

import six
from six.moves import StringIO
from io import StringIO

from .error_holder import ErrorHolder

Expand Down Expand Up @@ -65,7 +64,7 @@ def _count_relevant_tb_levels(tb):


def _decode(line, encoding):
if isinstance(line, six.text_type):
if isinstance(line, str):
return line
try:
return line.decode(encoding)
Expand Down
5 changes: 1 addition & 4 deletions haas/tests/builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,10 @@
import os
import textwrap

from six import add_metaclass

from ..testing import unittest


@add_metaclass(abc.ABCMeta)
class Importable(object):
class Importable(object, metaclass=abc.ABCMeta):

def __init__(self, name, contents=()):
self.name = name
Expand Down
2 changes: 1 addition & 1 deletion haas/tests/test_buffering.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from datetime import datetime, timedelta
import sys

from six.moves import StringIO
from io import StringIO

from haas.tests.compat import mock
from ..plugins.i_result_handler_plugin import IResultHandlerPlugin
Expand Down
5 changes: 1 addition & 4 deletions haas/tests/test_loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@

import unittest as python_unittest

import six

from haas.testing import unittest

from . import _test_cases
Expand Down Expand Up @@ -111,8 +109,7 @@ def assertSuiteClasses(self, suite, klass):

def test_find_all_cases_in_module(self):
cases = self.loader.get_test_cases_from_module(_test_cases)
six.assertCountEqual(
self,
self.assertCountEqual(
cases, [_test_cases.TestCase, _test_cases.PythonTestCase])

def test_load_all_cases_in_module(self):
Expand Down
2 changes: 1 addition & 1 deletion haas/tests/test_parallel_runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from datetime import datetime, timedelta
import time

from six.moves import StringIO
from io import StringIO

from ..plugins.discoverer import _create_import_error_test
from ..plugins.parallel_runner import ChildResultHandler, ParallelTestRunner
Expand Down
8 changes: 4 additions & 4 deletions haas/tests/test_quiet_result_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

from datetime import datetime, timedelta

from six.moves import StringIO
from io import StringIO

from ..plugins.result_handler import QuietTestResultHandler
from ..result import (
Expand Down Expand Up @@ -46,7 +46,7 @@ def test_output_stop_test_run(self, stderr):
output = stderr.getvalue()
self.assertTrue(output.startswith('\n' + handler.separator2))
self.assertTrue(output.endswith('OK\n'))
self.assertRegexpMatches(
self.assertRegex(
output.replace('\n', ''), r'--+.*?Ran 0 tests.*?OK')

@mock.patch('sys.stderr', new_callable=StringIO)
Expand Down Expand Up @@ -226,7 +226,7 @@ def test_no_output_with_error_on_stop_test_run(self, stderr):
output = stderr.getvalue().replace('\n', '')
description = handler.get_test_description(
case,).replace('(', r'\(').replace(')', r'\)')
self.assertRegexpMatches(
self.assertRegex(
output, '{0}.*?Traceback.*?RuntimeError'.format(
description))

Expand Down Expand Up @@ -254,7 +254,7 @@ def test_no_output_with_failure_on_stop_test_run(self, stderr):
output = stderr.getvalue().replace('\n', '')
description = handler.get_test_description(
case,).replace('(', r'\(').replace(')', r'\)').replace('\n', '')
self.assertRegexpMatches(
self.assertRegex(
output, '{0}.*?Traceback.*?AssertionError'.format(
description))
# The contents of unittest.TestCase should not be in the traceback
Expand Down
8 changes: 4 additions & 4 deletions haas/tests/test_standard_result_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

from datetime import datetime, timedelta

from six.moves import StringIO
from io import StringIO

from ..plugins.result_handler import StandardTestResultHandler
from ..result import (
Expand Down Expand Up @@ -46,7 +46,7 @@ def test_output_stop_test_run(self, stderr):
output = stderr.getvalue()
self.assertTrue(output.startswith('\n' + handler.separator2))
self.assertTrue(output.endswith('OK\n'))
self.assertRegexpMatches(
self.assertRegex(
output.replace('\n', ''), r'--+.*?Ran 0 tests.*?OK')

@mock.patch('sys.stderr', new_callable=StringIO)
Expand Down Expand Up @@ -226,7 +226,7 @@ def test_no_output_with_error_on_stop_test_run(self, stderr):
output = stderr.getvalue().replace('\n', '')
description = handler.get_test_description(
case).replace('(', r'\(').replace(')', r'\)')
self.assertRegexpMatches(
self.assertRegex(
output, '{0}.*?Traceback.*?RuntimeError'.format(
description))

Expand Down Expand Up @@ -254,7 +254,7 @@ def test_no_output_with_failure_on_stop_test_run(self, stderr):
output = stderr.getvalue().replace('\n', '')
description = handler.get_test_description(
case).replace('(', r'\(').replace(')', r'\)').replace('\n', '')
self.assertRegexpMatches(
self.assertRegex(
output, '{0}.*?Traceback.*?AssertionError'.format(
description))
# The contents of unittest.TestCase should not be in the traceback
Expand Down
Loading