Skip to content

Commit

Permalink
Merge 4f42625 into 82bd32b
Browse files Browse the repository at this point in the history
  • Loading branch information
bilelmoussaoui committed Jun 6, 2019
2 parents 82bd32b + 4f42625 commit cc2be1a
Show file tree
Hide file tree
Showing 7 changed files with 108 additions and 127 deletions.
7 changes: 5 additions & 2 deletions pyfavicon/__init__.py
Expand Up @@ -123,7 +123,7 @@ def size(self) -> (int, int):
return self._size

@property
def path(self) -> str:
def path(self) -> pathlib.Path:
return self._path

@property
Expand Down Expand Up @@ -206,7 +206,7 @@ def _generate_icon_name(self, website_url: yarl.URL) -> str:
if Favicon.DOWNLOAD_DIR:
self._path = Favicon.DOWNLOAD_DIR.joinpath(image_name)
else:
self._path = os.path.join(gettempdir(), image_name)
self._path = pathlib.Path(gettempdir()).joinpath(image_name)


class Icons:
Expand Down Expand Up @@ -241,6 +241,9 @@ def append(self, icon: Icon):

def __iter__(self):
return self

def __len__(self):
return len(self._data)

def __next__(self):
if self._current >= len(self._data):
Expand Down
3 changes: 2 additions & 1 deletion setup.py
Expand Up @@ -30,7 +30,8 @@
tests_require=[
'pytest',
'coveralls',
'pytest-cov'
'pytest-cov',
'pytest-asyncio'
],
test_suite='tests',
)
66 changes: 36 additions & 30 deletions tests/test_base64_favicon.py
@@ -1,43 +1,49 @@
import unittest
import asyncio
from pyfavicon import Favicon, FaviconType
from pathlib import Path
import filecmp
import tempfile
import pytest


class HTMLTest(unittest.TestCase):
favicon = Favicon(download_dir=Path(tempfile.gettempdir()))
html_file = Path('./tests/html/base64_favicon_link.html')

def setUp(self):
self.favicon = Favicon(download_dir=Path(tempfile.gettempdir()))

def test_url_icon_link_type(self):
files = [
Path('./tests/html/base64_favicon_link.html'),
]
@pytest.mark.asyncio
async def test_base64_type():
favicons = await favicon.from_file(html_file)
icon = favicons[0]
assert icon.type is FaviconType.DATA

async def run_test():
for html_file in files:
favicons = await self.favicon.from_file(html_file)
icon = favicons[0]
self.assertEqual(icon.type, FaviconType.DATA)
# Ensure that save works correctly
self.assertTupleEqual(icon.size, (16, 16))
self.assertEqual(icon.extension, 'ico')
await icon.save()
self.assertTrue(icon.path.exists())
# Compare file content
temp_file = Path(tempfile.NamedTemporaryFile().name)
with temp_file.open('wb') as fd:
fd.write(icon.data)
self.assertTrue(filecmp.cmp(temp_file, icon.path))
@pytest.mark.asyncio
async def test_base64_size():
favicons = await favicon.from_file(html_file)
icon = favicons[0]

assert icon.size == (16, 16)

# Remove the test file
temp_file.unlink()
icon.path.unlink()
@pytest.mark.asyncio
async def test_base64_extension():
favicons = await favicon.from_file(html_file)
icon = favicons[0]

assert icon.extension == 'ico'

asyncio.run(run_test())
@pytest.mark.asyncio
async def test_base64_save():
favicons = await favicon.from_file(html_file)
icon = favicons[0]

await icon.save()
assert icon.path.exists()

# Compare file content
temp_file = Path(tempfile.NamedTemporaryFile().name)
with temp_file.open('wb') as fd:
fd.write(icon.data)
assert filecmp.cmp(temp_file, icon.path)

# Remove the test file
temp_file.unlink()
icon.path.unlink()

if __name__ == '__main__':
unittest.main()
32 changes: 10 additions & 22 deletions tests/test_download_favicon.py
@@ -1,28 +1,16 @@
import unittest
import asyncio
from pyfavicon import Favicon
from pathlib import Path
import tempfile
import yarl
import pytest

favicon = Favicon(download_dir=Path(tempfile.gettempdir()))

class HTMLTest(unittest.TestCase):

def setUp(self):
self.favicon = Favicon(download_dir=Path(tempfile.gettempdir()))

def test_url_icon_link_type(self):

async def run_test():
icons = await self.favicon.from_url(yarl.URL('https://gitlab.com'))
icon = icons[0]
# Ensure that save works correctly
await icon.save()
self.assertTrue(icon.path.exists())
# Remove the test file
icon.path.unlink()
asyncio.run(run_test())


if __name__ == '__main__':
unittest.main()
@pytest.mark.asyncio
async def test_icon_download():
icons = await favicon.from_url('https://gitlab.com')
for icon in icons:
await icon.save()
assert icon.path.exists()

icon.path.unlink()
26 changes: 11 additions & 15 deletions tests/test_favicon_size.py
@@ -1,7 +1,5 @@
import unittest
import asyncio
from pyfavicon import Favicon

import pytest

GITLAB_FAVICONS = {
'https://about.gitlab.com/ico/favicon.ico': (-1, -1),
Expand All @@ -22,19 +20,17 @@
'https://about.gitlab.com/ico/mstile-144x144.png': (144, 144)
}

