Index: tests/test_diff_script.py |
=================================================================== |
--- a/tests/test_diff_script.py |
+++ b/tests/test_diff_script.py |
@@ -20,8 +20,11 @@ |
import pytest |
import subprocess |
import io |
+import os |
+import re |
from test_differ import BASE, LATEST |
+from abp.filters.diff_script import main as diff_script |
@pytest.fixture |
@@ -29,16 +32,34 @@ |
"""Directory with example filter lists.""" |
rootdir = tmpdir.join('root') |
rootdir.mkdir() |
- rootdir.join('base.txt').write_text(BASE, encoding='utf8') |
rootdir.join('latest.txt').write_text(LATEST, encoding='utf8') |
- |
return rootdir |
@pytest.fixture |
-def dstfile(tmpdir): |
- """Destination file for saving the diff output.""" |
- return tmpdir.join('dst') |
+def archive_dir(rootdir): |
+ return rootdir.mkdir('archive') |
+ |
+ |
+@pytest.fixture |
+def diff_dir(rootdir): |
+ return rootdir.mkdir('diff') |
+ |
+ |
+@pytest.fixture |
+def archived_files(archive_dir): |
+ base2 = BASE + '&adnet=\n' |
+ base2 = re.sub(r'! Version: \d+', '! Version: 112', base2) |
+ archive_dir.join('list111.txt').write_text(BASE, encoding='utf8') |
+ archive_dir.join('list112.txt').write_text(base2, encoding='utf8') |
+ return [str(x) for x in archive_dir.listdir()] |
+ |
+ |
+@pytest.fixture |
+def base_no_version(archive_dir): |
+ base = re.sub(r'! Version: \d+', '! ', BASE) |
+ archive_dir.join('list113.txt').write_text(base, encoding='utf8') |
+ return [str(x) for x in archive_dir.listdir()] |
def run_script(*args, **kw): |
@@ -52,34 +73,67 @@ |
return proc.returncode, stderr.decode('utf-8'), stdout.decode('utf-8') |
-def test_diff_with_outfile(rootdir, dstfile): |
- run_script(str(rootdir.join('base.txt')), |
- str(rootdir.join('latest.txt')), |
- str(dstfile)) |
- with io.open(str(dstfile), encoding='utf-8') as dst: |
- result = dst.read() |
- assert '+ &ad_channel=\xa3' in result |
+def test_diff_with_dict(rootdir, archived_files, diff_dir): |
+ diff_script([str(rootdir.join('latest.txt')), '-o', str(diff_dir)] + |
+ archived_files) |
+ for _, _, files in os.walk(str(diff_dir)): |
Vasily Kuznetsov
2018/10/29 23:47:34
I just realized that we don't really need `os.walk
rhowell
2018/10/31 00:20:58
Yeah, this seems to be less code and more efficien
|
+ for file in files: |
+ with io.open(str(diff_dir.join(file)), encoding='utf-8') as dst: |
+ result = dst.read() |
+ assert '- &ad.vid=$~xmlhttprequest' in result |
+ assert '+ &ad_channel=\xa3' in result |
+ assert '! Version: 123' in result |
-def test_no_outfile(rootdir): |
- _, _, out = run_script(str(rootdir.join('base.txt')), |
- str(rootdir.join('latest.txt'))) |
- assert '[Adblock Plus Diff]' in out |
+def test_diff_with_outfile(rootdir, archived_files, diff_dir): |
+ run_script(str(rootdir.join('latest.txt')), '-o', str(diff_dir), |
+ *archived_files) |
+ for _, _, files in os.walk(str(diff_dir)): |
+ assert files |
+ for file in files: |
+ with io.open(str(diff_dir.join(file)), encoding='utf-8') as dst: |
+ result = dst.read() |
+ assert '- &ad.vid=$~xmlhttprequest' in result |
+ assert '+ &ad_channel=\xa3' in result |
+ assert '! Version: 123' in result |
+ |
+ |
+def test_diff_no_outfile(rootdir, archived_files, diff_dir): |
+ run_script(str(rootdir.join('latest.txt')), *archived_files) |
Vasily Kuznetsov
2018/10/29 23:47:34
We need to change to a temporary directory before
rhowell
2018/10/31 00:20:58
Done.
|
+ for file in ['diff111.txt', 'diff112.txt']: |
+ with io.open(file, encoding='utf-8') as dst: |
+ result = dst.read() |
+ assert '- &ad.vid=$~xmlhttprequest' in result |
+ assert '+ &ad_channel=\xa3' in result |
+ assert '! Version: 123' in result |
+ os.remove(file) |
def test_no_base_file(rootdir): |
- code, err, _ = run_script('wrong.txt', str(rootdir.join('latest.txt'))) |
- assert code == 1 |
- assert 'No such file or directory' in err |
+ code, err, _ = run_script(str(rootdir.join('latest.txt'))) |
+ assert code == 2 |
+ assert 'usage: fldiff' in err |
-def test_no_latest_file(rootdir): |
+def test_wrong_file(rootdir): |
code, err, _ = run_script(str(rootdir.join('base.txt')), 'wrong.txt') |
assert code == 1 |
assert 'No such file or directory' in err |
-def test_diff_to_self(rootdir): |
- _, _, out = run_script(str(rootdir.join('latest.txt')), |
- str(rootdir.join('latest.txt'))) |
- assert out == '[Adblock Plus Diff]\n' |
+def test_diff_to_self(rootdir, diff_dir): |
+ run_script(str(rootdir.join('latest.txt')), '-o', str(diff_dir), |
+ str(rootdir.join('latest.txt'))) |
+ for _, _, files in os.walk(str(diff_dir)): |
+ assert files |
+ for file in files: |
+ with io.open(str(diff_dir.join(file)), encoding='utf-8') as dst: |
+ result = dst.read() |
+ assert result == '[Adblock Plus Diff]\n' |
+ |
+ |
+def test_no_version(rootdir, base_no_version): |
+ code, err, _ = run_script(str(rootdir.join('latest.txt')), '-o', |
+ str(diff_dir), *base_no_version) |
+ assert code == 1 |
+ assert 'Unable to find Version in /tmp/' in err |