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

Side by Side Diff: 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
Patch Set: Created July 20, 2017, 1:38 p.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff | Download patch
« no previous file with comments | « update-copyright/tests/data/sample_file.py ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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')
18 def generate_sample_file():
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')
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')
22 text += '# {}right (C) 2006-2014 example GmbH'.format('Copy')
23 sample_file = open(os.path.join(data_path, 'sample_file.py'), 'w')
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)
25 sample_file.close()
26 yield generate_sample_file # teardown
27 subprocess.call(['rm', os.path.join(data_path, 'sample_file.py')])
28
29
17 def create_repo(path): 30 def create_repo(path):
18 subprocess.check_call(['hg', 'init', path]) 31 subprocess.check_call(['hg', 'init', path])
19 with open(os.path.join(path, '.hg', 'hgrc'), 'w+') as hgrc: 32 with open(os.path.join(path, '.hg', 'hgrc'), 'w+') as hgrc:
20 set_user = '[ui]\nusername = Test User <test@example.com>' 33 set_user = '[ui]\nusername = Test User <test@example.com>'
21 hgrc.write(set_user) 34 hgrc.write(set_user)
22 shutil.copy(os.path.join(data_path, 'sample_file.py'), path) 35 shutil.copy(os.path.join(data_path, 'sample_file.py'), path)
23 subprocess.check_call(['hg', 'commit', '-Am', 'Initial commit', 36 subprocess.check_call(['hg', 'commit', '-Am', 'Initial commit',
24 '--repository', path]) 37 '--repository', path])
25 38
26 39
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
60 return temp_dir 73 return temp_dir
61 74
62 75
63 def test_extract_urls(): 76 def test_extract_urls():
64 data_url = urllib.parse.urljoin('file:///', data_path) 77 data_url = urllib.parse.urljoin('file:///', data_path)
65 urls = [data_url + '/repo_1/', 78 urls = [data_url + '/repo_1/',
66 data_url + '/repo_2/'] 79 data_url + '/repo_2/']
67 assert urls == extract_urls(os.path.join(data_url, 'hg_page.html')) 80 assert urls == extract_urls(os.path.join(data_url, 'hg_page.html'))
68 81
69 82
70 def test_text_replacement(temp_repo): 83 def test_text_replacement(generate_sample_file, temp_repo):
71 updated = 0 84 updated = 0
72 filename = temp_repo.join('sample_file.py').strpath 85 filename = temp_repo.join('sample_file.py').strpath
73 text_replace(temp_repo.strpath, filename) 86 text_replace(temp_repo.strpath, filename)
74 with open(filename) as file: 87 with open(filename) as file:
75 text = file.read() 88 text = file.read()
76 pattern = re.compile(r'(copyright.*?\d{4})(?:-\d{4})?\s+eyeo gmbh', 89 pattern = re.compile(r'(copyright.*?\d{4})(?:-\d{4})?\s+eyeo gmbh',
77 re.I) 90 re.I)
78 for year in re.finditer(pattern, text): 91 for year in re.finditer(pattern, text):
79 dates = re.search(r'(\d{4})-(\d{4})', year.group(0)) 92 dates = re.search(r'(\d{4})-(\d{4})', year.group(0))
80 if dates.group(2) == str(datetime.datetime.now().year): 93 if dates.group(2) == str(datetime.datetime.now().year):
(...skipping 25 matching lines...) Expand all
106 119
107 # assert hg log for repo_1 120 # assert hg log for repo_1
108 log_1 = subprocess.run(['hg', 'log', '--repository', 121 log_1 = subprocess.run(['hg', 'log', '--repository',
109 os.path.join(base_dir, 'repo_1')], 122 os.path.join(base_dir, 'repo_1')],
110 stdout=subprocess.PIPE) 123 stdout=subprocess.PIPE)
111 assert 'Noissue - Updated copyright year' in str(log_1.stdout) 124 assert 'Noissue - Updated copyright year' in str(log_1.stdout)
112 125
113 # assert the .patch file for repo_2 126 # assert the .patch file for repo_2
114 assert'Noissue - Updated copyright year' in open('repo_2.patch').read() 127 assert'Noissue - Updated copyright year' in open('repo_2.patch').read()
115 subprocess.call(['rm', 'repo_2.patch']) # cleanup 128 subprocess.call(['rm', 'repo_2.patch']) # cleanup
129 # subprocess.call(['rm', os.path.join(data_path, 'sample_file.py')])
OLDNEW
« no previous file with comments | « update-copyright/tests/data/sample_file.py ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld