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