|
| 1 | +from typing import List |
| 2 | +import os |
| 3 | +import shutil |
| 4 | +import subprocess |
| 5 | +import time |
| 6 | +import unittest |
| 7 | + |
| 8 | +from django.test.testcases import TestCase |
| 9 | + |
| 10 | + |
| 11 | + |
| 12 | +THIS_DIR = os.path.dirname(os.path.abspath(__file__)) |
| 13 | + |
| 14 | + |
| 15 | +class TestDjangoSass(TestCase): |
| 16 | + |
| 17 | + def setUp(self): |
| 18 | + self.outdir = os.path.join(THIS_DIR, "out") |
| 19 | + |
| 20 | + def tearDown(self): |
| 21 | + # Clean up output files |
| 22 | + shutil.rmtree(self.outdir, ignore_errors=True) |
| 23 | + |
| 24 | + def assert_output(self, real_outpath: str): |
| 25 | + # Verify that the output file exists. |
| 26 | + print(real_outpath) |
| 27 | + self.assertTrue(os.path.isfile(real_outpath)) |
| 28 | + |
| 29 | + # Verify that the file contains expected output from all sass files. |
| 30 | + with open(real_outpath, encoding="utf8") as f: |
| 31 | + contents = f.read() |
| 32 | + self.assertTrue("/* Tests: app1/scss/_include.scss */" in contents) |
| 33 | + self.assertTrue("/* Tests: app2/scss/_samedir.scss */" in contents) |
| 34 | + self.assertTrue("/* Tests: app2/scss/subdir/_subdir.scss */" in contents) |
| 35 | + self.assertTrue("/* Tests: app2/scss/test.scss */" in contents) |
| 36 | + |
| 37 | + def test_cli(self): |
| 38 | + # Input and output paths relative to django static dirs. |
| 39 | + inpath = os.path.join("app2", "static", "app2", "scss", "test.scss") |
| 40 | + outpath = os.path.join(self.outdir, "test_file.css") |
| 41 | + # Command to run |
| 42 | + cmd = ["python", "manage.py", "sass", inpath, outpath] |
| 43 | + # Run the management command on testproject. |
| 44 | + proc = subprocess.run(cmd, cwd=THIS_DIR) |
| 45 | + # Verify the process exited cleanly. |
| 46 | + self.assertEqual(proc.returncode, 0) |
| 47 | + # Assert output is correct. |
| 48 | + self.assert_output(outpath) |
| 49 | + |
| 50 | + def test_cli_dir(self): |
| 51 | + # Input and output paths relative to django static dirs. |
| 52 | + inpath = os.path.join("app2", "static", "app2", "scss") |
| 53 | + outpath = self.outdir |
| 54 | + # Expected output path on filesystem. |
| 55 | + real_outpath = os.path.join(self.outdir, "test.css") |
| 56 | + # Command to run |
| 57 | + cmd = ["python", "manage.py", "sass", inpath, outpath] |
| 58 | + # Run the management command on testproject. |
| 59 | + proc = subprocess.run(cmd, cwd=THIS_DIR) |
| 60 | + # Verify the process exited cleanly. |
| 61 | + self.assertEqual(proc.returncode, 0) |
| 62 | + # Assert output is correct. |
| 63 | + self.assert_output(real_outpath) |
| 64 | + |
| 65 | + def test_cli_srcmap(self): |
| 66 | + # Input and output paths relative to django static dirs. |
| 67 | + inpath = os.path.join("app2", "static", "app2", "scss", "test.scss") |
| 68 | + outpath = os.path.join(self.outdir, "test.css") |
| 69 | + # Command to run |
| 70 | + cmd = ["python", "manage.py", "sass", "-g", inpath, outpath] |
| 71 | + # Run the management command on testproject. |
| 72 | + proc = subprocess.run(cmd, cwd=THIS_DIR) |
| 73 | + # Verify the process exited cleanly. |
| 74 | + self.assertEqual(proc.returncode, 0) |
| 75 | + # Assert output is correct. |
| 76 | + self.assert_output(outpath) |
| 77 | + self.assertTrue(os.path.isfile(os.path.join(self.outdir, "test.css.map"))) |
| 78 | + |
| 79 | + @unittest.skip |
| 80 | + def test_cli_watch(self): |
| 81 | + # Input and output paths relative to django static dirs. |
| 82 | + inpath = os.path.join("app2", "static", "app2", "scss", "test.scss") |
| 83 | + outpath = os.path.join(self.outdir, "test.css") |
| 84 | + # Command to run |
| 85 | + cmd = ["python", "manage.py", "sass", "--watch", inpath, outpath] |
| 86 | + # Run the management command on testproject. |
| 87 | + proc = subprocess.Popen(cmd, cwd=THIS_DIR) |
| 88 | + time.sleep(0.5) |
| 89 | + # TODO: This test is not working. Do not know how to intentionally send |
| 90 | + # a KeyboardInterrupt to the subprocess without having unittest/pytest |
| 91 | + # immediately die when it sees the interrupt. |
| 92 | + try: |
| 93 | + proc.send_signal(subprocess.signal.CTRL_C_EVENT) |
| 94 | + except KeyboardInterrupt: |
| 95 | + # We actually want the keyboard interrupt. |
| 96 | + pass |
| 97 | + returncode = proc.wait() |
| 98 | + # Verify the process exited cleanly. |
| 99 | + self.assertEqual(returncode, 0) |
| 100 | + # Assert output is correct. |
| 101 | + self.assert_output(outpath) |
0 commit comments