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

Side by Side Diff: update-copyright/tests/test_update_copyright.py

Issue 29459580: Issue 5250 - Add copyright update script (Closed) Base URL: https://hg.adblockplus.org/codingtools
Patch Set: Created June 8, 2017, 1:39 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
OLDNEW
(Empty)
1 #!/usr/bin/env python3
2
3 import os
4 import re
5 import datetime
6 import subprocess
7 import pytest
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
9
10 from update_copyright.update_copyright import extract_urls
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
15 CURRENT_YEAR = datetime.datetime.now().year
16 local_dir = os.path.dirname(os.path.abspath(__file__))
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')
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)
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')
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/'),
21 os.path.join(full_data_dir + '/repo_2/')]
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.
22
23
24 @pytest.fixture()
25 def temp_repo(tmpdir):
26 # Returns 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')
28 temp_repo = str(temp_dir)
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
37
38
39 @pytest.fixture()
40 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
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)
43 tmp_repo = tmpdir.mkdir('tmp_dir')
44 temp_dir = str(tmp_repo)
45 subprocess.check_call(['cp', os.path.join(data_dir, 'hg_page.html'),
46 temp_dir])
47 repo_1 = temp_dir + '/repo_1'
48 repo_2 = temp_dir + '/repo_2'
49 subprocess.check_call(['mkdir', repo_1])
50 subprocess.check_call(['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'),
52 repo_1])
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
62 # 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:
65 hook = '[hooks]\npretxnchangegroup = return True'
66 hgrc.write(hook)
67 return str(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
69
70 def test_extract_urls():
71 assert url_list == extract_urls(hg_page)
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.
72
73
74 def test_text_replacement(temp_repo):
75 filename = os.path.join(temp_repo, 'sample_file.py')
76 text_replace(temp_repo, filename)
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
77 with open(filename) as file:
78 try:
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',
84 re.I)
85 for year in re.finditer(pattern, text):
86 dates = re.search(r'(\d{4})-(\d{4})', year.group(0))
87 assert dates.group(2) == str(CURRENT_YEAR)
88
89
90 def test_hg_commit(temp_repo):
91 subprocess.check_call(['hg', 'clone', temp_repo, 'temp'])
92 subprocess.check_call(['touch', 'temp/foo'])
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'])
94 hg_commit('temp', temp_repo)
95
96 # Make sure both files contain the commmit message from hg log
97 log_1 = subprocess.run(['hg', 'log', '--repository', temp_repo],
98 stdout=subprocess.PIPE)
99 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
102
103 def test_all(base_dir):
104 main(urllib.parse.urljoin('file://localhost', os.path.join(
105 base_dir, 'hg_page.html')), base_dir)
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
107 # assert hg log for repo_1
108 log_1 = subprocess.run(['hg', 'log', '--repository',
109 os.path.join(base_dir, 'repo_1')],
110 stdout=subprocess.PIPE)
111 assert 'Noissue - Updated copyright year' in str(log_1.stdout)
112
113 # assert the .patch file for repo_2
114 with open(os.path.join(data_dir, 'tst.patch'), 'r') as file1:
115 with open('repo_2.patch') as file2:
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'])
OLDNEW

Powered by Google App Engine
This is Rietveld