Skip to content

Commit

Permalink
refactor(test): add util.chdir, util.copied_fixtures_dir contextmanag…
Browse files Browse the repository at this point in the history
…ers to clean up tests (#1053)

* test: add chdir contextmanager utility

* refactor(test): use chdir contextmanager to clean up some tests

refactor(test): use chdir contextmanager to clean up some tests

refactor(test): use chdir contextmanager to clean up some tests

refactor(test): use chdir contextmanager to clean up some tests

* fix(test): remove refactored fixture reference

* refactor(test): add util.copied_fixtures_dir contextmanager to DRY up tests

* docs: add docstrings for the 2 new contextmanagers

* chore: fix lint

* chore: fix lint

Co-authored-by: Benjamin E. Coe <bencoe@google.com>
  • Loading branch information
chingor13 and bcoe committed Apr 20, 2021
1 parent ff6b759 commit 5a6f30b
Show file tree
Hide file tree
Showing 10 changed files with 319 additions and 396 deletions.
208 changes: 68 additions & 140 deletions tests/test_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@
import os
import requests_mock
import synthtool as s
import tempfile
import shutil
from . import util


MOCK = Path(__file__).parent / "generationmock"
Expand Down Expand Up @@ -53,172 +52,101 @@ def test_get_default_branch():

def test_py_samples_clientlib():
path_to_gen = MOCK / "client_library"
with tempfile.TemporaryDirectory() as tempdir:
workdir = shutil.copytree(path_to_gen, Path(tempdir) / "client_library")
cwd = os.getcwd()
os.chdir(workdir)

try:
sample_files = common.py_samples(
unit_cov_level=97, cov_level=99, samples=True
)
s.move(sample_files, excludes=["noxfile.py"])
assert os.path.isfile(workdir / "samples" / "README.md")
finally:
os.chdir(cwd)
with util.copied_fixtures_dir(path_to_gen) as workdir:
sample_files = common.py_samples(unit_cov_level=97, cov_level=99, samples=True)
s.move(sample_files, excludes=["noxfile.py"])
assert os.path.isfile(workdir / "samples" / "README.md")


def test_py_samples_custom_path():
path_to_gen = MOCK / "custom_path"
with tempfile.TemporaryDirectory() as tempdir:
workdir = shutil.copytree(path_to_gen, Path(tempdir) / "custom_path")
cwd = os.getcwd()
os.chdir(workdir)
with util.copied_fixtures_dir(path_to_gen) as workdir:
sample_files = common.py_samples(unit_cov_level=97, cov_level=99, samples=True)
s.move(sample_files, excludes=["noxfile.py"])
assert os.path.isfile(workdir / "custom_samples_folder" / "README.md")


try:
def test_py_samples_custom_path_DNE():
path_to_gen = MOCK / "custom_path_DNE"
with util.copied_fixtures_dir(path_to_gen) as workdir:
with raises(Exception) as e:
os.chdir(workdir / "custom_path_DNE")
sample_files = common.py_samples(
unit_cov_level=97, cov_level=99, samples=True
)
s.move(sample_files, excludes=["noxfile.py"])
assert os.path.isfile(workdir / "custom_samples_folder" / "README.md")
finally:
os.chdir(cwd)


def test_py_samples_custom_path_DNE():
with tempfile.TemporaryDirectory() as tempdir:
workdir = shutil.copytree(
MOCK / "custom_path_DNE", Path(tempdir) / "custom_path_DNE"
)
cwd = os.getcwd()
os.chdir(workdir)

try:
with raises(Exception) as e:
os.chdir(workdir / "custom_path_DNE")
sample_files = common.py_samples(
unit_cov_level=97, cov_level=99, samples=True
)
s.move(sample_files, excludes=["noxfile.py"])
assert "'nonexistent_folder' does not exist" in str(e.value)
finally:
os.chdir(cwd)
assert "'nonexistent_folder' does not exist" in str(e.value)


def test_py_samples_samples_folder():
path_to_gen = MOCK / "samples_folder"
with tempfile.TemporaryDirectory() as tempdir:
workdir = shutil.copytree(path_to_gen, Path(tempdir) / "samples_folder")
cwd = os.getcwd()
os.chdir(workdir)

try:
sample_files = common.py_samples(
unit_cov_level=97, cov_level=99, samples=True
)
s.move(sample_files, excludes=["noxfile.py"])
assert os.path.isfile(workdir / "README.md")
finally:
os.chdir(cwd)
with util.copied_fixtures_dir(path_to_gen) as workdir:
sample_files = common.py_samples(unit_cov_level=97, cov_level=99, samples=True)
s.move(sample_files, excludes=["noxfile.py"])
assert os.path.isfile(workdir / "README.md")


def test_py_samples_override():
path_to_gen = MOCK / "override_path"
with tempfile.TemporaryDirectory() as tempdir:
workdir = shutil.copytree(path_to_gen, Path(tempdir) / "override_path")
cwd = os.getcwd()
os.chdir(workdir)

try:
sample_files = common.py_samples(
unit_cov_level=97, cov_level=99, samples=True
)
for path in sample_files:
s.move(path, excludes=["noxfile.py"])
assert os.path.isfile(workdir / "README.md")
assert os.path.isfile(workdir / "override" / "README.md")
finally:
os.chdir(cwd)
with util.copied_fixtures_dir(path_to_gen) as workdir:
sample_files = common.py_samples(unit_cov_level=97, cov_level=99, samples=True)
for path in sample_files:
s.move(path, excludes=["noxfile.py"])
assert os.path.isfile(workdir / "README.md")
assert os.path.isfile(workdir / "override" / "README.md")


def test_py_samples_override_content():
path_to_gen = MOCK / "override_path"
with tempfile.TemporaryDirectory() as tempdir:
workdir = shutil.copytree(path_to_gen, Path(tempdir) / "override_path")
cwd = os.getcwd()
with util.copied_fixtures_dir(path_to_gen) as workdir:
sample_files = common.py_samples(unit_cov_level=97, cov_level=99, samples=True)
for path in sample_files:
s.move(path, excludes=["noxfile.py"])
os.chdir(workdir)

try:
sample_files = common.py_samples(
unit_cov_level=97, cov_level=99, samples=True
)
for path in sample_files:
s.move(path, excludes=["noxfile.py"])
os.chdir(workdir)
with open("README.md") as f:
result = f.read()
assert "Hello World" in result
assert "Quickstart" not in result
os.chdir(workdir / "override")
with open("README.md") as f:
result = f.read()
assert "Hello World" not in result
assert "Quickstart" in result
finally:
os.chdir(cwd)
with open("README.md") as f:
result = f.read()
assert "Hello World" in result
assert "Quickstart" not in result
os.chdir(workdir / "override")
with open("README.md") as f:
result = f.read()
assert "Hello World" not in result
assert "Quickstart" in result


def test_py_samples_multiple_override():
path_to_gen = MOCK / "multiple_override_path"
with tempfile.TemporaryDirectory() as tempdir:
workdir = shutil.copytree(path_to_gen, Path(tempdir) / "multiple_override_path")
cwd = os.getcwd()
os.chdir(workdir)

try:
sample_files = common.py_samples(
unit_cov_level=97, cov_level=99, samples=True
)
for path in sample_files:
s.move(path, excludes=["noxfile.py"])
assert os.path.isfile(workdir / "README.md")
assert os.path.isfile(workdir / "override" / "README.md")
assert os.path.isfile(workdir / "another_override" / "README.md")
os.chdir(workdir / "another_override")
with open("README.md") as f:
result = f.read()
assert "Hello World" in result
assert "Hello Synthtool" in result
finally:
os.chdir(cwd)
with util.copied_fixtures_dir(path_to_gen) as workdir:
sample_files = common.py_samples(unit_cov_level=97, cov_level=99, samples=True)
for path in sample_files:
s.move(path, excludes=["noxfile.py"])
assert os.path.isfile(workdir / "README.md")
assert os.path.isfile(workdir / "override" / "README.md")
assert os.path.isfile(workdir / "another_override" / "README.md")
os.chdir(workdir / "another_override")
with open("README.md") as f:
result = f.read()
assert "Hello World" in result
assert "Hello Synthtool" in result


def test_py_samples_multiple_override_content():
path_to_gen = MOCK / "multiple_override_path"
with tempfile.TemporaryDirectory() as tempdir:
workdir = shutil.copytree(path_to_gen, Path(tempdir) / "multiple_override_path")
cwd = os.getcwd()
with util.copied_fixtures_dir(path_to_gen) as workdir:
sample_files = common.py_samples(unit_cov_level=97, cov_level=99, samples=True)
for path in sample_files:
s.move(path, excludes=["noxfile.py"])
os.chdir(workdir / "override")
with open("README.md") as f:
result = f.read()
assert "Quickstart" in result
assert "first_arg" in result
os.chdir(workdir / "another_override")
with open("README.md") as f:
result = f.read()
assert "Hello World" in result
assert "Hello Synthtool" in result
os.chdir(workdir)

try:
sample_files = common.py_samples(
unit_cov_level=97, cov_level=99, samples=True
)
for path in sample_files:
s.move(path, excludes=["noxfile.py"])
os.chdir(workdir / "override")
with open("README.md") as f:
result = f.read()
assert "Quickstart" in result
assert "first_arg" in result
os.chdir(workdir / "another_override")
with open("README.md") as f:
result = f.read()
assert "Hello World" in result
assert "Hello Synthtool" in result
os.chdir(workdir)
with open("README.md") as f:
result = f.read()
assert "Last Example" in result
finally:
os.chdir(cwd)
with open("README.md") as f:
result = f.read()
assert "Last Example" in result
38 changes: 15 additions & 23 deletions tests/test_language_java.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
from synthtool.languages import java
import requests_mock
import pytest
from . import util

FIXTURES = Path(__file__).parent / "fixtures"
TEMPLATES_PATH = Path(__file__).parent.parent / "synthtool" / "gcp" / "templates"
Expand Down Expand Up @@ -90,29 +91,20 @@ def assert_valid_yaml(file):
except yaml.YAMLError:
pytest.fail(f"unable to parse YAML: {file}")

with tempfile.TemporaryDirectory() as tempdir:
workdir = shutil.copytree(
FIXTURES / "java_templates" / "standard", Path(tempdir) / "standard"
)
cwd = os.getcwd()
os.chdir(workdir)

try:
# generate the common templates
java.common_templates(template_path=TEMPLATES_PATH)
assert os.path.isfile("renovate.json")

# lint xml, yaml files
# use os.walk because glob ignores hidden directories
for (dirpath, _, filenames) in os.walk(tempdir):
for file in filenames:
(_, ext) = os.path.splitext(file)
if ext == ".xml":
assert_valid_xml(os.path.join(dirpath, file))
elif ext == ".yaml" or ext == ".yml":
assert_valid_yaml(os.path.join(dirpath, file))
finally:
os.chdir(cwd)
with util.copied_fixtures_dir(FIXTURES / "java_templates" / "standard") as workdir:
# generate the common templates
java.common_templates(template_path=TEMPLATES_PATH)
assert os.path.isfile("renovate.json")

# lint xml, yaml files
# use os.walk because glob ignores hidden directories
for (dirpath, _, filenames) in os.walk(workdir):
for file in filenames:
(_, ext) = os.path.splitext(file)
if ext == ".xml":
assert_valid_xml(os.path.join(dirpath, file))
elif ext == ".yaml" or ext == ".yml":
assert_valid_yaml(os.path.join(dirpath, file))


def test_remove_method():
Expand Down
Loading

0 comments on commit 5a6f30b

Please sign in to comment.