Total Complexity | 22 |
Total Lines | 125 |
Duplicated Lines | 29.6 % |
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 |
||
10 | class CommandFilesTests(unittest.TestCase): |
||
11 | |||
12 | def setUp(self): |
||
13 | self._base_dir = tmp.mkdtemp() |
||
14 | self.nb_commands = 3 |
||
15 | self.command1 = "1\n" |
||
16 | self.command2 = "2\n" |
||
17 | self.command3 = "3\n" |
||
18 | |||
19 | command_filename = os.path.join(self._base_dir, "commands.txt") |
||
20 | |||
21 | with open(command_filename, "w+") as commands_file: |
||
22 | commands_file.write(self.command1 + self.command2 + self.command3) |
||
23 | |||
24 | self.command_manager = CommandManager(command_filename) |
||
25 | |||
26 | def tearDown(self): |
||
27 | shutil.rmtree(self._base_dir) |
||
28 | |||
29 | def test_set_commands_to_run(self): |
||
30 | # SetUp |
||
31 | commands = ["4", "5", "6"] |
||
32 | |||
33 | # The function to test |
||
34 | self.command_manager.set_commands_to_run(commands) |
||
35 | |||
36 | # Test validation |
||
37 | with open(self.command_manager._commands_filename, "r") as commands_file: |
||
38 | assert_equal(commands_file.read(), self.command1 + self.command2 + self.command3 + "4\n5\n6\n") |
||
39 | |||
40 | assert_true(not os.path.isfile(self.command_manager._running_commands_filename)) |
||
41 | |||
42 | assert_true(not os.path.isfile(self.command_manager._finished_commands_filename)) |
||
43 | |||
44 | def test_get_failed_commands(self): |
||
45 | # Setup |
||
46 | command = self.command_manager.get_command_to_run() |
||
47 | self.command_manager.set_running_command_as_finished(command, 1) |
||
48 | |||
49 | # The function to test |
||
50 | failed_commands = self.command_manager.get_failed_commands() |
||
51 | |||
52 | # Test validation |
||
53 | assert_equal(len(failed_commands), 1) |
||
54 | assert_equal(failed_commands[0], self.command1) |
||
55 | |||
56 | def test_get_failed_commands_empty(self): |
||
57 | # The function to test |
||
58 | failed_commands = self.command_manager.get_failed_commands() |
||
59 | |||
60 | # Test validation |
||
61 | assert_equal(len(failed_commands), 0) |
||
62 | |||
63 | def test_get_command_to_run(self): |
||
64 | # The function to test |
||
65 | command = self.command_manager.get_command_to_run() |
||
66 | |||
67 | # Test validation |
||
68 | assert_equal(command, self.command1.strip()) |
||
69 | |||
70 | with open(self.command_manager._commands_filename, "r") as commands_file: |
||
71 | assert_equal(commands_file.read(), self.command2 + self.command3) |
||
72 | |||
73 | with open(self.command_manager._running_commands_filename, "r") as running_commands_file: |
||
74 | assert_equal(running_commands_file.read(), self.command1) |
||
75 | |||
76 | assert_true(not os.path.isfile(self.command_manager._finished_commands_filename)) |
||
77 | |||
78 | def test_get_nb_commands_to_run(self): |
||
79 | assert_equal(self.command_manager.get_nb_commands_to_run(), self.nb_commands) |
||
80 | |||
81 | View Code Duplication | def test_set_running_command_as_finished(self): |
|
|
|||
82 | # SetUp |
||
83 | command = self.command_manager.get_command_to_run() |
||
84 | |||
85 | # The function to test |
||
86 | self.command_manager.set_running_command_as_finished(command) |
||
87 | |||
88 | # Test validation |
||
89 | with open(self.command_manager._commands_filename, "r") as commands_file: |
||
90 | assert_equal(commands_file.read(), self.command2 + self.command3) |
||
91 | |||
92 | with open(self.command_manager._running_commands_filename, "r") as running_commands_file: |
||
93 | assert_equal(running_commands_file.read(), "") |
||
94 | |||
95 | with open(self.command_manager._finished_commands_filename, "r") as finished_commands_file: |
||
96 | assert_equal(finished_commands_file.read(), self.command1) |
||
97 | |||
98 | assert_true(not os.path.isfile(self.command_manager._failed_commands_filename)) |
||
99 | |||
100 | View Code Duplication | def test_set_running_command_as_failed(self): |
|
101 | # SetUp |
||
102 | command = self.command_manager.get_command_to_run() |
||
103 | error_code = 1 |
||
104 | |||
105 | # The function to test |
||
106 | self.command_manager.set_running_command_as_finished(command, error_code) |
||
107 | |||
108 | # Test validation |
||
109 | with open(self.command_manager._commands_filename, "r") as commands_file: |
||
110 | assert_equal(commands_file.read(), self.command2 + self.command3) |
||
111 | |||
112 | with open(self.command_manager._running_commands_filename, "r") as running_commands_file: |
||
113 | assert_equal(running_commands_file.read(), "") |
||
114 | |||
115 | with open(self.command_manager._failed_commands_filename, "r") as failed_commands_file: |
||
116 | assert_equal(failed_commands_file.read(), self.command1) |
||
117 | |||
118 | assert_true(not os.path.isfile(self.command_manager._finished_commands_filename)) |
||
119 | |||
120 | def test_reset_running_commands(self): |
||
121 | # SetUp |
||
122 | self.command_manager.get_command_to_run() |
||
123 | |||
124 | # The function to test |
||
125 | self.command_manager.reset_running_commands() |
||
126 | |||
127 | # Test validation |
||
128 | with open(self.command_manager._commands_filename, "r") as commands_file: |
||
129 | assert_equal(commands_file.read(), self.command1 + self.command2 + self.command3) |
||
130 | |||
131 | with open(self.command_manager._running_commands_filename, "r") as running_commands_file: |
||
132 | assert_equal(running_commands_file.read(), "") |
||
133 | |||
134 | assert_true(not os.path.isfile(self.command_manager._finished_commands_filename)) |
||
135 |