Use py.path.local in save_script
This commit is contained in:
parent
6aafe02320
commit
f033b228b1
@ -18,10 +18,10 @@
|
|||||||
|
|
||||||
"""Tests for qutebrowser.browser.greasemonkey."""
|
"""Tests for qutebrowser.browser.greasemonkey."""
|
||||||
|
|
||||||
import os
|
|
||||||
import logging
|
import logging
|
||||||
|
|
||||||
import pytest
|
import pytest
|
||||||
|
import py.path # pylint: disable=no-name-in-module
|
||||||
from PyQt5.QtCore import QUrl
|
from PyQt5.QtCore import QUrl
|
||||||
|
|
||||||
from qutebrowser.browser import greasemonkey
|
from qutebrowser.browser import greasemonkey
|
||||||
@ -41,20 +41,16 @@ console.log("Script is running.");
|
|||||||
pytestmark = pytest.mark.usefixtures('data_tmpdir')
|
pytestmark = pytest.mark.usefixtures('data_tmpdir')
|
||||||
|
|
||||||
|
|
||||||
def save_script(script_text, filename):
|
def _save_script(script_text, filename):
|
||||||
script_path = greasemonkey._scripts_dir()
|
# pylint: disable=no-member
|
||||||
try:
|
file_path = py.path.local(greasemonkey._scripts_dir()) / filename
|
||||||
os.mkdir(script_path)
|
# pylint: enable=no-member
|
||||||
except FileExistsError:
|
file_path.write_text(script_text, encoding='utf-8', ensure=True)
|
||||||
pass
|
|
||||||
file_path = os.path.join(script_path, filename)
|
|
||||||
with open(file_path, 'w', encoding='utf-8') as f:
|
|
||||||
f.write(script_text)
|
|
||||||
|
|
||||||
|
|
||||||
def test_all():
|
def test_all():
|
||||||
"""Test that a script gets read from file, parsed and returned."""
|
"""Test that a script gets read from file, parsed and returned."""
|
||||||
save_script(test_gm_script, 'test.user.js')
|
_save_script(test_gm_script, 'test.user.js')
|
||||||
|
|
||||||
gm_manager = greasemonkey.GreasemonkeyManager()
|
gm_manager = greasemonkey.GreasemonkeyManager()
|
||||||
assert (gm_manager.all_scripts()[0].name ==
|
assert (gm_manager.all_scripts()[0].name ==
|
||||||
@ -71,7 +67,7 @@ def test_all():
|
|||||||
])
|
])
|
||||||
def test_get_scripts_by_url(url, expected_matches):
|
def test_get_scripts_by_url(url, expected_matches):
|
||||||
"""Check Greasemonkey include/exclude rules work."""
|
"""Check Greasemonkey include/exclude rules work."""
|
||||||
save_script(test_gm_script, 'test.user.js')
|
_save_script(test_gm_script, 'test.user.js')
|
||||||
gm_manager = greasemonkey.GreasemonkeyManager()
|
gm_manager = greasemonkey.GreasemonkeyManager()
|
||||||
|
|
||||||
scripts = gm_manager.scripts_for(QUrl(url))
|
scripts = gm_manager.scripts_for(QUrl(url))
|
||||||
@ -81,7 +77,7 @@ def test_get_scripts_by_url(url, expected_matches):
|
|||||||
|
|
||||||
def test_no_metadata(caplog):
|
def test_no_metadata(caplog):
|
||||||
"""Run on all sites at document-end is the default."""
|
"""Run on all sites at document-end is the default."""
|
||||||
save_script("var nothing = true;\n", 'nothing.user.js')
|
_save_script("var nothing = true;\n", 'nothing.user.js')
|
||||||
|
|
||||||
with caplog.at_level(logging.WARNING):
|
with caplog.at_level(logging.WARNING):
|
||||||
gm_manager = greasemonkey.GreasemonkeyManager()
|
gm_manager = greasemonkey.GreasemonkeyManager()
|
||||||
@ -93,7 +89,7 @@ def test_no_metadata(caplog):
|
|||||||
|
|
||||||
def test_bad_scheme(caplog):
|
def test_bad_scheme(caplog):
|
||||||
"""qute:// isn't in the list of allowed schemes."""
|
"""qute:// isn't in the list of allowed schemes."""
|
||||||
save_script("var nothing = true;\n", 'nothing.user.js')
|
_save_script("var nothing = true;\n", 'nothing.user.js')
|
||||||
|
|
||||||
with caplog.at_level(logging.WARNING):
|
with caplog.at_level(logging.WARNING):
|
||||||
gm_manager = greasemonkey.GreasemonkeyManager()
|
gm_manager = greasemonkey.GreasemonkeyManager()
|
||||||
|
Loading…
Reference in New Issue
Block a user