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

Work around issue with locustfiles named "locust.py" #2587

Merged
Merged
Show file tree
Hide file tree
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
10 changes: 10 additions & 0 deletions locust/test/test_load_locustfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,16 @@ def test_load_locust_file_from_relative_path(self):
os.path.join(os.path.relpath(mocked.directory, os.getcwd()), mocked.filename)
)

def test_load_locust_file_called_locust_dot_py(self):
with mock_locustfile() as mocked:
new_filename = mocked.file_path.replace(mocked.filename, "locust.py")
os.rename(mocked.file_path, new_filename)
try:
docstring, user_classes, shape_classes = main.load_locustfile(new_filename)
finally:
# move it back, so it can be deleted
os.rename(new_filename, mocked.file_path)

def test_load_locust_file_with_a_dot_in_filename(self):
with mock_locustfile(filename_prefix="mocked.locust.file") as mocked:
docstring, user_classes, shape_classes = main.load_locustfile(mocked.file_path)
Expand Down
2 changes: 2 additions & 0 deletions locust/util/load_locustfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ def load_locustfile(path) -> tuple[str | None, dict[str, User], list[LoadTestSha

# Perform the import
module_name = os.path.splitext(locustfile)[0]
if module_name == "locust":
module_name = "locustfile" # Avoid conflict with locust package
loader = importlib.machinery.SourceFileLoader(module_name, path)
spec = importlib.util.spec_from_file_location(module_name, path, loader=loader)
if spec is None:
Expand Down
Loading