OLD | NEW |
| (Empty) |
1 #!/usr/bin/env python3 | |
2 | |
3 import os | |
4 import re | |
5 import datetime | |
6 import subprocess | |
7 import shutil | |
8 import urllib.parse | |
9 | |
10 import pytest | |
11 | |
12 from update_copyright import extract_urls, text_replace, hg_commit, main | |
13 | |
14 data_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'data') | |
15 | |
16 | |
17 @pytest.fixture(scope='session') | |
18 def sample_file(tmpdir_factory): | |
19 text = '# {}right (C) 2006-2015 eyeo GmbH\n'.format('Copy') | |
20 text += "value = '{}right (C) 2006-2016 Eyeo GmbH'\n".format('Copy') | |
21 text += '# {}right (C) 2006-2014 example GmbH'.format('Copy') | |
22 sample_file = tmpdir_factory.mktemp('sample_dir').join('sample_file.py') | |
23 sample_file.write(text) | |
24 return str(sample_file) | |
25 | |
26 | |
27 def create_repo(sample_file, path): | |
28 subprocess.check_call(['hg', 'init', path]) | |
29 with open(os.path.join(path, '.hg', 'hgrc'), 'w+') as hgrc: | |
30 set_user = '[ui]\nusername = Test User <test@example.com>' | |
31 hgrc.write(set_user) | |
32 shutil.copy(sample_file, path) | |
33 subprocess.check_call(['hg', 'commit', '-Am', 'Initial commit', | |
34 '--repository', path]) | |
35 | |
36 | |
37 @pytest.fixture() | |
38 def temp_dir(tmpdir): | |
39 temp_dir = tmpdir.mkdir('temp_dir') | |
40 return temp_dir | |
41 | |
42 | |
43 @pytest.fixture() | |
44 def temp_repo(sample_file, tmpdir): | |
45 """"Returns a path to a temporary repo containing one sample file""" | |
46 temp_repo = tmpdir.mkdir('tmp_dir') | |
47 create_repo(sample_file, str(temp_repo)) | |
48 return temp_repo | |
49 | |
50 | |
51 @pytest.fixture() | |
52 def base_dir(sample_file, tmpdir): | |
53 """Returns a temporary directory that contains one html page and two | |
54 repositories (one with push access, the other without)""" | |
55 tmp_repo = tmpdir.mkdir('tmp_dir') | |
56 temp_dir = str(tmp_repo) | |
57 subprocess.check_call(['cp', os.path.join(data_path, 'hg_page.html'), | |
58 temp_dir]) | |
59 repo_1 = os.path.join(temp_dir, 'repo_1') | |
60 repo_2 = os.path.join(temp_dir, 'repo_2') | |
61 os.mkdir(repo_1) | |
62 os.mkdir(repo_2) | |
63 create_repo(sample_file, repo_1) | |
64 create_repo(sample_file, repo_2) | |
65 | |
66 # Make repo_2 read-only | |
67 with open(os.path.join(repo_2, '.hg/hgrc'), 'w') as hgrc: | |
68 hook = '[hooks]\npretxnchangegroup = return True' | |
69 hgrc.write(hook) | |
70 return temp_dir | |
71 | |
72 | |
73 def test_extract_urls(): | |
74 data_url = urllib.parse.urljoin('file:///', data_path) | |
75 urls = [data_url + '/repo_1/', | |
76 data_url + '/repo_2/'] | |
77 assert urls == extract_urls(os.path.join(data_url, 'hg_page.html')) | |
78 | |
79 | |
80 def test_text_replacement(sample_file, temp_repo): | |
81 updated = 0 | |
82 text_replace(temp_repo.strpath, 'sample_file.py') | |
83 with open(os.path.join(temp_repo.strpath, 'sample_file.py')) as file: | |
84 text = file.read() | |
85 pattern = re.compile(r'(copyright.*?\d{4})(?:-\d{4})?\s+eyeo gmbh', | |
86 re.I) | |
87 for year in re.finditer(pattern, text): | |
88 dates = re.search(r'(\d{4})-(\d{4})', year.group(0)) | |
89 if dates.group(2) == str(datetime.datetime.now().year): | |
90 updated += 1 | |
91 | |
92 # test that non-eyeo copyright information is left alone | |
93 assert '2014 example' in text | |
94 # test for copyright information in both strings and comments | |
95 assert updated == 2 | |
96 | |
97 | |
98 def test_hg_commit(temp_repo, temp_dir): | |
99 directory = str(temp_dir) | |
100 repo = str(temp_repo) | |
101 subprocess.check_call(['hg', 'clone', repo, directory]) | |
102 open(os.path.join(directory, 'foo'), 'w').close() | |
103 subprocess.check_call(['hg', 'add', '--repository', directory]) | |
104 hg_commit(directory, repo) | |
105 | |
106 # Make sure both files contain the commmit message from hg log | |
107 log_1 = subprocess.run(['hg', 'log', '--repository', repo], | |
108 stdout=subprocess.PIPE) | |
109 assert 'Noissue - Updated copyright year' in str(log_1.stdout) | |
110 | |
111 | |
112 def test_all(sample_file, base_dir): | |
113 main(urllib.parse.urljoin('file:///', os.path.join( | |
114 base_dir, 'hg_page.html')), None) | |
115 | |
116 # assert hg log for repo_1 | |
117 log_1 = subprocess.run(['hg', 'log', '--repository', | |
118 os.path.join(base_dir, 'repo_1')], | |
119 stdout=subprocess.PIPE) | |
120 assert 'Noissue - Updated copyright year' in str(log_1.stdout) | |
121 | |
122 # assert the .patch file for repo_2 | |
123 assert'Noissue - Updated copyright year' in open('repo_2.patch').read() | |
124 subprocess.call(['rm', 'repo_2.patch']) # cleanup | |
OLD | NEW |