| Index: update-copyright/tests/test_update_copyright.py |
| =================================================================== |
| new file mode 100644 |
| --- /dev/null |
| +++ b/update-copyright/tests/test_update_copyright.py |
| @@ -0,0 +1,109 @@ |
| +#!/usr/bin/env python3 |
| + |
| +import os |
| +import re |
| +import datetime |
| +import subprocess |
| +import shutil |
| +import urllib.parse |
| + |
| +import pytest |
| + |
| +from update_copyright import extract_urls, text_replace, hg_commit, main |
| + |
| +data_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'data') |
| + |
| + |
| +def create_repo(path): |
| + subprocess.check_call(['hg', 'init', path]) |
| + with open(os.path.join(path, '.hg', 'hgrc'), 'w+') as hgrc: |
| + set_user = '[ui]\nusername = Test User <test@example.com>' |
| + hgrc.write(set_user) |
| + shutil.copy(os.path.join(data_path, 'sample_file.py'), path) |
| + subprocess.check_call(['hg', 'commit', '-Am', 'Initial commit', |
| + '--repository', path]) |
| + |
| + |
| +@pytest.fixture() |
| +def temp_dir(tmpdir): |
| + temp_dir = tmpdir.mkdir('temp_dir') |
| + return temp_dir |
| + |
| + |
| +@pytest.fixture() |
| +def temp_repo(tmpdir): |
| + """"Returns a path to a temporary repo containing one sample file""" |
| + temp_repo = tmpdir.mkdir('tmp_dir') |
| + create_repo(str(temp_repo)) |
| + return temp_repo |
| + |
| + |
| +@pytest.fixture() |
| +def base_dir(tmpdir): |
| + """Returns a temporary directory that contains one html page and two |
| + repositories (one with push access, the other without)""" |
| + repo = tmpdir.mkdir('tmp_dir') |
| + temp_dir = str(repo) |
| + subprocess.check_call(['cp', os.path.join(data_path, 'hg_page.html'), |
| + temp_dir]) |
| + repo_1 = temp_dir + '/repo_1' |
|
Vasily Kuznetsov
2017/06/27 16:18:51
Any reason why you're not using `os.path.join` her
rosie
2017/07/03 15:33:44
Done.
rosie
2017/07/03 15:33:44
repo_1 and repo_2 were only needed as strings, so
|
| + repo_2 = temp_dir + '/repo_2' |
| + subprocess.check_call(['mkdir', repo_1]) |
|
Vasily Kuznetsov
2017/06/27 16:18:51
You can also use `os.mkdir` for this -- simpler th
rosie
2017/07/03 15:33:43
Done.
rosie
2017/07/03 15:33:44
Switched it to `os.mkdir`. The `create_repo` funct
Vasily Kuznetsov
2017/07/03 19:25:45
It looks like the same pattern is followed in the
|
| + subprocess.check_call(['mkdir', repo_2]) |
| + create_repo(repo_1) |
| + create_repo(repo_2) |
| + |
| + # Make repo_2 read-only |
| + subprocess.check_call(['touch', os.path.join(repo_2, '.hg/hgrc')]) |
|
Vasily Kuznetsov
2017/06/27 16:18:51
Do we actually need to touch `hgrc` before we writ
rosie
2017/07/03 15:33:43
Done.
|
| + with open(os.path.join(repo_2, '.hg/hgrc'), 'w') as hgrc: |
| + hook = '[hooks]\npretxnchangegroup = return True' |
| + hgrc.write(hook) |
| + return repo |
| + |
| + |
| +def test_extract_urls(): |
| + data_url = urllib.parse.urljoin('file:///', data_path) |
| + urls = [os.path.join(data_url + '/repo_1/'), |
|
Vasily Kuznetsov
2017/06/27 16:18:51
Seems like we're passing only one argument to `os.
rosie
2017/07/03 15:33:44
Done.
|
| + os.path.join(data_url + '/repo_2/')] |
| + assert urls == extract_urls(os.path.join(data_url, 'hg_page.html')) |
| + |
| + |
| +def test_text_replacement(temp_repo): |
| + filename = temp_repo.join('sample_file.py').strpath |
| + text_replace(temp_repo.strpath, filename) |
| + with open(filename) as file: |
| + text = file.read() |
| + pattern = re.compile(r'(copyright.*?\d{4})(?:-\d{4})?\s+eyeo gmbh', |
| + re.I) |
| + for year in re.finditer(pattern, text): |
| + dates = re.search(r'(\d{4})-(\d{4})', year.group(0)) |
| + assert dates.group(2) == str(datetime.datetime.now().year) |
| + |
| + |
| +def test_hg_commit(temp_repo, temp_dir): |
| + directory = str(temp_dir) |
| + subprocess.check_call(['hg', 'clone', temp_repo.strpath, directory]) |
|
Vasily Kuznetsov
2017/06/27 16:18:51
You could also assign `temp_repo.strpath` to a var
rosie
2017/07/03 15:33:43
Done.
|
| + open(os.path.join(directory, 'foo'), 'w').close() |
| + subprocess.check_call(['hg', 'add', '--repository', directory]) |
| + hg_commit(directory, temp_repo.strpath) |
| + |
| + # Make sure both files contain the commmit message from hg log |
| + log_1 = subprocess.run(['hg', 'log', '--repository', temp_repo.strpath], |
| + stdout=subprocess.PIPE) |
| + assert 'Noissue - Updated copyright year' in str(log_1.stdout) |
| + |
| + |
| +def test_all(base_dir): |
| + main(urllib.parse.urljoin('file:///', |
| + base_dir.join('hg_page.html').strpath), |
| + base_dir) |
| + |
| + # assert hg log for repo_1 |
| + log_1 = subprocess.run(['hg', 'log', '--repository', |
| + base_dir.join('repo_1').strpath], |
| + stdout=subprocess.PIPE) |
| + assert 'Noissue - Updated copyright year' in str(log_1.stdout) |
| + |
| + # assert the .patch file for repo_2 |
| + assert'Noissue - Updated copyright year' in open('repo_2.patch').read() |
| + subprocess.call(['rm', 'repo_2.patch']) # cleanup |