Skip to content

[FR] [DAC] Add Flag to Enable All Prebuilt Tests #3684

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 29 additions & 9 deletions detection_rules/custom_rules.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,23 +43,43 @@ def create_config_content() -> str:
return yaml.safe_dump(config_content, default_flow_style=False)


def create_test_config_content() -> str:
def create_test_config_content(enable_prebuilt_tests: bool) -> str:
"""Generate the content for the test_config.yaml with special content and references."""

def format_test_string(test_string: str, comment_char: str) -> str:
"""Generate a yaml formatted string with a comment character."""
return f"{comment_char} - {test_string}"

comment_char = "#" if enable_prebuilt_tests else ""
example_test_config_path = DEFAULT_CONFIG_PATH.parent.joinpath("example_test_config.yaml")
content = f"# For more details, refer to the example configuration:\n# {example_test_config_path}\n" \
"# Define tests to explicitly bypass, with all others being run.\n" \
"# To run all tests, set bypass to empty or leave this file commented out.\n\n" \
"unit_tests:\n bypass:\n# - tests.test_all_rules.TestValidRules.test_schema_and_dupes\n" \
"# - tests.test_packages.TestRegistryPackage.test_registry_package_config\n"

return content
lines = [
"# For more details, refer to the example configuration:",
f"# {example_test_config_path}",
"# Define tests to explicitly bypass, with all others being run.",
"# To run all tests, set bypass to empty or leave this file commented out.",
"",
Comment on lines +60 to +61
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this just be a \n ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because of the return '\n'.join(lines), the "" is the equivalent to a a \n. This was just to clean up the text to potentially make it easier to add or subtract lines later.

"unit_tests:",
" bypass:",
format_test_string("tests.test_gh_workflows.TestWorkflows.test_matrix_to_lock_version_defaults", comment_char),
format_test_string(
"tests.test_schemas.TestVersionLockSchema.test_version_lock_has_nested_previous", comment_char
),
format_test_string("tests.test_packages.TestRegistryPackage.test_registry_package_config", comment_char),
format_test_string("tests.test_all_rules.TestValidRules.test_schema_and_dupes", comment_char),
]

return "\n".join(lines)


@custom_rules.command('setup-config')
@click.argument('directory', type=Path)
@click.argument('kibana-version', type=str, default=load_etc_dump('packages.yaml')['package']['name'])
@click.option('--overwrite', is_flag=True, help="Overwrite the existing _config.yaml file.")
def setup_config(directory: Path, kibana_version: str, overwrite: bool):
@click.option(
"--enable-prebuilt-tests", "-e", is_flag=True, help="Enable all prebuilt tests instead of default subset."
)
def setup_config(directory: Path, kibana_version: str, overwrite: bool, enable_prebuilt_tests: bool):
"""Setup the custom rules configuration directory and files with defaults."""

config = directory / '_config.yaml'
Expand Down Expand Up @@ -111,7 +131,7 @@ def setup_config(directory: Path, kibana_version: str, overwrite: bool):
package_config.write_text(yaml.safe_dump(package_content, default_flow_style=False))

# Create and configure test_config.yaml
test_config.write_text(create_test_config_content())
test_config.write_text(create_test_config_content(enable_prebuilt_tests))

# Create and configure _config.yaml
config.write_text(create_config_content())
Expand Down
Loading