-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
52c9f01
commit 4d03423
Showing
3 changed files
with
56 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
def safe_block_name(name: str) -> str: | ||
"""Sanitize a block name to conform to Prefect Cloud's naming requirements. | ||
Block names must only contain lowercase letters, numbers, and dashes. | ||
Args: | ||
name: The name to sanitize | ||
Returns: | ||
A sanitized name containing only lowercase letters, numbers, and dashes | ||
""" | ||
# Replace any non-alphanumeric chars with dashes and ensure lowercase | ||
sanitized = "".join(c if c.isalnum() else "-" for c in name.lower()) | ||
# Remove consecutive dashes and strip dashes from ends | ||
return "-".join(filter(None, sanitized.split("-"))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
from prefect_cloud.utilities.blocks import safe_block_name | ||
|
||
|
||
def test_safe_block_name(): | ||
# Test basic lowercase conversion | ||
assert safe_block_name("MyBlock") == "myblock" | ||
|
||
# Test special characters replaced with dashes | ||
assert safe_block_name("my@block!name") == "my-block-name" | ||
|
||
# Test multiple consecutive special chars become single dash | ||
assert safe_block_name("my!!block##name") == "my-block-name" | ||
|
||
# Test handling of existing dashes | ||
assert safe_block_name("my-block--name") == "my-block-name" | ||
|
||
# Test stripping dashes from ends | ||
assert safe_block_name("-my-block-") == "my-block" | ||
|
||
# Test real world GitHub examples | ||
assert ( | ||
safe_block_name("PrefectHQ/prefect-cloud-credentials") | ||
== "prefecthq-prefect-cloud-credentials" | ||
) | ||
assert ( | ||
safe_block_name("User123/My-Repo!!-credentials") | ||
== "user123-my-repo-credentials" | ||
) | ||
|
||
# Test empty segments are removed | ||
assert safe_block_name("my---block") == "my-block" | ||
|
||
# Test spaces handled correctly | ||
assert safe_block_name("my block name") == "my-block-name" | ||
|
||
# Test mixed case with special chars | ||
assert safe_block_name("My!!BLOCK@@name") == "my-block-name" |