Skip to content

Commit

Permalink
If outpath does not exist and appears to be a dir, create it (#11)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
vsalvino authored Aug 21, 2021
1 parent 73ac69c commit 16fbb49
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 4 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,8 @@ Changelog
---------

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

#### 1.0.1
* Maintanence release, no functional changes.
Expand Down
13 changes: 12 additions & 1 deletion django_sass/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,17 @@ def compile_sass(
# Handle input files.
outfile = None
if os.path.isfile(inpath):

sassargs.update({"filename": inpath})
if os.path.isdir(outpath):

# If outpath does not exist, guess if it should be a dir and create it.
if not os.path.exists(outpath):
if not outpath.endswith(".css"):
os.makedirs(outpath)

# If outpath is a directory, create a child file.
# Otherwise use provided file path.
if os.path.exists(outpath) and os.path.isdir(outpath):
outfile = os.path.join(
outpath,
os.path.basename(
Expand All @@ -97,6 +106,8 @@ def compile_sass(
)
else:
outfile = outpath

# Create source map if specified.
if source_map:
sassargs.update({"source_map_filename": outfile + ".map"})

Expand Down
4 changes: 3 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ target-version = ['py36', 'py37', 'py38']
# Regular expression of files to exclude.
exclude = '''
/(
migrations
.venv
| venv
| migrations
)/
'''
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[flake8]
max-line-length = 100
exclude = migrations
exclude = .venv,venv,migrations

[mypy]
ignore_missing_imports = True
Expand Down
13 changes: 13 additions & 0 deletions testproject/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,19 @@ def test_cli_dir(self):
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")
Expand Down

0 comments on commit 16fbb49

Please sign in to comment.