~azzar1/unity/add-show-desktop-key

« back to all changes in this revision

Viewing changes to ivle/worksheet/utils.py

  • Committer: William Grant
  • Date: 2009-12-10 00:37:03 UTC
  • mto: This revision was merged to the branch mainline in revision 1397.
  • Revision ID: grantw@unimelb.edu.au-20091210003703-sygwhrz4u3szpobu
Factor out console-based exercise testing into ivle.worksheet.test_exercise_submission().

Show diffs side-by-side

added added

removed removed

Lines of Context:
23
23
This module provides functions for tutorial and worksheet computations.
24
24
"""
25
25
 
 
26
import os.path
 
27
 
26
28
from storm.locals import And, Asc, Desc, Store
27
29
import genshi
28
30
 
29
31
import ivle.database
30
32
from ivle.database import ExerciseAttempt, ExerciseSave, Worksheet, \
31
33
                          WorksheetExercise, Exercise
 
34
import ivle.webapp.tutorial.test
32
35
 
33
36
__all__ = ['ExerciseNotFound', 'get_exercise_status',
34
37
           'get_exercise_stored_text', 'get_exercise_attempts',
35
 
           'get_exercise_attempt',
 
38
           'get_exercise_attempt', 'test_exercise_submission',
36
39
          ]
37
40
 
38
41
class ExerciseNotFound(Exception):
281
284
        worksheet_exercise.seq_no = ex_num
282
285
        worksheet_exercise.optional = optional
283
286
 
 
287
 
 
288
def test_exercise_submission(config, user, exercise, code):
 
289
    """Test the given code against an exercise.
 
290
 
 
291
    The code is run in a console process as the provided user.
 
292
    """
 
293
    # Start a console to run the tests on
 
294
    jail_path = os.path.join(config['paths']['jails']['mounts'],
 
295
                             user.login)
 
296
    working_dir = os.path.join("/home", user.login)
 
297
    cons = ivle.console.Console(config, user.unixid, jail_path,
 
298
                                working_dir)
 
299
 
 
300
    # Parse the file into a exercise object using the test suite
 
301
    exercise_obj = ivle.webapp.tutorial.test.parse_exercise_file(
 
302
        exercise, cons)
 
303
 
 
304
    # Run the test cases. Get the result back as a JSONable object.
 
305
    # Return it.
 
306
    test_results = exercise_obj.run_tests(code)
 
307
 
 
308
    # Close the console
 
309
    cons.close()
 
310
 
 
311
    return test_results