| Left: | ||
| Right: |
| OLD | NEW |
|---|---|
| (Empty) | |
| 1 #!/usr/bin/env python3 | |
| 2 | |
| 3 import os | |
| 4 import re | |
| 5 import datetime | |
| 6 import subprocess | |
| 7 import shutil | |
| 8 import urllib.parse | |
| 9 | |
| 10 import pytest | |
| 11 | |
| 12 from update_copyright import extract_urls, text_replace, hg_commit, main | |
| 13 | |
| 14 data_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'data') | |
| 15 | |
| 16 | |
| 17 def create_repo(path): | |
| 18 subprocess.check_call(['hg', 'init', path]) | |
| 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 | |
| 31 | |
| 32 | |
| 33 @pytest.fixture() | |
| 34 def temp_repo(tmpdir): | |
| 35 """"Returns a path to a temporary repo containing one sample file""" | |
| 36 temp_repo = tmpdir.mkdir('tmp_dir') | |
| 37 create_repo(str(temp_repo)) | |
| 38 return temp_repo | |
| 39 | |
| 40 | |
| 41 @pytest.fixture() | |
| 42 def base_dir(tmpdir): | |
| 43 """Returns a temporary directory that contains one html page and two | |
| 44 repositories (one with push access, the other without)""" | |
| 45 repo = tmpdir.mkdir('tmp_dir') | |
| 46 temp_dir = str(repo) | |
| 47 subprocess.check_call(['cp', os.path.join(data_path, 'hg_page.html'), | |
| 48 temp_dir]) | |
| 49 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
| |
| 50 repo_2 = temp_dir + '/repo_2' | |
| 51 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
| |
| 52 subprocess.check_call(['mkdir', repo_2]) | |
| 53 create_repo(repo_1) | |
| 54 create_repo(repo_2) | |
| 55 | |
| 56 # Make repo_2 read-only | |
| 57 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.
| |
| 58 with open(os.path.join(repo_2, '.hg/hgrc'), 'w') as hgrc: | |
| 59 hook = '[hooks]\npretxnchangegroup = return True' | |
| 60 hgrc.write(hook) | |
| 61 return repo | |
| 62 | |
| 63 | |
| 64 def test_extract_urls(): | |
| 65 data_url = urllib.parse.urljoin('file:///', data_path) | |
| 66 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.
| |
| 67 os.path.join(data_url + '/repo_2/')] | |
| 68 assert urls == extract_urls(os.path.join(data_url, 'hg_page.html')) | |
| 69 | |
| 70 | |
| 71 def test_text_replacement(temp_repo): | |
| 72 filename = temp_repo.join('sample_file.py').strpath | |
| 73 text_replace(temp_repo.strpath, filename) | |
| 74 with open(filename) as file: | |
| 75 text = file.read() | |
| 76 pattern = re.compile(r'(copyright.*?\d{4})(?:-\d{4})?\s+eyeo gmbh', | |
| 77 re.I) | |
| 78 for year in re.finditer(pattern, text): | |
| 79 dates = re.search(r'(\d{4})-(\d{4})', year.group(0)) | |
| 80 assert dates.group(2) == str(datetime.datetime.now().year) | |
| 81 | |
| 82 | |
| 83 def test_hg_commit(temp_repo, temp_dir): | |
| 84 directory = str(temp_dir) | |
| 85 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.
| |
| 86 open(os.path.join(directory, 'foo'), 'w').close() | |
| 87 subprocess.check_call(['hg', 'add', '--repository', directory]) | |
| 88 hg_commit(directory, temp_repo.strpath) | |
| 89 | |
| 90 # Make sure both files contain the commmit message from hg log | |
| 91 log_1 = subprocess.run(['hg', 'log', '--repository', temp_repo.strpath], | |
| 92 stdout=subprocess.PIPE) | |
| 93 assert 'Noissue - Updated copyright year' in str(log_1.stdout) | |
| 94 | |
| 95 | |
| 96 def test_all(base_dir): | |
| 97 main(urllib.parse.urljoin('file:///', | |
| 98 base_dir.join('hg_page.html').strpath), | |
| 99 base_dir) | |
| 100 | |
| 101 # assert hg log for repo_1 | |
| 102 log_1 = subprocess.run(['hg', 'log', '--repository', | |
| 103 base_dir.join('repo_1').strpath], | |
| 104 stdout=subprocess.PIPE) | |
| 105 assert 'Noissue - Updated copyright year' in str(log_1.stdout) | |
| 106 | |
| 107 # assert the .patch file for repo_2 | |
| 108 assert'Noissue - Updated copyright year' in open('repo_2.patch').read() | |
| 109 subprocess.call(['rm', 'repo_2.patch']) # cleanup | |
| OLD | NEW |