set proglang based on exenv

try to guess exenv based on proglang
This commit is contained in:
Karol
2022-09-24 15:24:12 +02:00
parent f5758ecb5e
commit 77999f75df
4 changed files with 42 additions and 10 deletions

View File

@ -23,7 +23,7 @@ module ProformaService
title: @exercise.title,
description: @exercise.description,
internal_description: nil,
# proglang: proglang, where can we get this information?
proglang: proglang,
files: task_files,
tests: tests,
uuid: uuid,
@ -44,6 +44,12 @@ module ProformaService
)
end
def proglang
regex = %r{^openhpi/co_execenv_(?<language>[^:]*):(?<version>[^-]*)(?>-.*)?$}
match = regex.match @exercise.execution_environment.docker_image
match ? {name: match[:language], version: match[:version]} : nil
end
def uuid
@exercise.update(uuid: SecureRandom.uuid) if @exercise.uuid.nil?
@exercise.uuid

View File

@ -26,12 +26,26 @@ module ProformaService
allow_file_creation: string_to_bool(@task.meta_data[:CodeOcean]&.dig(:allow_file_creation)),
allow_auto_completion: string_to_bool(@task.meta_data[:CodeOcean]&.dig(:allow_auto_completion)),
expected_difficulty: @task.meta_data[:CodeOcean]&.dig(:expected_difficulty),
execution_environment_id: @task.meta_data[:CodeOcean]&.dig(:execution_environment_id),
execution_environment_id: execution_environment_id,
files: files
)
end
def execution_environment_id
from_meta_data = @task.meta_data[:CodeOcean]&.dig(:execution_environment_id)
return from_meta_data if from_meta_data
return nil unless @task.proglang
ex_envs_with_name_and_version = ExecutionEnvironment.where('docker_image ilike ?', "%#{@task.proglang[:name]}%#{@task.proglang[:version]}%")
return ex_envs_with_name_and_version.first.id if ex_envs_with_name_and_version.any?
ex_envs_with_name = ExecutionEnvironment.where('docker_image like ?', "%#{@task.proglang[:name]}%")
return ex_envs_with_name.first.id if ex_envs_with_name.any?
nil
end
def string_to_bool(str)
return true if str == 'true'
return false if str == 'false'

View File

@ -19,22 +19,19 @@ RSpec.describe ProformaService::ConvertExerciseToTask do
let(:convert_to_task) { described_class.new(exercise: exercise) }
let(:exercise) do
create(:dummy,
execution_environment: execution_environment,
instructions: 'instruction',
uuid: SecureRandom.uuid,
files: files + tests)
end
let(:files) { [] }
let(:tests) { [] }
let(:execution_environment) { create(:java) }
it 'creates a task with all basic attributes' do
expect(task).to have_attributes(
title: exercise.title,
description: exercise.description,
# internal_description: exercise.instructions,
# proglang: {
# name: exercise.execution_environment.language,
# version: exercise.execution_environment.version
# },
uuid: exercise.uuid,
language: described_class::DEFAULT_LANGUAGE,
meta_data: {
@ -48,13 +45,18 @@ RSpec.describe ProformaService::ConvertExerciseToTask do
files: {},
},
},
# parent_uuid: exercise.clone_relations.first&.origin&.uuid,
files: [],
tests: [],
model_solutions: []
)
end
context 'when exercise has execution_environment with correct docker-image name' do
it 'creates a task with the correct proglang attribute' do
expect(task).to have_attributes(proglang: {name: 'java', version: '8'})
end
end
context 'when exercise has a mainfile' do
let(:files) { [file] }
let(:file) { build(:file) }

View File

@ -32,7 +32,7 @@ describe ProformaService::ConvertTaskToExercise do
Proforma::Task.new(
title: 'title',
description: 'description',
# proglang: {name: 'proglang-name', version: 'proglang-version'},
proglang: {name: 'python', version: '3.4'},
uuid: 'uuid',
parent_uuid: 'parent_uuid',
language: 'language',
@ -57,7 +57,7 @@ describe ProformaService::ConvertTaskToExercise do
allow_file_creation: allow_file_creation,
allow_auto_completion: allow_auto_completion,
expected_difficulty: expected_difficulty,
execution_environment_id: execution_environment.id,
execution_environment_id: execution_environment&.id,
files: files_meta_data,
},
}
@ -87,6 +87,16 @@ describe ProformaService::ConvertTaskToExercise do
)
end
context 'when execution environment is not set in meta_data' do
let(:execution_environment) { nil }
before { create(:python) }
it 'sets the execution_environment based on proglang name and value' do
expect(convert_to_exercise_service).to have_attributes(execution_environment: have_attributes(name: 'Python 3.4'))
end
end
context 'when task has a file' do
let(:files) { [file] }
let(:file) do