Coverage for src/robotide/contrib/testrunner/Command.py: 86%

56 statements  

« prev     ^ index     » next       coverage.py v7.8.0, created at 2025-05-06 10:40 +0100

1# Copyright 2010 Orbitz WorldWide 

2# 

3# Licensed under the Apache License, Version 2.0 (the "License"); 

4# you may not use this file except in compliance with the License. 

5# You may obtain a copy of the License at 

6# 

7# http://www.apache.org/licenses/LICENSE-2.0 

8# 

9# Unless required by applicable law or agreed to in writing, software 

10# distributed under the License is distributed on an "AS IS" BASIS, 

11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 

12# See the License for the specific language governing permissions and 

13# limitations under the License. 

14 

15# Modified by NSN 

16# Copyright 2010-2012 Nokia Solutions and Networks 

17# Copyright 2013-2015 Nokia Networks 

18# Copyright 2016- Robot Framework Foundation 

19# 

20# Licensed under the Apache License, Version 2.0 (the "License"); 

21# you may not use this file except in compliance with the License. 

22# You may obtain a copy of the License at 

23# 

24# http://www.apache.org/licenses/LICENSE-2.0 

25# 

26# Unless required by applicable law or agreed to in writing, software 

27# distributed under the License is distributed on an "AS IS" BASIS, 

28# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 

29# See the License for the specific language governing permissions and 

30# limitations under the License. 

31 

32import inspect 

33import os 

34 

35from robotide.contrib.testrunner import TestRunnerAgent 

36try: 

37 from robotide.lib.robot.utils import encoding 

38except ImportError: 

39 from robotide.lib.robot.utils.encodingsniffer import get_system_encoding 

40 encoding.SYSTEM_ENCODING = get_system_encoding() 

41 

42 

43class Command: 

44 def __init__(self): 

45 self._prefix = '' 1bda

46 self._tests_suite_file = '' 1bda

47 self._listener = None 1bda

48 self._args_file = '' 1bda

49 

50 def with_prefix(self, prefix): 

51 self._prefix = prefix 1ba

52 return self 1ba

53 

54 def with_listener(self, port, pause_on_failure=False): 

55 if port: 55 ↛ 58line 55 didn't jump to line 58 because the condition on line 55 was always true1a

56 self._listener = (port, pause_on_failure) 1a

57 else: 

58 self._listener = None 

59 return self 1a

60 

61 def with_tests_suite_file(self, tests_suit_file): 

62 self._tests_suite_file = tests_suit_file 1a

63 return self 1a

64 

65 def with_args_file(self, args_file): 

66 self._args_file = args_file 1ba

67 return self 1ba

68 

69 def build(self): 

70 command = [] 1bda

71 if self._prefix: 1bda

72 command.append(self._prefix) 1ba

73 

74 if self._args_file: 1bda

75 command.extend(["-A", self._args_file]) 1ba

76 

77 if self._listener: 1bda

78 command.extend(["--listener", self._get_listener_to_cmd()]) 1a

79 

80 if self._tests_suite_file: 1bda

81 command.append(self._tests_suite_file) 1a

82 

83 return self._format_command(command) 1bda

84 

85 def _get_listener_to_cmd(self): 

86 path = self._get_listener_path() 1a

87 if path[-1] in ['c', 'o']: 87 ↛ 88line 87 didn't jump to line 88 because the condition on line 87 was never true1a

88 path = path[:-1] 

89 return '%s:%s:%s' % (path, self._listener[0], self._listener[1]) 1a

90 

91 def _get_listener_path(self): 

92 return os.path.abspath(inspect.getfile(TestRunnerAgent)) 

93 

94 @staticmethod 

95 def _format_command(args): 

96 """Quote a list as if it were a command line command 

97 

98 This isn't perfect but seems to work for the normal use 

99 cases. I'm not entirely sure what the perfect algorithm 

100 is since *nix and windows have different quoting 

101 behaviors. 

102 """ 

103 result = [] 1bda

104 for arg in args: 1bda

105 if "'" in arg or " " in arg or "&" in arg: 1ba

106 # for windows, if there are spaces we need to use 

107 # double quotes. Single quotes cause problems 

108 result.append('"%s"' % arg) 1ba

109 elif '"' in arg: 109 ↛ 110line 109 didn't jump to line 110 because the condition on line 109 was never true1ba

110 result.append("'%s'" % arg) 

111 else: 

112 result.append(arg) 1ba

113 return " ".join(result) 1bda