Coverage for graphqler / fuzzer / engine / retrier / utils.py: 94%

16 statements  

« prev     ^ index     » next       coverage.py v7.13.4, created at 2026-03-18 23:20 -0400

1""" 

2Utilities for the retrier 

3""" 

4 

5import re 

6 

7 

8def find_block_end(payload: str, line_number: int) -> int: 

9 """Finds the end line number for a block given a query or mutation. 

10 The query or mutation must be formatted with trailing curly braces and elements on their 

11 own lines. 

12 Cases: 

13 - when it's an object 

14 - when it's just an element 

15 

16 Args: 

17 payload (str): The payload either a mutation or query 

18 line_number (int): The line number where we want to find the block 

19 

20 Returns: 

21 int: Where the block ends 

22 """ 

23 

24 lines = payload.split("\n") 

25 if lines[line_number][-1] == "{": 

26 target_indentation = len(re.match(r"^\s*", lines[line_number]).group(0)) 

27 current_line_number = line_number + 1 

28 current_indentation = len(re.match(r"^\s*", lines[current_line_number]).group(0)) 

29 while current_indentation > target_indentation: 

30 current_line_number += 1 

31 current_indentation = len(re.match(r"^\s*", lines[current_line_number]).group(0)) 

32 return current_line_number 

33 else: 

34 return line_number 

35 

36 

37def remove_lines_within_range(payload: str, start_line: int, end_line: int) -> str: 

38 """Removes lines within a range from start_line to end_line inclusive. 

39 The query or mutation must be formatted with trailing curly braces and elements on their 

40 own lines. 

41 

42 Args: 

43 payload (str): The payload (either a query or mutation) 

44 start_line (int): The starting line number 

45 end_line (int): The end line number 

46 

47 Returns: 

48 str: _description_ 

49 """ 

50 lines = payload.split("\n") 

51 new_lines = lines[0:start_line] + lines[end_line + 1 :] 

52 return "\n".join(new_lines)