| Conditions | 17 |
| Total Lines | 78 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 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 test_generate_symlinks() 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 | # -*- coding: utf-8 -*- |
||
| 36 | @pytest.mark.usefixtures('clean_system', 'remove_test_dir') |
||
| 37 | def test_generate_symlinks(): |
||
| 38 | generate.generate_files( |
||
| 39 | context={ |
||
| 40 | 'cookiecutter': { |
||
| 41 | 'name': TEST_OUTPUT_DIR, |
||
| 42 | "link_dir": "rendered_dir", |
||
| 43 | "sym_to_nontemp": "rendered_sym_to_original", |
||
| 44 | "sym_to_temp": "rendered_sym_to_rendered_dir", |
||
| 45 | "_copy_without_render": [ |
||
| 46 | "copy_no_render" |
||
| 47 | ] |
||
| 48 | } |
||
| 49 | }, |
||
| 50 | repo_dir='tests/test-generate-symlinks' |
||
| 51 | ) |
||
| 52 | |||
| 53 | dir_contents = os.listdir(TEST_OUTPUT_DIR) |
||
| 54 | |||
| 55 | assert 'copy_no_render' in dir_contents |
||
| 56 | assert 'original' in dir_contents |
||
| 57 | assert 'rendered_dir' in dir_contents |
||
| 58 | assert 'rendered_sym_to_original' in dir_contents |
||
| 59 | assert 'rendered_sym_to_rendered_dir' in dir_contents |
||
| 60 | assert 'symlink' in dir_contents |
||
| 61 | |||
| 62 | # Test links that have been rendered and copied |
||
| 63 | def _test_symlink(root, link, points_to): |
||
| 64 | assert islink(os.path.join(root, link)) |
||
| 65 | |||
| 66 | actual_points_to = readlink(os.path.join(root, link)) |
||
| 67 | |||
| 68 | if actual_points_to.endswith(os.sep): |
||
| 69 | actual_points_to = actual_points_to[:-1] |
||
| 70 | |||
| 71 | assert actual_points_to == points_to |
||
| 72 | |||
| 73 | # normal symlink, not rendered target |
||
| 74 | _test_symlink(TEST_OUTPUT_DIR, 'symlink', 'original') |
||
| 75 | |||
| 76 | # normal symlink, rendered target |
||
| 77 | _test_symlink(TEST_OUTPUT_DIR, 'symlink_to_rendered', 'rendered_dir') |
||
| 78 | |||
| 79 | # rendered symlink, not rendered target |
||
| 80 | _test_symlink(TEST_OUTPUT_DIR, 'rendered_sym_to_original', 'original') |
||
| 81 | |||
| 82 | # rendered symlink, rendered target |
||
| 83 | _test_symlink(TEST_OUTPUT_DIR, 'rendered_sym_to_rendered_dir', |
||
| 84 | 'rendered_dir') |
||
| 85 | |||
| 86 | # Test links that have not been rendered |
||
| 87 | non_rendered_dir = os.path.join(TEST_OUTPUT_DIR, 'copy_no_render') |
||
| 88 | non_rendered_dir_contents = os.listdir(non_rendered_dir) |
||
| 89 | |||
| 90 | assert 'original' in non_rendered_dir_contents |
||
| 91 | assert 'symlink' in non_rendered_dir_contents |
||
| 92 | assert 'symlink_to_rendered' in non_rendered_dir_contents |
||
| 93 | assert '{{ cookiecutter.link_dir }}' in non_rendered_dir_contents |
||
| 94 | assert '{{ cookiecutter.sym_to_nontemp }}' in non_rendered_dir_contents |
||
| 95 | assert '{{ cookiecutter.sym_to_temp }}' in non_rendered_dir_contents |
||
| 96 | |||
| 97 | # normal symlink, not rendered target |
||
| 98 | _test_symlink(non_rendered_dir, 'symlink', 'original') |
||
| 99 | |||
| 100 | # normal symlink, rendered target |
||
| 101 | _test_symlink(non_rendered_dir, |
||
| 102 | 'symlink_to_rendered', |
||
| 103 | '{{ cookiecutter.link_dir }}') |
||
| 104 | |||
| 105 | # rendered symlink, not rendered target |
||
| 106 | _test_symlink(non_rendered_dir, |
||
| 107 | '{{ cookiecutter.sym_to_nontemp }}', |
||
| 108 | 'original') |
||
| 109 | |||
| 110 | # rendered symlink, rendered target |
||
| 111 | _test_symlink(non_rendered_dir, |
||
| 112 | '{{ cookiecutter.sym_to_temp }}', |
||
| 113 | '{{ cookiecutter.link_dir }}') |
||
| 114 |