| 1 |
import os |
| 2 |
import shutil |
| 3 |
import subprocess |
| 4 |
import time |
| 5 |
import unittest |
| 6 |
from typing import List |
| 7 |
|
| 8 |
from django_sass import find_static_paths, find_static_scss |
| 9 |
|
| 10 |
|
| 11 |
THIS_DIR = os.path.dirname(os.path.abspath(__file__)) |
| 12 |
|
| 13 |
SCSS_CONTAINS = [ |
| 14 |
"/* Tests: app1/scss/_include.scss */", |
| 15 |
"/* Tests: app2/scss/_samedir.scss */", |
| 16 |
"/* Tests: app2/scss/subdir/_subdir.scss */", |
| 17 |
"/* Tests: app2/scss/test.scss */", |
| 18 |
] |
| 19 |
|
| 20 |
|
| 21 |
class TestDjangoSass(unittest.TestCase): |
| 22 |
def setUp(self): |
| 23 |
self.outdir = os.path.join(THIS_DIR, "out") |
| 24 |
|
| 25 |
def tearDown(self): |
| 26 |
|
| 27 |
shutil.rmtree(self.outdir, ignore_errors=True) |
| 28 |
|
| 29 |
def assert_output( |
| 30 |
self, |
| 31 |
inpath: str, |
| 32 |
outpath: str, |
| 33 |
real_outpath: str, |
| 34 |
contains: List[str], |
| 35 |
args: List[str] = None, |
| 36 |
): |
| 37 |
|
| 38 |
args = args or [] |
| 39 |
cmd = ["python", "manage.py", "sass", *args, inpath, outpath] |
| 40 |
|
| 41 |
proc = subprocess.run(cmd, cwd=THIS_DIR) |
| 42 |
|
| 43 |
self.assertEqual(proc.returncode, 0) |
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
with open(real_outpath, encoding="utf8") as f: |
| 49 |
contents = f.read() |
| 50 |
for compiled_data in contains: |
| 51 |
self.assertTrue(compiled_data in contents) |
| 52 |
|
| 53 |
def test_find_static_paths(self): |
| 54 |
paths = find_static_paths() |
| 55 |
|
| 56 |
self.assertTrue(os.path.join(THIS_DIR, "app1", "static") in paths) |
| 57 |
self.assertTrue(os.path.join(THIS_DIR, "app2", "static") in paths) |
| 58 |
|
| 59 |
def test_find_static_sass(self): |
| 60 |
files = find_static_scss() |
| 61 |
|
| 62 |
self.assertTrue( |
| 63 |
os.path.join( |
| 64 |
THIS_DIR, "app1", "static", "app1", "scss", "_include.scss" |
| 65 |
) |
| 66 |
in files |
| 67 |
) |
| 68 |
self.assertTrue( |
| 69 |
os.path.join( |
| 70 |
THIS_DIR, "app2", "static", "app2", "scss", "_samedir.scss" |
| 71 |
) |
| 72 |
in files |
| 73 |
) |
| 74 |
self.assertTrue( |
| 75 |
os.path.join( |
| 76 |
THIS_DIR, "app2", "static", "app2", "scss", "test.scss" |
| 77 |
) |
| 78 |
in files |
| 79 |
) |
| 80 |
self.assertTrue( |
| 81 |
os.path.join( |
| 82 |
THIS_DIR, |
| 83 |
"app2", |
| 84 |
"static", |
| 85 |
"app2", |
| 86 |
"scss", |
| 87 |
"subdir", |
| 88 |
"_subdir.scss", |
| 89 |
) |
| 90 |
in files |
| 91 |
) |
| 92 |
self.assertTrue( |
| 93 |
os.path.join( |
| 94 |
THIS_DIR, "app3", "static", "app3", "sass", "indent_test.sass" |
| 95 |
) |
| 96 |
in files |
| 97 |
) |
| 98 |
|
| 99 |
def test_cli(self): |
| 100 |
|
| 101 |
inpath = os.path.join("app2", "static", "app2", "scss", "test.scss") |
| 102 |
outpath = os.path.join(self.outdir, "test_file.css") |
| 103 |
self.assert_output( |
| 104 |
inpath=inpath, |
| 105 |
outpath=outpath, |
| 106 |
real_outpath=outpath, |
| 107 |
contains=SCSS_CONTAINS, |
| 108 |
) |
| 109 |
|
| 110 |
def test_cli_dir(self): |
| 111 |
|
| 112 |
inpath = os.path.join("app2", "static", "app2", "scss") |
| 113 |
|
| 114 |
real_outpath = os.path.join(self.outdir, "test.css") |
| 115 |
self.assert_output( |
| 116 |
inpath=inpath, |
| 117 |
outpath=self.outdir, |
| 118 |
real_outpath=real_outpath, |
| 119 |
contains=SCSS_CONTAINS, |
| 120 |
) |
| 121 |
|
| 122 |
def test_cli_infile_outdir(self): |
| 123 |
|
| 124 |
inpath = os.path.join("app2", "static", "app2", "scss", "test.scss") |
| 125 |
outpath = os.path.join(self.outdir, "does-not-exist") |
| 126 |
|
| 127 |
real_outpath = os.path.join(outpath, "test.css") |
| 128 |
self.assert_output( |
| 129 |
inpath=inpath, |
| 130 |
outpath=outpath, |
| 131 |
real_outpath=real_outpath, |
| 132 |
contains=SCSS_CONTAINS, |
| 133 |
) |
| 134 |
|
| 135 |
def test_sass_compiles(self): |
| 136 |
|
| 137 |
inpath = os.path.join("app3", "static", "app3", "sass") |
| 138 |
|
| 139 |
real_outpath = os.path.join(self.outdir, "indent_test.css") |
| 140 |
self.assert_output( |
| 141 |
inpath=inpath, |
| 142 |
outpath=self.outdir, |
| 143 |
real_outpath=real_outpath, |
| 144 |
contains=["/* Tests: app3/sass/indent_test.sass */"], |
| 145 |
) |
| 146 |
|
| 147 |
def test_cli_srcmap(self): |
| 148 |
|
| 149 |
inpath = os.path.join("app2", "static", "app2", "scss", "test.scss") |
| 150 |
outpath = os.path.join(self.outdir, "test.css") |
| 151 |
self.assert_output( |
| 152 |
inpath=inpath, |
| 153 |
outpath=outpath, |
| 154 |
real_outpath=outpath, |
| 155 |
contains=SCSS_CONTAINS, |
| 156 |
args=["-g"], |
| 157 |
) |
| 158 |
self.assertTrue( |
| 159 |
os.path.isfile(os.path.join(self.outdir, "test.css.map")) |
| 160 |
) |
| 161 |
|
| 162 |
@unittest.skip("Test needs fixed...") |
| 163 |
def test_cli_watch(self): |
| 164 |
|
| 165 |
inpath = os.path.join("app2", "static", "app2", "scss", "test.scss") |
| 166 |
outpath = os.path.join(self.outdir, "test.css") |
| 167 |
|
| 168 |
cmd = ["python", "manage.py", "sass", "--watch", inpath, outpath] |
| 169 |
|
| 170 |
proc = subprocess.Popen(cmd, cwd=THIS_DIR) |
| 171 |
time.sleep(0.5) |
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
try: |
| 176 |
proc.send_signal(subprocess.signal.CTRL_C_EVENT) |
| 177 |
except KeyboardInterrupt: |
| 178 |
|
| 179 |
pass |
| 180 |
returncode = proc.wait() |
| 181 |
|
| 182 |
self.assertEqual(returncode, 0) |
| 183 |
|
| 184 |
self.assert_output(outpath) |