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

Powered by Google App Engine
This is Rietveld