Skip to content

Commit

Permalink
Raise proper FileNotFound errors (#128)
Browse files Browse the repository at this point in the history
  • Loading branch information
maxirmx authored and ronaldtse committed Dec 11, 2023
1 parent a98165b commit 4cc7af6
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 1 deletion.
3 changes: 2 additions & 1 deletion lib/expressir/express/parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,12 @@ class Parser
# @param [Boolean] include_source attach original source code to model elements
# @return [Model::Repository]
def self.from_file(file, skip_references: nil, include_source: nil)
raise Errno::ENOENT, "File not found: #{file}" unless File.exist?(file)

# An important note re memory management
# parse, syntax, visitor methods return complex tree structures created in native (C++) extension
# visit method references nodes and leaves of this structures but it is totally untransparent for Ruby GarbageCllector
# so in this class we keep those C++ structure marked for GC so they are not freed
# so in this class we keep those C++ structure marked for GC so they are not freed
@parser = ::ExpressParser::ParserExt.new(file.to_s)
@parse_tree = @parser.syntax()

Expand Down
7 changes: 7 additions & 0 deletions spec/expressir/express/cache_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,13 @@
end

describe ".from_file" do
it "throws an exception if the cache file does not exist" do |example|
print "\n[#{example.description}] "
expect do
Expressir::Express::Cache.from_file("non-existing-file", test_overwrite_version: TEST_VERSION)
end.to raise_error(Errno::ENOENT)
end

it "parses a file" do |example|
print "\n[#{example.description}] "
temp_file = Tempfile.new
Expand Down
7 changes: 7 additions & 0 deletions spec/expressir/express/parser_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,13 @@

RSpec.describe Expressir::Express::Parser do
describe ".from_file" do
it "throws an exception if the file to parse does not exist" do |example|
print "\n[#{example.description}] "
expect do
Expressir::Express::Parser.from_file("non-existing-file")
end.to raise_error(Errno::ENOENT)
end

it "parses a file (single.exp)" do |example|
print "\n[#{example.description}] "
exp_file = Expressir.root_path.join("spec", "syntax", "single.exp")
Expand Down

0 comments on commit 4cc7af6

Please sign in to comment.