Skip to content

Commit 293dc65

Browse files
committed
Adding unit tests
1 parent 2e34078 commit 293dc65

File tree

6 files changed

+119
-10
lines changed

6 files changed

+119
-10
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,7 @@ coverage.xml
255255
*.cover
256256
.hypothesis/
257257
.pytest_cache/
258+
junit/
258259

259260
# Translations
260261
*.mo
Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
/* Tests: app1/scss/_include.scss */
2+
13
.app1-include {
24
color: red;
3-
}
5+
}
Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
/* Tests: app2/scss/_samedir.scss */
2+
13
.app2-samedir {
24
color: red;
3-
}
5+
}
Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
/* Tests: app2/scss/subdir/_subdir.scss */
2+
13
.app2-subdir {
24
color: red;
3-
}
5+
}
Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,20 @@
1-
/* app1/scss/_include.scss */
1+
// app1/scss/_include.scss
22
@import "app1/scss/include";
33

4-
/* app2/_samedir.scss */
4+
// app2/_samedir.scss
55
@import "app2/scss/samedir";
66

7-
/* app2/subdir/_subdir.scss */
7+
// app2/subdir/_subdir.scss
88
@import "app2/scss/subdir/subdir";
99

10-
/* _samedir.scss */
10+
// _samedir.scss
1111
@import "samedir";
1212

13-
/* subdir/_subdir.scss */
13+
// subdir/_subdir.scss
1414
@import "subdir/subdir";
1515

16-
/* test.scss */
16+
// test.scss
17+
/* Tests: app2/scss/test.scss */
1718
.test {
1819
color: red;
19-
}
20+
}

testproject/tests.py

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
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

Comments
 (0)