Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Delta Between Two Patch Sets: update-copyright/tests/test_update_copyright.py

Issue 29493601: Issue 5431 - Fix test for copyright update script (Closed) Base URL: https://hg.adblockplus.org/codingtools
Left Patch Set: Created July 20, 2017, 1:38 p.m.
Right Patch Set: Clean-up and remove duplicate code Created Aug. 5, 2017, 6:40 p.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
Left: Side by side diff | Download
Right: Side by side diff | Download
« no previous file with change/comment | « update-copyright/tests/data/sample_file.py ('k') | no next file » | no next file with change/comment »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
LEFTRIGHT
1 #!/usr/bin/env python3 1 #!/usr/bin/env python3
2 2
3 import os 3 import os
4 import re 4 import re
5 import datetime 5 import datetime
6 import subprocess 6 import subprocess
7 import shutil 7 import shutil
8 import urllib.parse 8 import urllib.parse
9 9
10 import pytest 10 import pytest
11 11
12 from update_copyright import extract_urls, text_replace, hg_commit, main 12 from update_copyright import extract_urls, text_replace, hg_commit, main
13 13
14 data_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'data') 14 data_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'data')
15 15
16 16
17 @pytest.fixture(scope='module') 17 @pytest.fixture(scope='session')
18 def generate_sample_file(): 18 def sample_file(tmpdir_factory):
Vasily Kuznetsov 2017/07/20 15:01:30 It would be a bit more clear to call the fixture `
rosie 2017/08/03 17:52:45 Done.
19 # Generate sample_file.py programmatically
20 text = '# {}right (C) 2006-2015 eyeo GmbH\n'.format('Copy') 19 text = '# {}right (C) 2006-2015 eyeo GmbH\n'.format('Copy')
Vasily Kuznetsov 2017/07/20 15:01:30 If is important to have different years in differe
rosie 2017/08/03 17:52:45 The third line uses 'example GmbH' instead of 'eye
Vasily Kuznetsov 2017/08/04 16:54:46 Acknowledged.
21 text += "value = '{}right (C) 2006-2016 Eyeo GmbH'\n".format('Copy') 20 text += "value = '{}right (C) 2006-2016 Eyeo GmbH'\n".format('Copy')
22 text += '# {}right (C) 2006-2014 example GmbH'.format('Copy') 21 text += '# {}right (C) 2006-2014 example GmbH'.format('Copy')
23 sample_file = open(os.path.join(data_path, 'sample_file.py'), 'w') 22 sample_file = tmpdir_factory.mktemp('sample_dir').join('sample_file.py')
Vasily Kuznetsov 2017/07/20 15:01:30 Would it work to create this file in a temporary d
Vasily Kuznetsov 2017/07/20 15:01:30 You could use `with` here instead, then you don't
rosie 2017/08/03 17:52:45 Done.
rosie 2017/08/03 17:52:45 Done.
24 sample_file.write(text) 23 sample_file.write(text)
25 sample_file.close() 24 return str(sample_file)
26 yield generate_sample_file # teardown
27 subprocess.call(['rm', os.path.join(data_path, 'sample_file.py')])
28 25
29 26
30 def create_repo(path): 27 def create_repo(sample_file, path):
31 subprocess.check_call(['hg', 'init', path]) 28 subprocess.check_call(['hg', 'init', path])
32 with open(os.path.join(path, '.hg', 'hgrc'), 'w+') as hgrc: 29 with open(os.path.join(path, '.hg', 'hgrc'), 'w+') as hgrc:
33 set_user = '[ui]\nusername = Test User <test@example.com>' 30 set_user = '[ui]\nusername = Test User <test@example.com>'
34 hgrc.write(set_user) 31 hgrc.write(set_user)
35 shutil.copy(os.path.join(data_path, 'sample_file.py'), path) 32 shutil.copy(sample_file, path)
36 subprocess.check_call(['hg', 'commit', '-Am', 'Initial commit', 33 subprocess.check_call(['hg', 'commit', '-Am', 'Initial commit',
37 '--repository', path]) 34 '--repository', path])
38 35
39 36
40 @pytest.fixture() 37 @pytest.fixture()
41 def temp_dir(tmpdir): 38 def temp_dir(tmpdir):
42 temp_dir = tmpdir.mkdir('temp_dir') 39 temp_dir = tmpdir.mkdir('temp_dir')
43 return temp_dir 40 return temp_dir
44 41
45 42
46 @pytest.fixture() 43 @pytest.fixture()
47 def temp_repo(tmpdir): 44 def temp_repo(sample_file, tmpdir):
48 """"Returns a path to a temporary repo containing one sample file""" 45 """"Returns a path to a temporary repo containing one sample file"""
49 temp_repo = tmpdir.mkdir('tmp_dir') 46 temp_repo = tmpdir.mkdir('tmp_dir')
50 create_repo(str(temp_repo)) 47 create_repo(sample_file, str(temp_repo))
51 return temp_repo 48 return temp_repo
52 49
53 50
54 @pytest.fixture() 51 @pytest.fixture()
55 def base_dir(tmpdir): 52 def base_dir(sample_file, tmpdir):
56 """Returns a temporary directory that contains one html page and two 53 """Returns a temporary directory that contains one html page and two
57 repositories (one with push access, the other without)""" 54 repositories (one with push access, the other without)"""
58 tmp_repo = tmpdir.mkdir('tmp_dir') 55 tmp_repo = tmpdir.mkdir('tmp_dir')
59 temp_dir = str(tmp_repo) 56 temp_dir = str(tmp_repo)
60 subprocess.check_call(['cp', os.path.join(data_path, 'hg_page.html'), 57 subprocess.check_call(['cp', os.path.join(data_path, 'hg_page.html'),
61 temp_dir]) 58 temp_dir])
62 repo_1 = os.path.join(temp_dir, 'repo_1') 59 repo_1 = os.path.join(temp_dir, 'repo_1')
63 repo_2 = os.path.join(temp_dir, 'repo_2') 60 repo_2 = os.path.join(temp_dir, 'repo_2')
64 os.mkdir(repo_1) 61 os.mkdir(repo_1)
65 os.mkdir(repo_2) 62 os.mkdir(repo_2)
66 create_repo(repo_1) 63 create_repo(sample_file, repo_1)
67 create_repo(repo_2) 64 create_repo(sample_file, repo_2)
68 65
69 # Make repo_2 read-only 66 # Make repo_2 read-only
70 with open(os.path.join(repo_2, '.hg/hgrc'), 'w') as hgrc: 67 with open(os.path.join(repo_2, '.hg/hgrc'), 'w') as hgrc:
71 hook = '[hooks]\npretxnchangegroup = return True' 68 hook = '[hooks]\npretxnchangegroup = return True'
72 hgrc.write(hook) 69 hgrc.write(hook)
73 return temp_dir 70 return temp_dir
74 71
75 72
76 def test_extract_urls(): 73 def test_extract_urls():
77 data_url = urllib.parse.urljoin('file:///', data_path) 74 data_url = urllib.parse.urljoin('file:///', data_path)
78 urls = [data_url + '/repo_1/', 75 urls = [data_url + '/repo_1/',
79 data_url + '/repo_2/'] 76 data_url + '/repo_2/']
80 assert urls == extract_urls(os.path.join(data_url, 'hg_page.html')) 77 assert urls == extract_urls(os.path.join(data_url, 'hg_page.html'))
81 78
82 79
83 def test_text_replacement(generate_sample_file, temp_repo): 80 def test_text_replacement(sample_file, temp_repo):
84 updated = 0 81 updated = 0
85 filename = temp_repo.join('sample_file.py').strpath 82 text_replace(temp_repo.strpath, 'sample_file.py')
86 text_replace(temp_repo.strpath, filename) 83 with open(os.path.join(temp_repo.strpath, 'sample_file.py')) as file:
87 with open(filename) as file:
88 text = file.read() 84 text = file.read()
89 pattern = re.compile(r'(copyright.*?\d{4})(?:-\d{4})?\s+eyeo gmbh', 85 pattern = re.compile(r'(copyright.*?\d{4})(?:-\d{4})?\s+eyeo gmbh',
90 re.I) 86 re.I)
91 for year in re.finditer(pattern, text): 87 for year in re.finditer(pattern, text):
92 dates = re.search(r'(\d{4})-(\d{4})', year.group(0)) 88 dates = re.search(r'(\d{4})-(\d{4})', year.group(0))
93 if dates.group(2) == str(datetime.datetime.now().year): 89 if dates.group(2) == str(datetime.datetime.now().year):
94 updated += 1 90 updated += 1
95 91
96 # test that non-eyeo copyright information are left alone 92 # test that non-eyeo copyright information is left alone
97 assert '2014 example' in text 93 assert '2014 example' in text
98 # test for copyright information in both strings and comments 94 # test for copyright information in both strings and comments
99 assert updated == 2 95 assert updated == 2
100 96
101 97
102 def test_hg_commit(temp_repo, temp_dir): 98 def test_hg_commit(temp_repo, temp_dir):
103 directory = str(temp_dir) 99 directory = str(temp_dir)
104 repo = str(temp_repo) 100 repo = str(temp_repo)
105 subprocess.check_call(['hg', 'clone', repo, directory]) 101 subprocess.check_call(['hg', 'clone', repo, directory])
106 open(os.path.join(directory, 'foo'), 'w').close() 102 open(os.path.join(directory, 'foo'), 'w').close()
107 subprocess.check_call(['hg', 'add', '--repository', directory]) 103 subprocess.check_call(['hg', 'add', '--repository', directory])
108 hg_commit(directory, repo) 104 hg_commit(directory, repo)
109 105
110 # Make sure both files contain the commmit message from hg log 106 # Make sure both files contain the commmit message from hg log
111 log_1 = subprocess.run(['hg', 'log', '--repository', repo], 107 log_1 = subprocess.run(['hg', 'log', '--repository', repo],
112 stdout=subprocess.PIPE) 108 stdout=subprocess.PIPE)
113 assert 'Noissue - Updated copyright year' in str(log_1.stdout) 109 assert 'Noissue - Updated copyright year' in str(log_1.stdout)
114 110
115 111
116 def test_all(base_dir): 112 def test_all(sample_file, base_dir):
117 main(urllib.parse.urljoin('file:///', os.path.join( 113 main(urllib.parse.urljoin('file:///', os.path.join(
118 base_dir, 'hg_page.html')), None) 114 base_dir, 'hg_page.html')), None)
119 115
120 # assert hg log for repo_1 116 # assert hg log for repo_1
121 log_1 = subprocess.run(['hg', 'log', '--repository', 117 log_1 = subprocess.run(['hg', 'log', '--repository',
122 os.path.join(base_dir, 'repo_1')], 118 os.path.join(base_dir, 'repo_1')],
123 stdout=subprocess.PIPE) 119 stdout=subprocess.PIPE)
124 assert 'Noissue - Updated copyright year' in str(log_1.stdout) 120 assert 'Noissue - Updated copyright year' in str(log_1.stdout)
125 121
126 # assert the .patch file for repo_2 122 # assert the .patch file for repo_2
127 assert'Noissue - Updated copyright year' in open('repo_2.patch').read() 123 assert'Noissue - Updated copyright year' in open('repo_2.patch').read()
128 subprocess.call(['rm', 'repo_2.patch']) # cleanup 124 subprocess.call(['rm', 'repo_2.patch']) # cleanup
129 # subprocess.call(['rm', os.path.join(data_path, 'sample_file.py')])
LEFTRIGHT

Powered by Google App Engine
This is Rietveld