fix (potential) problems with empty file_extensions

This commit is contained in:
Karol
2023-02-06 22:29:48 +01:00
committed by Sebastian Serth
parent 93188ba04d
commit 90d713a1d4
4 changed files with 22 additions and 4 deletions

View File

@ -264,7 +264,7 @@ $(document).on('turbolinks:load', function () {
var inferFileAttributes = function () {
$(document).on('change', 'input[type="file"]', function () {
var filename = $(this).val().split(/\\|\//g).pop();
var file_extension = '.' + filename.split('.')[1];
var file_extension = filename.includes('.') ? '.' + filename.split('.')[1] : '';
var file_type = findFileTypeByFileExtension(file_extension);
var name = filename.split('.')[0];
var parent = $(this).parents('li');

View File

@ -101,8 +101,8 @@ module ProformaService
end
def file_type(extension)
FileType.find_or_create_by(file_extension: extension.presence) do |file_type|
file_type.name = extension[1..]
FileType.find_or_create_by(file_extension: extension) do |file_type|
file_type.name = "Imported #{extension}"
file_type.user = @user
file_type.indent_size = 4
file_type.editor_mode = 'ace/mode/plain_text'

View File

@ -157,6 +157,24 @@ describe ProformaService::ConvertTaskToExercise do
expect { convert_to_exercise_service.save! }.to change(Exercise, :count).by(1)
end
context 'when file is a Makefile' do
let(:filename) { "#{path}Makefile" }
it 'creates an exercise with a file with a Filetype, that has the correct attributes' do
expect(convert_to_exercise_service.files.first).to have_attributes(
file_type: be_a(FileType).and(have_attributes(file_extension: '', name: 'Imported'))
)
end
context 'when FileType for Makefile exists' do
let!(:makefile_filetype) { create(:makefile) }
it 'creates an exercise with a file with a Filetype, that has the correct attributes' do
expect(convert_to_exercise_service.files.first).to have_attributes(file_type: makefile_filetype)
end
end
end
context 'when file is a main_file' do
let(:meta_data) do
{