From 0025bb21f602855d585b9fe0fc551b62207de732 Mon Sep 17 00:00:00 2001 From: debugtalk Date: Tue, 22 Mar 2022 17:00:09 +0800 Subject: [PATCH] fix: keep negative index in jmespath unchanged when converting pytest files --- docs/CHANGELOG.md | 1 + httprunner/compat.py | 4 +++- tests/compat_test.py | 4 ++++ 3 files changed, 8 insertions(+), 1 deletion(-) diff --git a/docs/CHANGELOG.md b/docs/CHANGELOG.md index ebffeb3f..a74f69de 100644 --- a/docs/CHANGELOG.md +++ b/docs/CHANGELOG.md @@ -2,6 +2,7 @@ ## 3.1.8 (2022-03-22) +- fix: keep negative index in jmespath unchanged when converting pytest files, e.g. body.users[-1] - change: load yaml file with FullLoader ## 3.1.7 (2022-03-22) diff --git a/httprunner/compat.py b/httprunner/compat.py index 4d76efda..c14c9d1f 100644 --- a/httprunner/compat.py +++ b/httprunner/compat.py @@ -58,9 +58,11 @@ def _convert_jmespath(raw: Text) -> Text: raw_list = [] for item in raw.split("."): - if "-" in item: + if "-" in item and "[-" not in item: # add quotes for field with separator # e.g. headers.Content-Type => headers."Content-Type" + # also need to avoid replacing negative index in jmespath + # e.g. body.users[-1] => body.users[-1], keep unchanged item = item.strip('"') raw_list.append(f'"{item}"') elif item.isdigit(): diff --git a/tests/compat_test.py b/tests/compat_test.py index d0a1dec0..987b1785 100644 --- a/tests/compat_test.py +++ b/tests/compat_test.py @@ -45,6 +45,10 @@ class TestCompat(unittest.TestCase): compat._convert_jmespath("body.data.buildings.0.building_id"), "body.data.buildings[0].building_id", ) + self.assertEqual( + compat._convert_jmespath("body.users[-1]"), + "body.users[-1]", + ) with self.assertRaises(SystemExit): compat._convert_jmespath("2.buildings.0.building_id")