Skip to content

Commit

Permalink
Add support for .sass indent-style syntax. (#8)
Browse files Browse the repository at this point in the history
Co-authored-by: Artemis21 <[email protected]>
  • Loading branch information
Artemis21 and Artemis21 authored Aug 21, 2021
1 parent 7e68ba8 commit 73ac69c
Show file tree
Hide file tree
Showing 8 changed files with 85 additions and 42 deletions.
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -151,8 +151,6 @@ Limitations
@import '../file';
```

* Only files ending in `.scss` are supported for now.

* Only supports `-g`, `-p`, and `-t` options similar to `pysassc`. Ideally
`django-sass` will be as similar as possible to the `pysassc` command line
interface.
Expand Down Expand Up @@ -262,6 +260,9 @@ Then run the unit tests:
Changelog
---------

#### 1.1.0
* New: Will now compile Sass files as well as SCSS files.

#### 1.0.1
* Maintanence release, no functional changes.
* Add additional type hints within the codebase.
Expand Down
13 changes: 8 additions & 5 deletions django_sass/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,15 @@ def find_static_paths() -> List[str]:

def find_static_scss() -> List[str]:
"""
Finds all static scss files available in this Django project.
Finds all static scss/sass files available in this Django project.
:returns:
List of paths of static scss files.
List of paths of static scss/sass files.
"""
scss_files = []
for finder in get_finders():
for path, storage in finder.list([]):
if path.endswith(".scss"):
if path.endswith(".scss") or path.endswith(".sass"):
fullpath = finder.find(path)
scss_files.append(fullpath)
return scss_files
Expand All @@ -51,7 +51,7 @@ def compile_sass(
and writes output CSS and/or sourcemaps to file.
:param str inpath:
Path to SCSS file or directory of SCSS files.
Path to SCSS/Sass file or directory of SCSS/Sass files.
:param str outpath:
Path to a CSS file or directory in which to write output. The path will
be created if it does not exist.
Expand Down Expand Up @@ -90,7 +90,10 @@ def compile_sass(
sassargs.update({"filename": inpath})
if os.path.isdir(outpath):
outfile = os.path.join(
outpath, os.path.basename(inpath.replace(".scss", ".css"))
outpath,
os.path.basename(
inpath.replace(".scss", ".css").replace(".sass", ".css")
),
)
else:
outfile = outpath
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

setup(
name="django-sass",
version="1.0.1",
version="1.1.0",
author="CodeRed LLC",
author_email="[email protected]",
url="https://github.com/coderedcorp/django-sass",
Expand Down
Empty file added testproject/app3/__init__.py
Empty file.
5 changes: 5 additions & 0 deletions testproject/app3/apps.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
from django.apps import AppConfig


class App3Config(AppConfig):
name = "app3"
4 changes: 4 additions & 0 deletions testproject/app3/static/app3/sass/indent_test.sass
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/* Tests: app3/sass/indent_test.sass */

.test_item
border: 1px solid red
1 change: 1 addition & 0 deletions testproject/testproject/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
INSTALLED_APPS = [
"app1",
"app2",
"app3",
"django_sass",
"django.contrib.admin",
"django.contrib.auth",
Expand Down
97 changes: 63 additions & 34 deletions testproject/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,20 @@
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):
Expand All @@ -18,20 +26,29 @@ def tearDown(self):
# Clean up output files
shutil.rmtree(self.outdir, ignore_errors=True)

def assert_output(self, real_outpath: str):
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.
print(real_outpath)
self.assertTrue(os.path.isfile(real_outpath))
# 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()
self.assertTrue("/* Tests: app1/scss/_include.scss */" in contents)
self.assertTrue("/* Tests: app2/scss/_samedir.scss */" in contents)
self.assertTrue(
"/* Tests: app2/scss/subdir/_subdir.scss */" in contents
)
self.assertTrue("/* Tests: app2/scss/test.scss */" in contents)
for compiled_data in contains:
self.assertTrue(compiled_data in contents)

def test_find_static_paths(self):
paths = find_static_paths()
Expand Down Expand Up @@ -72,47 +89,59 @@ def test_find_static_sass(self):
)
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")
# Command to run
cmd = ["python", "manage.py", "sass", 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)
# Assert output is correct.
self.assert_output(outpath)
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")
outpath = self.outdir
# Expected output path on filesystem.
real_outpath = os.path.join(self.outdir, "test.css")
# Command to run
cmd = ["python", "manage.py", "sass", 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)
# Assert output is correct.
self.assert_output(real_outpath)
self.assert_output(
inpath=inpath,
outpath=self.outdir,
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")
# Command to run
cmd = ["python", "manage.py", "sass", "-g", 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)
# Assert output is correct.
self.assert_output(outpath)
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"))
)
Expand Down

0 comments on commit 73ac69c

Please sign in to comment.