diff --git a/CHANGELOG.asciidoc b/CHANGELOG.asciidoc index 920f20f66..220138205 100644 --- a/CHANGELOG.asciidoc +++ b/CHANGELOG.asciidoc @@ -157,6 +157,7 @@ Fixed - The default `sk` keybinding now sets the commandline to `:bind` correctly - Fixed hang when using multiple spaces in a row with the URL completion - Fixed crash when closing a window without focusing it +- Userscripts now can access QUTE_FIFO correctly on Windows v0.8.3 (unreleased) ------------------- diff --git a/qutebrowser/commands/userscripts.py b/qutebrowser/commands/userscripts.py index 0a8b19524..a8d741f6c 100644 --- a/qutebrowser/commands/userscripts.py +++ b/qutebrowser/commands/userscripts.py @@ -280,15 +280,8 @@ class _WindowsUserscriptRunner(_BaseUserscriptRunner): This also means the userscript *has* to use >> (append) rather than > (overwrite) to write to the file! - - Attributes: - _oshandle: The oshandle of the temp file. """ - def __init__(self, win_id, parent=None): - super().__init__(win_id, parent) - self._oshandle = None - def _cleanup(self): """Clean up temporary files after the userscript finished.""" if self._cleaned_up: @@ -301,12 +294,7 @@ class _WindowsUserscriptRunner(_BaseUserscriptRunner): except OSError: log.procs.exception("Failed to read command file!") - try: - os.close(self._oshandle) - except OSError: - log.procs.exception("Failed to close file handle!") super()._cleanup() - self._oshandle = None self.finished.emit() @pyqtSlot() @@ -323,7 +311,9 @@ class _WindowsUserscriptRunner(_BaseUserscriptRunner): self._kwargs = kwargs try: - self._oshandle, self._filepath = tempfile.mkstemp(text=True) + handle = tempfile.NamedTemporaryFile(delete=False) + handle.close() + self._filepath = handle.name except OSError as e: message.error(self._win_id, "Error while creating tempfile: " "{}".format(e)) diff --git a/tests/end2end/data/userscripts/echo.bat b/tests/end2end/data/userscripts/echo.bat new file mode 100644 index 000000000..0f37ae2cd --- /dev/null +++ b/tests/end2end/data/userscripts/echo.bat @@ -0,0 +1,6 @@ +@echo off +rem This is needed because echo does not exist as an external program on +rem Windows, so we can't call echo(.exe) from qutebrowser, but it's useful for +rem tests. This little file is callable via :spawn and mimics (in a very naive +rem way) the echo command line utility. +echo %* diff --git a/tests/end2end/data/userscripts/open_current_url.bat b/tests/end2end/data/userscripts/open_current_url.bat new file mode 100644 index 000000000..e57eb2567 --- /dev/null +++ b/tests/end2end/data/userscripts/open_current_url.bat @@ -0,0 +1 @@ +echo open -t %QUTE_URL% >> "%QUTE_FIFO%" diff --git a/tests/end2end/features/conftest.py b/tests/end2end/features/conftest.py index 75532b04b..03f082c22 100644 --- a/tests/end2end/features/conftest.py +++ b/tests/end2end/features/conftest.py @@ -36,6 +36,18 @@ from qutebrowser.browser import pdfjs from helpers import utils +def _get_echo_exe_path(): + """Return the path to an echo-like command, depending on the system. + + Return: + Path to the "echo"-utility. + """ + if sys.platform == "win32": + return os.path.join(utils.abs_datapath(), 'userscripts', 'echo.bat') + else: + return 'echo' + + @pytest.hookimpl(hookwrapper=True) def pytest_runtest_makereport(item, call): """Add a BDD section to the test output.""" @@ -224,6 +236,7 @@ def run_command(quteproc, httpbin, tmpdir, command): command = command.replace('(testdata)', utils.abs_datapath()) command = command.replace('(tmpdir)', str(tmpdir)) command = command.replace('(dirsep)', os.sep) + command = command.replace('(echo-exe)', _get_echo_exe_path()) quteproc.send_cmd(command, count=count, invalid=invalid) diff --git a/tests/end2end/features/spawn.feature b/tests/end2end/features/spawn.feature index 2d43b011b..5cdeac029 100644 --- a/tests/end2end/features/spawn.feature +++ b/tests/end2end/features/spawn.feature @@ -1,7 +1,7 @@ Feature: :spawn Scenario: Running :spawn - When I run :spawn -v echo "Hello" + When I run :spawn -v (echo-exe) "Hello" Then the message "Command exited successfully." should be shown Scenario: Running :spawn with command that does not exist @@ -19,18 +19,18 @@ Feature: :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 + When I run :spawn (echo-exe) {url} + Then "Executing * with args ['about:blank'], userscript=False" should be logged Scenario: Running :spawn with url variable in fully encoded format When I open data/title with spaces.html - And I run :spawn echo {url} - Then "Executing echo with args ['http://localhost:(port)/data/title%20with%20spaces.html'], userscript=False" should be logged + And I run :spawn (echo-exe) {url} + Then "Executing * with args ['http://localhost:(port)/data/title%20with%20spaces.html'], userscript=False" should be logged Scenario: Running :spawn with url variable in pretty decoded format When I open data/title with spaces.html - And I run :spawn echo {url:pretty} - Then "Executing echo with args ['http://localhost:(port)/data/title with spaces.html'], userscript=False" should be logged + And I run :spawn (echo-exe) {url:pretty} + Then "Executing * with args ['http://localhost:(port)/data/title with spaces.html'], userscript=False" should be logged @posix Scenario: Running :spawn with userscript @@ -40,3 +40,12 @@ Feature: :spawn Then the following tabs should be open: - about:blank - about:blank (active) + + @windows + Scenario: Running :spawn with userscript on Windows + When I open about:blank + And I run :spawn -u (testdata)/userscripts/open_current_url.bat + And I wait until about:blank is loaded + Then the following tabs should be open: + - about:blank + - about:blank (active)