Fix Doc2Text::Odt::Document.parse_and_save when using tmp files as input #25
Closed
alecslupu wants to merge 5 commits into
Closed
Fix Doc2Text::Odt::Document.parse_and_save when using tmp files as input #25alecslupu wants to merge 5 commits into
Doc2Text::Odt::Document.parse_and_save when using tmp files as input #25alecslupu wants to merge 5 commits into
Conversation
There was a problem hiding this comment.
Pull request overview
This PR aims to fix ODT unpacking/parsing when the input document is provided via a temporary file path (e.g., Tempfile), addressing failures caused by how rubyzip resolves extraction destinations for absolute paths.
Changes:
- Adjust ODT (and other XML-based) unpacking to extract into a temporary directory rather than alongside the input file.
- Modify rubyzip extraction invocation to account for destination directory resolution.
- Add a spec case that exercises unpacking from a temporary file.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 6 comments.
| File | Description |
|---|---|
lib/doc2text/xml_based_document_file.rb |
Changes extraction target directory strategy and alters how zip entries are extracted. |
spec/unpack_odt_spec.rb |
Adds a spec for unpacking an ODT from a Tempfile. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.
Comments suppressed due to low confidence (2)
spec/unpack_odt_spec.rb:26
- The fixture being copied is a ZIP/binary file. Using
File.read/tempfile.writewithout binary mode can corrupt the bytes on Windows due to newline translation. PreferFile.binreadandtempfile.binmode(or open the Tempfile in binary) when copying the .odt bytes.
tempfile = Tempfile.new('text_styles.odt')
tempfile.write File.read(File.join 'spec', 'fixtures', 'text_styles.odt')
tempfile.rewind
tempfile.close
spec/unpack_odt_spec.rb:27
- Cleanup here isn't protected if an expectation fails (e.g.,
tempfile.unlink/@odt.cleanwon't run), which can leave temp files/dirs behind and make runs flaky. Wrap the example inbegin/ensureand ensure both the tempfile and@odt.cleanare executed in theensureblock (e.g., viatempfile.close!).
tempfile = Tempfile.new('text_styles.odt')
tempfile.write File.read(File.join 'spec', 'fixtures', 'text_styles.odt')
tempfile.rewind
tempfile.close
Comment on lines
14
to
+17
| zip_file.each do |entry| | ||
| zipped_file_extract_path = File.join extract_path, entry.name | ||
| FileUtils.mkdir_p File.dirname(zipped_file_extract_path) | ||
| zip_file.extract entry, zipped_file_extract_path | ||
| zip_file.extract entry, entry.name, destination_directory: extract_path |
Comment on lines
+21
to
+24
| context "when the odt is a temporary file" do | ||
| it "runs from a temp file" do | ||
| tempfile = Tempfile.new('text_styles.odt') | ||
| tempfile.write File.read(File.join 'spec', 'fixtures', 'text_styles.odt') |
Owner
|
See #26 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
In decidim we have the following snippet:
inside the
parse_and_save, the input is/tmp/doc-to-markdown-odt20260509-18225-lmbxu2, and output_filename is/tmp/20260509-18225-dtzzo3.Running the spec, on my end, i get an error like:
From the stack trace, I could identify that the spec (running with
--backtrace) is actually failing in theunpackmethod ofDoc2Text::XmlBasedDocument::DocumentFile.The line
zip_file.extract entry, zipped_file_extract_pathexpects a third named argumentdestination_directory:, which has a default of.. This means that when you pass a file having the full path (ex a temp file), the destination_directory becomescwd+ original path, leading to strange path.I have managed to fix on my local, using the attached snippet.