nncli

NextCloud Notes Command Line Interface
git clone git://git.danielmoch.com/nncli.git
Log | Files | Refs | LICENSE

commit 4e6123d781e6443f2531da57facf3c3bc2edf06e
parent 35bdd7b5fa644d2e02df7ab22ed13a5796b3898f
Author: Daniel Moch <daniel@danielmoch.com>
Date:   Sun, 12 Aug 2018 19:43:36 -0400

Add pytest support, tests for config.py

Diffstat:
M.gitignore | 2++
Mnnotes_cli/config.py | 1+
Arequirements-test.txt | 3+++
Asetup.cfg | 12++++++++++++
Msetup.py | 5+++--
Atests/test_config.py | 106+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Atests/test_version.py | 32++++++++++++++++++++++++++++++++
7 files changed, 159 insertions(+), 2 deletions(-)

diff --git a/.gitignore b/.gitignore @@ -3,3 +3,5 @@ dist/ MANIFEST *.egg-info/ nnotes_cli/version.py +.coverage +.pytest_cache/ diff --git a/nnotes_cli/config.py b/nnotes_cli/config.py @@ -55,6 +55,7 @@ def __init__(self, custom_file=None): { 'cfg_nn_username' : '', 'cfg_nn_password' : '', + 'cfg_nn_password_eval' : '', 'cfg_db_path' : self.cache_home, 'cfg_search_categories' : 'yes', # with regex searches 'cfg_sort_mode' : 'date', # 'alpha' or 'date' diff --git a/requirements-test.txt b/requirements-test.txt @@ -0,0 +1,3 @@ +pytest +pytest-cov +pytest-runner diff --git a/setup.cfg b/setup.cfg @@ -0,0 +1,12 @@ +[aliases] +test=pytest + +[tool:pytest] +testpaths = tests +addopts = --cov=nnotes_cli --cov-report=term-missing + +[yapf] +coalesce_brackets = true +dedent_closing_brackets = true +space_between_ending_comma_and_closing_bracket = false +split_arguments_when_comma_terminated = true diff --git a/setup.py b/setup.py @@ -1,4 +1,3 @@ -#!/usr/bin/env python3 # # The MIT License (MIT) # @@ -29,7 +28,8 @@ from setuptools import setup import nnotes_cli -deps = ['urwid', 'requests'] +deps = open('requirements.txt').readlines() +test_deps = open('requirements-test.txt').readlines() with open("README.md", "r") as fh: long_description = fh.read() @@ -45,6 +45,7 @@ license=nnotes_cli.__license__, requires=deps, install_requires=deps, + tests_require=test_deps, use_scm_version= {'write_to': 'nnotes_cli/version.py'}, setup_requires=['setuptools_scm'], packages=['nnotes_cli'], diff --git a/tests/test_config.py b/tests/test_config.py @@ -0,0 +1,106 @@ +# +# The MIT License (MIT) +# +# Copyright (c) 2018 Daniel Moch +# +# Permission is hereby granted, free of charge, to any person obtaining a copy +# of this software and associated documentation files (the "Software"), to deal +# in the Software without restriction, including without limitation the rights +# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +# copies of the Software, and to permit persons to whom the Software is +# furnished to do so, subject to the following conditions: +# +# The above copyright notice and this permission notice shall be included in all +# copies or substantial portions of the Software. +# +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +# SOFTWARE. +# +import os + +from nnotes_cli.config import Config +from pytest import raises + +def test_init(): + config = Config() + + assert config.home == os.path.abspath(os.path.expanduser('~')) + + if 'XDG_CONFIG_HOME' in os.environ: + del(os.environ['XDG_CONFIG_HOME']) + else: + os.environ['XDG_CONFIG_HOME'] = 'foo' + + config = Config() + + assert config.home == os.path.abspath(os.path.expanduser('~')) + + if 'XDG_CACHE_HOME' in os.environ: + del(os.environ['XDG_CACHE_HOME']) + else: + os.environ['XDG_CACHE_HOME'] = 'bar' + + config = Config() + + assert config.home == os.path.abspath(os.path.expanduser('~')) + +def test_custom_file(): + with open('test_cfg', 'w') as config_file: + config_file.write('[nncli]\n') + config_file.write('cfg_nn_username=user\n') + config_file.write('cfg_nn_password=password\n') + config_file.write('cfg_nn_host=nextcloud.example.org\n') + + config = Config('test_cfg') + os.remove('test_cfg') + +def test_bad_password_eval(): + with open('test_cfg', 'w') as config_file: + config_file.write('[nncli]\n') + config_file.write('cfg_nn_username=user\n') + config_file.write('cfg_nn_password_eval=password\n') + config_file.write('cfg_nn_host=nextcloud.example.org\n') + + with raises(SystemExit): + config = Config('test_cfg') + os.remove('test_cfg') + +def test_empty_config(): + with open('test_cfg', 'w') as config_file: + config_file.write('\n') + + config = Config('test_cfg') + os.remove('test_cfg') + +def test_get_config(): + config = Config('test_cfg') + assert config.get_config('sort_mode') == 'date' + +def test_get_config_descr(): + config = Config('test_cfg') + assert config.get_config_descr('sort_mode') == 'Sort mode' + +def test_get_keybind(): + config = Config('test_cfg') + assert config.get_keybind('help') == 'h' + +def test_get_keybind_use(): + config = Config('test_cfg') + assert config.get_keybind_use('help') == [ 'common' ] + +def test_get_keybind_descr(): + config = Config('test_cfg') + assert config.get_keybind_descr('help') == 'Help' + +def test_get_color(): + config = Config('test_cfg') + assert config.get_color('default_fg') == 'default' + +def test_get_color_descr(): + config = Config('test_cfg') + assert config.get_color_descr('default_fg') == 'Default fg' diff --git a/tests/test_version.py b/tests/test_version.py @@ -0,0 +1,32 @@ +# +# The MIT License (MIT) +# +# Copyright (c) 2018 Daniel Moch +# +# Permission is hereby granted, free of charge, to any person obtaining a copy +# of this software and associated documentation files (the "Software"), to deal +# in the Software without restriction, including without limitation the rights +# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +# copies of the Software, and to permit persons to whom the Software is +# furnished to do so, subject to the following conditions: +# +# The above copyright notice and this permission notice shall be included in all +# copies or substantial portions of the Software. +# +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +# SOFTWARE. +# +from setuptools_scm import get_version +from nnotes_cli import version +import nnotes_cli + +def test_version(): + vers = get_version(root="..", relative_to=__file__) + + assert nnotes_cli.__version__ == vers + assert version.version == vers