From fb5a29c8a89ee9f43dcc87a27a00d0faf174bb84 Mon Sep 17 00:00:00 2001 From: Maciej Strzelczyk Date: Mon, 28 Mar 2022 18:03:18 +0200 Subject: [PATCH] chore(samples): Adding a test to make sure snippets are up-to-date (#243) * chore(samples): Adding a test to make sure snippets are up-to-date --- packages/google-cloud-compute/samples/sgs.py | 1 + .../create_with_existing_disks.py | 4 +-- .../google-cloud-compute/samples/test_sgs.py | 25 +++++++++++++++++++ 3 files changed, 28 insertions(+), 2 deletions(-) diff --git a/packages/google-cloud-compute/samples/sgs.py b/packages/google-cloud-compute/samples/sgs.py index d8b2dbeb8d0f..e3fa70a8b94c 100644 --- a/packages/google-cloud-compute/samples/sgs.py +++ b/packages/google-cloud-compute/samples/sgs.py @@ -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.*"), ) diff --git a/packages/google-cloud-compute/samples/snippets/instances/create_start_instance/create_with_existing_disks.py b/packages/google-cloud-compute/samples/snippets/instances/create_start_instance/create_with_existing_disks.py index 62a118ee850d..df36012ea616 100644 --- a/packages/google-cloud-compute/samples/snippets/instances/create_start_instance/create_with_existing_disks.py +++ b/packages/google-cloud-compute/samples/snippets/instances/create_start_instance/create_with_existing_disks.py @@ -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() diff --git a/packages/google-cloud-compute/samples/test_sgs.py b/packages/google-cloud-compute/samples/test_sgs.py index dcc030a17f23..fafae9ed3c7c 100644 --- a/packages/google-cloud-compute/samples/test_sgs.py +++ b/packages/google-cloud-compute/samples/test_sgs.py @@ -16,6 +16,8 @@ from pathlib import Path import tempfile +import pytest + from . import sgs FIXTURE_INGREDIENTS = Path("sgs_test_fixtures/ingredients") @@ -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()