favicon = Favicon()

class HTMLTest(unittest.TestCase):

def setUp(self):
self.favicon = Favicon()
@pytest.mark.asyncio
async def test_icon_size():
icons = await favicon.from_url('https://gitlab.com')
assert len(icons) != 0

def test_icon_sizes(self):
async def run_test():
icons = await self.favicon.from_url('https://gitlab.com')
for icon in icons:
self.assertEqual(GITLAB_FAVICONS[str(icon.link)], icon.size)
for icon in icons:
assert GITLAB_FAVICONS[str(icon.link)] == icon.size

largest = icons.get_largest(extension='png')
self.assertEqual(largest.size, (190, 175))
self.assertEqual(largest.extension, 'png')
asyncio.run(run_test())
largest = icons.get_largest(extension='png')
assert largest.size == (190, 175)
assert largest.extension == 'png'
24 changes: 9 additions & 15 deletions tests/test_favicon_url.py
@@ -1,6 +1,5 @@
import unittest
import asyncio
from pyfavicon import Favicon
import pytest

CASES = [
('<link rel="icon" href="/favicon.ico">', 'https://gitlab.com/favicon.ico'),
Expand All @@ -14,20 +13,15 @@
('<link rel="shortcut icon" href="favicon.ico" />', 'https://gitlab.com/favicon.ico')
]

favicon = Favicon()

class TestFaviconUrl(unittest.TestCase):
def setUp(self):
self.favicon = Favicon()

def test_favicon_url(self):
async def run_tests():
favicon = Favicon()
for html_content, expected_result in CASES:
icons = await favicon.from_html(html_content,
"https://gitlab.com")
self.assertEqual(str(icons[0].link), expected_result)
asyncio.run(run_tests())
@pytest.mark.asyncio
async def test_favicon_url():
for html_content, expected_result in CASES:
icons = await favicon.from_html(html_content,
"https://gitlab.com")

assert len(icons) != 0
assert str(icons[0].link) == expected_result

if __name__ == "__main__":
unittest.main()
77 changes: 35 additions & 42 deletions tests/test_from_html.py
@@ -1,58 +1,51 @@
import unittest
import asyncio
from pyfavicon import Favicon, FaviconType
from pathlib import Path
import pytest

favicon = Favicon()

class HTMLTest(unittest.TestCase):

def setUp(self):
self.favicon = Favicon()
@pytest.mark.asyncio
async def test_link_tag():
files = [
Path('./tests/html/url_icon_link.html'),
Path('./tests/html/url_shortcut_icon_link.html'),
Path('./tests/html/url_apple_touch_icon_precomposed_link.html'),
Path('./tests/html/url_apple_touch_icon_link.html'),
Path('./tests/html/url_fluid_icon_link.html'),
]

def test_url_icon_link_type(self):
files = [
Path('./tests/html/url_icon_link.html'),
Path('./tests/html/url_shortcut_icon_link.html'),
Path('./tests/html/url_apple_touch_icon_precomposed_link.html'),
Path('./tests/html/url_apple_touch_icon_link.html'),
Path('./tests/html/url_fluid_icon_link.html'),
]
for html_file in files:
icons = await favicon.from_file(html_file,
'https://github.com')
assert len(icons) != 0
icon = icons[0]

assert icon.type is FaviconType.URL
assert str(icon.link) == 'https://github.githubassets.com/favicon.ico'

async def run_test():
for html_file in files:
favicons = await self.favicon.from_file(html_file,
'https://github.com')
icon = favicons[0]

self.assertEqual(icon.type, FaviconType.URL)
self.assertEqual(str(icon.link),
'https://github.githubassets.com/favicon.ico')
asyncio.run(run_test())
@pytest.mark.asyncio
async def test_meta_tag():
html_file = Path('./tests/html/meta_favicon.html')

def test_meta_link(self):
html_file = Path('./tests/html/meta_favicon.html')
icons = await favicon.from_file(html_file, 'https://gitlab.com')
assert len(icons) != 0

async def run_test():
icons = await self.favicon.from_file(html_file,
'https://gitlab.com')
icon = icons[0]
icon = icons[0]

self.assertEqual(icon.type, FaviconType.URL)
self.assertEqual(str(icon.link),
'https://assets.gitlab-static.net/assets/msapplication-tile-1196ec67452f618d39cdd85e2e3a542f76574c071051ae7effbfde01710eb17d.png')
asyncio.run(run_test())
assert icon.type is FaviconType.URL
assert str(icon.link) == 'https://assets.gitlab-static.net/assets/msapplication-tile-1196ec67452f618d39cdd85e2e3a542f76574c071051ae7effbfde01710eb17d.png'

def test_largest_icon(self):
html_file = Path('./tests/html/largest_gitlab.html')

async def run_tests():
icons = await self.favicon.from_file(html_file)
@pytest.mark.asyncio
async def test_largest_icon():
html_file = Path('./tests/html/largest_gitlab.html')

largest_icon = icons.get_largest()
self.assertTupleEqual(largest_icon.size, (188, 188))
icons = await favicon.from_file(html_file)
assert len(icons) != 0

asyncio.run(run_tests())
largest_icon = icons.get_largest()
assert largest_icon


if __name__ == '__main__':
unittest.main()
assert largest_icon.size == (188, 188)

0 comments on commit cc2be1a

Please sign in to comment.