Skip to content

Commit

Permalink
Merge pull request #2587 from locustio/workaround-issue-with-locustfi…
Browse files Browse the repository at this point in the history
…le-being-named-locust.py

Work around issue with locustfiles named "locust.py"
  • Loading branch information
cyberw authored Feb 6, 2024
2 parents 7c8efb7 + 439ea56 commit a804b85
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 0 deletions.
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

0 comments on commit a804b85

Please sign in to comment.