2017-06-27 09:46:55 +02:00
|
|
|
"""Test export functions."""
|
2017-06-27 09:02:42 +02:00
|
|
|
import unittest
|
2017-07-23 12:53:33 +02:00
|
|
|
import unittest.mock
|
|
|
|
import io
|
2017-08-15 13:39:16 +02:00
|
|
|
import shutil
|
2017-08-10 01:17:11 +02:00
|
|
|
import os
|
2017-06-27 09:24:27 +02:00
|
|
|
|
2017-07-23 03:50:46 +02:00
|
|
|
from pywal import export
|
2017-06-27 09:02:42 +02:00
|
|
|
from pywal import util
|
|
|
|
|
|
|
|
|
2017-06-29 04:15:41 +02:00
|
|
|
COLORS = util.read_file_json("tests/test_files/test_file.json")
|
2017-07-22 15:50:40 +02:00
|
|
|
COLORS["colors"].update(COLORS["special"])
|
2017-07-22 15:48:47 +02:00
|
|
|
|
2017-08-15 13:39:16 +02:00
|
|
|
TMP_DIR = "/tmp/wal"
|
2017-06-27 09:24:27 +02:00
|
|
|
|
|
|
|
|
2017-06-27 09:02:42 +02:00
|
|
|
class TestExportColors(unittest.TestCase):
|
2017-06-30 01:54:10 +02:00
|
|
|
"""Test the export functions."""
|
2017-06-27 09:02:42 +02:00
|
|
|
|
2017-08-15 13:39:16 +02:00
|
|
|
def setUp(self):
|
|
|
|
"""> Setup export tests."""
|
|
|
|
util.create_dir(TMP_DIR)
|
2017-07-22 15:48:47 +02:00
|
|
|
|
2017-08-15 13:39:16 +02:00
|
|
|
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)
|
2017-07-22 15:48:47 +02:00
|
|
|
self.assertTrue(result)
|
|
|
|
|
2017-08-15 13:39:16 +02:00
|
|
|
def is_file_contents(self, tmp_file, pattern):
|
|
|
|
"""> Check for pattern in file."""
|
|
|
|
content = util.read_file(tmp_file)
|
2017-08-15 13:47:02 +02:00
|
|
|
self.assertEqual(content[6], pattern)
|
2017-08-10 01:17:11 +02:00
|
|
|
|
2017-08-15 13:39:16 +02:00
|
|
|
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'")
|
2017-07-22 15:48:47 +02:00
|
|
|
|
|
|
|
def test_css_template(self):
|
|
|
|
"""> Test substitutions in template file (css)."""
|
2017-08-15 13:39:16 +02:00
|
|
|
tmp_file = os.path.join(TMP_DIR, "test.css")
|
|
|
|
export.color(COLORS, "css", tmp_file)
|
2017-06-29 11:52:43 +02:00
|
|
|
|
2017-08-15 13:39:16 +02:00
|
|
|
self.is_file(tmp_file)
|
|
|
|
self.is_file_contents(tmp_file, " --background: #1F211E;")
|
2017-06-27 09:02:42 +02:00
|
|
|
|
2017-08-10 01:17:11 +02:00
|
|
|
|
2017-08-15 13:39:16 +02:00
|
|
|
class TestInvalidExport(unittest.TestCase):
|
|
|
|
"""Test export function error handling."""
|
2017-06-29 16:47:13 +02:00
|
|
|
|
2017-07-23 12:53:33 +02:00
|
|
|
def test_invalid_template(self):
|
|
|
|
"""> Test template validation."""
|
2017-08-09 03:22:51 +02:00
|
|
|
error_msg = "warning: template 'dummy' doesn't exist."
|
2017-08-15 13:39:16 +02:00
|
|
|
tmp_file = os.path.join(TMP_DIR, "test.css")
|
2017-07-23 12:53:33 +02:00
|
|
|
|
|
|
|
# Since this function prints a message on fail we redirect
|
|
|
|
# it's output so that we can read it.
|
|
|
|
with unittest.mock.patch('sys.stdout', new=io.StringIO()) as fake_out:
|
2017-08-15 13:39:16 +02:00
|
|
|
export.color(COLORS, "dummy", tmp_file)
|
2017-07-23 12:53:33 +02:00
|
|
|
self.assertEqual(fake_out.getvalue().strip(), error_msg)
|
|
|
|
|
2017-06-27 09:02:42 +02:00
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
unittest.main()
|