Fixing a few tests

This commit is contained in:
Michael Lazar
2017-09-20 23:26:51 -04:00
parent fc2cd296e4
commit db07149e4a
3 changed files with 92 additions and 71 deletions

View File

@@ -503,8 +503,8 @@ class Theme(object):
r = round(int(color[1:3], 16) / 51.0) # Normalize between 0-5 r = round(int(color[1:3], 16) / 51.0) # Normalize between 0-5
g = round(int(color[3:5], 16) / 51.0) g = round(int(color[3:5], 16) / 51.0)
b = round(int(color[5:7], 16) / 51.0) b = round(int(color[5:7], 16) / 51.0)
n = 36 * r + 6 * g + b + 16 n = int(36 * r + 6 * g + b + 16)
return 'ansi_{0}'.format(n) return 'ansi_{0:d}'.format(n)
except ValueError: except ValueError:
return None return None

View File

@@ -141,7 +141,7 @@ def test_page_cycle_theme(reddit, terminal, config, oauth):
# Previous - will loop to one of the 256 color themes # Previous - will loop to one of the 256 color themes
page.controller.trigger(curses.KEY_F2) page.controller.trigger(curses.KEY_F2)
assert page.term.theme.source == 'installed' assert page.term.theme.source in ('preset', 'installed')
# Reset # Reset
page.term.set_theme() page.term.set_theme()

View File

@@ -1,11 +1,12 @@
import os import os
import shutil
import curses import curses
from collections import OrderedDict from collections import OrderedDict
from tempfile import NamedTemporaryFile from contextlib import contextmanager
from tempfile import mkdtemp, NamedTemporaryFile
import pytest import pytest
from rtv.theme import Theme from rtv.theme import Theme
from rtv.config import DEFAULT_THEMES from rtv.config import DEFAULT_THEMES
from rtv.exceptions import ConfigError from rtv.exceptions import ConfigError
@@ -28,6 +29,21 @@ INVALID_ELEMENTS = OrderedDict([
]) ])
@contextmanager
def _ephemeral_directory():
# All of the temporary files for the theme tests must
# be initialized in separate directories, so the tests
# can run in parallel without accidentally loading theme
# files from each other
dirname = None
try:
dirname = mkdtemp()
yield dirname
finally:
if dirname:
shutil.rmtree(dirname, ignore_errors=True)
def test_theme_invalid_source(): def test_theme_invalid_source():
with pytest.raises(ValueError): with pytest.raises(ValueError):
@@ -115,7 +131,8 @@ args, ids = INVALID_ELEMENTS.values(), list(INVALID_ELEMENTS)
@pytest.mark.parametrize('line', args, ids=ids) @pytest.mark.parametrize('line', args, ids=ids)
def test_theme_from_file_invalid(line): def test_theme_from_file_invalid(line):
with NamedTemporaryFile(mode='w+') as fp: with _ephemeral_directory() as dirname:
with NamedTemporaryFile(mode='w+', dir=dirname) as fp:
fp.write('[theme]\n') fp.write('[theme]\n')
fp.write(line) fp.write(line)
fp.flush() fp.flush()
@@ -125,7 +142,8 @@ def test_theme_from_file_invalid(line):
def test_theme_from_file(): def test_theme_from_file():
with NamedTemporaryFile(mode='w+') as fp: with _ephemeral_directory() as dirname:
with NamedTemporaryFile(mode='w+', dir=dirname) as fp:
with pytest.raises(ConfigError): with pytest.raises(ConfigError):
Theme.from_file(fp.name, 'installed') Theme.from_file(fp.name, 'installed')
@@ -150,7 +168,8 @@ def test_theme_from_file():
def test_theme_from_name(): def test_theme_from_name():
with NamedTemporaryFile(mode='w+', suffix='.cfg') as fp: with _ephemeral_directory() as dirname:
with NamedTemporaryFile(mode='w+', suffix='.cfg', dir=dirname) as fp:
path, filename = os.path.split(fp.name) path, filename = os.path.split(fp.name)
theme_name = filename[:-4] theme_name = filename[:-4]
@@ -203,7 +222,8 @@ def test_theme_initialize_attributes_monochrome(stdscr):
def test_theme_list_themes(): def test_theme_list_themes():
with NamedTemporaryFile(mode='w+', suffix='.cfg') as fp: with _ephemeral_directory() as dirname:
with NamedTemporaryFile(mode='w+', suffix='.cfg', dir=dirname) as fp:
path, filename = os.path.split(fp.name) path, filename = os.path.split(fp.name)
theme_name = filename[:-4] theme_name = filename[:-4]
@@ -223,7 +243,8 @@ def test_theme_list_themes():
def test_theme_list_themes_invalid(): def test_theme_list_themes_invalid():
with NamedTemporaryFile(mode='w+', suffix='.cfg') as fp: with _ephemeral_directory() as dirname:
with NamedTemporaryFile(mode='w+', suffix='.cfg', dir=dirname) as fp:
path, filename = os.path.split(fp.name) path, filename = os.path.split(fp.name)
theme_name = filename[:-4] theme_name = filename[:-4]
@@ -248,4 +269,4 @@ def test_theme_presets_define_all_elements():
super(MockTheme, self).__init__(name, source, elements, use_color) super(MockTheme, self).__init__(name, source, elements, use_color)
themes, errors = MockTheme.list_themes() themes, errors = MockTheme.list_themes()
assert sum(theme.source == 'preset' for theme in themes) >= 4 assert sum([theme.source == 'preset' for theme in themes]) >= 4