| Conditions | 10 |
| Total Lines | 60 |
| Lines | 0 |
| Ratio | 0 % |
| Tests | 15 |
| CRAP Score | 16.4 |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
Complex classes like BaseTest.spawn() often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
| 1 | 1 | from plugin.core.helpers.variable import merge |
|
| 72 | 1 | @classmethod |
|
| 73 | def spawn(cls, name, search_paths): |
||
| 74 | # Find path to python executable |
||
| 75 | 1 | python_exe = cls.find_python_executable() |
|
| 76 | |||
| 77 | 1 | if not python_exe: |
|
| 78 | raise Exception('Unable to find python executable') |
||
| 79 | |||
| 80 | # Ensure test host exists |
||
| 81 | 1 | if not os.path.exists(HOST_PATH): |
|
| 82 | raise Exception('Unable to find "host.py" script') |
||
| 83 | |||
| 84 | # Build test process arguments |
||
| 85 | 1 | args = [ |
|
| 86 | python_exe, HOST_PATH, |
||
| 87 | '--module', cls.__module__, |
||
| 88 | '--name', name, |
||
| 89 | |||
| 90 | '--search-paths="%s"' % ( |
||
| 91 | ';'.join(search_paths) |
||
| 92 | ), |
||
| 93 | ] |
||
| 94 | |||
| 95 | # Spawn test (in sub-process) |
||
| 96 | 1 | log.debug('Starting test: %s:%s', cls.__module__, name) |
|
| 97 | |||
| 98 | 1 | process = Popen( |
|
| 99 | args, |
||
| 100 | stdout=subprocess.PIPE, |
||
| 101 | stderr=subprocess.PIPE |
||
| 102 | ) |
||
| 103 | |||
| 104 | # Wait for test to complete |
||
| 105 | 1 | stdout, stderr = process.communicate() |
|
| 106 | |||
| 107 | 1 | if stderr: |
|
| 108 | log.debug('Test returned messages:\n%s', stderr.replace("\r\n", "\n")) |
||
| 109 | |||
| 110 | # Parse output |
||
| 111 | 1 | result = None |
|
| 112 | |||
| 113 | 1 | if stdout: |
|
| 114 | 1 | try: |
|
| 115 | 1 | result = json.loads(stdout) |
|
| 116 | except Exception as ex: |
||
| 117 | log.warn('Invalid output returned %r - %s', stdout, ex, exc_info=True) |
||
| 118 | |||
| 119 | # Build result |
||
| 120 | 1 | if process.returncode != 0: |
|
| 121 | # Test failed |
||
| 122 | if result and result.get('message'): |
||
| 123 | if result.get('traceback'): |
||
| 124 | log.info('%s - %s', result['message'], result['traceback']) |
||
| 125 | |||
| 126 | raise Exception(result['message']) |
||
| 127 | |||
| 128 | raise Exception('Unknown error (code: %s)' % process.returncode) |
||
| 129 | |||
| 130 | # Test successful |
||
| 131 | 1 | return result |
|
| 132 | |||
| 221 |