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