Coverage for testrail_api_reporter/utils/reporter_utils.py: 100%

1# -*- coding: utf-8 -*-

2""" This module contains service functions for reporter """

3 from logging import Logger

4 from os import popen

5 from typing import Optional, Any, Union

6

7 import requests

8

9

10 def format_error(error: Union[list, str, Exception]) -> str:

11 """

12 Service function for parse errors to human-readable format

13

14 :param error: initial error

15 :return: formatted string with error details

16 """

17 err_msg = ""

18 error = error if isinstance(error, list) else [error]

19 for err in error:

20 err_msg = f"{err_msg} : {err}"

21 return err_msg

22

23

24 def upload_image(filename: str, api_token: str) -> dict:

25 """

26 Service function to upload images to third-party image hosting

27

28 :param filename: filename or path to image, which should be uploaded

29 :param api_token: unique API token for image upload on https://freeimage.host

30 :return: dict with urls with image itself and its thumbnail

31 """

32 payload = {"action": "upload", "key": api_token, "format": "json"}

33 with open(filename, "rb") as source_file:

34 response = requests.post(

35 url="https://freeimage.host/api/1/upload",

36 data=payload,

37 timeout=5,

38 verify=True,

39 files={"source": source_file},

40 )

41 return {

42 "image": response.json()["image"]["url"],

43 "thumb": response.json()["image"]["thumb"]["url"],

44 }

45

46

47 def delete_file(filename: str, debug: bool = True, logger: Optional[Logger] = None):

48 """

49 Service function to delete file from filesystem

50

51 :param filename: filename or path to file, which should be deleted

52 :param debug: debug output is enabled, may be True or False, optional, by default, is True

53 :param logger: logger, optional

54 """

55 popen(f"rm {filename}").read()

56 if debug:

57 if logger:

58 logger.debug(f"Removed {filename}")

59

60

61 def zip_file(filename: str, suffix: Optional[str] = None, debug: bool = True, logger: Optional[Logger] = None) -> str:

62 """

63 Service function to ZIP file

64

65 :param filename: filename or path to file, which should be zipped

66 :param suffix: suffix for zipped file, optional

67 :param debug: debug output is enabled may be True or False, optional, by default is True

68 :param logger: logger, optional

69 :return: zipped filename

70 """

71 if suffix is None:

72 suffix = ""

73 zipped_file = f'{filename.split(".")[0]}{suffix}.zip'

74 popen(f"zip -r {zipped_file} {filename}").read()

75 if debug:

76 if logger:

77 logger.debug(f"ZIPped {filename} to {zipped_file}")

78 return zipped_file

79

80

81 def check_captions_and_files(

82 captions: Union[list, None, Any], files: list, debug: bool = True, logger: Optional[Logger] = None

83) -> Optional[list]:

84 """

85 Service function to check captions and file lists

86

87 :param captions: list of captions for files, list of strings, if not provided, no captions will be added

88 :param files: list of images urls

89 :param debug: debug output is enabled, may be True or False, optional

90 :param logger: logger object, optional

91 :return: captions list or None

92 """

93 return_value = captions

94 if not isinstance(captions, list) or not isinstance(files, list):

95 if debug:

96 if logger:

97 logger.debug("Captions are not a list, thus no legend will be displayed")

98 return_value = None

99 elif len(captions) != len(files):

100 if debug:

101 if logger:

102 logger.debug(

103 "Caption and file lists are not the same length %s != %s thus no legend will be displayed",

104 len(captions),

105 len(files),

106 )

107 return_value = None

108 return return_value

109

110

111 def init_get_cases_process() -> tuple:

112 """

113 Service function to initialize a process

114

115 :return: cases_list, first_run, criteria, response, retry

116 """

117 cases_list: list = []

118 first_run = True

119 criteria = None

120 response = None

121 retry = 0

122 return cases_list, first_run, criteria, response, retry