| Total Complexity | 40 |
| Total Lines | 202 |
| Duplicated Lines | 44.55 % |
| Changes | 0 | ||
Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like elodie.tests.plugins_test 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 | from __future__ import absolute_import |
||
| 2 | # Project imports |
||
| 3 | import mock |
||
| 4 | import os |
||
| 5 | import sys |
||
| 6 | from tempfile import gettempdir |
||
| 7 | |||
| 8 | sys.path.insert(0, os.path.abspath(os.path.dirname(os.path.dirname(os.path.dirname(os.path.realpath(__file__)))))) |
||
| 9 | |||
| 10 | from . import helper |
||
| 11 | from elodie.config import load_config |
||
| 12 | from elodie.plugins.plugins import Plugins |
||
| 13 | |||
| 14 | @mock.patch('elodie.config.config_file', '%s/config.ini-load-plugins-unset-backwards-compat' % gettempdir()) |
||
| 15 | def test_load_plugins_unset_backwards_compat(): |
||
| 16 | with open('%s/config.ini-load-plugins-unset-backwards-compat' % gettempdir(), 'w') as f: |
||
| 17 | f.write(""" |
||
| 18 | """) |
||
| 19 | if hasattr(load_config, 'config'): |
||
| 20 | del load_config.config |
||
| 21 | |||
| 22 | plugins = Plugins() |
||
| 23 | plugins.load() |
||
| 24 | |||
| 25 | if hasattr(load_config, 'config'): |
||
| 26 | del load_config.config |
||
| 27 | |||
| 28 | assert plugins.plugins == [], plugins.plugins |
||
| 29 | |||
| 30 | @mock.patch('elodie.config.config_file', '%s/config.ini-load-plugins-exists-not-set' % gettempdir()) |
||
| 31 | def test_load_plugins_exists_not_set(): |
||
| 32 | with open('%s/config.ini-load-plugins-exists-not-set' % gettempdir(), 'w') as f: |
||
| 33 | f.write(""" |
||
| 34 | [Plugins] |
||
| 35 | """) |
||
| 36 | if hasattr(load_config, 'config'): |
||
| 37 | del load_config.config |
||
| 38 | |||
| 39 | plugins = Plugins() |
||
| 40 | plugins.load() |
||
| 41 | |||
| 42 | if hasattr(load_config, 'config'): |
||
| 43 | del load_config.config |
||
| 44 | |||
| 45 | assert plugins.plugins == [], plugins.plugins |
||
| 46 | |||
| 47 | View Code Duplication | @mock.patch('elodie.config.config_file', '%s/config.ini-load-plugins-one' % gettempdir()) |
|
|
|
|||
| 48 | def test_load_plugins_one(): |
||
| 49 | with open('%s/config.ini-load-plugins-one' % gettempdir(), 'w') as f: |
||
| 50 | f.write(""" |
||
| 51 | [Plugins] |
||
| 52 | plugins=Dummy |
||
| 53 | """) |
||
| 54 | if hasattr(load_config, 'config'): |
||
| 55 | del load_config.config |
||
| 56 | |||
| 57 | plugins = Plugins() |
||
| 58 | plugins.load() |
||
| 59 | |||
| 60 | if hasattr(load_config, 'config'): |
||
| 61 | del load_config.config |
||
| 62 | |||
| 63 | assert plugins.plugins == ['Dummy'], plugins.plugins |
||
| 64 | assert len(plugins.classes) == 1, len(plugins.classes) |
||
| 65 | |||
| 66 | View Code Duplication | @mock.patch('elodie.config.config_file', '%s/config.ini-load-plugins-one-with-invalid' % gettempdir()) |
|
| 67 | def test_load_plugins_one_with_invalid(): |
||
| 68 | with open('%s/config.ini-load-plugins-one' % gettempdir(), 'w') as f: |
||
| 69 | f.write(""" |
||
| 70 | [Plugins] |
||
| 71 | plugins=DNE |
||
| 72 | """) |
||
| 73 | if hasattr(load_config, 'config'): |
||
| 74 | del load_config.config |
||
| 75 | |||
| 76 | plugins = Plugins() |
||
| 77 | plugins.load() |
||
| 78 | |||
| 79 | if hasattr(load_config, 'config'): |
||
| 80 | del load_config.config |
||
| 81 | |||
| 82 | assert plugins.plugins == [], plugins.plugins |
||
| 83 | assert len(plugins.classes) == 0, len(plugins.classes) |
||
| 84 | |||
| 85 | @mock.patch('elodie.config.config_file', '%s/config.ini-load-plugins-many' % gettempdir()) |
||
| 86 | def test_load_plugins_many(): |
||
| 87 | with open('%s/config.ini-load-plugins-many' % gettempdir(), 'w') as f: |
||
| 88 | f.write(""" |
||
| 89 | [Plugins] |
||
| 90 | plugins=ThrowError,Dummy |
||
| 91 | """) |
||
| 92 | if hasattr(load_config, 'config'): |
||
| 93 | del load_config.config |
||
| 94 | |||
| 95 | plugins = Plugins() |
||
| 96 | plugins.load() |
||
| 97 | |||
| 98 | if hasattr(load_config, 'config'): |
||
| 99 | del load_config.config |
||
| 100 | |||
| 101 | assert plugins.plugins == ['ThrowError','Dummy'], plugins.plugins |
||
| 102 | assert plugins.classes['ThrowError'].__name__ == 'ThrowError', plugins.classes['ThrowError'].__name__ |
||
| 103 | assert plugins.classes['Dummy'].__name__ == 'Dummy', plugins.classes['Dummy'].__name__ |
||
| 104 | assert len(plugins.classes) == 2, len(plugins.classes) |
||
| 105 | |||
| 106 | @mock.patch('elodie.config.config_file', '%s/config.ini-load-plugins-many-with-invalid' % gettempdir()) |
||
| 107 | def test_load_plugins_set_many_with_invalid(): |
||
| 108 | with open('%s/config.ini-load-plugins-many-with-invalid' % gettempdir(), 'w') as f: |
||
| 109 | f.write(""" |
||
| 110 | [Plugins] |
||
| 111 | plugins=ThrowError,Dummy,DNE |
||
| 112 | """) |
||
| 113 | if hasattr(load_config, 'config'): |
||
| 114 | del load_config.config |
||
| 115 | |||
| 116 | plugins = Plugins() |
||
| 117 | plugins.load() |
||
| 118 | |||
| 119 | if hasattr(load_config, 'config'): |
||
| 120 | del load_config.config |
||
| 121 | |||
| 122 | assert plugins.plugins == ['ThrowError','Dummy'], plugins.plugins |
||
| 123 | |||
| 124 | @mock.patch('elodie.config.config_file', '%s/config.ini-run-before' % gettempdir()) |
||
| 125 | def test_run_before(): |
||
| 126 | with open('%s/config.ini-run-before' % gettempdir(), 'w') as f: |
||
| 127 | f.write(""" |
||
| 128 | [Plugins] |
||
| 129 | plugins=Dummy |
||
| 130 | """) |
||
| 131 | if hasattr(load_config, 'config'): |
||
| 132 | del load_config.config |
||
| 133 | |||
| 134 | plugins = Plugins() |
||
| 135 | plugins.load() |
||
| 136 | before_ran_1 = plugins.classes['Dummy'].before_ran |
||
| 137 | plugins.run_all_before('', '', '') |
||
| 138 | before_ran_2 = plugins.classes['Dummy'].before_ran |
||
| 139 | |||
| 140 | if hasattr(load_config, 'config'): |
||
| 141 | del load_config.config |
||
| 142 | |||
| 143 | assert before_ran_1 == False, before_ran_1 |
||
| 144 | assert before_ran_2 == True, before_ran_2 |
||
| 145 | |||
| 146 | View Code Duplication | @mock.patch('elodie.config.config_file', '%s/config.ini-throw-error' % gettempdir()) |
|
| 147 | def test_throw_error(): |
||
| 148 | with open('%s/config.ini-throw-error' % gettempdir(), 'w') as f: |
||
| 149 | f.write(""" |
||
| 150 | [Plugins] |
||
| 151 | plugins=ThrowError |
||
| 152 | """) |
||
| 153 | if hasattr(load_config, 'config'): |
||
| 154 | del load_config.config |
||
| 155 | |||
| 156 | plugins = Plugins() |
||
| 157 | plugins.load() |
||
| 158 | status = plugins.run_all_before('', '', '') |
||
| 159 | |||
| 160 | if hasattr(load_config, 'config'): |
||
| 161 | del load_config.config |
||
| 162 | |||
| 163 | assert status == False, status |
||
| 164 | |||
| 165 | View Code Duplication | @mock.patch('elodie.config.config_file', '%s/config.ini-throw-error-one-of-many' % gettempdir()) |
|
| 166 | def test_throw_error_one_of_many(): |
||
| 167 | with open('%s/config.ini-throw-error-one-of-many' % gettempdir(), 'w') as f: |
||
| 168 | f.write(""" |
||
| 169 | [Plugins] |
||
| 170 | plugins=Dummy,ThrowError |
||
| 171 | """) |
||
| 172 | if hasattr(load_config, 'config'): |
||
| 173 | del load_config.config |
||
| 174 | |||
| 175 | plugins = Plugins() |
||
| 176 | plugins.load() |
||
| 177 | status = plugins.run_all_before('', '', '') |
||
| 178 | |||
| 179 | if hasattr(load_config, 'config'): |
||
| 180 | del load_config.config |
||
| 181 | |||
| 182 | assert status == False, status |
||
| 183 | |||
| 184 | View Code Duplication | @mock.patch('elodie.config.config_file', '%s/config.ini-throw-runtime-error' % gettempdir()) |
|
| 185 | def test_throw_error_runtime_error(): |
||
| 186 | with open('%s/config.ini-throw-runtime-error' % gettempdir(), 'w') as f: |
||
| 187 | f.write(""" |
||
| 188 | [Plugins] |
||
| 189 | plugins=RuntimeError |
||
| 190 | """) |
||
| 191 | if hasattr(load_config, 'config'): |
||
| 192 | del load_config.config |
||
| 193 | |||
| 194 | plugins = Plugins() |
||
| 195 | plugins.load() |
||
| 196 | status = plugins.run_all_before('', '', '') |
||
| 197 | |||
| 198 | if hasattr(load_config, 'config'): |
||
| 199 | del load_config.config |
||
| 200 | |||
| 201 | assert status == True, status |
||
| 202 |