Skip to content

Commit

Permalink
Fix parsing directories (#1203)
Browse files Browse the repository at this point in the history
* Throw error if passing directory instead of file to `parse_file`
* Add test for it
  • Loading branch information
JCGoran committed Mar 7, 2024
1 parent 347f786 commit f8c8d23
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/parser/nmodl_driver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ std::shared_ptr<ast::Program> NmodlDriver::parse_stream(std::istream& in) {

std::shared_ptr<ast::Program> NmodlDriver::parse_file(const fs::path& filename,
const location* loc) {
if (fs::is_directory(filename)) {
throw std::runtime_error("NMODL Parser Error : path " + filename.string() +
" appears to be a directory, please provide a file instead");
}
std::ifstream in(filename);
if (!in.good()) {
std::ostringstream oss;
Expand Down
12 changes: 12 additions & 0 deletions test/unit/pybind/test_parser.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import nmodl
from pathlib import Path
import pytest


def test_parse_directory():
"""
Make sure we raise an error when parsing a directory instead of a file
"""

with pytest.raises(RuntimeError):
nmodl.NmodlDriver().parse_file(str(Path(__file__).parent))

0 comments on commit f8c8d23

Please sign in to comment.