-
Notifications
You must be signed in to change notification settings - Fork 78
fix(codaveri): fix crash on import question with empty data files #8534
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
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
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
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
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
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
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
Binary file added
BIN
+3.95 KB
spec/fixtures/course/programming_question_template_codaveri_empty_data_file.zip
Binary file not shown.
115 changes: 115 additions & 0 deletions
115
...services/course/assessment/question/programming_codaveri/language_package_service_spec.rb
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,115 @@ | ||
| # frozen_string_literal: true | ||
| require 'rails_helper' | ||
|
|
||
| RSpec.describe Course::Assessment::Question::ProgrammingCodaveri::LanguagePackageService do | ||
| let(:package_path) do | ||
| File.join(Rails.root, 'spec/fixtures/course/programming_question_template_codaveri_empty_data_file.zip') | ||
| end | ||
| let(:package) { Course::Assessment::ProgrammingPackage.new(package_path) } | ||
| let(:main_files) { package.main_files } | ||
| let(:test_files) { package.test_files } | ||
| subject { described_class.new(nil, package) } | ||
|
|
||
| def extract(filename, content) | ||
| subject.send(:extract_supporting_file, filename, content) | ||
| subject.data_files.last | ||
| end | ||
|
|
||
| describe '.extract_supporting_file' do | ||
| context 'when the file is valid UTF-8 plaintext' do | ||
| it 'extracts the content as utf8' do | ||
| filename = Pathname.new('data.csv') | ||
|
|
||
| expect(extract(filename, main_files[filename])).to eq( | ||
| type: 'internal', | ||
| path: 'data.csv', | ||
| content: "codon,amino_acid\nAAA,Lys\nAAC,Asn\n", | ||
| encoding: 'utf8' | ||
| ) | ||
| end | ||
| end | ||
|
|
||
| context 'when the file is not valid UTF-8' do | ||
| it 'extracts the content as base64' do | ||
| filename = Pathname.new('binary.dat') | ||
|
|
||
| expect(extract(filename, main_files[filename])).to eq( | ||
| type: 'internal', | ||
| path: 'binary.dat', | ||
| content: Base64.strict_encode64("\xFF\xFE\x00\x01\x02".b), | ||
| encoding: 'base64' | ||
| ) | ||
| end | ||
| end | ||
|
|
||
| # Zero-byte zip entries are read back as a frozen empty string, which used to be mutated | ||
| # in place by `force_encoding` and raise FrozenError. | ||
| context 'when the file is empty' do | ||
| it 'extracts the file as empty utf8 content' do | ||
| filename = Pathname.new('empty.csv') | ||
| content = main_files[filename] | ||
| expect(content).to be_frozen | ||
|
|
||
| expect(extract(filename, content)).to eq( | ||
| type: 'internal', | ||
| path: 'empty.csv', | ||
| content: '', | ||
| encoding: 'utf8' | ||
| ) | ||
| end | ||
|
|
||
| it 'extracts a zero-byte file in the tests folder' do | ||
| filename = Pathname.new('empty.csv') | ||
|
|
||
| expect { extract(filename, test_files[filename]) }.not_to raise_error | ||
| end | ||
| end | ||
|
|
||
| it 'does not re-tag the encoding of the content read from the package' do | ||
| filename = Pathname.new('data.csv') | ||
| content = main_files[filename] | ||
|
|
||
| extract(filename, content) | ||
|
|
||
| expect(content.encoding).to eq(Encoding::ASCII_8BIT) | ||
| end | ||
|
|
||
| it 'appends every extracted file to the data files' do | ||
| subject.send(:extract_supporting_file, Pathname.new('data.csv'), main_files[Pathname.new('data.csv')]) | ||
| subject.send(:extract_supporting_file, Pathname.new('empty.csv'), test_files[Pathname.new('empty.csv')]) | ||
|
|
||
| expect(subject.data_files.map { |file| file[:path] }).to eq(['data.csv', 'empty.csv']) | ||
| end | ||
| end | ||
|
|
||
| describe '.utf8_encodable?' do | ||
| it 'accepts any valid UTF-8 content' do | ||
| expect(subject.send(:utf8_encodable?, Pathname.new('data.csv'), 'plaintext')).to eq(true) | ||
| expect(subject.send(:utf8_encodable?, Pathname.new('empty.csv'), '')).to eq(true) | ||
| expect(subject.send(:utf8_encodable?, Pathname.new('binary.dat'), "\xFF\xFE".b.force_encoding('UTF-8'))). | ||
| to eq(false) | ||
| end | ||
|
|
||
| # The Java package service narrows the base implementation, pending Codaveri 'utf8' encoding | ||
| # support for all plaintext files in compiled languages. | ||
| context 'when the concrete service restricts utf8 encoding' do | ||
| subject { Course::Assessment::Question::ProgrammingCodaveri::Java::JavaPackageService.new(nil, package) } | ||
|
|
||
| it 'only accepts plaintext files that require compiling' do | ||
| expect(subject.send(:utf8_encodable?, Pathname.new('Helper.java'), 'class Helper {}')).to eq(true) | ||
| expect(subject.send(:utf8_encodable?, Pathname.new('data.csv'), 'plaintext')).to eq(false) | ||
| end | ||
|
|
||
| it 'extracts an empty supporting file as base64' do | ||
| filename = Pathname.new('empty.csv') | ||
|
|
||
| expect(extract(filename, main_files[filename])).to eq( | ||
| type: 'internal', | ||
| path: 'empty.csv', | ||
| content: '', | ||
| encoding: 'base64' | ||
| ) | ||
| end | ||
| end | ||
| end | ||
| end |
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.
Uh oh!
There was an error while loading. Please reload this page.