Skip to content

Commit 16fbb49

Browse files
authored
If outpath does not exist and appears to be a dir, create it (#11)
If the user specifies an input file, but output path that does not exist, the program currently crashes trying to check if the output path is a directory. In this situation, first check if the output path exists, if not, guess if it should be a directory or file, and create it if necessary.
1 parent 73ac69c commit 16fbb49

File tree

5 files changed

+31
-4
lines changed

5 files changed

+31
-4
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -261,7 +261,8 @@ Changelog
261261
---------
262262

263263
#### 1.1.0
264-
* New: Will now compile Sass files as well as SCSS files.
264+
* New: Now compiles `.sass` files as well as `.scss` files.
265+
* Fix bug when input path is a file and output path does not exist.
265266

266267
#### 1.0.1
267268
* Maintanence release, no functional changes.

django_sass/__init__.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,8 +87,17 @@ def compile_sass(
8787
# Handle input files.
8888
outfile = None
8989
if os.path.isfile(inpath):
90+
9091
sassargs.update({"filename": inpath})
91-
if os.path.isdir(outpath):
92+
93+
# If outpath does not exist, guess if it should be a dir and create it.
94+
if not os.path.exists(outpath):
95+
if not outpath.endswith(".css"):
96+
os.makedirs(outpath)
97+
98+
# If outpath is a directory, create a child file.
99+
# Otherwise use provided file path.
100+
if os.path.exists(outpath) and os.path.isdir(outpath):
92101
outfile = os.path.join(
93102
outpath,
94103
os.path.basename(
@@ -97,6 +106,8 @@ def compile_sass(
97106
)
98107
else:
99108
outfile = outpath
109+
110+
# Create source map if specified.
100111
if source_map:
101112
sassargs.update({"source_map_filename": outfile + ".map"})
102113

pyproject.toml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ target-version = ['py36', 'py37', 'py38']
44
# Regular expression of files to exclude.
55
exclude = '''
66
/(
7-
migrations
7+
.venv
8+
| venv
9+
| migrations
810
)/
911
'''

setup.cfg

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[flake8]
22
max-line-length = 100
3-
exclude = migrations
3+
exclude = .venv,venv,migrations
44

55
[mypy]
66
ignore_missing_imports = True

testproject/tests.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,19 @@ def test_cli_dir(self):
119119
contains=SCSS_CONTAINS,
120120
)
121121

122+
def test_cli_infile_outdir(self):
123+
# Input is a file; output is non-existant path (without .css extension).
124+
inpath = os.path.join("app2", "static", "app2", "scss", "test.scss")
125+
outpath = os.path.join(self.outdir, "does-not-exist")
126+
# Expected output path on filesystem.
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+
122135
def test_sass_compiles(self):
123136
# Input and output paths relative to django static dirs.
124137
inpath = os.path.join("app3", "static", "app3", "sass")

0 commit comments

Comments
 (0)