in tools/url-checker/create_test_files.py [0:0]
def generate_relative_urls(test_root, num_urls=10):
"""Generate a list of relative URLs pointing to actual files in the test directory."""
relative_urls = []
# Walk the directory structure to find all files
all_files = []
root_len = len(test_root) + 1 # +1 for the trailing slash
for root, dirs, files in os.walk(test_root):
for file in files:
full_path = os.path.join(root, file)
rel_path = full_path[root_len:] # Path relative to test_root
all_files.append(rel_path)
if not all_files:
# Fallback if no files found yet
return ["README.md", "docs/guide.md", "assets/images/logo.png"]
# Generate different types of relative URLs
for _ in range(num_urls):
if not all_files:
break
target_file = random.choice(all_files)
# Randomly choose URL type:
url_type = random.randint(1, 4)
if url_type == 1:
# Direct path
relative_urls.append(target_file)
elif url_type == 2:
# Path with ./ prefix
relative_urls.append(f"./{target_file}")
elif url_type == 3:
# Path with ../ prefix(es)
parts = target_file.split('/')
if len(parts) > 1:
# Go up at least one directory
up_levels = random.randint(1, min(3, len(parts) - 1))
parent_path = '../' * up_levels
remaining_path = '/'.join(parts[up_levels:])
relative_urls.append(f"{parent_path}{remaining_path}")
else:
relative_urls.append(target_file)
elif url_type == 4:
# Directory path ending with /
parts = target_file.split('/')
if len(parts) > 1:
dir_path = '/'.join(parts[:-1]) + '/'
relative_urls.append(dir_path)
else:
relative_urls.append(target_file)
# Add some deliberately invalid URLs for testing
num_invalid = max(1, num_urls // 10)
for _ in range(num_invalid):
invalid_path = f"non/existent/path/{random_word()}.{random.choice(list(NUM_FILES.keys()))}"
relative_urls.append(invalid_path)
return relative_urls