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

test: pytest conversion for test lib and infra #1196

Merged
merged 7 commits into from
Apr 24, 2024
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
64 changes: 20 additions & 44 deletions test/test_infra.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,56 +13,32 @@
# limitations under the License.

import os
import pathlib
import subprocess
import sys
import tempfile
import typing
import unittest

import pytest

def get_python_filepaths(include_tests: bool = True):
"""Helper to retrieve paths of Python files."""
python_paths: typing.List[str] = []
roots = ['ops']
if include_tests:
roots.append('test')
for root in roots:
for dirpath, _, filenames in os.walk(root):
for filename in filenames:
if filename.endswith(".py"):
python_paths.append(os.path.join(dirpath, filename))
return python_paths


class ImportersTestCase(unittest.TestCase):

@pytest.mark.parametrize("mod_name", [
'charm',
'framework',
'main',
'model',
'testing',
])
def test_import(mod_name: str, tmp_path: pathlib.Path):
template = "from ops import {module_name}"

def test_imports(self):
mod_names = [
'charm',
'framework',
'main',
'model',
'testing',
]

for name in mod_names:
with self.subTest(name=name):
self.check(name)

def check(self, name: str):
"""Helper function to run the test."""
fd, testfile = tempfile.mkstemp()
self.addCleanup(os.unlink, testfile)
testfile = tmp_path / "foo.py"
with open(testfile, 'w', encoding='utf8') as fh:
fh.write(template.format(module_name=mod_name))

with open(fd, 'w', encoding='utf8') as fh:
fh.write(self.template.format(module_name=name))
environ = os.environ.copy()
if 'PYTHONPATH' in environ:
environ['PYTHONPATH'] = os.getcwd() + os.pathsep + environ['PYTHONPATH']
else:
environ['PYTHONPATH'] = os.getcwd()

environ = os.environ.copy()
if 'PYTHONPATH' in environ:
environ['PYTHONPATH'] = os.getcwd() + os.pathsep + environ['PYTHONPATH']
else:
environ['PYTHONPATH'] = os.getcwd()
proc = subprocess.run([sys.executable, testfile], env=environ)
assert proc.returncode == 0
proc = subprocess.run([sys.executable, testfile], env=environ)
assert proc.returncode == 0
Loading
Loading