File tree Expand file tree Collapse file tree 2 files changed +17
-1
lines changed Expand file tree Collapse file tree 2 files changed +17
-1
lines changed Original file line number Diff line number Diff line change @@ -25,6 +25,7 @@ def initialize(path_or_io, options = {})
2525
2626 # if path-or_io is string && does not contain a null byte
2727 if ( path_or_io . instance_of? ( String ) && !/\u0000 / . match? ( path_or_io ) )
28+ raise Errno ::EIO . new ( 'Invalid file format' ) if !File . extname ( path_or_io ) . eql? ( '.docx' )
2829 @zip = Zip ::File . open ( path_or_io )
2930 else
3031 @zip = Zip ::File . open_buffer ( path_or_io )
@@ -38,7 +39,7 @@ def initialize(path_or_io, options = {})
3839 load_styles
3940 yield ( self ) if block_given?
4041 ensure
41- @zip . close
42+ @zip . close unless @zip . nil?
4243 end
4344
4445 # This stores the current global document properties, for now
Original file line number Diff line number Diff line change 1818 end . to_not raise_error
1919 end
2020 end
21+
22+ context 'When reading a un-supported file' do
23+ it 'should throw file not supported error' do
24+ expect do
25+ Docx ::Document . open ( @fixtures_path + '/invalid_format.pdf' )
26+ end . to raise_error ( Errno ::EIO , 'Input/output error - Invalid file format' )
27+ end
28+
29+ it 'should throw file not found error' do
30+ invalid_path = @fixtures_path + '/invalid_file_path.docx'
31+ expect do
32+ Docx ::Document . open ( invalid_path )
33+ end . to raise_error ( Zip ::Error , "File #{ invalid_path } not found" )
34+ end
35+ end
2136 end
2237
2338 describe 'reading' do
You can’t perform that action at this time.
0 commit comments