2020-08-10 14:02:40 +00:00
|
|
|
"""test_extract_odf_links.py - Unit tests for ODF link extraction"""
|
|
|
|
# Copyright © 2020 Brett Smith
|
|
|
|
#
|
|
|
|
# This program is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU Affero General Public License as published by
|
|
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU Affero General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU Affero General Public License
|
|
|
|
# along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
import io
|
2020-08-30 14:03:57 +00:00
|
|
|
import sys
|
2020-08-10 14:02:40 +00:00
|
|
|
|
|
|
|
import pytest
|
|
|
|
|
2020-08-30 13:18:41 +00:00
|
|
|
from pathlib import Path
|
|
|
|
|
2020-08-10 14:02:40 +00:00
|
|
|
from . import testutil
|
|
|
|
|
|
|
|
from conservancy_beancount.tools import extract_odf_links
|
|
|
|
|
|
|
|
SRC_PATH = testutil.test_path('repository/LinksReport.ods')
|
2020-09-03 13:45:31 +00:00
|
|
|
BAD_PATH_S = str(testutil.test_path('repository/Projects/Bad Link.txt'))
|
2020-08-10 14:02:40 +00:00
|
|
|
|
2020-08-30 13:18:41 +00:00
|
|
|
INCLUDED_FILE_LINKS = {
|
|
|
|
Path('/repository/Projects/project-data.yml'),
|
|
|
|
Path('Projects/project-data.yml'),
|
|
|
|
Path('Projects/Bad Link.txt'),
|
2020-08-10 14:02:40 +00:00
|
|
|
}
|
|
|
|
|
2020-08-30 13:18:41 +00:00
|
|
|
def expected_links(rel_path):
|
|
|
|
return frozenset(
|
|
|
|
str(path if path.is_absolute() else rel_path / path)
|
|
|
|
for path in INCLUDED_FILE_LINKS
|
|
|
|
)
|
|
|
|
|
2020-08-30 14:03:57 +00:00
|
|
|
def check_output(stdout, sep, rel_path):
|
|
|
|
actual = stdout.getvalue().split(sep)
|
|
|
|
if actual and not actual[-1]:
|
|
|
|
actual.pop()
|
|
|
|
expected = expected_links(rel_path)
|
|
|
|
assert len(actual) == len(expected)
|
|
|
|
assert set(actual) == expected
|
|
|
|
|
2020-08-10 14:02:40 +00:00
|
|
|
@pytest.mark.parametrize('arglist,sep', [
|
|
|
|
(['-0'], '\0'),
|
|
|
|
(['-d', '\\v'], '\v'),
|
|
|
|
([str(SRC_PATH)], '\n'), # Test that links aren't duplicated
|
|
|
|
])
|
|
|
|
def test_extract_file_links(arglist, sep, caplog):
|
|
|
|
arglist.append(str(SRC_PATH))
|
|
|
|
stdout = io.StringIO()
|
|
|
|
stderr = io.StringIO()
|
|
|
|
exitcode = extract_odf_links.main(arglist, stdout, stderr)
|
|
|
|
assert exitcode == 0
|
|
|
|
assert not stderr.getvalue()
|
2020-08-30 14:03:57 +00:00
|
|
|
check_output(stdout, sep, SRC_PATH.parent)
|
2020-08-10 14:02:40 +00:00
|
|
|
assert caplog.records
|
|
|
|
assert any(
|
|
|
|
log.levelname == 'WARNING'
|
2020-09-03 13:45:31 +00:00
|
|
|
and log.message.startswith('link path not found: ')
|
|
|
|
and log.message.endswith(BAD_PATH_S)
|
2020-08-10 14:02:40 +00:00
|
|
|
for log in caplog.records
|
|
|
|
)
|
2020-08-30 14:03:57 +00:00
|
|
|
|
|
|
|
@pytest.mark.parametrize('rel_path', [
|
|
|
|
Path('/run'),
|
|
|
|
Path('/tmp'),
|
|
|
|
])
|
|
|
|
def test_extract_relative_to(rel_path):
|
|
|
|
arglist = ['--relative', str(rel_path), '-0', '-']
|
|
|
|
stdout = io.StringIO()
|
|
|
|
stderr = io.StringIO()
|
|
|
|
orig_stdin = sys.stdin
|
|
|
|
try:
|
|
|
|
sys.stdin = SRC_PATH.open('rb')
|
|
|
|
exitcode = extract_odf_links.main(arglist, stdout, stderr)
|
|
|
|
finally:
|
|
|
|
sys.stdin = orig_stdin
|
|
|
|
assert exitcode == 0
|
|
|
|
assert not stderr.getvalue()
|
|
|
|
check_output(stdout, '\0', rel_path)
|
|
|
|
|
|
|
|
def test_reading_stdin_requires_relative_to():
|
|
|
|
with pytest.raises(SystemExit) as exc_check:
|
|
|
|
extract_odf_links.main(['-'])
|
|
|
|
assert exc_check.value.args[0] == 2
|