summaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorFlorian Bruhin <me@the-compiler.org>2021-01-19 13:37:14 +0100
committerFlorian Bruhin <me@the-compiler.org>2021-01-19 13:37:14 +0100
commit0f8f52782ce5e4c4a57fba493760736adebf13b3 (patch)
tree5d8b5cf77b934cbbf86eeeada472569b121c1b42 /tests
parentd45b68a98dcd169b23087becacacea9790097849 (diff)
parent2d6d1dad4309aaaebdcb608bb46445fd62343a37 (diff)
downloadqutebrowser-0f8f52782ce5e4c4a57fba493760736adebf13b3.tar.gz
qutebrowser-0f8f52782ce5e4c4a57fba493760736adebf13b3.zip
Merge remote-tracking branch 'origin/pr/5987'
Diffstat (limited to 'tests')
-rw-r--r--tests/end2end/data/fileselect.html21
-rw-r--r--tests/end2end/features/editor.feature29
-rw-r--r--tests/end2end/features/test_editor_bdd.py17
-rw-r--r--tests/helpers/fixtures.py10
-rw-r--r--tests/unit/utils/test_utils.py38
5 files changed, 113 insertions, 2 deletions
diff --git a/tests/end2end/data/fileselect.html b/tests/end2end/data/fileselect.html
new file mode 100644
index 000000000..a31936c72
--- /dev/null
+++ b/tests/end2end/data/fileselect.html
@@ -0,0 +1,21 @@
+<!DOCTYPE html>
+<html>
+ <head>
+ <meta charset="utf-8">
+ <title>Testing fileselection</title>
+ <script type="text/javascript">
+ function onLoad() {
+ for (let id of ['#single_file', '#multiple_files']) {
+ const input = document.querySelector(id);
+ input.addEventListener('change', function (e) {
+ console.log(`Files: ${Array.from(input.files).map(f => f.name).join(", ")}`);
+ });
+ }
+ }
+ </script>
+ </head>
+ <body onload="onLoad()">
+ <input type="file" id="single_file" name="single_file" accept=".txt">
+ <input type="file" id="multiple_files" name="multiple_files" accept=".txt" multiple>
+ </body>
+</html>
diff --git a/tests/end2end/features/editor.feature b/tests/end2end/features/editor.feature
index 4d56440f4..31e67d4b1 100644
--- a/tests/end2end/features/editor.feature
+++ b/tests/end2end/features/editor.feature
@@ -190,3 +190,32 @@ Feature: Opening external editors
And I run :edit-command
Then the error "command must start with one of :/?" should be shown
And "Leaving mode KeyMode.command *" should not be logged
+
+ ## select single file
+
+ Scenario: Select one file with single command
+ When I set up a fake "single_file" fileselector selecting "tests/end2end/data/numbers/1.txt"
+ And I open data/fileselect.html
+ And I run :click-element id single_file
+ Then the javascript message "Files: 1.txt" should be logged
+
+ Scenario: Select two files with single command
+ When I set up a fake "single_file" fileselector selecting "tests/end2end/data/numbers/1.txt tests/end2end/data/numbers/2.txt"
+ And I open data/fileselect.html
+ And I run :click-element id single_file
+ Then the javascript message "Files: 1.txt" should be logged
+ And the warning "More than one files choosen, using only the first" should be shown
+
+ ## select multiple files
+
+ Scenario: Select one file with multiple command
+ When I set up a fake "multiple_files" fileselector selecting "tests/end2end/data/numbers/1.txt"
+ And I open data/fileselect.html
+ And I run :click-element id multiple_files
+ Then the javascript message "Files: 1.txt" should be logged
+
+ Scenario: Select two files with multiple command
+ When I set up a fake "multiple_files" fileselector selecting "tests/end2end/data/numbers/1.txt tests/end2end/data/numbers/2.txt"
+ And I open data/fileselect.html
+ And I run :click-element id multiple_files
+ Then the javascript message "Files: 1.txt, 2.txt" should be logged
diff --git a/tests/end2end/features/test_editor_bdd.py b/tests/end2end/features/test_editor_bdd.py
index 36719324a..01e4aeea0 100644
--- a/tests/end2end/features/test_editor_bdd.py
+++ b/tests/end2end/features/test_editor_bdd.py
@@ -178,3 +178,20 @@ def save_editor_wait(tmpdir):
# for posix, there IS a member so we need to ignore useless-suppression
# pylint: disable=no-member,useless-suppression
os.kill(pid, signal.SIGUSR2)
+
+
+@bdd.when(bdd.parsers.parse('I set up a fake "{kind}" fileselector '
+ 'selecting "{files}"'))
+def set_up_fileselector(quteproc, py_proc, kind, files):
+ """Set up fileselect.xxx.command to select the file(s)."""
+ cmd, args = py_proc(r"""
+ import os
+ import sys
+ tmp_file = sys.argv[1]
+ with open(tmp_file, 'w') as f:
+ for selected_file in sys.argv[2:]:
+ f.write(os.path.abspath(selected_file) + "\n")
+ """)
+ fileselect_cmd = json.dumps([cmd, *args, '{}', *files.split(' ')])
+ quteproc.set_setting('fileselect.handler', 'external')
+ quteproc.set_setting(f'fileselect.{kind}.command', fileselect_cmd)
diff --git a/tests/helpers/fixtures.py b/tests/helpers/fixtures.py
index 2d853df08..7de34a9a7 100644
--- a/tests/helpers/fixtures.py
+++ b/tests/helpers/fixtures.py
@@ -503,13 +503,19 @@ def cookiejar_and_cache(stubs, monkeypatch):
@pytest.fixture
-def py_proc():
+def py_proc(tmp_path):
"""Get a python executable and args list which executes the given code."""
if getattr(sys, 'frozen', False):
pytest.skip("Can't be run when frozen")
def func(code):
- return (sys.executable, ['-c', textwrap.dedent(code.strip('\n'))])
+ code = textwrap.dedent(code.strip('\n'))
+ if '\n' in code:
+ py_file = tmp_path / 'py_proc.py'
+ py_file.write_text(code)
+ return (sys.executable, [str(py_file)])
+ else:
+ return (sys.executable, ['-c', code])
return func
diff --git a/tests/unit/utils/test_utils.py b/tests/unit/utils/test_utils.py
index 2b1392664..06dd203c5 100644
--- a/tests/unit/utils/test_utils.py
+++ b/tests/unit/utils/test_utils.py
@@ -878,3 +878,41 @@ def test_parse_duration_hypothesis(duration):
])
def test_mimetype_extension(mimetype, extension):
assert utils.mimetype_extension(mimetype) == extension
+
+
+class TestCleanupFileContext:
+
+ def test_no_file(self, tmp_path, caplog):
+ tmpfile = tmp_path / 'tmp.txt'
+ with caplog.at_level(logging.ERROR, 'misc'):
+ with utils.cleanup_file(tmpfile):
+ pass
+ assert len(caplog.messages) == 1
+ assert caplog.messages[0].startswith("Failed to delete tempfile")
+ assert not tmpfile.exists()
+
+ def test_no_error(self, tmp_path):
+ tmpfile = tmp_path / 'tmp.txt'
+ with tmpfile.open('w'):
+ pass
+ with utils.cleanup_file(tmpfile):
+ pass
+ assert not tmpfile.exists()
+
+ def test_error(self, tmp_path):
+ tmpfile = tmp_path / 'tmp.txt'
+ with tmpfile.open('w'):
+ pass
+ with pytest.raises(RuntimeError):
+ with utils.cleanup_file(tmpfile):
+ raise RuntimeError
+ assert not tmpfile.exists()
+
+ def test_directory(self, tmp_path, caplog):
+ assert tmp_path.is_dir()
+ # removal of file fails since it's a directory
+ with caplog.at_level(logging.ERROR, 'misc'):
+ with utils.cleanup_file(tmp_path):
+ pass
+ assert len(caplog.messages) == 1
+ assert caplog.messages[0].startswith("Failed to delete tempfile")