From 69da5d7545677909ee2aa3ac652fe6362074dfc6 Mon Sep 17 00:00:00 2001 From: Philipp Hansch Date: Fri, 25 Mar 2016 18:19:35 +0100 Subject: [PATCH 01/10] Add BDD tests for spawn command Issue-Link: https://github.com/The-Compiler/qutebrowser/issues/999 --- tests/integration/features/spawn.feature | 17 +++++++++++++++++ tests/integration/features/test_spawn.py | 22 ++++++++++++++++++++++ 2 files changed, 39 insertions(+) create mode 100644 tests/integration/features/spawn.feature create mode 100644 tests/integration/features/test_spawn.py diff --git a/tests/integration/features/spawn.feature b/tests/integration/features/spawn.feature new file mode 100644 index 000000000..5ebd4f4a3 --- /dev/null +++ b/tests/integration/features/spawn.feature @@ -0,0 +1,17 @@ +Feature: :spawn + + Scenario: Running :spawn + When I run :spawn -v echo "Hello" + Then the message "Command exited successfully." should be shown + + Scenario: Running :spawn with command that does not exist + When I run :spawn command_does_not_exist127623 + Then the error "Error while spawning command: The process failed to start." should be shown + + Scenario: Running :spawn with invalid quoting + When I run :spawn """ + Then the error "Error while splitting command: No closing quotation" should be shown + + Scenario: Running :spawn with url variable + When I run :spawn echo {url} + Then "Executing echo with args ['about:blank'], userscript=False" should be logged diff --git a/tests/integration/features/test_spawn.py b/tests/integration/features/test_spawn.py new file mode 100644 index 000000000..8f7e62259 --- /dev/null +++ b/tests/integration/features/test_spawn.py @@ -0,0 +1,22 @@ +# vim: ft=python fileencoding=utf-8 sts=4 sw=4 et: + +# Copyright 2015-2016 Florian Bruhin (The Compiler) +# +# This file is part of qutebrowser. +# +# qutebrowser is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# qutebrowser 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 General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with qutebrowser. If not, see . + +import pytest_bdd as bdd +bdd.scenarios('spawn.feature') + From 008353849107d2bd26876f86adc385009476ba2e Mon Sep 17 00:00:00 2001 From: Philipp Hansch Date: Fri, 25 Mar 2016 18:21:38 +0100 Subject: [PATCH 02/10] Add basic test for spawning userscripts --- tests/integration/data/userscripts/open_current_url | 3 +++ tests/integration/features/spawn.feature | 7 +++++++ 2 files changed, 10 insertions(+) create mode 100755 tests/integration/data/userscripts/open_current_url diff --git a/tests/integration/data/userscripts/open_current_url b/tests/integration/data/userscripts/open_current_url new file mode 100755 index 000000000..6d7f6dc36 --- /dev/null +++ b/tests/integration/data/userscripts/open_current_url @@ -0,0 +1,3 @@ +#!/bin/bash + +echo "open -t $QUTE_URL" >> "$QUTE_FIFO" diff --git a/tests/integration/features/spawn.feature b/tests/integration/features/spawn.feature index 5ebd4f4a3..3f31a7acc 100644 --- a/tests/integration/features/spawn.feature +++ b/tests/integration/features/spawn.feature @@ -15,3 +15,10 @@ Feature: :spawn Scenario: Running :spawn with url variable When I run :spawn echo {url} Then "Executing echo with args ['about:blank'], userscript=False" should be logged + + Scenario: Running :spawn with userscript + When I run :spawn --userscript open_current_url + And I wait until about:blank is loaded + Then the following tabs should be open: + - about:blank + - about:blank (active) From 5811a25299edb6f63f52950fe148fd8fbbe98dea Mon Sep 17 00:00:00 2001 From: Philipp Hansch Date: Mon, 28 Mar 2016 23:48:37 +0200 Subject: [PATCH 03/10] Add a datapath replacement for spawning userscripts This adds a `(datapath)` replacement for feature tests. `(datapath)` will expand to the absolute path to the integration data directory. --- tests/helpers/utils.py | 12 ++++++++++++ tests/integration/features/conftest.py | 6 ++++-- tests/integration/features/spawn.feature | 2 +- 3 files changed, 17 insertions(+), 3 deletions(-) diff --git a/tests/helpers/utils.py b/tests/helpers/utils.py index 1c8f0db28..6ce7569b5 100644 --- a/tests/helpers/utils.py +++ b/tests/helpers/utils.py @@ -22,6 +22,7 @@ import re import pprint +import os.path def print_i(text, indent, error=False): @@ -101,3 +102,14 @@ def pattern_match(*, pattern, value): """ re_pattern = '.*'.join(re.escape(part) for part in pattern.split('*')) return re.fullmatch(re_pattern, value) is not None + +def abs_datapath(from_file): + """Returns the absolute datapath. + + __FILE__ must be given as an argument + + Return: + The absolute path to the tests/integration/data directory. + """ + file_abs = os.path.abspath(os.path.dirname(from_file)) + return os.path.join(file_abs, '..', 'data') diff --git a/tests/integration/features/conftest.py b/tests/integration/features/conftest.py index 03559ede7..b22cf0924 100644 --- a/tests/integration/features/conftest.py +++ b/tests/integration/features/conftest.py @@ -129,6 +129,8 @@ def run_command(quteproc, httpbin, command): else: count = None command = command.replace('(port)', str(httpbin.port)) + command = command.replace('(datapath)', utils.abs_datapath(__file__)) + quteproc.send_cmd(command, count=count) @@ -334,8 +336,8 @@ def check_contents(quteproc, filename): The filename is interpreted relative to tests/integration/data. """ content = quteproc.get_content(plain=False) - path = os.path.join(os.path.abspath(os.path.dirname(__file__)), '..', - 'data', os.path.join(*filename.split('/'))) + path = os.path.join(utils.abs_datapath(__file__), + os.path.join(*filename.split('/'))) with open(path, 'r', encoding='utf-8') as f: file_content = f.read() assert content == file_content diff --git a/tests/integration/features/spawn.feature b/tests/integration/features/spawn.feature index 3f31a7acc..32b77fd41 100644 --- a/tests/integration/features/spawn.feature +++ b/tests/integration/features/spawn.feature @@ -17,7 +17,7 @@ Feature: :spawn Then "Executing echo with args ['about:blank'], userscript=False" should be logged Scenario: Running :spawn with userscript - When I run :spawn --userscript open_current_url + When I run :spawn --userscript (datapath)/userscripts/open_current_url And I wait until about:blank is loaded Then the following tabs should be open: - about:blank From 449adc2dc1722885c4869892b4c238b96efb197d Mon Sep 17 00:00:00 2001 From: Philipp Hansch Date: Mon, 28 Mar 2016 23:53:26 +0200 Subject: [PATCH 04/10] Try not to confuse syntax highlighting with quotes --- tests/integration/features/spawn.feature | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/integration/features/spawn.feature b/tests/integration/features/spawn.feature index 32b77fd41..9ec2a046f 100644 --- a/tests/integration/features/spawn.feature +++ b/tests/integration/features/spawn.feature @@ -9,7 +9,7 @@ Feature: :spawn Then the error "Error while spawning command: The process failed to start." should be shown Scenario: Running :spawn with invalid quoting - When I run :spawn """ + When I run :spawn ""'"" Then the error "Error while splitting command: No closing quotation" should be shown Scenario: Running :spawn with url variable From e9ae2156d3b768824ca030cfa189c07e31f51ad6 Mon Sep 17 00:00:00 2001 From: Philipp Hansch Date: Tue, 29 Mar 2016 00:08:14 +0200 Subject: [PATCH 05/10] Comply with flake8 --- tests/helpers/utils.py | 3 ++- tests/integration/features/test_spawn.py | 1 - 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/helpers/utils.py b/tests/helpers/utils.py index 6ce7569b5..003947e3c 100644 --- a/tests/helpers/utils.py +++ b/tests/helpers/utils.py @@ -103,8 +103,9 @@ def pattern_match(*, pattern, value): re_pattern = '.*'.join(re.escape(part) for part in pattern.split('*')) return re.fullmatch(re_pattern, value) is not None + def abs_datapath(from_file): - """Returns the absolute datapath. + """Get the absolute datapath. __FILE__ must be given as an argument diff --git a/tests/integration/features/test_spawn.py b/tests/integration/features/test_spawn.py index 8f7e62259..e9ddf0301 100644 --- a/tests/integration/features/test_spawn.py +++ b/tests/integration/features/test_spawn.py @@ -19,4 +19,3 @@ import pytest_bdd as bdd bdd.scenarios('spawn.feature') - From c016c77da46a5f777bed3f4838e0962a60589e0f Mon Sep 17 00:00:00 2001 From: Philipp Hansch Date: Tue, 29 Mar 2016 01:37:40 +0200 Subject: [PATCH 06/10] Try to fix Windows path issue with new custom step On windows, using '/' in pathnames won't work, so it's impossible to use to describe a path in a feature spec. The solution is to move the path logic out of the feature spec and hand it over to `os.path.join` in a new custom step for userscripts. --- tests/integration/features/conftest.py | 14 +++++++++++++- tests/integration/features/spawn.feature | 2 +- 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/tests/integration/features/conftest.py b/tests/integration/features/conftest.py index b22cf0924..2ef565e66 100644 --- a/tests/integration/features/conftest.py +++ b/tests/integration/features/conftest.py @@ -129,11 +129,23 @@ def run_command(quteproc, httpbin, command): else: count = None command = command.replace('(port)', str(httpbin.port)) - command = command.replace('(datapath)', utils.abs_datapath(__file__)) quteproc.send_cmd(command, count=count) +@bdd.when(bdd.parsers.parse("I execute the userscript {userscript}")) +def run_userscript(quteproc, userscript): + """Run a userscript located in tests/integration/data/userscripts. + + Wrapper around :spawn --userscript {userscript} that uses an absolute + path. + """ + abs_userscript_path = os.path.join(utils.abs_datapath(__file__), + 'userscripts', userscript) + cmd = ':spawn --userscript {abs_userscript_path}' + quteproc.send_cmd(cmd.format(abs_userscript_path=abs_userscript_path)) + + @bdd.when(bdd.parsers.parse("I reload")) def reload(qtbot, httpbin, quteproc, command): """Reload and wait until a new request is received.""" diff --git a/tests/integration/features/spawn.feature b/tests/integration/features/spawn.feature index 9ec2a046f..eba2496fc 100644 --- a/tests/integration/features/spawn.feature +++ b/tests/integration/features/spawn.feature @@ -17,7 +17,7 @@ Feature: :spawn Then "Executing echo with args ['about:blank'], userscript=False" should be logged Scenario: Running :spawn with userscript - When I run :spawn --userscript (datapath)/userscripts/open_current_url + When I execute the userscript open_current_url And I wait until about:blank is loaded Then the following tabs should be open: - about:blank From b1ecdf29243d340770546b98a0e9cc944021f85a Mon Sep 17 00:00:00 2001 From: Philipp Hansch Date: Tue, 29 Mar 2016 12:11:01 +0200 Subject: [PATCH 07/10] Try to fix path issue on windows --- tests/integration/features/conftest.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/tests/integration/features/conftest.py b/tests/integration/features/conftest.py index 2ef565e66..05e186002 100644 --- a/tests/integration/features/conftest.py +++ b/tests/integration/features/conftest.py @@ -140,8 +140,10 @@ def run_userscript(quteproc, userscript): Wrapper around :spawn --userscript {userscript} that uses an absolute path. """ - abs_userscript_path = os.path.join(utils.abs_datapath(__file__), - 'userscripts', userscript) + joined_path = os.path.join(utils.abs_datapath(__file__), + 'userscripts', userscript) + abs_userscript_path = os.path.abspath(joined_path) + cmd = ':spawn --userscript {abs_userscript_path}' quteproc.send_cmd(cmd.format(abs_userscript_path=abs_userscript_path)) From 8a619ea84cf1f839d395fb8a1fdab4fed83449d7 Mon Sep 17 00:00:00 2001 From: Philipp Hansch Date: Tue, 29 Mar 2016 13:25:04 +0200 Subject: [PATCH 08/10] Prevent slashes from being stripped on Windows --- tests/integration/features/conftest.py | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/tests/integration/features/conftest.py b/tests/integration/features/conftest.py index 05e186002..67c5b2ce5 100644 --- a/tests/integration/features/conftest.py +++ b/tests/integration/features/conftest.py @@ -140,11 +140,10 @@ def run_userscript(quteproc, userscript): Wrapper around :spawn --userscript {userscript} that uses an absolute path. """ - joined_path = os.path.join(utils.abs_datapath(__file__), - 'userscripts', userscript) - abs_userscript_path = os.path.abspath(joined_path) + abs_userscript_path = os.path.join(utils.abs_datapath(__file__), + 'userscripts', userscript) - cmd = ':spawn --userscript {abs_userscript_path}' + cmd = ':spawn --userscript "{abs_userscript_path}"' quteproc.send_cmd(cmd.format(abs_userscript_path=abs_userscript_path)) From 5ae0b0cf872d6c48a02d6fa810bc6169dc26a536 Mon Sep 17 00:00:00 2001 From: Philipp Hansch Date: Tue, 29 Mar 2016 14:17:47 +0200 Subject: [PATCH 09/10] Ignore test on Windows The userscript is a bash script and there is no bash on windows. One solution could be to use a python userscript, but there may be other issues (file associations), too. --- tests/integration/features/spawn.feature | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/integration/features/spawn.feature b/tests/integration/features/spawn.feature index eba2496fc..03506c18b 100644 --- a/tests/integration/features/spawn.feature +++ b/tests/integration/features/spawn.feature @@ -16,6 +16,7 @@ Feature: :spawn When I run :spawn echo {url} Then "Executing echo with args ['about:blank'], userscript=False" should be logged + @posix Scenario: Running :spawn with userscript When I execute the userscript open_current_url And I wait until about:blank is loaded From e0e8bc805bce4296da596ee7ffb41b57b80882e4 Mon Sep 17 00:00:00 2001 From: Philipp Hansch Date: Tue, 29 Mar 2016 19:52:46 +0200 Subject: [PATCH 10/10] Remove unneeded argument Instead, figure out the data directory from the helpers directory. --- tests/helpers/utils.py | 10 ++++------ tests/integration/features/conftest.py | 6 +++--- 2 files changed, 7 insertions(+), 9 deletions(-) diff --git a/tests/helpers/utils.py b/tests/helpers/utils.py index 003947e3c..d3ef3cd70 100644 --- a/tests/helpers/utils.py +++ b/tests/helpers/utils.py @@ -104,13 +104,11 @@ def pattern_match(*, pattern, value): return re.fullmatch(re_pattern, value) is not None -def abs_datapath(from_file): - """Get the absolute datapath. - - __FILE__ must be given as an argument +def abs_datapath(): + """Get the absolute path to the integration data directory. Return: The absolute path to the tests/integration/data directory. """ - file_abs = os.path.abspath(os.path.dirname(from_file)) - return os.path.join(file_abs, '..', 'data') + file_abs = os.path.abspath(os.path.dirname(__file__)) + return os.path.join(file_abs, '..', 'integration', 'data') diff --git a/tests/integration/features/conftest.py b/tests/integration/features/conftest.py index 67c5b2ce5..a099a1887 100644 --- a/tests/integration/features/conftest.py +++ b/tests/integration/features/conftest.py @@ -140,10 +140,10 @@ def run_userscript(quteproc, userscript): Wrapper around :spawn --userscript {userscript} that uses an absolute path. """ - abs_userscript_path = os.path.join(utils.abs_datapath(__file__), + abs_userscript_path = os.path.join(utils.abs_datapath(), 'userscripts', userscript) - cmd = ':spawn --userscript "{abs_userscript_path}"' + cmd = ':spawn --userscript {abs_userscript_path}' quteproc.send_cmd(cmd.format(abs_userscript_path=abs_userscript_path)) @@ -349,7 +349,7 @@ def check_contents(quteproc, filename): The filename is interpreted relative to tests/integration/data. """ content = quteproc.get_content(plain=False) - path = os.path.join(utils.abs_datapath(__file__), + path = os.path.join(utils.abs_datapath(), os.path.join(*filename.split('/'))) with open(path, 'r', encoding='utf-8') as f: file_content = f.read()