File: tests.py

package info (click to toggle)
django-sass 1.1.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 200 kB
  • sloc: python: 388; makefile: 4
file content (184 lines) | stat: -rw-r--r-- 6,322 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
import os
import shutil
import subprocess
import time
import unittest
from typing import List

from django_sass import find_static_paths, find_static_scss


THIS_DIR = os.path.dirname(os.path.abspath(__file__))

SCSS_CONTAINS = [
    "/* Tests: app1/scss/_include.scss */",
    "/* Tests: app2/scss/_samedir.scss */",
    "/* Tests: app2/scss/subdir/_subdir.scss */",
    "/* Tests: app2/scss/test.scss */",
]


class TestDjangoSass(unittest.TestCase):
    def setUp(self):
        self.outdir = os.path.join(THIS_DIR, "out")

    def tearDown(self):
        # Clean up output files
        shutil.rmtree(self.outdir, ignore_errors=True)

    def assert_output(
        self,
        inpath: str,
        outpath: str,
        real_outpath: str,
        contains: List[str],
        args: List[str] = None,
    ):
        # Command to run
        args = args or []
        cmd = ["python", "manage.py", "sass", *args, inpath, outpath]
        # Run the management command on testproject.
        proc = subprocess.run(cmd, cwd=THIS_DIR)
        # Verify the process exited cleanly.
        self.assertEqual(proc.returncode, 0)
        # Verify that the output file exists.
        # self.assertTrue(os.path.isfile(real_outpath))

        # Verify that the file contains expected output from all sass files.
        with open(real_outpath, encoding="utf8") as f:
            contents = f.read()
            for compiled_data in contains:
                self.assertTrue(compiled_data in contents)

    def test_find_static_paths(self):
        paths = find_static_paths()
        # Assert that it found both of our apps' static dirs.
        self.assertTrue(os.path.join(THIS_DIR, "app1", "static") in paths)
        self.assertTrue(os.path.join(THIS_DIR, "app2", "static") in paths)

    def test_find_static_sass(self):
        files = find_static_scss()
        # Assert that it found all of our scss files.
        self.assertTrue(
            os.path.join(
                THIS_DIR, "app1", "static", "app1", "scss", "_include.scss"
            )
            in files
        )
        self.assertTrue(
            os.path.join(
                THIS_DIR, "app2", "static", "app2", "scss", "_samedir.scss"
            )
            in files
        )
        self.assertTrue(
            os.path.join(
                THIS_DIR, "app2", "static", "app2", "scss", "test.scss"
            )
            in files
        )
        self.assertTrue(
            os.path.join(
                THIS_DIR,
                "app2",
                "static",
                "app2",
                "scss",
                "subdir",
                "_subdir.scss",
            )
            in files
        )
        self.assertTrue(
            os.path.join(
                THIS_DIR, "app3", "static", "app3", "sass", "indent_test.sass"
            )
            in files
        )

    def test_cli(self):
        # Input and output paths relative to django static dirs.
        inpath = os.path.join("app2", "static", "app2", "scss", "test.scss")
        outpath = os.path.join(self.outdir, "test_file.css")
        self.assert_output(
            inpath=inpath,
            outpath=outpath,
            real_outpath=outpath,
            contains=SCSS_CONTAINS,
        )

    def test_cli_dir(self):
        # Input and output paths relative to django static dirs.
        inpath = os.path.join("app2", "static", "app2", "scss")
        # Expected output path on filesystem.
        real_outpath = os.path.join(self.outdir, "test.css")
        self.assert_output(
            inpath=inpath,
            outpath=self.outdir,
            real_outpath=real_outpath,
            contains=SCSS_CONTAINS,
        )

    def test_cli_infile_outdir(self):
        # Input is a file; output is non-existant path (without .css extension).
        inpath = os.path.join("app2", "static", "app2", "scss", "test.scss")
        outpath = os.path.join(self.outdir, "does-not-exist")
        # Expected output path on filesystem.
        real_outpath = os.path.join(outpath, "test.css")
        self.assert_output(
            inpath=inpath,
            outpath=outpath,
            real_outpath=real_outpath,
            contains=SCSS_CONTAINS,
        )

    def test_sass_compiles(self):
        # Input and output paths relative to django static dirs.
        inpath = os.path.join("app3", "static", "app3", "sass")
        # Expected output path on filesystem.
        real_outpath = os.path.join(self.outdir, "indent_test.css")
        self.assert_output(
            inpath=inpath,
            outpath=self.outdir,
            real_outpath=real_outpath,
            contains=["/* Tests: app3/sass/indent_test.sass */"],
        )

    def test_cli_srcmap(self):
        # Input and output paths relative to django static dirs.
        inpath = os.path.join("app2", "static", "app2", "scss", "test.scss")
        outpath = os.path.join(self.outdir, "test.css")
        self.assert_output(
            inpath=inpath,
            outpath=outpath,
            real_outpath=outpath,
            contains=SCSS_CONTAINS,
            args=["-g"],
        )
        self.assertTrue(
            os.path.isfile(os.path.join(self.outdir, "test.css.map"))
        )

    @unittest.skip("Test needs fixed...")
    def test_cli_watch(self):
        # Input and output paths relative to django static dirs.
        inpath = os.path.join("app2", "static", "app2", "scss", "test.scss")
        outpath = os.path.join(self.outdir, "test.css")
        # Command to run
        cmd = ["python", "manage.py", "sass", "--watch", inpath, outpath]
        # Run the management command on testproject.
        proc = subprocess.Popen(cmd, cwd=THIS_DIR)
        time.sleep(0.5)
        # TODO: This test is not working. Do not know how to intentionally send
        # a KeyboardInterrupt to the subprocess without having unittest/pytest
        # immediately die when it sees the interrupt.
        try:
            proc.send_signal(subprocess.signal.CTRL_C_EVENT)
        except KeyboardInterrupt:
            # We actually want the keyboard interrupt.
            pass
        returncode = proc.wait()
        # Verify the process exited cleanly.
        self.assertEqual(returncode, 0)
        # Assert output is correct.
        self.assert_output(outpath)