Skip to content

Commit 64917ff

Browse files
authored
Merge pull request #3310 from andrewbaldwin44/task/refactor-parse-options
Refactor parse_options
2 parents 7a13eed + 6a2696d commit 64917ff

File tree

8 files changed

+45
-46
lines changed

8 files changed

+45
-46
lines changed

locust/argument_parser.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -359,8 +359,8 @@ def parse_locustfile_option(args=None) -> tuple[argparse.Namespace, list[str]]:
359359
options, unknown = parser.parse_known_args(args=args)
360360

361361
if options.help or options.version:
362-
# if --help or --version is specified we'll call parse_options which will print the help/version message
363-
parse_options(args=args)
362+
# if --help or --version is specified we'll call parse_args which will print the help/version message
363+
get_parser().parse_args(args=args)
364364

365365
return (options, unknown)
366366

@@ -908,6 +908,7 @@ def get_parser(default_config_files=DEFAULT_CONFIG_FILES) -> LocustArgumentParse
908908

909909

910910
def parse_options(args=None) -> configargparse.Namespace:
911+
print("Warning: This function is deprecated and may be removed in a future version")
911912
parser = get_parser()
912913
parsed_opts = parser.parse_args(args=args)
913914
if parsed_opts.stats_history_enabled and (parsed_opts.csv_prefix is None):

locust/debug.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ def run_single_user(
129129
locust.log.setup_logging(loglevel)
130130

131131
if not _env:
132-
options = argument_parser.parse_options()
132+
options = argument_parser.get_parser().parse_args()
133133

134134
# in case your test goes looking for the file name of your locustfile
135135
frame = inspect.stack()[1]

locust/main.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@
2323
from . import log, stats
2424
from .argument_parser import (
2525
get_locustfiles_locally,
26+
get_parser,
2627
parse_locustfile_option,
27-
parse_options,
2828
)
2929
from .env import Environment
3030
from .html import get_html_report, process_html_filename
@@ -184,7 +184,8 @@ def main():
184184
) = merge_locustfiles_content(locustfiles)
185185

186186
# parse all command line options
187-
options = parse_options()
187+
parser = get_parser()
188+
options = parser.parse_args()
188189

189190
if getattr(options, "cloud", None):
190191
sys.exit(locust_cloud.main(locustfiles=locustfiles))
@@ -599,6 +600,8 @@ def start_automatic_run():
599600

600601
if options.csv_prefix:
601602
gevent.spawn(stats_csv_writer.stats_writer).link_exception(greenlet_exception_handler)
603+
if options.stats_history_enabled and (options.csv_prefix is None):
604+
parser.error("'--csv-full-history' requires '--csv'.")
602605

603606
if options.headless:
604607
start_automatic_run()

locust/test/test_load_locustfile.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
from locust import main
2-
from locust.argument_parser import parse_options
2+
from locust.argument_parser import get_parser
33
from locust.main import create_environment
44
from locust.user import HttpUser, TaskSet, User
55
from locust.user.users import PytestUser
@@ -142,7 +142,7 @@ def tick(self):
142142
self.assertEqual(shape_classes[0].__class__.__name__, "UserLoadTestShape")
143143

