Skip to content

Commit

Permalink
chore(samples): Adding a test to make sure snippets are up-to-date (#243
Browse files Browse the repository at this point in the history
)

* chore(samples): Adding a test to make sure snippets are up-to-date
  • Loading branch information
m-strzelczyk authored Mar 28, 2022
1 parent ff549bd commit fb5a29c
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 2 deletions.
1 change: 1 addition & 0 deletions packages/google-cloud-compute/samples/sgs.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ def __repr__(self):
re.compile(r".*requirements-test\.txt$"),
re.compile(r".*?/tests/.*"),
re.compile(r".*?/__pycache__/.*"),
re.compile(r".*?sponge_log.xml.*"),
)


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,11 @@

def get_disk(project_id: str, zone: str, disk_name: str) -> compute_v1.Disk:
"""
Deletes a disk from a project.
Gets a disk from a project.
Args:
project_id: project ID or project number of the Cloud project you want to use.
zone: name of the zone in which is the disk you want to delete.
zone: name of the zone where the disk exists.
disk_name: name of the disk you want to retrieve.
"""
disk_client = compute_v1.DisksClient()
Expand Down
25 changes: 25 additions & 0 deletions packages/google-cloud-compute/samples/test_sgs.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
from pathlib import Path
import tempfile

import pytest

from . import sgs

FIXTURE_INGREDIENTS = Path("sgs_test_fixtures/ingredients")
Expand All @@ -30,3 +32,26 @@ def test_sgs_generate():
for test_file in map(Path, glob.glob(f"{tmp_dir}/**")):
match_file = FIXTURE_OUTPUT / test_file.relative_to(tmp_dir)
assert test_file.read_bytes() == match_file.read_bytes()


def test_snippets_freshness():
"""
Make sure that the snippets/ folder is up-to-date and matches
ingredients/ and recipes/. This test will generate SGS output
in a temporary directory and compare it to the content of
snippets/ folder.
"""
with tempfile.TemporaryDirectory() as tmp_dir:
args = Namespace(output_dir=tmp_dir)
sgs.generate(args, Path("ingredients/").absolute(), Path("recipes/").absolute())
print(list(map(Path, glob.glob(f"{tmp_dir}/**"))))
for test_file in map(Path, glob.glob(f"{tmp_dir}/**", recursive=True)):
match_file = Path("snippets/") / test_file.relative_to(tmp_dir)
if test_file.is_file():
if test_file.read_bytes() != match_file.read_bytes():
pytest.fail(
f"This test fails because file {match_file} seems to be outdated. Please run "
f"`python sgs.py generate` to update your snippets."
)
elif test_file.is_dir():
assert match_file.is_dir()

0 comments on commit fb5a29c

Please sign in to comment.