locust 2.23.2.dev26__py3-none-any.whl → 2.23.2.dev39__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
locust/_version.py CHANGED
@@ -12,5 +12,5 @@ __version__: str
12
12
  __version_tuple__: VERSION_TUPLE
13
13
  version_tuple: VERSION_TUPLE
14
14
 
15
- __version__ = version = '2.23.2.dev26'
16
- __version_tuple__ = version_tuple = (2, 23, 2, 'dev26')
15
+ __version__ = version = '2.23.2.dev39'
16
+ __version_tuple__ = version_tuple = (2, 23, 2, 'dev39')
locust/argument_parser.py CHANGED
@@ -12,10 +12,16 @@ import socket
12
12
  import sys
13
13
  import tempfile
14
14
  import textwrap
15
+ from collections import OrderedDict
15
16
  from typing import Any, NamedTuple
16
17
  from urllib.parse import urlparse
17
18
  from uuid import uuid4
18
19
 
20
+ if sys.version_info >= (3, 11):
21
+ import tomllib
22
+ else:
23
+ import tomli as tomllib
24
+
19
25
  import configargparse
20
26
  import gevent
21
27
  import requests
@@ -23,7 +29,7 @@ import requests
23
29
  version = locust.__version__
24
30
 
25
31
 
26
- DEFAULT_CONFIG_FILES = ["~/.locust.conf", "locust.conf"]
32
+ DEFAULT_CONFIG_FILES = ("~/.locust.conf", "locust.conf", "pyproject.toml")
27
33
 
28
34
 
29
35
  class LocustArgumentParser(configargparse.ArgumentParser):
@@ -63,6 +69,31 @@ class LocustArgumentParser(configargparse.ArgumentParser):
63
69
  }
64
70
 
65
71
 
72
+ class LocustTomlConfigParser(configargparse.TomlConfigParser):
73
+ def parse(self, stream):
74
+ try:
75
+ config = tomllib.loads(stream.read())
76
+ except Exception as e:
77
+ raise configargparse.ConfigFileParserException(f"Couldn't parse TOML file: {e}")
78
+
79
+ # convert to dict and filter based on section names
80
+ result = OrderedDict()
81
+
82
+ for section in self.sections:
83
+ data = configargparse.get_toml_section(config, section)
84
+ if data:
85
+ for key, value in data.items():
86
+ if isinstance(value, list):
87
+ result[key] = value
88
+ elif value is None:
89
+ pass
90
+ else:
91
+ result[key] = str(value)
92
+ break
93
+
94
+ return result
95
+
96
+
66
97
  def _is_package(path):
67
98
  """
68
99
  Is the given path a Python package?
@@ -186,6 +217,12 @@ def download_locustfile_from_url(url: str) -> str:
186
217
  def get_empty_argument_parser(add_help=True, default_config_files=DEFAULT_CONFIG_FILES) -> LocustArgumentParser:
187
218
  parser = LocustArgumentParser(
188
219
  default_config_files=default_config_files,
220
+ config_file_parser_class=configargparse.CompositeConfigParser(
221
+ [
222
+ LocustTomlConfigParser(["tool.locust"]),
223
+ configargparse.DefaultConfigFileParser,
224
+ ]
225
+ ),
189
226
  add_env_var_help=False,
190
227
  add_config_file_help=False,
191
228
  add_help=add_help,
@@ -38,17 +38,50 @@ class TestParser(unittest.TestCase):
38
38
  opts = self.parser.parse_args(args)
39
39
  self.assertEqual(opts.skip_log_setup, True)
40
40
 
41
- def test_parameter_parsing(self):
42
- with NamedTemporaryFile(mode="w") as file:
43
- os.environ["LOCUST_LOCUSTFILE"] = "locustfile_from_env"
44
- file.write("host host_from_config\nweb-host webhost_from_config")
41
+ def test_parse_options_from_conf_file(self):
42
+ with NamedTemporaryFile(mode="w", suffix=".conf") as file:
43
+ config_data = """\
44
+ locustfile = ./test_locustfile.py
45
+ web-host = 127.0.0.1
46
+ web-port = 45787
47
+ headless
48
+ tags = [Critical, Normal]
49
+ """
50
+
51
+ file.write(config_data)
45
52
  file.flush()
46
53
  parser = get_parser(default_config_files=[file.name])
47
- options = parser.parse_args(["-H", "host_from_args"])
48
- del os.environ["LOCUST_LOCUSTFILE"]
49
- self.assertEqual(options.web_host, "webhost_from_config")
50
- self.assertEqual(options.locustfile, "locustfile_from_env")
51
- self.assertEqual(options.host, "host_from_args") # overridden
54
+ options = parser.parse_args(["-H", "https://example.com"])
55
+
56
+ self.assertEqual("./test_locustfile.py", options.locustfile)
57
+ self.assertEqual("127.0.0.1", options.web_host)
58
+ self.assertEqual(45787, options.web_port)
59
+ self.assertTrue(options.headless)
60
+ self.assertEqual(["Critical", "Normal"], options.tags)
61
+ self.assertEqual("https://example.com", options.host)
62
+
63
+ def test_parse_options_from_toml_file(self):
64
+ with NamedTemporaryFile(mode="w", suffix=".toml") as file:
65
+ config_data = """\
66
+ [tool.locust]
67
+ locustfile = "./test_locustfile.py"
68
+ web-host = "127.0.0.1"
69
+ web-port = 45787
70
+ headless = true
71
+ tags = ["Critical", "Normal"]
72
+ """
73
+
74
+ file.write(config_data)
75
+ file.flush()
76
+ parser = get_parser(default_config_files=[file.name])
77
+ options = parser.parse_args(["-H", "https://example.com"])
78
+
79
+ self.assertEqual("./test_locustfile.py", options.locustfile)
80
+ self.assertEqual("127.0.0.1", options.web_host)
81
+ self.assertEqual(45787, options.web_port)
82
+ self.assertTrue(options.headless)
83
+ self.assertEqual(["Critical", "Normal"], options.tags)
84
+ self.assertEqual("https://example.com", options.host)
52
85
 
53
86
 
54
87
  class TestArgumentParser(LocustTestCase):