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

Fix folder spec for plugin filename regex #632

Open
wants to merge 2 commits into
base: dev
Choose a base branch
from

Conversation

andrewhand
Copy link
Collaborator

Fixed bug where API would not except plugin filenames as a directory and added test to support.

@jkglasbrenner
Copy link
Collaborator

jkglasbrenner commented Sep 10, 2024

Here's a Python test file that contains the regex that seems to handle most cases, as well as tests to run.

EDIT: I added a couple of additional tests in invalid_paths that verify that attempting to use a relative path will fail.

import re

python_filepath_pattern = r'^(?!.*/_)(?!_/)([a-zA-Z][a-zA-Z0-9_]*/)*[a-zA-Z_][a-zA-Z0-9_]*(?<!_)\.py$'

def validate_python_filepath(filepath):
    return re.match(python_filepath_pattern, filepath) is not None

# Valid paths (for reference)
valid_paths = [
    r'hello.py',
    r'hello_world.py',
    r'hello_world/main.py',
    r'package/sub_package/module.py',
    r'a/b/c/d/e/f/g/h/i/j/k/l/m/n/o/p.py',  # Many nested directories
    r'_underscore_start.py',
]

# Invalid paths (expanded)
invalid_paths = [
    r'hello world.py',        # Space in filename
    r'3ight/hello.py',        # Directory starting with a number
    r'hello_world//main.py',  # Double slash
    r'module.py.txt',         # Wrong extension
    r'/absolute/path.py',     # Absolute path
    r'.py',                   # Just the extension
    r'hello.py/',             # Ends with a slash
    r'/hello.py',             # Starts with a slash
    r'hello..py',             # Double dot in extension
    r'hello.py.py',           # Double .py extension
    r'hello_.py',             # Ends with underscore before extension
    r'_/hello.py',            # Single underscore directory name
    r'hello/_.py',            # Single underscore filename
    r'hello/_file.py',        # Underscore start in nested file
    r'HELLO.PY',              # Uppercase extension (assuming case-sensitive)
    r'hello.pY',              # Mixed case extension
    r'hello/world/.py',       # Hidden file in nested directory
    r'hello/.world/file.py',  # Hidden directory
    r' hello.py',             # Leading space
    r'hello.py ',             # Trailing space
    r'\thello.py',            # Tab character
    r'hello\world.py',       # Backslash instead of forward slash
    r'hello:world.py',        # Invalid character (colon)
    r'[email protected]',        # Invalid character (at sign)
    r'hello/world.py/extra',  # Extra content after .py
    r'',                      # Empty string
    r'hello/',                # Directory without file
    r'hello.py/world.py',     # .py in middle of path
    r'1/2/3/4.py',            # All numeric directory names
    r'../sample.py',          # No relative paths
    r'..sample.py',           # No prefix with dots
]

# Test all paths
for path in valid_paths + invalid_paths:
    result = 'Valid' if validate_python_filepath(path) else 'Invalid'
    print(f"{path}: {result}")

Here's the output when you run the above:

hello.py: Valid
hello_world.py: Valid
hello_world/main.py: Valid
package/sub_package/module.py: Valid
a/b/c/d/e/f/g/h/i/j/k/l/m/n/o/p.py: Valid
_underscore_start.py: Valid
hello world.py: Invalid
3ight/hello.py: Invalid
hello_world//main.py: Invalid
module.py.txt: Invalid
/absolute/path.py: Invalid
.py: Invalid
hello.py/: Invalid
/hello.py: Invalid
hello..py: Invalid
hello.py.py: Invalid
hello_.py: Invalid
_/hello.py: Invalid
hello/_.py: Invalid
hello/_file.py: Invalid
HELLO.PY: Invalid
hello.pY: Invalid
hello/world/.py: Invalid
hello/.world/file.py: Invalid
 hello.py: Invalid
hello.py : Invalid
\thello.py: Invalid
hello\world.py: Invalid
hello:world.py: Invalid
[email protected]: Invalid
hello/world.py/extra: Invalid
: Invalid
hello/: Invalid
hello.py/world.py: Invalid
1/2/3/4.py: Invalid
../sample.py: Invalid
..sample.py: Invalid

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[BUG] REST API does not allow folder specification in filename
2 participants