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: Created June 8, 2017, 1:39 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 pytest 7 import shutil
Vasily Kuznetsov 2017/06/19 16:25:30 PEP8 suggests that the order of imports should be
rosie 2017/06/23 14:39:24 Done.
8 import urllib.parse 8 import urllib.parse
9 9
10 from update_copyright.update_copyright import extract_urls 10 import pytest
Vasily Kuznetsov 2017/06/19 16:25:30 According to PEP8, it would be ok to put all impor
rosie 2017/06/23 14:39:21 Done.
11 from update_copyright.update_copyright import text_replace
12 from update_copyright.update_copyright import hg_commit
13 from update_copyright.update_copyright import main
14 11
15 CURRENT_YEAR = datetime.datetime.now().year 12 from update_copyright import extract_urls, text_replace, hg_commit, main
16 local_dir = os.path.dirname(os.path.abspath(__file__)) 13
Vasily Kuznetsov 2017/06/19 16:25:30 This is the path to the directory that contains th
rosie 2017/06/23 14:39:21 Done.
rosie 2017/06/23 14:39:21 Good idea. I would change it, but this variable en
17 data_dir = os.path.join(local_dir, 'data') 14 data_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'data')
Vasily Kuznetsov 2017/06/19 16:25:29 Then this would be `data_path`.
rosie 2017/06/23 14:39:21 Done.
18 full_data_dir = urllib.parse.urljoin('file://localhost', data_dir) 15
Vasily Kuznetsov 2017/06/19 16:25:30 This one is a URL of the data directory, so we can
rosie 2017/06/23 14:39:24 Done.
19 hg_page = os.path.join(full_data_dir, 'hg_page.html') 16
Vasily Kuznetsov 2017/06/19 16:25:29 This one would be `hg_page_url` then.
rosie 2017/06/23 14:39:23 Ended up not needing this variable either
rosie 2017/06/23 14:39:23 Done.
20 url_list = [os.path.join(full_data_dir + '/repo_1/'), 17 def create_repo(path):
21 os.path.join(full_data_dir + '/repo_2/')] 18 subprocess.check_call(['hg', 'init', path])
Jon Sonesen 2017/06/13 16:30:20 I don't think all these constants are required, su
rosie 2017/06/23 14:39:22 Done.
19 with open(os.path.join(path, '.hg', 'hgrc'), 'w+') as hgrc:
20 set_user = '[ui]\nusername = Test User <test@example.com>'
21 hgrc.write(set_user)
22 shutil.copy(os.path.join(data_path, 'sample_file.py'), path)
23 subprocess.check_call(['hg', 'commit', '-Am', 'Initial commit',
24 '--repository', path])
25
26
27 @pytest.fixture()
28 def temp_dir(tmpdir):
29 temp_dir = tmpdir.mkdir('temp_dir')
30 return temp_dir
22 31
23 32
24 @pytest.fixture() 33 @pytest.fixture()
25 def temp_repo(tmpdir): 34 def temp_repo(tmpdir):
26 # Returns a temporary repo containing one sample file 35 """"Returns a path to a temporary repo containing one sample file"""
Vasily Kuznetsov 2017/06/19 16:25:29 This should be a docstring. Normally docstrings de
rosie 2017/06/23 14:39:24 Done.
27 temp_dir = tmpdir.mkdir('tmp_dir') 36 temp_repo = tmpdir.mkdir('tmp_dir')
28 temp_repo = str(temp_dir) 37 create_repo(str(temp_repo))
29 subprocess.check_call(['hg', 'init', temp_repo])
30 subprocess.check_call(['cp', os.path.join(data_dir, 'sample_file.py'),
Vasily Kuznetsov 2017/06/19 16:25:30 It's better (faster) to use `shutil.copy` (see htt
rosie 2017/06/23 14:39:23 Done.
31 temp_repo])
32 subprocess.check_call(['hg', 'add', os.path.join(temp_repo,
Vasily Kuznetsov 2017/06/19 16:25:30 You can combine this with the next call of `hg` by
rosie 2017/06/23 14:39:22 Done.
33 'sample_file.py'), '--repository', temp_repo])
34 subprocess.check_call(['hg', 'commit', '-m', 'Initial commit',
35 '--repository', temp_repo])
36 return temp_repo 38 return temp_repo
37 39
38 40
39 @pytest.fixture() 41 @pytest.fixture()
40 def base_dir(tmpdir): 42 def base_dir(tmpdir):
Vasily Kuznetsov 2017/06/19 16:25:29 As we've discussed, it would be better to refactor
rosie 2017/06/23 14:39:23 Done.
41 # Returns a temporary directory that contains one html page and two 43 """Returns a temporary directory that contains one html page and two
Vasily Kuznetsov 2017/06/19 16:25:30 This should also be a docstring.
rosie 2017/06/23 14:39:24 Done.
42 # repositories (one with push access, the other without) 44 repositories (one with push access, the other without)"""
43 tmp_repo = tmpdir.mkdir('tmp_dir') 45 tmp_repo = tmpdir.mkdir('tmp_dir')
44 temp_dir = str(tmp_repo) 46 temp_dir = str(tmp_repo)
45 subprocess.check_call(['cp', os.path.join(data_dir, 'hg_page.html'), 47 subprocess.check_call(['cp', os.path.join(data_path, 'hg_page.html'),
46 temp_dir]) 48 temp_dir])
47 repo_1 = temp_dir + '/repo_1' 49 repo_1 = os.path.join(temp_dir, 'repo_1')
48 repo_2 = temp_dir + '/repo_2' 50 repo_2 = os.path.join(temp_dir, 'repo_2')
49 subprocess.check_call(['mkdir', repo_1]) 51 os.mkdir(repo_1)
50 subprocess.check_call(['mkdir', repo_2]) 52 os.mkdir(repo_2)
Jon Sonesen 2017/06/13 16:30:19 you can avoid the use of temporary variables here
rosie 2017/06/23 14:39:24 Setting up the temporary directory base_dir like t
51 subprocess.check_call(['cp', os.path.join(data_dir, 'sample_file.py'), 53 create_repo(repo_1)
52 repo_1]) 54 create_repo(repo_2)
53 subprocess.check_call(['cp', os.path.join(data_dir, 'sample_file.py'),
54 repo_2])
55 subprocess.check_call(['hg', 'init', repo_1])
56 subprocess.check_call(['hg', 'init', repo_2])
57 subprocess.check_call(['hg', 'commit', '-Am', '"Initial commit"',
58 '--repository', repo_1])
59 subprocess.check_call(['hg', 'commit', '-Am', '"Initial commit"',
60 '--repository', repo_2])
61 55
62 # Make repo_2 read-only 56 # Make repo_2 read-only
63 subprocess.check_call(['touch', os.path.join(repo_2, '.hg/hgrc')])
64 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:
65 hook = '[hooks]\npretxnchangegroup = return True' 58 hook = '[hooks]\npretxnchangegroup = return True'
66 hgrc.write(hook) 59 hgrc.write(hook)
67 return str(temp_dir) 60 return temp_dir
Jon Sonesen 2017/06/13 16:30:20 returning the actual path object may be preferable
rosie 2017/06/23 14:39:22 Done.
68 61
69 62
70 def test_extract_urls(): 63 def test_extract_urls():
71 assert url_list == extract_urls(hg_page) 64 data_url = urllib.parse.urljoin('file:///', data_path)
Jon Sonesen 2017/06/13 16:30:19 here is where you would just hand code the url lis
rosie 2017/06/23 14:39:22 Done.
65 urls = [data_url + '/repo_1/',
66 data_url + '/repo_2/']
67 assert urls == extract_urls(os.path.join(data_url, 'hg_page.html'))
72 68
73 69
74 def test_text_replacement(temp_repo): 70 def test_text_replacement(temp_repo):
75 filename = os.path.join(temp_repo, 'sample_file.py') 71 updated = 0
76 text_replace(temp_repo, filename) 72 filename = temp_repo.join('sample_file.py').strpath
Jon Sonesen 2017/06/13 16:30:19 Have you tested that this is working? it seems tha
rosie 2017/06/23 14:39:23 In the temp_repo fixture, 'sample_file.py' is copi
73 text_replace(temp_repo.strpath, filename)
77 with open(filename) as file: 74 with open(filename) as file:
78 try: 75 text = file.read()
79 text = file.read()
80 except UnicodeDecodeError:
Vasily Kuznetsov 2017/06/19 16:25:30 I think you shouldn't catch this. The test should
rosie 2017/06/23 14:39:22 Done.
81 print("Error: Couldn't read {}{}".format(temp_repo, filename))
82 return
83 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',
84 re.I) 77 re.I)
85 for year in re.finditer(pattern, text): 78 for year in re.finditer(pattern, text):
86 dates = re.search(r'(\d{4})-(\d{4})', year.group(0)) 79 dates = re.search(r'(\d{4})-(\d{4})', year.group(0))
87 assert dates.group(2) == str(CURRENT_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
88 87
89 88
90 def test_hg_commit(temp_repo): 89 def test_hg_commit(temp_repo, temp_dir):
91 subprocess.check_call(['hg', 'clone', temp_repo, 'temp']) 90 directory = str(temp_dir)
92 subprocess.check_call(['touch', 'temp/foo']) 91 repo = str(temp_repo)
Jon Sonesen 2017/06/13 16:30:19 you will want to use path.join for the temp/foo he
Vasily Kuznetsov 2017/06/19 16:25:30 And it's probably better to use something like `op
rosie 2017/06/23 14:39:23 Done.
93 subprocess.check_call(['hg', 'add', '--repository', 'temp']) 92 subprocess.check_call(['hg', 'clone', repo, directory])
94 hg_commit('temp', temp_repo) 93 open(os.path.join(directory, 'foo'), 'w').close()
94 subprocess.check_call(['hg', 'add', '--repository', directory])
95 hg_commit(directory, repo)
95 96
96 # Make sure both files contain the commmit message from hg log 97 # Make sure both files contain the commmit message from hg log
97 log_1 = subprocess.run(['hg', 'log', '--repository', temp_repo], 98 log_1 = subprocess.run(['hg', 'log', '--repository', repo],
98 stdout=subprocess.PIPE) 99 stdout=subprocess.PIPE)
99 assert 'Noissue - Updated copyright year' in str(log_1.stdout) 100 assert 'Noissue - Updated copyright year' in str(log_1.stdout)
100 subprocess.call(['rm', '-r', 'temp/']) # cleanup
Vasily Kuznetsov 2017/06/19 16:25:30 It would be better to use `shutil.rmtree` instead.
rosie 2017/06/23 14:39:22 Done.
101 101
102 102
103 def test_all(base_dir): 103 def test_all(base_dir):
104 main(urllib.parse.urljoin('file://localhost', os.path.join( 104 main(urllib.parse.urljoin('file:///', os.path.join(
105 base_dir, 'hg_page.html')), base_dir) 105 base_dir, 'hg_page.html')), None)
Jon Sonesen 2017/06/13 16:30:20 localhost isnt needed here, it is causing the test
rosie 2017/06/23 14:39:21 Looks like I should have used 'file:///' instead o
rosie 2017/06/23 14:39:24 Done.
106 106
107 # assert hg log for repo_1 107 # assert hg log for repo_1
108 log_1 = subprocess.run(['hg', 'log', '--repository', 108 log_1 = subprocess.run(['hg', 'log', '--repository',
109 os.path.join(base_dir, 'repo_1')], 109 os.path.join(base_dir, 'repo_1')],
110 stdout=subprocess.PIPE) 110 stdout=subprocess.PIPE)
111 assert 'Noissue - Updated copyright year' in str(log_1.stdout) 111 assert 'Noissue - Updated copyright year' in str(log_1.stdout)
112 112
113 # assert the .patch file for repo_2 113 # assert the .patch file for repo_2
114 with open(os.path.join(data_dir, 'tst.patch'), 'r') as file1: 114 assert'Noissue - Updated copyright year' in open('repo_2.patch').read()
115 with open('repo_2.patch') as file2: 115 subprocess.call(['rm', 'repo_2.patch']) # cleanup
116 same = set(file1).intersection(file2)
Vasily Kuznetsov 2017/06/19 16:25:29 This seems unnecessarily complicated. Perhaps you
rosie 2017/06/23 14:39:21 Done.
117 assert 'Noissue - Updated copyright year\n' in same
118 subprocess.call(['rm', 'repo_2.patch'])
LEFTRIGHT

Powered by Google App Engine
This is Rietveld