Skip to content

Commit

Permalink
refactor: improve the code with feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
MaferMazu committed Dec 31, 2024
1 parent bf4a14f commit 5dd6748
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 16 deletions.
21 changes: 15 additions & 6 deletions .github/workflows/integration-test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -27,18 +27,27 @@ jobs:
LMS_HOST=local.edly.io
CMS_HOST=studio.local.edly.io
TUTOR_ROOT=$(pwd)
COURSE_KEY=course-v1:MITx+grading-library+course
EOF
shell: bash

- name: Install and prepare Tutor, Codejail and tests
- name: Install Tutor
run: pip install "tutor${{ matrix.tutor_version }}"
shell: bash

- name: Install, enable and initialize Tutor Codejail Plugin
run: |
pip install "tutor${{ matrix.tutor_version }}"
pip install git+https://github.com/edunext/tutor-contrib-codejail
tutor config save
tutor plugins enable codejail
tutor local do init --limit codejail
tutor mounts add cms:mitx-grading-library/integration_tests/integration_test.py:/openedx/edx-platform/integration_test.py
tutor local launch -I
shell: bash

- name: Mount Integration Test
run: tutor mounts add cms:mitx-grading-library/integration_tests/integration_test.py:/openedx/edx-platform/integration_test.py
shell: bash

- name: Launch Tutor
run: tutor local launch -I
shell: bash

- name: Import MITx Demo Course
Expand All @@ -48,5 +57,5 @@ jobs:

- name: Run integration tests
run: |
tutor local run cms python3 integration_test.py
tutor local run cms python integration_test.py "$COURSE_KEY"
shell: bash
26 changes: 16 additions & 10 deletions integration_tests/integration_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@
to be able to use the python_lib.zip that contains the library.
"""

import logging
import os
import sys

import django
import logging
from opaque_keys.edx.keys import CourseKey
from xmodule.capa.safe_exec import safe_exec
from xmodule.util.sandboxing import SandboxService
from xmodule.contentstore.django import contentstore
from opaque_keys.edx.keys import CourseKey
from xmodule.util.sandboxing import SandboxService

# Set up Django environment
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "cms.envs.test")
Expand All @@ -19,7 +21,7 @@
log = logging.getLogger(__name__)

# Define the code to be executed
all_code = """
GRADING_CLASSES_CODE = """
from mitxgraders import *
# Grading Classes
Expand Down Expand Up @@ -91,12 +93,11 @@
"""


def execute_code(all_code, course_key_str):
def execute_code(course_key_str):
"""
Executes the provided code in a sandboxed environment with the specified course context.
Args:
all_code (str): The code to be executed.
course_key_str (str): The string representation of the course key.
Returns:
Expand All @@ -105,7 +106,8 @@ def execute_code(all_code, course_key_str):
course_key = CourseKey.from_string(course_key_str)
sandbox_service = SandboxService(
course_id=course_key,
contentstore=contentstore)
contentstore=contentstore
)
zip_lib = sandbox_service.get_python_lib_zip()

extra_files = []
Expand All @@ -116,7 +118,7 @@ def execute_code(all_code, course_key_str):
python_path.append("python_lib.zip")

safe_exec(
all_code,
code=GRADING_CLASSES_CODE,
globals_dict={},
python_path=python_path,
extra_files=extra_files,
Expand All @@ -127,5 +129,9 @@ def execute_code(all_code, course_key_str):


if __name__ == "__main__":
course_key_str = "course-v1:MITx+grading-library+course"
execute_code(all_code, course_key_str)
if len(sys.argv) != 2:
print("Usage: python integration_test.py <course_key>")
sys.exit(1)

course_key_str = sys.argv[1]
execute_code(course_key_str)

0 comments on commit 5dd6748

Please sign in to comment.