protected override CompileResult CompileCode(string sourceCode)
        {
            // First delete old working directory
            DirectoryHelpers.SafeDeleteDirectory(this.workingDirectory, true);

            this.workingDirectory = DirectoryHelpers.CreateTempDirectory();

            var sourceFilePath = JavaCodePreprocessorHelper.CreateSubmissionFile(sourceCode, this.workingDirectory);

            var compileResult = this.Compiler.Compile(
                this.CompilerPath,
                sourceFilePath,
                this.CompilerAdditionalArguments);

            return(compileResult);
        }
Ejemplo n.º 2
0
        private void AddTestsToUserSubmission(ExecutionContext context, string submissionZipFilePath)
        {
            var testNumber = 0;
            var filePaths  = new string[context.Tests.Count()];

            foreach (var test in context.Tests)
            {
                var className    = JavaCodePreprocessorHelper.GetPublicClassName(test.Input);
                var testFileName =
                    $"{this.WorkingDirectory}\\{className}{GlobalConstants.JavaSourceFileExtension}";
                File.WriteAllText(testFileName, test.Input);
                filePaths[testNumber] = testFileName;
                this.TestNames.Add(className);
                testNumber++;
            }

            FileHelpers.AddFilesToZipArchive(submissionZipFilePath, string.Empty, filePaths);
            FileHelpers.DeleteFiles(filePaths);
        }
Ejemplo n.º 3
0
        protected override void AddTestsToUserSubmission(ExecutionContext context, string submissionZipFilePath)
        {
            var testNumber = 0;
            var filePaths  = new string[context.Tests.Count()];

            foreach (var test in context.Tests)
            {
                var className    = JavaCodePreprocessorHelper.GetPublicClassName(test.Input);
                var testFileName =
                    $"{this.WorkingDirectory}\\{className}{GlobalConstants.JavaSourceFileExtension}";
                File.WriteAllText(testFileName, $"package {this.PackageName};{Environment.NewLine}{test.Input}");
                filePaths[testNumber] = testFileName;
                this.TestNames.Add($"{this.PackageName}.{className}");
                testNumber++;
            }

            FileHelpers.AddFilesToZipArchive(
                submissionZipFilePath,
                this.ProjectTestDirectoryInSubmissionZip,
                filePaths);
            FileHelpers.DeleteFiles(filePaths);
        }
 protected virtual string CreateSubmissionFile <TInput>(IExecutionContext <TInput> executionContext)
 => JavaCodePreprocessorHelper.CreateSubmissionFile(
     executionContext.Code,
     this.WorkingDirectory);
Ejemplo n.º 5
0
        public override ExecutionResult Execute(ExecutionContext executionContext)
        {
            var result = new ExecutionResult();

            // Create a temp file with the submission code
            string submissionFilePath;

            try
            {
                submissionFilePath = this.CreateSubmissionFile(executionContext);
            }
            catch (ArgumentException exception)
            {
                result.IsCompiledSuccessfully = false;
                result.CompilerComment        = exception.Message;

                return(result);
            }

            var compilerPath      = this.GetCompilerPathFunc(executionContext.CompilerType);
            var combinedArguments = executionContext.AdditionalCompilerArguments + this.ClassPath;

            var executor = new RestrictedProcessExecutor();
            var checker  = Checker.CreateChecker(
                executionContext.CheckerAssemblyName,
                executionContext.CheckerTypeName,
                executionContext.CheckerParameter);

            if (!string.IsNullOrWhiteSpace(executionContext.TaskSkeletonAsString))
            {
                FileHelpers.UnzipFile(submissionFilePath, this.WorkingDirectory);

                string className = JavaCodePreprocessorHelper.GetPublicClassName(executionContext.TaskSkeletonAsString);
                string filePath  = $"{this.WorkingDirectory}\\{className}{GlobalConstants.JavaSourceFileExtension}";
                File.WriteAllText(filePath, executionContext.TaskSkeletonAsString);
                FileHelpers.AddFilesToZipArchive(submissionFilePath, string.Empty, filePath);

                var preprocessCompileResult = this.Compile(
                    executionContext.CompilerType,
                    compilerPath,
                    combinedArguments,
                    submissionFilePath);

                result.IsCompiledSuccessfully = preprocessCompileResult.IsCompiledSuccessfully;
                result.CompilerComment        = preprocessCompileResult.CompilerComment;
                if (!result.IsCompiledSuccessfully)
                {
                    return(result);
                }

                var preprocessExecutor = new StandardProcessExecutor();

                var preprocessArguments = new List <string>();
                preprocessArguments.Add(this.ClassPath);
                preprocessArguments.Add(AdditionalExecutionArguments);
                preprocessArguments.Add(className);
                preprocessArguments.Add(this.WorkingDirectory);
                preprocessArguments.AddRange(this.UserClassNames);

                var preprocessExecutionResult = preprocessExecutor.Execute(
                    this.JavaExecutablePath,
                    string.Empty,
                    executionContext.TimeLimit,
                    executionContext.MemoryLimit,
                    preprocessArguments,
                    this.WorkingDirectory);

                if (preprocessExecutionResult.ReceivedOutput.Contains(JvmInsufficientMemoryMessage))
                {
                    throw new InsufficientMemoryException(JvmInsufficientMemoryMessage);
                }

                var filesToAdd = preprocessExecutionResult
                                 .ReceivedOutput
                                 .Split(new string[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries);
                foreach (var file in filesToAdd)
                {
                    var path = Path.GetDirectoryName(file);
                    FileHelpers.AddFilesToZipArchive(submissionFilePath, path, this.WorkingDirectory + "\\" + file);
                }

                File.Delete(filePath);
            }

            var compilerResult = this.Compile(
                executionContext.CompilerType,
                compilerPath,
                combinedArguments,
                submissionFilePath);

            result.IsCompiledSuccessfully = compilerResult.IsCompiledSuccessfully;
            result.CompilerComment        = compilerResult.CompilerComment;
            if (!result.IsCompiledSuccessfully)
            {
                return(result);
            }

            var arguments = new List <string>();

            arguments.Add(this.ClassPath);
            arguments.Add(AdditionalExecutionArguments);
            arguments.Add(JUnitRunnerClassName);
            arguments.AddRange(this.UserClassNames);

            var processExecutionResult = executor.Execute(
                this.JavaExecutablePath,
                string.Empty,
                executionContext.TimeLimit,
                executionContext.MemoryLimit,
                arguments,
                this.WorkingDirectory,
                true);

            if (processExecutionResult.ReceivedOutput.Contains(JvmInsufficientMemoryMessage))
            {
                throw new InsufficientMemoryException(JvmInsufficientMemoryMessage);
            }

            var errorsByFiles = this.GetTestErrors(processExecutionResult.ReceivedOutput);
            var testIndex     = 0;

            foreach (var test in executionContext.Tests)
            {
                var message  = "Test Passed!";
                var testFile = this.TestNames[testIndex++];
                if (errorsByFiles.ContainsKey(testFile))
                {
                    message = errorsByFiles[testFile];
                }

                var testResult = this.ExecuteAndCheckTest(test, processExecutionResult, checker, message);
                result.TestResults.Add(testResult);
            }

            return(result);
        }