Code cleanup - Pep8

This commit is contained in:
Corentin Jule 2015-12-03 22:00:39 +01:00
parent 2d850f7106
commit 8dd0249af9

View File

@ -1,11 +1,34 @@
#!/usr/bin/env python3
# vim: ft=python fileencoding=utf-8 sts=4 sw=4 et:
# Copyright 2014-2015 Florian Bruhin (The Compiler) <mail@qutebrowser.org>
#
# 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 <http://www.gnu.org/licenses/>.
"""Tests for qutebrowser.browser.adblock"""
import pytest import pytest
from qutebrowser.browser import adblock 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
import os import os
import zipfile import zipfile
import io
# TODO Should I use it ? And how ?
# @pytest.yield_fixture # @pytest.yield_fixture
# def default_config(): # def default_config():
# """Fixture that provides and registers an empty default config object.""" # """Fixture that provides and registers an empty default config object."""
@ -14,57 +37,61 @@ import io
# yield config_obj # yield config_obj
# objreg.delete('config') # objreg.delete('config')
def create_text_files(files_names, directory): def create_text_files(files_names, directory):
"""Returns a list of created text files""" """Returns a list of text files created
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') as f : with open(test_file, 'w') as f:
f.write('inside ' + file_name) f.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 zf:
for file_name in files : for file_name in files:
zf.write(file_name, arcname=os.path.basename(file_name)) zf.write(file_name, arcname=os.path.basename(file_name))
# Removes path from file name # Removes path from file name
return zf, zipfile_name return zf, 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(['testa'], tmpdir)[0] zf = create_zipfile(['test_a'], tmpdir)[0]
assert adblock.guess_zip_filename(zf) == 'testa' assert adblock.guess_zip_filename(zf) == 'test_a'
# guess_zip_filename doesn't include the root slash /
# whereas os.path.join() does, so we exclude first character
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"""
zf = create_zipfile(['testa','testb','hosts','testc'], tmpdir)[0] names = ['test_a', 'test_b', 'hosts', 'test_c']
zf = create_zipfile(names, tmpdir)[0]
assert adblock.guess_zip_filename(zf) == 'hosts' assert adblock.guess_zip_filename(zf) == 'hosts'
# guess_zip_filename doesn't include the root slash /
# whereas os.path.join() does, so we exclude first character
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"""
zf = create_zipfile(['testa','testb','testd','testc'], tmpdir)[0] names = ['test_a', 'test_b', 'test_d', 'test_c']
zf = create_zipfile(names, tmpdir)[0]
with pytest.raises(FileNotFoundError): with pytest.raises(FileNotFoundError):
adblock.guess_zip_filename(zf) adblock.guess_zip_filename(zf)
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):
zf_name = create_zipfile(['testa','testb','hosts','testc'], tmpdir)[1] names = ['test_a', 'test_b', 'hosts', 'test_c']
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"
@ -72,36 +99,34 @@ class TestGetFileObj :
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"""
# FIXME Error since we deleted host-blocking-whitelist
# If we don't remove host-block-whitelist, test behaves as in a mismatch
# def test_with_no_whitelist(self): # def test_with_no_whitelist(self):
# config_obj = config.ConfigManager(configdir=None, fname=None, relaxed=True) # config_obj = config.ConfigManager(configdir=None, fname=None, relaxed=True)
# config_obj.remove_option('content','host-blocking-whitelist')
# objreg.register('config', config_obj) # objreg.register('config', config_obj)
# assert adblock.is_whitelisted_host('pimpmytest.com') == False # assert adblock.is_whitelisted_host('pimpmytest.com') == False
# objreg.delete('config') # objreg.delete('config')
def test_with_no_whitelist(self):
# FIXME Behaves like a mismatch
config_obj = config.ConfigManager(configdir=None, fname=None, relaxed=True)
default_config.remove_option('content','host-blocking-whitelist')
objreg.register('config', config_obj)
assert adblock.is_whitelisted_host('pimpmytest.com') == False
objreg.delete('config')
def test_with_match(self): def test_with_match(self):
config_obj = config.ConfigManager(configdir=None, fname=None, relaxed=True) config_obj = config.ConfigManager(configdir=None, fname=None, relaxed=True)
config_obj.set('conf','content','host-blocking-whitelist','qutebrowser.org') config_obj.set('conf', 'content', 'host-blocking-whitelist', 'qutebrowser.org')
objreg.register('config', config_obj) objreg.register('config', config_obj)
assert adblock.is_whitelisted_host('qutebrowser.org') == True assert adblock.is_whitelisted_host('qutebrowser.org') == True
objreg.delete('config') objreg.delete('config')
def test_without_match(self): def test_without_match(self):
config_obj = config.ConfigManager(configdir=None, fname=None, relaxed=True) config_obj = config.ConfigManager(configdir=None, fname=None, relaxed=True)
config_obj.set('conf','content','host-blocking-whitelist','cutebrowser.org') config_obj.set('conf', 'content', 'host-blocking-whitelist', 'cutebrowser.org')
objreg.register('config', config_obj) objreg.register('config', config_obj)
assert adblock.is_whitelisted_host('qutebrowser.org') == False assert adblock.is_whitelisted_host('qutebrowser.org') == False
objreg.delete('config') objreg.delete('config')
class TestHostBlocker :
class TestHostBlocker:
# TODO
pass pass
#testBlocker = adblock.HostBlocker()