Skip to content

Commit

Permalink
Create a separate test file for show cmd tests
Browse files Browse the repository at this point in the history
  • Loading branch information
tpwo committed Nov 3, 2023
1 parent ba8e78b commit 34e9153
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 87 deletions.
10 changes: 10 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
from __future__ import annotations

import pytest

from pyzet import constants as C
from pyzet.main import main

TEST_CFG = ('--config', f'testing/{C.CONFIG_FILE}')


@pytest.fixture
def pyzet_init(tmp_path):
init_dir = tmp_path.as_posix()
main([*TEST_CFG, 'init', init_dir])
yield init_dir
87 changes: 0 additions & 87 deletions tests/main_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,6 @@ def set_info_lvl(caplog):
caplog.set_level(logging.INFO)


@pytest.fixture
def pyzet_init(tmp_path):
init_dir = tmp_path.as_posix()
main([*TEST_CFG, 'init', init_dir])
yield init_dir


def test_no_argv(capsys):
# It should just print the usage help
main([])
Expand Down Expand Up @@ -146,86 +139,6 @@ def test_edit_error_missing_repo_in_yaml():
)


def test_show(capsys):
main([*TEST_CFG, 'show', 'text', '20211016205158'])

out, err = capsys.readouterr()
assert out.endswith(
'# Zet test entry\n\nHello there!\n\nTags:\n\n'
' #test-tag #another-tag #tag-after-two-spaces\n'
)
assert err == ''


def test_show_default(capsys):
# by default, the command shows a zettel with the highest ID (the newest)
main([*TEST_CFG, 'show', 'text'])

out, err = capsys.readouterr()
assert out.endswith('# Zettel with UTF-8\n\nZażółć gęślą jaźń.\n')
assert err == ''


def test_show_utf8(capsys):
main([*TEST_CFG, 'show', 'text', '20220101220852'])

out, err = capsys.readouterr()
assert out.endswith('# Zettel with UTF-8\n\nZażółć gęślą jaźń.\n')
assert err == ''


def test_show_mdlink(capsys):
main([*TEST_CFG, 'show', 'mdlink', '20211016205158'])

out, err = capsys.readouterr()
assert out == '* [20211016205158](../20211016205158) Zet test entry\n'
assert err == ''


@pytest.mark.parametrize(
('raw', 'expected'),
(
(
'https://github.com/tpwo/pyzet',
'https://github.com/tpwo/pyzet/tree/main/zettels/20211016205159',
),
(
'https://github.com/tpwo/pyzet.git',
'https://github.com/tpwo/pyzet/tree/main/zettels/20211016205159',
),
(
'[email protected]:tpwo/pyzet',
'https://github.com/tpwo/pyzet/tree/main/zettels/20211016205159',
),
(
'[email protected]:tpwo/pyzet.git',
'https://github.com/tpwo/pyzet/tree/main/zettels/20211016205159',
),
(
'[email protected]:user/repo.git',
'https://gitlab.com/user/repo/-/tree/main/zettels/20211016205159',
),
(
'[email protected]:user/repo.git',
'https://bitbucket.org/user/repo/src/main/zettels/20211016205159',
),
),
)
def test_show_url(raw, expected, pyzet_init, capsys):
subprocess.run(('git', '-C', pyzet_init, 'remote', 'add', 'origin', raw))
id_ = '20211016205159'
test_zettel = Path(pyzet_init, C.ZETDIR, id_)
test_zettel.mkdir(parents=True)
with open(Path(test_zettel, C.ZETTEL_FILENAME), 'w') as file:
file.write('# Test')

main([*TEST_CFG, '--repo', pyzet_init, 'show', 'url', id_])

out, err = capsys.readouterr()
assert out == expected + '\n'
assert err == ''


def test_list(capsys):
main([*TEST_CFG, 'list'])

Expand Down
90 changes: 90 additions & 0 deletions tests/show_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
from __future__ import annotations

import subprocess
from pathlib import Path

import pytest

from pyzet import constants as C
from pyzet.main import main
from tests.conftest import TEST_CFG


def test_show(capsys):
main([*TEST_CFG, 'show', 'text', '20211016205158'])

out, err = capsys.readouterr()
assert out.endswith(
'# Zet test entry\n\nHello there!\n\nTags:\n\n'
' #test-tag #another-tag #tag-after-two-spaces\n'
)
assert err == ''


def test_show_default(capsys):
# by default, the command shows a zettel with the highest ID (the newest)
main([*TEST_CFG, 'show', 'text'])

out, err = capsys.readouterr()
assert out.endswith('# Zettel with UTF-8\n\nZażółć gęślą jaźń.\n')
assert err == ''


def test_show_utf8(capsys):
main([*TEST_CFG, 'show', 'text', '20220101220852'])

out, err = capsys.readouterr()
assert out.endswith('# Zettel with UTF-8\n\nZażółć gęślą jaźń.\n')
assert err == ''


def test_show_mdlink(capsys):
main([*TEST_CFG, 'show', 'mdlink', '20211016205158'])

out, err = capsys.readouterr()
assert out == '* [20211016205158](../20211016205158) Zet test entry\n'
assert err == ''


@pytest.mark.parametrize(
('raw', 'expected'),
(
(
'https://github.com/tpwo/pyzet',
'https://github.com/tpwo/pyzet/tree/main/zettels/20211016205159',
),
(
'https://github.com/tpwo/pyzet.git',
'https://github.com/tpwo/pyzet/tree/main/zettels/20211016205159',
),
(
'[email protected]:tpwo/pyzet',
'https://github.com/tpwo/pyzet/tree/main/zettels/20211016205159',
),
(
'[email protected]:tpwo/pyzet.git',
'https://github.com/tpwo/pyzet/tree/main/zettels/20211016205159',
),
(
'[email protected]:user/repo.git',
'https://gitlab.com/user/repo/-/tree/main/zettels/20211016205159',
),
(
'[email protected]:user/repo.git',
'https://bitbucket.org/user/repo/src/main/zettels/20211016205159',
),
),
)
def test_show_url(raw, expected, pyzet_init, capsys):
subprocess.run(('git', '-C', pyzet_init, 'remote', 'add', 'origin', raw))
id_ = '20211016205159'
test_zettel = Path(pyzet_init, C.ZETDIR, id_)
test_zettel.mkdir(parents=True)
with open(Path(test_zettel, C.ZETTEL_FILENAME), 'w') as file:
file.write('# Test')

main([*TEST_CFG, '--repo', pyzet_init, 'show', 'url', id_])

out, err = capsys.readouterr()
assert out == expected + '\n'
assert err == ''

0 comments on commit 34e9153

Please sign in to comment.