| Conditions | 13 | 
| Total Lines | 73 | 
| Lines | 0 | 
| Ratio | 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 TestSmartWorker.test_main() 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 | import os  | 
            ||
| 34 | def test_main(self):  | 
            ||
| 35 | command = ['python2', self.base_worker_script, self.command_manager._commands_filename, self.logs_dir]  | 
            ||
| 36 | assert_equal(call(command), 0)  | 
            ||
| 37 | # Simulate a resume, i.e. re-run the command, the output/error should be concatenated.  | 
            ||
| 38 | self.command_manager.set_commands_to_run(self.commands)  | 
            ||
| 39 | assert_equal(call(command), 0)  | 
            ||
| 40 | |||
| 41 | # Check output logs  | 
            ||
| 42 | filenames = os.listdir(self.logs_dir)  | 
            ||
| 43 |         outlogs = [os.path.join(self.logs_dir, filename) for filename in filenames if filename.endswith(".out")] | 
            ||
| 44 | for log_filename in outlogs:  | 
            ||
| 45 | with open(log_filename) as logfile:  | 
            ||
| 46 | # From log's filename (i.e. uid) retrieve executed command associated with this log  | 
            ||
| 47 | uid = os.path.splitext(os.path.basename(log_filename))[0]  | 
            ||
| 48 | executed_command = self.commands[self.commands_uid.index(uid)]  | 
            ||
| 49 | |||
| 50 | # Since the command was run twice.  | 
            ||
| 51 | for i in range(2):  | 
            ||
| 52 | # First line is the datetime of the executed command in comment.  | 
            ||
| 53 | line = logfile.readline().strip()  | 
            ||
| 54 | |||
| 55 | if i == 0:  | 
            ||
| 56 |                         assert_true("Started" in line) | 
            ||
| 57 | else:  | 
            ||
| 58 |                         assert_true("Resumed" in line) | 
            ||
| 59 | |||
| 60 |                     assert_true(line.startswith("## SMART-DISPATCH")) | 
            ||
| 61 |                     assert_true(time.strftime("%Y-%m-%d %H:%M:") in line)  # Don't check seconds. | 
            ||
| 62 | |||
| 63 | # Second line is the executed command in comment.  | 
            ||
| 64 | line = logfile.readline().strip()  | 
            ||
| 65 | assert_true(executed_command in line)  | 
            ||
| 66 | |||
| 67 | # Next should be the command's output  | 
            ||
| 68 | line = logfile.readline().strip()  | 
            ||
| 69 | assert_equal(line, executed_command[-1]) # We know those are 'echo' of a digit  | 
            ||
| 70 | |||
| 71 | # Empty line  | 
            ||
| 72 | assert_equal(logfile.readline().strip(), "")  | 
            ||
| 73 | |||
| 74 | # Log should be empty now  | 
            ||
| 75 |                 assert_equal("", logfile.read()) | 
            ||
| 76 | |||
| 77 | # Check error logs  | 
            ||
| 78 |         errlogs = [os.path.join(self.logs_dir, filename) for filename in filenames if filename.endswith(".err")] | 
            ||
| 79 | for log_filename in errlogs:  | 
            ||
| 80 | with open(log_filename) as logfile:  | 
            ||
| 81 | # From log's filename (i.e. uid) retrieve executed command associated with this log  | 
            ||
| 82 | uid = os.path.splitext(os.path.basename(log_filename))[0]  | 
            ||
| 83 | executed_command = self.commands[self.commands_uid.index(uid)]  | 
            ||
| 84 | |||
| 85 | # Since the command was run twice.  | 
            ||
| 86 | for i in range(2):  | 
            ||
| 87 | # First line is the datetime of the executed command in comment.  | 
            ||
| 88 | line = logfile.readline().strip()  | 
            ||
| 89 | |||
| 90 | if i == 0:  | 
            ||
| 91 |                         assert_true("Started" in line) | 
            ||
| 92 | else:  | 
            ||
| 93 |                         assert_true("Resumed" in line) | 
            ||
| 94 | |||
| 95 |                     assert_true(line.startswith("## SMART-DISPATCH")) | 
            ||
| 96 |                     assert_true(time.strftime("%Y-%m-%d %H:%M:") in line)  # Don't check seconds. | 
            ||
| 97 | |||
| 98 | # Second line is the executed command in comment.  | 
            ||
| 99 | line = logfile.readline().strip()  | 
            ||
| 100 | assert_true(executed_command in line)  | 
            ||
| 101 | |||
| 102 | # Empty line  | 
            ||
| 103 | assert_equal(logfile.readline().strip(), "")  | 
            ||
| 104 | |||
| 105 | # Log should be empty now  | 
            ||
| 106 |                 assert_equal("", logfile.read()) | 
            ||
| 107 | |||
| 120 |