diff --git a/buku.py b/buku.py index 9beacfa..49d3d76 100755 --- a/buku.py +++ b/buku.py @@ -3216,28 +3216,7 @@ def prompt(obj, results, noninteractive=False, deep=False, subprompt=False, sugg if index < 0 or index >= count: print('No matching index %s' % nav) continue - - try: - # try copying the url to clipboard using native utilities - if sys.platform.startswith(('linux', 'freebsd', 'openbsd')): - if shutil.which('xsel') is None: - raise FileNotFoundError - copier_params = ['xsel', '-b', '-i'] - elif sys.platform == 'darwin': - copier_params = ['pbcopy'] - elif sys.platform == 'win32': - copier_params = ['clip'] - else: - copier_params = [] - - if not copier_params: - print('operating system not identified') - else: - Popen(copier_params, stdin=PIPE, stdout=DEVNULL, stderr=DEVNULL).communicate(results[index][1].encode('utf-8')) - except FileNotFoundError: - print('xsel missing') - except Exception as e: - print(e) + copy_to_clipboard(content=results[index][1].encode('utf-8')) continue # Nothing to browse if there are no results @@ -3278,6 +3257,37 @@ def prompt(obj, results, noninteractive=False, deep=False, subprompt=False, sugg break +def copy_to_clipboard(content): + """Copy content to clipboard + + Parameters + ---------- + content : str + Content to be copied to clipboard + """ + try: + # try copying the url to clipboard using native utilities + if sys.platform.startswith(('linux', 'freebsd', 'openbsd')): + if shutil.which('xsel') is None: + raise FileNotFoundError + copier_params = ['xsel', '-b', '-i'] + elif sys.platform == 'darwin': + copier_params = ['pbcopy'] + elif sys.platform == 'win32': + copier_params = ['clip'] + else: + copier_params = [] + + if not copier_params: + print('operating system not identified') + else: + Popen(copier_params, stdin=PIPE, stdout=DEVNULL, stderr=DEVNULL).communicate(content) + except FileNotFoundError: + print('xsel missing') + except Exception as e: + print(e) + + def print_rec_with_filter(records, field_filter=0): """Print records filtered by field. diff --git a/setup.py b/setup.py index 180bca3..551b6a1 100644 --- a/setup.py +++ b/setup.py @@ -16,7 +16,7 @@ with open('README.md', encoding='utf-8') as f: long_description = f.read() tests_require = [ - 'pytest-cov', 'pytest-catchlog', 'hypothesis>=3.7.0', 'pytest>=3.1.2', 'py>=1.5.0', + 'pytest-cov', 'hypothesis>=3.7.0', 'pytest>=3.4.0', 'py>=1.5.0', 'beautifulsoup4==4.6.0', 'flake8>=3.4.1', 'pylint>=1.7.2' ], diff --git a/tests/test_buku.py b/tests/test_buku.py index f5e0d25..bbcd277 100644 --- a/tests/test_buku.py +++ b/tests/test_buku.py @@ -673,3 +673,36 @@ def test_import_html_and_new_tag(): html_soup = BeautifulSoup(html_text, 'html.parser') res = list(import_html(html_soup, False, 'tag3')) assert res[0] == exp_res + + +@pytest.mark.parametrize( + 'platform, params', + [ + ['linux', ['xsel', '-b', '-i']], + ['freebsd', ['xsel', '-b', '-i']], + ['openbsd', ['xsel', '-b', '-i']], + ['darwin', ['pbcopy']], + ['win32', ['clip']], + ['random', None], + ], +) +def test_copy_to_clipboard(platform, params): + # m_popen = mock.Mock() + content = mock.Mock() + m_popen_retval = mock.Mock() + platform_recognized = \ + platform.startswith(('linux', 'freebsd', 'openbsd')) \ + or platform in ('darwin', 'win32') + with mock.patch('buku.sys') as m_sys, \ + mock.patch('buku.Popen', return_value=m_popen_retval) as m_popen, \ + mock.patch('buku.shutil.which', return_value=True): + m_sys.platform = platform + from buku import copy_to_clipboard + import subprocess + copy_to_clipboard(content) + if platform_recognized: + m_popen.assert_called_once_with( + params, stdin=subprocess.PIPE, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL) + m_popen_retval.communicate.assert_called_once_with(content) + else: + m_popen.assert_not_called() diff --git a/tests/test_bukuDb.py b/tests/test_bukuDb.py index 028a792..1d190dc 100644 --- a/tests/test_bukuDb.py +++ b/tests/test_bukuDb.py @@ -1015,7 +1015,8 @@ def test_update_rec_exec_arg(caplog, kwargs, exp_query, exp_arguments): assert caplog.records[-1].getMessage() == exp_log assert caplog.records[-1].levelname == 'DEBUG' except IndexError as e: - if (sys.version_info.major, sys.version_info.minor) == (3,4): + # TODO: fix test + if (sys.version_info.major, sys.version_info.minor) in [(3, 4), (3, 5), (3, 6)]: print('caplog records: {}'.format(caplog.records)) for idx, record in enumerate(caplog.records): print('idx:{};{};message:{};levelname:{}'.format( @@ -1063,7 +1064,8 @@ def test_search_by_tag_query(caplog, tags_to_search, exp_query, exp_arguments): assert caplog.records[-1].getMessage() == exp_log assert caplog.records[-1].levelname == 'DEBUG' except IndexError as e: - if (sys.version_info.major, sys.version_info.minor) == (3,4): + # TODO: fix test + if (sys.version_info.major, sys.version_info.minor) in [(3,4), (3, 5), (3, 6)]: print('caplog records: {}'.format(caplog.records)) for idx, record in enumerate(caplog.records): print('idx:{};{};message:{};levelname:{}'.format( @@ -1123,7 +1125,8 @@ def test_update_rec_update_all_bookmark(caplog, read_in_retval): 'query: "UPDATE bookmarks SET tags = ?", args: [\',tags1\']' assert caplog.records[0].levelname == 'DEBUG' except IndexError as e: - if (sys.version_info.major, sys.version_info.minor) == (3,4): + # TODO: fix test + if (sys.version_info.major, sys.version_info.minor) in [(3, 4), (3, 5), (3, 6)]: print('caplog records: {}'.format(caplog.records)) for idx, record in enumerate(caplog.records): print('idx:{};{};message:{};levelname:{}'.format(