Total Complexity | 6 |
Total Lines | 52 |
Duplicated Lines | 82.69 % |
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 |
||
2 | import unittest |
||
3 | from time import sleep |
||
4 | |||
5 | from dirutility.open.open import tardir |
||
6 | |||
7 | |||
8 | View Code Duplication | class TestArchive(unittest.TestCase): |
|
|
|||
9 | |||
10 | def test_tardir(self): |
||
11 | tempfile = '/Users/Shared/kasdkfasdf.txt' |
||
12 | with open(tempfile, 'w') as wfd: |
||
13 | wfd.write(tempfile) |
||
14 | with open(tempfile, 'r') as rfd1, open(tempfile, 'r') as rfd2: |
||
15 | paths = { |
||
16 | 'abc': 'abc abc abc', |
||
17 | 'kbs': { |
||
18 | 'haha': {}, |
||
19 | 'yoyo': { |
||
20 | 'haha': rfd1 |
||
21 | } |
||
22 | }, |
||
23 | 'abcd': { |
||
24 | 'kbs': rfd2 |
||
25 | }, |
||
26 | } |
||
27 | with tardir('/Users/Shared/haha.tar.gz', **paths) as tarfile: |
||
28 | self.assertTrue(os.path.exists(tarfile.filepath)) |
||
29 | self.assertFalse(os.path.exists(tarfile.filepath)) |
||
30 | with open(tempfile, 'r') as rfd1, open(tempfile, 'r') as rfd2: |
||
31 | paths = { |
||
32 | 'abc': 'abc abc abc', |
||
33 | 'kbs': { |
||
34 | 'haha': {}, |
||
35 | 'yoyo': { |
||
36 | 'haha': rfd1 |
||
37 | } |
||
38 | }, |
||
39 | 'abcd': { |
||
40 | 'kbs': rfd2 |
||
41 | }, |
||
42 | } |
||
43 | with tardir('/Users/Shared/haha.zip', **paths, mode='w:zip', withdir=True) as tarfile: |
||
44 | self.assertTrue(os.path.exists(tarfile.filepath)) |
||
45 | sleep(10) |
||
46 | self.assertFalse(os.path.exists(tarfile.filepath)) |
||
47 | os.remove(tempfile) |
||
48 | |||
49 | |||
50 | if __name__ == '__main__': |
||
51 | unittest.main() |
||
52 |