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