Skip to content

Commit

Permalink
Merge pull request #42845 from tchaikov/wip-51483
Browse files Browse the repository at this point in the history
common/options: validate see-also

Reviewed-by: Deepika Upadhyay <dupadhya@redhat.com>
  • Loading branch information
tchaikov committed Aug 24, 2021
2 parents 8c54a70 + a66daec commit ea890d7
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 2 deletions.
3 changes: 2 additions & 1 deletion cmake/modules/AddCephTest.cmake
Expand Up @@ -2,7 +2,8 @@

#adds makes target/script into a test, test to check target, sets necessary environment variables
function(add_ceph_test test_name test_path)
add_test(NAME ${test_name} COMMAND ${test_path} ${ARGN})
add_test(NAME ${test_name} COMMAND ${test_path} ${ARGN}
COMMAND_EXPAND_LISTS)
if(TARGET ${test_name})
add_dependencies(tests ${test_name})
set_property(TARGET ${test_name}
Expand Down
9 changes: 8 additions & 1 deletion src/common/options/CMakeLists.txt
@@ -1,5 +1,6 @@
set(common_options_srcs build_options.cc)
set(legacy_options_headers)
set(options_yamls)

# to mimic the behavior of file(CONFIGURE ...)
file(GENERATE OUTPUT configure_file.cmake
Expand All @@ -26,11 +27,13 @@ function(add_options name)
set(yaml_file ${CMAKE_CURRENT_BINARY_DIR}/${name}.yaml)
file_configure("${yaml_in_file}"
"${yaml_file}" @ONLY)
list(APPEND options_yamls ${yaml_file})
set(options_yamls ${options_yamls} PARENT_SCOPE)
set(cc_file "${name}_options.cc")
set(h_file "${PROJECT_BINARY_DIR}/include/${name}_legacy_options.h")
add_custom_command(PRE_BUILD
OUTPUT ${cc_file} ${h_file}
COMMAND ${Python_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/y2c.py
COMMAND ${Python3_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/y2c.py
--input ${yaml_file}
--output ${cc_file}
--legacy ${h_file}
Expand Down Expand Up @@ -102,3 +105,7 @@ add_library(common-options-objs OBJECT
${common_options_srcs})
add_custom_target(legacy-option-headers
DEPENDS ${legacy_options_headers})

include(AddCephTest)
add_ceph_test(validate-options
${Python3_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/validate-options.py ${options_yamls})
49 changes: 49 additions & 0 deletions src/common/options/validate-options.py
@@ -0,0 +1,49 @@
#!/usr/bin/env python3

import argparse
import fileinput
import sys
import yaml
from typing import Any, Dict


class ValidationError(Exception):
pass


OptionType = Dict[str, Any]


def validate_see_also(opt: OptionType, opts: Dict[str, OptionType]) -> None:
see_also = opt.get('see_also')
if see_also is None:
return
for ref in see_also:
if ref not in opts:
msg = f'see_also contains "{ref}". But it is not found.'
raise ValidationError(msg)


def main() -> None:
parser = argparse.ArgumentParser(
formatter_class=argparse.ArgumentDefaultsHelpFormatter)
parser.add_argument('yamls', nargs='*')
opts = parser.parse_args()
options = {}
for yaml_file in opts.yamls:
with open(yaml_file) as f:
yml = yaml.load(f, yaml.SafeLoader)
options.update({opt['name']: opt for opt in yml['options']})
for name, opt in options.items():
try:
validate_see_also(opt, options)
except ValidationError as e:
raise Exception(f'failed to validate "{name}": {e}')


if __name__ == '__main__':
try:
main()
except Exception as e:
print(e, file=sys.stderr)
sys.exit(1)

0 comments on commit ea890d7

Please sign in to comment.