| Total Lines | 101 |
| Duplicated Lines | 58.42 % |
| 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:
| 1 | import os |
||
| 95 | class CollectDirsTest(unittest.TestCase): |
||
| 96 | |||
| 97 | def setUp(self): |
||
| 98 | current_dir = os.path.split(__file__)[0] |
||
| 99 | self.collectors_test_dir = os.path.join(current_dir, |
||
| 100 | "collectors_test_dir") |
||
| 101 | |||
| 102 | def test_dir_empty(self): |
||
| 103 | self.assertRaises(TypeError, collect_dirs) |
||
| 104 | |||
| 105 | def test_dir_invalid(self): |
||
| 106 | self.assertEqual(collect_dirs(["invalid_path"]), []) |
||
| 107 | |||
| 108 | View Code Duplication | def test_dir_collection(self): |
|
|
|
|||
| 109 | self.assertEqual( |
||
| 110 | sorted(i for i in |
||
| 111 | collect_dirs([os.path.join(self.collectors_test_dir, |
||
| 112 | "**")]) |
||
| 113 | if "__pycache__" not in i), |
||
| 114 | sorted([os.path.normcase(os.path.join( |
||
| 115 | self.collectors_test_dir, "bears")), |
||
| 116 | os.path.normcase(os.path.join(self.collectors_test_dir, |
||
| 117 | "bears_local_global")), |
||
| 118 | os.path.normcase(os.path.join(self.collectors_test_dir, |
||
| 119 | "others")), |
||
| 120 | os.path.normcase(os.path.join(self.collectors_test_dir, |
||
| 121 | "others", |
||
| 122 | "c_files")), |
||
| 123 | os.path.normcase(os.path.join(self.collectors_test_dir, |
||
| 124 | "others", |
||
| 125 | "py_files")), |
||
| 126 | os.path.normcase(self.collectors_test_dir+os.sep)])) |
||
| 127 | |||
| 128 | View Code Duplication | def test_dir_string_collection(self): |
|
| 129 | self.assertEqual( |
||
| 130 | sorted(i for i in |
||
| 131 | collect_dirs(os.path.join(self.collectors_test_dir, |
||
| 132 | "**")) |
||
| 133 | if "__pycache__" not in i), |
||
| 134 | sorted([os.path.normcase(os.path.join( |
||
| 135 | self.collectors_test_dir, "bears")), |
||
| 136 | os.path.normcase(os.path.join(self.collectors_test_dir, |
||
| 137 | "bears_local_global")), |
||
| 138 | os.path.normcase(os.path.join(self.collectors_test_dir, |
||
| 139 | "others")), |
||
| 140 | os.path.normcase(os.path.join(self.collectors_test_dir, |
||
| 141 | "others", |
||
| 142 | "c_files")), |
||
| 143 | os.path.normcase(os.path.join(self.collectors_test_dir, |
||
| 144 | "others", |
||
| 145 | "py_files")), |
||
| 146 | os.path.normcase(self.collectors_test_dir+os.sep)])) |
||
| 147 | |||
| 148 | View Code Duplication | def test_ignored(self): |
|
| 149 | self.assertEqual( |
||
| 150 | sorted(i for i in |
||
| 151 | collect_dirs([os.path.join(self.collectors_test_dir, |
||
| 152 | "**")], |
||
| 153 | [os.path.normcase(os.path.join( |
||
| 154 | self.collectors_test_dir, |
||
| 155 | "others", |
||
| 156 | "py_files"))]) |
||
| 157 | if "__pycache__" not in i), |
||
| 158 | |||
| 159 | sorted([os.path.normcase(os.path.join( |
||
| 160 | self.collectors_test_dir, "bears")), |
||
| 161 | os.path.normcase(os.path.join(self.collectors_test_dir, |
||
| 162 | "bears_local_global")), |
||
| 163 | os.path.normcase(os.path.join(self.collectors_test_dir, |
||
| 164 | "others")), |
||
| 165 | os.path.normcase(os.path.join(self.collectors_test_dir, |
||
| 166 | "others", |
||
| 167 | "c_files")), |
||
| 168 | os.path.normcase(self.collectors_test_dir+os.sep)])) |
||
| 169 | |||
| 170 | def test_collect_registered_bears_dirs(self): |
||
| 171 | old_iter = pkg_resources.iter_entry_points |
||
| 172 | |||
| 173 | def test_iter_entry_points(name): |
||
| 174 | assert name == "hello" |
||
| 175 | |||
| 176 | class EntryPoint1: |
||
| 177 | |||
| 178 | @staticmethod |
||
| 179 | def load(): |
||
| 180 | class PseudoPlugin: |
||
| 181 | __file__ = "/path1/file1" |
||
| 182 | return PseudoPlugin() |
||
| 183 | |||
| 184 | class EntryPoint2: |
||
| 185 | |||
| 186 | @staticmethod |
||
| 187 | def load(): |
||
| 188 | raise pkg_resources.DistributionNotFound |
||
| 189 | |||
| 190 | return iter([EntryPoint1(), EntryPoint2()]) |
||
| 191 | |||
| 192 | pkg_resources.iter_entry_points = test_iter_entry_points |
||
| 193 | output = sorted(collect_registered_bears_dirs("hello")) |
||
| 194 | self.assertEqual(output, [os.path.abspath("/path1")]) |
||
| 195 | pkg_resources.iter_entry_points = old_iter |
||
| 196 | |||
| 310 |