144144
def test_create_environment(self):
145-
options = parse_options(
145+
options = get_parser().parse_args(
146146
args=[
147147
"--host",
148148
"https://custom-host",
@@ -153,7 +153,7 @@ def test_create_environment(self):
153153
self.assertEqual("https://custom-host", env.host)
154154
self.assertTrue(env.reset_stats)
155155

156-
options = parse_options(args=[])
156+
options = get_parser().parse_args(args=[])
157157
env = create_environment([], options)
158158
self.assertEqual(None, env.host)
159159
self.assertFalse(env.reset_stats)
@@ -172,7 +172,7 @@ def test_specify_config_file(self):
172172
),
173173
suffix=".conf",
174174
) as conf_file_path:
175-
options = parse_options(
175+
options = get_parser().parse_args(
176176
args=[
177177
"--config",
178178
conf_file_path,
@@ -186,7 +186,7 @@ def test_specify_config_file(self):
186186

187187
def test_command_line_arguments_override_config_file(self):
188188
with temporary_file("host=from_file", suffix=".conf") as conf_file_path:
189-
options = parse_options(
189+
options = get_parser().parse_args(
190190
args=[
191191
"--config",
192192
conf_file_path,
@@ -201,7 +201,7 @@ def test_locustfile_can_be_set_in_config_file(self):
201201
"locustfile my_locust_file.py",
202202
suffix=".conf",
203203
) as conf_file_path:
204-
options = parse_options(
204+
options = get_parser().parse_args(
205205
args=[
206206
"--config",
207207
conf_file_path,
@@ -225,19 +225,19 @@ def test_locustfile_from_url(self):
225225
)
226226

227227
def test_profile_flag(self):
228-
options = parse_options()
228+
options = get_parser().parse_args()
229229
self.assertEqual(None, options.profile)
230-
options = parse_options(args=["--profile", "test-profile"])
230+
options = get_parser().parse_args(args=["--profile", "test-profile"])
231231
self.assertEqual("test-profile", options.profile)
232232
with temporary_file("profile=test-profile-from-file", suffix=".conf") as conf_file_path:
233-
options = parse_options(
233+
options = get_parser().parse_args(
234234
args=[
235235
"--config",
236236
conf_file_path,
237237
]
238238
)
239239
self.assertEqual("test-profile-from-file", options.profile)
240-
options = parse_options(
240+
options = get_parser().parse_args(
241241
args=[
242242
"--config",
243243
conf_file_path,

locust/test/test_main.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,13 @@ def my_task(self):
260260
with TestProcess(f"locust -f {file_path} --headless", sigint_on_exit=False, expect_return_code=1) as tp:
261261
tp.expect("parameter need to be float and value between. 0 < percentile < 1 Eg 0.95")
262262

263+
def test_csv_full_history_requires_csv(self):
264+
with mock_locustfile() as mocked:
265+
with TestProcess(
266+
f"locust -f {mocked.file_path} --csv-full-history", sigint_on_exit=False, expect_return_code=2
267+
) as tp:
268+
tp.expect("locust: error: '--csv-full-history' requires '--csv'.")
269+
263270
def test_webserver_multiple_locustfiles(self):
264271
with mock_locustfile(content=MOCK_LOCUSTFILE_CONTENT_A) as mocked1:
265272
with mock_locustfile(content=MOCK_LOCUSTFILE_CONTENT_B) as mocked2:

locust/test/test_parser.py

Lines changed: 6 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
from locust.argument_parser import (
33
get_parser,
44
parse_locustfile_paths,
5-
parse_options,
65
ui_extra_args_dict,
76
)
87

@@ -107,7 +106,7 @@ def tearDown(self):
107106
self.parent_dir.cleanup()
108107

109108
def test_parse_options(self):
110-
options = parse_options(
109+
options = get_parser().parse_args(
111110
args=[
112111
"-f",
113112
"locustfile.py",
@@ -141,7 +140,7 @@ def test_parse_options_from_env(self):
141140
os.environ["LOCUST_RESET_STATS"] = "true"
142141
os.environ["LOCUST_STOP_TIMEOUT"] = "5"
143142
os.environ["LOCUST_USER_CLASSES"] = "MyUserClass"
144-
options = parse_options(args=[])
143+
options = get_parser().parse_args(args=[])
145144

146145
self.assertEqual("locustfile.py", options.locustfile)
147146
self.assertEqual(100, options.num_users)
@@ -307,7 +306,7 @@ def test_unknown_command_line_arg(self):
307306
err = StringIO()
308307
with self.assertRaises(SystemExit):
309308
with mock.patch("sys.stderr", new=err):
310-
parse_options(
309+
get_parser().parse_args(
311310
args=[
312311
"-f",
313312
"something.py",
@@ -337,7 +336,7 @@ def _(parser, **kw):
337336
help="Custom string arg",
338337
)
339338

340-
options = parse_options(
339+
options = get_parser().parse_args(
341340
args=[
342341
"-u",
343342
"666",
@@ -362,24 +361,13 @@ def _(parser, **kw):
362361
out = StringIO()
363362
with mock.patch("sys.stdout", new=out):
364363
with self.assertRaises(SystemExit):
365-
parse_options(args=["--help"])
364+
get_parser().parse_args(args=["--help"])
366365

367366
out.seek(0)
368367
stdout = out.read()
369368
self.assertIn("Custom boolean flag", stdout)
370369
self.assertIn("Custom string arg", stdout)
371370

372-
def test_csv_full_history_requires_csv(self):
373-
with mock.patch("sys.stderr", new=StringIO()):
374-
with self.assertRaises(SystemExit):
375-
parse_options(
376-
args=[
377-
"-f",
378-
"locustfile.py",
379-
"--csv-full-history",
380-
]
381-
)
382-
383371
def test_custom_argument_included_in_web_ui(self):
384372
@locust.events.init_command_line_parser.add_listener
385373
def _(parser, **kw):
@@ -389,7 +377,7 @@ def _(parser, **kw):
389377
parser.add_argument("--a4", help="a3 help", is_required=True)
390378

391379
args = ["-u", "666", "--a1", "v1", "--a2", "v2", "--a3", "v3"]
392-
options = parse_options(args=args)
380+
options = get_parser().parse_args(args=args)
393381
self.assertEqual(666, options.num_users)
394382
self.assertEqual("v1", options.a1)
395383
self.assertEqual("v2", options.a2)

locust/test/test_runners.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
import locust
44
from locust import LoadTestShape, __version__, constant, runners
5-
from locust.argument_parser import parse_options
5+
from locust.argument_parser import get_parser
66
from locust.dispatch import UsersDispatcher
77
from locust.env import Environment
88
from locust.exception import RPCError, RPCReceiveError, StopUser
@@ -1014,7 +1014,7 @@ def incr_stats(self):
10141014
patch_env("LOCUST_WAIT_FOR_WORKERS_REPORT_AFTER_RAMP_UP", "0.1"),
10151015
):
10161016
# start a Master runner
1017-
options = parse_options(["--enable-rebalancing"])
1017+
options = get_parser().parse_args(["--enable-rebalancing"])
10181018
master_env = Environment(user_classes=[TestUser], parsed_options=options)
10191019
master = master_env.create_master_runner("*", 0)
10201020
sleep(0)
@@ -1122,7 +1122,7 @@ def _(parser, **kw):
11221122
# start a Master runner
11231123
master_env = Environment(user_classes=[TestUser])
11241124
master = master_env.create_master_runner("*", 0)
1125-
master_env.parsed_options = parse_options(
1125+
master_env.parsed_options = get_parser().parse_args(
11261126
[
11271127
"--my-int-argument",
11281128
"42",

locust/test/test_web.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
import locust
44
from locust import LoadTestShape, constant, stats
5-
from locust.argument_parser import get_parser, parse_options
5+
from locust.argument_parser import get_parser
66
from locust.env import Environment
77
from locust.log import LogReader
88
from locust.runners import Runner
@@ -89,7 +89,7 @@ def test_index(self):
8989

9090
for html_name_to_test in html_to_option.keys():
9191
# Test that setting each spawn option individually populates the corresponding field in the html, and none of the others
92-
self.environment.parsed_options = parse_options(html_to_option[html_name_to_test])
92+
self.environment.parsed_options = get_parser().parse_args(html_to_option[html_name_to_test])
9393

9494
response = requests.get("http://127.0.0.1:%i/" % self.web_port)
9595
self.assertEqual(200, response.status_code)
@@ -105,7 +105,7 @@ def test_index_with_spawn_options(self):
105105
}
106106

107107
for html_name_to_test in html_to_option.keys():
108-
self.environment.parsed_options = parse_options(html_to_option[html_name_to_test])
108+
self.environment.parsed_options = get_parser().parse_args(html_to_option[html_name_to_test])
109109

110110
response = requests.get("http://127.0.0.1:%i/" % self.web_port)
111111
self.assertEqual(200, response.status_code)
@@ -255,7 +255,7 @@ def my_task(self):
255255
pass
256256

257257
self.environment.user_classes = [MyUser]
258-
self.environment.web_ui.parsed_options = parse_options()
258+
self.environment.web_ui.parsed_options = get_parser().parse_args()
259259
response = requests.post(
260260
"http://127.0.0.1:%i/swarm" % self.web_port,
261261
data={"user_count": 5, "spawn_rate": 5, "host": "https://localhost"},
@@ -807,7 +807,7 @@ def _(parser):
807807
is_multiple=True,
808808
)
809809

810-
parsed_options = parse_options()
810+
parsed_options = get_parser().parse_args()
811811
self.environment.user_classes = [MyUser]
812812
self.environment.parsed_options = parsed_options
813813
self.environment.web_ui.parsed_options = parsed_options
@@ -849,7 +849,7 @@ def my_task(self):
849849
def _(parser):
850850
parser.add_argument("--my-argument", type=int, help="Give me a number")
851851

852-
parsed_options = parse_options(args=["--my-argument", "24"])
852+
parsed_options = get_parser().parse_args(args=["--my-argument", "24"])
853853
self.environment.user_classes = [MyUser]
854854
self.environment.parsed_options = parsed_options
855855
self.environment.web_ui.parsed_options = parsed_options
@@ -869,7 +869,7 @@ def my_task(self):
869869
pass
870870

871871
self.environment.user_classes = [MyUser]
872-
self.environment.web_ui.parsed_options = parse_options()
872+
self.environment.web_ui.parsed_options = get_parser().parse_args()
873873
response = requests.post(
874874
"http://127.0.0.1:%i/swarm" % self.web_port,
875875
data={"user_count": 5, "spawn_rate": 5},
@@ -887,7 +887,7 @@ def my_task(self):
887887
pass
888888

889889
self.environment.user_classes = [MyUser]
890-
self.environment.web_ui.parsed_options = parse_options()
890+
self.environment.web_ui.parsed_options = get_parser().parse_args()
891891
response = requests.post(
892892
"http://127.0.0.1:%i/swarm" % self.web_port,
893893
data={"user_count": 5, "spawn_rate": 5, "host": "https://localhost", "run_time": "1s"},
@@ -910,7 +910,7 @@ def my_task(self):
910910
pass
911911

912912
self.environment.user_classes = [MyUser]
913-
self.environment.web_ui.parsed_options = parse_options()
913+
self.environment.web_ui.parsed_options = get_parser().parse_args()
914914
response = requests.post(
915915
"http://127.0.0.1:%i/swarm" % self.web_port,
916916
data={"user_count": 5, "spawn_rate": 5, "host": "https://localhost", "run_time": "bad"},
@@ -935,7 +935,7 @@ def my_task(self):
935935
pass
936936

937937
self.environment.user_classes = [MyUser]
938-
self.environment.web_ui.parsed_options = parse_options()
938+
self.environment.web_ui.parsed_options = get_parser().parse_args()
939939
response = requests.post(
940940
"http://127.0.0.1:%i/swarm" % self.web_port,
941941
data={"user_count": 5, "spawn_rate": 5, "host": "https://localhost", "run_time": ""},

0 commit comments

Comments
 (0)