tests [export]: Properly isolate test cases and remove duplicate code.

This commit is contained in:
Dylan Araps 2017-08-15 21:39:16 +10:00
parent 36e1c9d402
commit d07260470e

View File

@ -2,54 +2,69 @@
import unittest import unittest
import unittest.mock import unittest.mock
import io import io
import shutil
import os import os
from pywal import export from pywal import export
from pywal import util from pywal import util
# Import colors.
COLORS = util.read_file_json("tests/test_files/test_file.json") COLORS = util.read_file_json("tests/test_files/test_file.json")
COLORS["colors"].update(COLORS["special"]) COLORS["colors"].update(COLORS["special"])
util.create_dir("/tmp/wal") TMP_DIR = "/tmp/wal"
class TestExportColors(unittest.TestCase): class TestExportColors(unittest.TestCase):
"""Test the export functions.""" """Test the export functions."""
def test_all_templates(self): def setUp(self):
"""> Test substitutions in template file.""" """> Setup export tests."""
export.every(COLORS, "/tmp/wal") util.create_dir(TMP_DIR)
result = os.path.isfile("/tmp/wal/colors.sh") def tearDown(self):
"""> Clean up export tests."""
shutil.rmtree(TMP_DIR, ignore_errors=True)
def is_file(self, tmp_file):
"""> Test is something is a file."""
result = os.path.isfile(tmp_file)
self.assertTrue(result) self.assertTrue(result)
with open("/tmp/wal/colors.sh") as file: def is_file_contents(self, tmp_file, pattern):
content = file.read().splitlines() """> Check for pattern in file."""
content = util.read_file(tmp_file)
self.assertEqual(content[7], pattern)
self.assertEqual(content[6], "foreground='#F5F1F4'") def test_all_templates(self):
"""> Test substitutions in template file."""
tmp_file = os.path.join(TMP_DIR, "colors.sh")
export.every(COLORS, TMP_DIR)
self.is_file(tmp_file)
self.is_file_contents(tmp_file, "foreground='#F5F1F4'")
def test_css_template(self): def test_css_template(self):
"""> Test substitutions in template file (css).""" """> Test substitutions in template file (css)."""
export.color(COLORS, "css", "/tmp/wal/test.css") tmp_file = os.path.join(TMP_DIR, "test.css")
export.color(COLORS, "css", tmp_file)
result = os.path.isfile("/tmp/wal/test.css") self.is_file(tmp_file)
self.assertTrue(result) self.is_file_contents(tmp_file, " --background: #1F211E;")
with open("/tmp/wal/test.css") as file:
content = file.read().splitlines()
self.assertEqual(content[6], " --background: #1F211E;") class TestInvalidExport(unittest.TestCase):
"""Test export function error handling."""
def test_invalid_template(self): def test_invalid_template(self):
"""> Test template validation.""" """> Test template validation."""
error_msg = "warning: template 'dummy' doesn't exist." error_msg = "warning: template 'dummy' doesn't exist."
tmp_file = os.path.join(TMP_DIR, "test.css")
# Since this function prints a message on fail we redirect # Since this function prints a message on fail we redirect
# it's output so that we can read it. # it's output so that we can read it.
with unittest.mock.patch('sys.stdout', new=io.StringIO()) as fake_out: with unittest.mock.patch('sys.stdout', new=io.StringIO()) as fake_out:
export.color(COLORS, "dummy", "/tmp/wal/test.css") export.color(COLORS, "dummy", tmp_file)
self.assertEqual(fake_out.getvalue().strip(), error_msg) self.assertEqual(fake_out.getvalue().strip(), error_msg)