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

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

Issue 29459580: Issue 5250 - Add copyright update script (Closed) Base URL: https://hg.adblockplus.org/codingtools
Left Patch Set: Addressed more comments Created July 3, 2017, 3:31 p.m.
Right Patch Set: Fix indentation and default args Created July 17, 2017, 1:22 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') | update-copyright/tox.ini » ('j') | 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 def create_repo(path): 17 def create_repo(path):
18 print('19: Creating repo in: ', path)
Vasily Kuznetsov 2017/07/03 19:25:46 This looks like a debug print. It won't be visible
rosie 2017/07/04 13:38:25 Done.
19 subprocess.check_call(['hg', 'init', path]) 18 subprocess.check_call(['hg', 'init', path])
20 with open(os.path.join(path, '.hg', 'hgrc'), 'w+') as hgrc: 19 with open(os.path.join(path, '.hg', 'hgrc'), 'w+') as hgrc:
21 set_user = '[ui]\nusername = Test User <test@example.com>' 20 set_user = '[ui]\nusername = Test User <test@example.com>'
22 hgrc.write(set_user) 21 hgrc.write(set_user)
23 shutil.copy(os.path.join(data_path, 'sample_file.py'), path) 22 shutil.copy(os.path.join(data_path, 'sample_file.py'), path)
24 subprocess.check_call(['hg', 'commit', '-Am', 'Initial commit', 23 subprocess.check_call(['hg', 'commit', '-Am', 'Initial commit',
25 '--repository', path]) 24 '--repository', path])
26 25
27 26
28 @pytest.fixture() 27 @pytest.fixture()
(...skipping 11 matching lines...) Expand all
40 39
41 40
42 @pytest.fixture() 41 @pytest.fixture()
43 def base_dir(tmpdir): 42 def base_dir(tmpdir):
44 """Returns a temporary directory that contains one html page and two 43 """Returns a temporary directory that contains one html page and two
45 repositories (one with push access, the other without)""" 44 repositories (one with push access, the other without)"""
46 tmp_repo = tmpdir.mkdir('tmp_dir') 45 tmp_repo = tmpdir.mkdir('tmp_dir')
47 temp_dir = str(tmp_repo) 46 temp_dir = str(tmp_repo)
48 subprocess.check_call(['cp', os.path.join(data_path, 'hg_page.html'), 47 subprocess.check_call(['cp', os.path.join(data_path, 'hg_page.html'),
49 temp_dir]) 48 temp_dir])
50 repo_1 = str(os.path.join(temp_dir, 'repo_1')) 49 repo_1 = os.path.join(temp_dir, 'repo_1')
Vasily Kuznetsov 2017/07/03 19:25:46 Do you need the `str(...)` here? `os.path.join` re
rosie 2017/07/04 13:38:25 Done.
51 repo_2 = str(os.path.join(temp_dir, 'repo_2')) 50 repo_2 = os.path.join(temp_dir, 'repo_2')
52 os.mkdir(repo_1) 51 os.mkdir(repo_1)
53 os.mkdir(repo_2) 52 os.mkdir(repo_2)
54 create_repo(repo_1) 53 create_repo(repo_1)
55 create_repo(repo_2) 54 create_repo(repo_2)
56 55
57 # Make repo_2 read-only 56 # Make repo_2 read-only
58 with open(os.path.join(repo_2, '.hg/hgrc'), 'w') as hgrc: 57 with open(os.path.join(repo_2, '.hg/hgrc'), 'w') as hgrc:
59 hook = '[hooks]\npretxnchangegroup = return True' 58 hook = '[hooks]\npretxnchangegroup = return True'
60 hgrc.write(hook) 59 hgrc.write(hook)
61 return temp_dir 60 return temp_dir
62 61
63 62
64 def test_extract_urls(): 63 def test_extract_urls():
65 print('65: test_extract_urls')
Vasily Kuznetsov 2017/07/03 19:25:46 Also debug print ;)
rosie 2017/07/04 13:38:25 Done.
66 data_url = urllib.parse.urljoin('file:///', data_path) 64 data_url = urllib.parse.urljoin('file:///', data_path)
67 urls = [data_url + '/repo_1/', 65 urls = [data_url + '/repo_1/',
68 data_url + '/repo_2/'] 66 data_url + '/repo_2/']
69 assert urls == extract_urls(os.path.join(data_url, 'hg_page.html')) 67 assert urls == extract_urls(os.path.join(data_url, 'hg_page.html'))
70 68
71 69
72 def test_text_replacement(temp_repo): 70 def test_text_replacement(temp_repo):
73 print('73: test_text_replacement') 71 updated = 0
74 filename = temp_repo.join('sample_file.py').strpath 72 filename = temp_repo.join('sample_file.py').strpath
75 text_replace(temp_repo.strpath, filename) 73 text_replace(temp_repo.strpath, filename)
76 with open(filename) as file: 74 with open(filename) as file:
77 text = file.read() 75 text = file.read()
78 pattern = re.compile(r'(copyright.*?\d{4})(?:-\d{4})?\s+eyeo gmbh', 76 pattern = re.compile(r'(copyright.*?\d{4})(?:-\d{4})?\s+eyeo gmbh',
79 re.I) 77 re.I)
80 for year in re.finditer(pattern, text): 78 for year in re.finditer(pattern, text):
81 dates = re.search(r'(\d{4})-(\d{4})', year.group(0)) 79 dates = re.search(r'(\d{4})-(\d{4})', year.group(0))
82 assert dates.group(2) == str(datetime.datetime.now().year) 80 if dates.group(2) == str(datetime.datetime.now().year):
81 updated += 1
82
83 # test that non-eyeo copyright information are left alone
84 assert '2014 example' in text
85 # test for copyright information in both strings and comments
86 assert updated == 2
83 87
84 88
85 def test_hg_commit(temp_repo, temp_dir): 89 def test_hg_commit(temp_repo, temp_dir):
86 print('86: test_hg_commit')
87 directory = str(temp_dir) 90 directory = str(temp_dir)
88 repo = str(temp_repo) 91 repo = str(temp_repo)
89 subprocess.check_call(['hg', 'clone', repo, directory]) 92 subprocess.check_call(['hg', 'clone', repo, directory])
90 open(os.path.join(directory, 'foo'), 'w').close() 93 open(os.path.join(directory, 'foo'), 'w').close()
91 subprocess.check_call(['hg', 'add', '--repository', directory]) 94 subprocess.check_call(['hg', 'add', '--repository', directory])
92 hg_commit(directory, repo) 95 hg_commit(directory, repo)
93 96
94 # Make sure both files contain the commmit message from hg log 97 # Make sure both files contain the commmit message from hg log
95 log_1 = subprocess.run(['hg', 'log', '--repository', repo], 98 log_1 = subprocess.run(['hg', 'log', '--repository', repo],
96 stdout=subprocess.PIPE) 99 stdout=subprocess.PIPE)
97 assert 'Noissue - Updated copyright year' in str(log_1.stdout) 100 assert 'Noissue - Updated copyright year' in str(log_1.stdout)
98 101
99 102
100 def test_all(base_dir): 103 def test_all(base_dir):
101 print('101: test_all')
102 main(urllib.parse.urljoin('file:///', os.path.join( 104 main(urllib.parse.urljoin('file:///', os.path.join(
103 base_dir, 'hg_page.html')), None) 105 base_dir, 'hg_page.html')), None)
104 106
105 # assert hg log for repo_1 107 # assert hg log for repo_1
106 log_1 = subprocess.run(['hg', 'log', '--repository', 108 log_1 = subprocess.run(['hg', 'log', '--repository',
107 os.path.join(base_dir, 'repo_1')], 109 os.path.join(base_dir, 'repo_1')],
108 stdout=subprocess.PIPE) 110 stdout=subprocess.PIPE)
109 assert 'Noissue - Updated copyright year' in str(log_1.stdout) 111 assert 'Noissue - Updated copyright year' in str(log_1.stdout)
110 112
111 # assert the .patch file for repo_2 113 # assert the .patch file for repo_2
112 assert'Noissue - Updated copyright year' in open('repo_2.patch').read() 114 assert'Noissue - Updated copyright year' in open('repo_2.patch').read()
113 subprocess.call(['rm', 'repo_2.patch']) # cleanup 115 subprocess.call(['rm', 'repo_2.patch']) # cleanup
LEFTRIGHT

Powered by Google App Engine
This is Rietveld