2022-01-01 06:52:31 -08:00
|
|
|
# Copyright 2022 Google LLC
|
2020-11-07 01:40:09 -08:00
|
|
|
#
|
|
|
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
# you may not use this file except in compliance with the License.
|
|
|
|
# You may obtain a copy of the License at
|
|
|
|
#
|
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
#
|
|
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
# See the License for the specific language governing permissions and
|
|
|
|
# limitations under the License.
|
|
|
|
|
2022-11-17 10:15:42 -08:00
|
|
|
import collections
|
|
|
|
import re
|
2020-11-07 01:28:33 -08:00
|
|
|
from pathlib import Path
|
|
|
|
|
|
|
|
import marko
|
2022-10-14 05:56:16 -07:00
|
|
|
import pytest
|
2020-11-07 01:28:33 -08:00
|
|
|
|
2022-09-06 08:46:09 -07:00
|
|
|
FABRIC_ROOT = Path(__file__).parents[2]
|
|
|
|
MODULES_PATH = FABRIC_ROOT / 'modules/'
|
2022-09-09 06:33:59 -07:00
|
|
|
BLUEPRINTS_PATH = FABRIC_ROOT / 'blueprints/'
|
2020-11-07 01:28:33 -08:00
|
|
|
|
2022-11-17 10:15:42 -08:00
|
|
|
FILE_TEST_RE = re.compile(r'# tftest file (\w+) ([\S]+)')
|
|
|
|
|
|
|
|
Example = collections.namedtuple('Example', 'code files')
|
|
|
|
File = collections.namedtuple('File', 'path content')
|
|
|
|
|
2020-11-07 01:28:33 -08:00
|
|
|
|
|
|
|
def pytest_generate_tests(metafunc):
|
|
|
|
if 'example' in metafunc.fixturenames:
|
2022-09-06 08:46:09 -07:00
|
|
|
modules = [x for x in MODULES_PATH.iterdir() if x.is_dir()]
|
2022-09-09 06:33:59 -07:00
|
|
|
modules.extend(x for x in BLUEPRINTS_PATH.glob("*/*") if x.is_dir())
|
2020-11-07 01:28:33 -08:00
|
|
|
modules.sort()
|
|
|
|
examples = []
|
|
|
|
ids = []
|
2022-09-06 08:46:09 -07:00
|
|
|
|
2020-11-07 01:28:33 -08:00
|
|
|
for module in modules:
|
|
|
|
readme = module / 'README.md'
|
2022-01-01 06:52:31 -08:00
|
|
|
if not readme.exists():
|
|
|
|
continue
|
2020-11-07 01:28:33 -08:00
|
|
|
doc = marko.parse(readme.read_text())
|
|
|
|
index = 0
|
2022-01-28 11:35:40 -08:00
|
|
|
last_header = None
|
2022-11-17 10:15:42 -08:00
|
|
|
files = {}
|
|
|
|
|
|
|
|
#first pass: collect all tftest tagged files
|
|
|
|
for child in doc.children:
|
|
|
|
if isinstance(child, marko.block.FencedCode):
|
|
|
|
code = child.children[0].children
|
|
|
|
match = FILE_TEST_RE.search(code)
|
|
|
|
if match:
|
|
|
|
name, path = match.groups()
|
|
|
|
files[name] = File(path, code)
|
|
|
|
|
2020-11-07 01:28:33 -08:00
|
|
|
for child in doc.children:
|
2022-11-16 15:14:09 -08:00
|
|
|
if isinstance(child, marko.block.FencedCode):
|
2020-11-07 01:28:33 -08:00
|
|
|
index += 1
|
|
|
|
code = child.children[0].children
|
2022-01-28 11:15:35 -08:00
|
|
|
if 'tftest skip' in code:
|
2020-11-07 01:28:33 -08:00
|
|
|
continue
|
2022-11-17 10:15:42 -08:00
|
|
|
if child.lang == 'hcl':
|
|
|
|
examples.append(Example(code, files))
|
2022-11-16 15:14:09 -08:00
|
|
|
path = module.relative_to(FABRIC_ROOT)
|
|
|
|
name = f'{path}:{last_header}'
|
|
|
|
if index > 1:
|
|
|
|
name += f' {index}'
|
|
|
|
ids.append(name)
|
2022-01-28 11:35:40 -08:00
|
|
|
elif isinstance(child, marko.block.Heading):
|
|
|
|
last_header = child.children[0].children
|
|
|
|
index = 0
|
2020-11-07 01:28:33 -08:00
|
|
|
|
|
|
|
metafunc.parametrize('example', examples, ids=ids)
|