From 90d713a1d44b12e3a02417c8e3c829d16895d78e Mon Sep 17 00:00:00 2001 From: Karol Date: Mon, 6 Feb 2023 22:29:48 +0100 Subject: [PATCH] fix (potential) problems with empty file_extensions --- app/assets/javascripts/exercises.js.erb | 2 +- app/models/file_type.rb | 2 +- .../convert_task_to_exercise.rb | 4 ++-- .../convert_task_to_exercise_spec.rb | 18 ++++++++++++++++++ 4 files changed, 22 insertions(+), 4 deletions(-) diff --git a/app/assets/javascripts/exercises.js.erb b/app/assets/javascripts/exercises.js.erb index a91586dd..2709bdb0 100644 --- a/app/assets/javascripts/exercises.js.erb +++ b/app/assets/javascripts/exercises.js.erb @@ -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'); diff --git a/app/models/file_type.rb b/app/models/file_type.rb index c1dec216..79d2ad0a 100644 --- a/app/models/file_type.rb +++ b/app/models/file_type.rb @@ -26,7 +26,7 @@ class FileType < ApplicationRecord validates :indent_size, presence: true, unless: :binary? validates :name, presence: true validates :renderable, inclusion: [true, false] - validates :file_extension, length: { minimum:0, allow_nil: false} + validates :file_extension, length: {minimum: 0, allow_nil: false} %i[audio compressed csv excel image pdf powerpoint video word].each do |type| define_method("#{type}?") do diff --git a/app/services/proforma_service/convert_task_to_exercise.rb b/app/services/proforma_service/convert_task_to_exercise.rb index f9682bf3..f7d3959d 100644 --- a/app/services/proforma_service/convert_task_to_exercise.rb +++ b/app/services/proforma_service/convert_task_to_exercise.rb @@ -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' diff --git a/spec/services/proforma_service/convert_task_to_exercise_spec.rb b/spec/services/proforma_service/convert_task_to_exercise_spec.rb index 38439c95..e8273cc0 100644 --- a/spec/services/proforma_service/convert_task_to_exercise_spec.rb +++ b/spec/services/proforma_service/convert_task_to_exercise_spec.rb @@ -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 {