Add some docstrings

This commit is contained in:
Corentin Jule 2015-12-04 21:57:34 +01:00
commit 3426dd06f6

View File

@ -18,7 +18,7 @@
# You should have received a copy of the GNU General Public License # You should have received a copy of the GNU General Public License
# along with qutebrowser. If not, see <http://www.gnu.org/licenses/>. # along with qutebrowser. If not, see <http://www.gnu.org/licenses/>.
"""Tests for qutebrowser.browser.adblock""" """Tests for qutebrowser.browser.adblock."""
import os import os
import zipfile import zipfile
@ -29,60 +29,49 @@ from qutebrowser.browser import adblock
from qutebrowser.config import config from qutebrowser.config import config
from qutebrowser.utils import objreg from qutebrowser.utils import objreg
# TODO Should I use it ? And how ?
# @pytest.yield_fixture
# def default_config():
# """Fixture that provides and registers an empty default config object."""
# config_obj = config.ConfigManager(configdir=None,
# fname=None,
# relaxed=True)
# objreg.register('config', config_obj)
# yield config_obj
# objreg.delete('config')
def create_text_files(files_names, directory): def create_text_files(files_names, directory):
"""Returns a list of text files created """Returns a list of text files created
with given names in given directory""" with given names in given directory."""
directory = str(directory) directory = str(directory)
created_files = [] created_files = []
for file_name in files_names: for file_name in files_names:
test_file = os.path.join(directory, file_name) test_file = os.path.join(directory, file_name)
with open(test_file, 'w', encoding='utf-8') as f: with open(test_file, 'w', encoding='utf-8') as current_file:
f.write('inside ' + file_name) current_file.write('inside ' + file_name)
created_files.append(test_file) created_files.append(test_file)
return created_files return created_files
def create_zipfile(files_names, directory): def create_zipfile(files_names, directory):
"""Returns a zipfile populated with created files and its name""" """Returns a zipfile populated with created files and its name."""
directory = str(directory) directory = str(directory)
files = create_text_files(files_names, directory) files = create_text_files(files_names, directory)
# include created files in a ZipFile # include created files in a ZipFile
zipfile_name = os.path.join(directory, 'test.zip') zipfile_name = os.path.join(directory, 'test.zip')
with zipfile.ZipFile(zipfile_name, 'w') as zf: with zipfile.ZipFile(zipfile_name, 'w') as new_zipfile:
for file_name in files: for file_name in files:
zf.write(file_name, arcname=os.path.basename(file_name)) new_zipfile.write(file_name, arcname=os.path.basename(file_name))
# Removes path from file name # Removes path from file name
return zf, zipfile_name return new_zipfile, zipfile_name
class TestGuessZipFilename: class TestGuessZipFilename:
""" Test function adblock.guess_zip_filename() """ """Test function adblock.guess_zip_filename()."""
def test_with_single_file(self, tmpdir): def test_with_single_file(self, tmpdir):
"""Zip provided only contains a single file""" """Zip provided only contains a single file."""
zf = create_zipfile(['test_a'], tmpdir)[0] zf = create_zipfile(['test_a'], tmpdir)[0]
assert adblock.guess_zip_filename(zf) == 'test_a' assert adblock.guess_zip_filename(zf) == 'test_a'
def test_with_multiple_files(self, tmpdir): def test_with_multiple_files(self, tmpdir):
"""Zip provided contains multiple files including hosts""" """Zip provided contains multiple files including hosts."""
names = ['test_a', 'test_b', 'hosts', 'test_c'] names = ['test_a', 'test_b', 'hosts', 'test_c']
zf = create_zipfile(names, tmpdir)[0] zf = create_zipfile(names, tmpdir)[0]
assert adblock.guess_zip_filename(zf) == 'hosts' assert adblock.guess_zip_filename(zf) == 'hosts'
def test_without_hosts_file(self, tmpdir): def test_without_hosts_file(self, tmpdir):
"""Zip provided does not contain any hosts file""" """Zip provided does not contain any hosts file."""
names = ['test_a', 'test_b', 'test_d', 'test_c'] names = ['test_a', 'test_b', 'test_d', 'test_c']
zf = create_zipfile(names, tmpdir)[0] zf = create_zipfile(names, tmpdir)[0]
with pytest.raises(FileNotFoundError): with pytest.raises(FileNotFoundError):
@ -90,21 +79,23 @@ class TestGuessZipFilename:
class TestGetFileObj: class TestGetFileObj:
"""Test Function adblock.get_fileobj()""" """Test Function adblock.get_fileobj()."""
def test_with_zipfile(self, tmpdir): def test_with_zipfile(self, tmpdir):
"""File provided is a zipfile."""
names = ['test_a', 'test_b', 'hosts', 'test_c'] names = ['test_a', 'test_b', 'hosts', 'test_c']
zf_name = create_zipfile(names, tmpdir)[1] zf_name = create_zipfile(names, tmpdir)[1]
zipobj = open(zf_name, 'rb') zipobj = open(zf_name, 'rb')
assert adblock.get_fileobj(zipobj).read() == "inside hosts" assert adblock.get_fileobj(zipobj).read() == "inside hosts"
def test_with_text_file(self, tmpdir): def test_with_text_file(self, tmpdir):
"""File provided is not a zipfile."""
test_file = open(create_text_files(['testfile'], tmpdir)[0], 'rb') test_file = open(create_text_files(['testfile'], tmpdir)[0], 'rb')
assert adblock.get_fileobj(test_file).read() == "inside testfile" assert adblock.get_fileobj(test_file).read() == "inside testfile"
class TestIsWhitelistedHost: class TestIsWhitelistedHost:
"""Test function adblock.is_whitelisted_host""" """Test function adblock.is_whitelisted_host."""
# FIXME Error since we deleted host-blocking-whitelist # FIXME Error since we deleted host-blocking-whitelist
# If we don't remove host-block-whitelist, test behaves as in a mismatch # If we don't remove host-block-whitelist, test behaves as in a mismatch
@ -118,26 +109,29 @@ class TestIsWhitelistedHost:
# objreg.delete('config') # objreg.delete('config')
def test_with_match(self): def test_with_match(self):
"""Given host is in the whitelist."""
config_obj = config.ConfigManager(configdir=None, fname=None, config_obj = config.ConfigManager(configdir=None, fname=None,
relaxed=True) relaxed=True)
config_obj.set_command(0, section_='content', config_obj.set_command(0, section_='content',
option='host-blocking-whitelist', option='host-blocking-whitelist',
value='qutebrowser.org') value='qutebrowser.org')
objreg.register('config', config_obj) objreg.register('config', config_obj)
assert adblock.is_whitelisted_host('qutebrowser.org') assert adblock.is_whitelisted_host('qutebrowser.org')
objreg.delete('config') objreg.delete('config')
def test_without_match(self): def test_without_match(self):
"""Given host is not in the whitelist."""
config_obj = config.ConfigManager(configdir=None, fname=None, config_obj = config.ConfigManager(configdir=None, fname=None,
relaxed=True) relaxed=True)
config_obj.set_command(0, section_='content', config_obj.set_command(0, section_='content',
option='host-blocking-whitelist', option='host-blocking-whitelist',
value='cutebrowser.org') value='cutebrowser.org')
objreg.register('config', config_obj) objreg.register('config', config_obj)
assert not adblock.is_whitelisted_host('qutebrowser.org') assert not adblock.is_whitelisted_host('qutebrowser.org')
objreg.delete('config') objreg.delete('config')
class TestHostBlocker: class TestHostBlocker:
"""Test for class HostBlocker."""
# TODO # TODO
pass pass