Skip to content
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

Add new cucu tags command #549

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
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
40 changes: 40 additions & 0 deletions src/cucu/cli/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import glob
import json
import os
import re
import shutil
import signal
import sys
Expand Down Expand Up @@ -765,6 +766,45 @@ def vars(filepath):
print(tabulate(variables, tablefmt="fancy_grid"))


@main.command()
@click.argument("filepath", default="features")
def tags(filepath):
"""
print list of defined tags
"""
init_global_hook_variables()

# TODO: this is waaaaay too naive and won't work
# instead need to use the behave parse_file helper (or find some other state that's loaded in behave)
# maybe call behave_init with each file and pop some state from the library?
tags = _find_unique_tags(root_dir=filepath, pattern="*.feature", string_capture="^\(@[\w-]+\)")
Copy link
Author

Choose a reason for hiding this comment

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

This regex is too naive to be correct

Copy link
Author

Choose a reason for hiding this comment

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

It would be better to just recursively parse with the actual code in the the behave library - i.e. https://github.com/behave/behave/blob/main/behave/parser.py#L829


print(tabulate(tags, tablefmt="fancy_grid"))

def _find_unique_tags(root_dir, pattern, string_capture):
"""
Recursively searches files matching a pattern, extracting unique pattern matches.

Args:
root_dir: The root directory to start the search from.
pattern: The file pattern to match (e.g., "*.feature").
string_capture: The pattern to find strings within the files (e.g., r'"(.*?)"').

Returns:
A set of unique regex captures found in the files.
"""
unique = set()
for filename in glob.iglob(os.path.join(root_dir, "**", pattern), recursive=True):
try:
with open(filename, "r") as f:
content = f.read()
matches = re.findall(string_capture, content)
unique.update(matches)
except Exception as e:
print(f"Error processing file {filename}: {e}")
return unique


@main.command()
@click.option(
"-b",
Expand Down