Пример #1
0
        public override CompileResult Compile(string compilerPath, string inputDirectory, string additionalArguments)
        {
            if (compilerPath == null)
            {
                throw new ArgumentNullException(nameof(compilerPath));
            }

            if (inputDirectory == null)
            {
                throw new ArgumentNullException(nameof(inputDirectory));
            }

            if (!File.Exists(compilerPath))
            {
                return(new CompileResult(false, $"Compiler not found! Searched in: {compilerPath}"));
            }

            if (!Directory.Exists(inputDirectory))
            {
                return(new CompileResult(false, $"Input directory not found! Searched in: {inputDirectory}"));
            }

            this.CompilationDirectory = FileHelpers.BuildPath(inputDirectory, CompilationDirectoryName);

            if (Directory.Exists(this.CompilationDirectory))
            {
                DirectoryHelpers.SafeDeleteDirectory(this.CompilationDirectory, true);
            }

            Directory.CreateDirectory(this.CompilationDirectory);

            // Build compiler arguments
            var outputDirectory = this.CompilationDirectory;
            var arguments       = this.BuildCompilerArguments(inputDirectory, outputDirectory, additionalArguments);

            // Find compiler directory
            var directoryInfo = new FileInfo(compilerPath).Directory;

            if (directoryInfo == null)
            {
                return(new CompileResult(false, $"Compiler directory is null. Compiler path value: {compilerPath}"));
            }

            // Prepare process start information
            var processStartInfo = this.SetCompilerProcessStartInfo(compilerPath, directoryInfo, arguments);
            var compilerOutput   = ExecuteCompiler(processStartInfo, this.MaxProcessExitTimeOutInMilliseconds);

            outputDirectory = this.ChangeOutputFileAfterCompilation(outputDirectory);
            if (!compilerOutput.IsSuccessful)
            {
                return(new CompileResult(false, $"Compiled file is missing. Compiler output: {compilerOutput.Output}"));
            }

            if (!string.IsNullOrWhiteSpace(compilerOutput.Output))
            {
                return(new CompileResult(true, compilerOutput.Output, outputDirectory));
            }

            return(new CompileResult(outputDirectory));
        }
Пример #2
0
        private static void CreateInitFile(string directoryPath)
        {
            var filePath = FileHelpers.BuildPath(directoryPath, InitFileName);

            if (!FileHelpers.FileExists(filePath))
            {
                FileHelpers.WriteAllText(filePath, string.Empty);
            }
        }
Пример #3
0
        protected void SaveZipSubmission(byte[] submissionContent, string directory)
        {
            var submissionFilePath = FileHelpers.BuildPath(directory, ZippedSubmissionName);

            FileHelpers.WriteAllBytes(submissionFilePath, submissionContent);
            FileHelpers.RemoveFilesFromZip(submissionFilePath, RemoveMacFolderPattern);
            FileHelpers.UnzipFile(submissionFilePath, directory);
            FileHelpers.DeleteFile(submissionFilePath);
        }
        private string PrepareSubmissionFile(byte[] submissionFileContent)
        {
            var submissionFilePath = FileHelpers.BuildPath(this.WorkingDirectory, SubmissionFileName);

            File.WriteAllBytes(submissionFilePath, submissionFileContent);
            FileHelpers.RemoveFilesFromZip(submissionFilePath, RemoveMacFolderPattern);
            this.AddSandboxExecutorSourceFileToSubmissionZip(submissionFilePath);
            return(submissionFilePath);
        }
        protected string CreateNUnitLiteConsoleAppCsProjFile(string csProjTemplate)
        {
            var consoleAppCsProjPath = FileHelpers.BuildPath(
                this.NUnitLiteConsoleAppDirectory,
                $"{NUnitLiteConsoleAppFolderName}{CsProjFileExtension}");

            File.WriteAllText(consoleAppCsProjPath, csProjTemplate);

            return(consoleAppCsProjPath);
        }
        private void SaveTestsToFiles(IEnumerable <TestContext> tests)
        {
            foreach (var test in tests)
            {
                var testInputContent = this.PreprocessTestInput(test.Input);

                FileHelpers.SaveStringToFile(
                    testInputContent,
                    FileHelpers.BuildPath(this.TestsPath, $"{test.Id}{JavaScriptFileExtension}"));
            }
        }
Пример #7
0
        /// <summary>
        /// Constructs full path for the class file, by combining all subfolders with the file name.
        /// </summary>
        /// <param name="className">The name of the class</param>
        /// <param name="subfolderNames">The subfolders in which the class file should be created</param>
        /// <returns>Full path of the file that should be created for a class</returns>
        private string GetFilePathForClass(string className, IEnumerable <string> subfolderNames = null)
        {
            var pathArguments = new List <string> {
                this.projectDirectoryPath
            };

            pathArguments.AddRange(subfolderNames ?? Enumerable.Empty <string>());
            pathArguments.Add(GetFileNameWithExtensionForClass(className));

            return(FileHelpers.BuildPath(pathArguments.ToArray()));
        }
Пример #8
0
        protected void SaveTestFiles(IEnumerable <TestContext> tests, string compileDirectory)
        {
            var index = 0;

            foreach (var test in tests)
            {
                var testName       = this.TestNames[index++];
                var testedCodePath = FileHelpers.BuildPath(compileDirectory, $"{testName}{CSharpFileExtension}");
                this.TestPaths.Add(testedCodePath);
                File.WriteAllText(testedCodePath, test.Input);
            }
        }
Пример #9
0
        protected override IExecutionResult <TestResult> ExecuteAgainstTestsInput(
            IExecutionContext <TestsInputModel> executionContext,
            IExecutionResult <TestResult> result)
        {
            this.SaveZipSubmission(executionContext.FileContent, this.WorkingDirectory);

            var executor = this.CreateExecutor();
            var checker  = executionContext.Input.GetChecker();

            this.projectDirectoryPath      = FileHelpers.BuildPath(this.WorkingDirectory, ProjectFolderName);
            this.expectedProjectFilesCount = this.GetExpectedProjectFilesCount(
                executionContext.Input.TaskSkeletonAsString);

            return(this.RunTests(string.Empty, executor, checker, executionContext, result));
        }
        /// <summary>
        /// Saves all tests from the execution context as separate files in tests directory.
        /// Full paths to the files are preserved in a private field.
        /// </summary>
        /// <param name="tests">All tests from the execution context</param>
        private void SaveTests(IList <TestContext> tests)
        {
            var testsDirectoryName = FileHelpers.BuildPath(this.WorkingDirectory, TestsFolderName);

            this.testPaths = new string[tests.Count];

            for (var i = 0; i < tests.Count; i++)
            {
                var test         = tests[i];
                var testFileName = $"test_{i}{PythonFileExtension}";
                var testSavePath = FileHelpers.BuildPath(testsDirectoryName, testFileName);

                PythonStrategiesHelper.CreateFileInPackage(testSavePath, test.Input);

                this.testPaths[i] = testSavePath;
            }
        }
        protected (string csProjTemplate, string csProjPath) CreateNUnitLiteConsoleApp(
            IEnumerable <string> projectsToTestCsProjPaths)
        {
            var consoleAppEntryPointPath = FileHelpers.BuildPath(
                this.NUnitLiteConsoleAppDirectory,
                $"{NUnitLiteConsoleAppProgramName}{Constants.CSharpFileExtension}");

            File.WriteAllText(consoleAppEntryPointPath, NUnitLiteConsoleAppProgramTemplate);

            var references = projectsToTestCsProjPaths
                             .Select(path => this.projectReferenceTemplate.Replace(ProjectPathPlaceholder, path));

            var csProjTemplate = this.NUnitLiteConsoleAppCsProjTemplate
                                 .Replace(ProjectReferencesPlaceholder, string.Join(Environment.NewLine, references));

            var csProjPath = this.CreateNUnitLiteConsoleAppCsProjFile(csProjTemplate);

            return(csProjTemplate, csProjPath);
        }
        private void ExtractSubmissionFiles <TInput>(IExecutionContext <TInput> executionContext)
        {
            this.ValidateAllowedFileExtension(executionContext);

            var submissionFilePath = FileHelpers.BuildPath(this.WorkingDirectory, "temp");

            File.WriteAllBytes(submissionFilePath, executionContext.FileContent);
            using (ZipFile zip = ZipFile.Read(submissionFilePath))
            {
                zip.RemoveSelectedEntries("node_modules/*");
                zip.Save();
            }

            FileHelpers.RemoveFilesFromZip(submissionFilePath, RemoveMacFolderPattern);
            FileHelpers.UnzipFile(submissionFilePath, this.UserApplicationPath);

            Directory.CreateDirectory(this.TestsPath);
            Directory.CreateDirectory(this.NginxConfFileDirectory);
        }
Пример #13
0
        protected override IExecutionResult <TestResult> RunUnitTests(
            string consoleRunnerPath,
            IExecutionContext <TestsInputModel> executionContext,
            IExecutor executor,
            IChecker checker,
            IExecutionResult <TestResult> result,
            string csProjFilePath,
            string additionalExecutionArguments)
        {
            var additionalExecutionArgumentsArray = additionalExecutionArguments
                                                    .Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);

            var compilerPath   = this.GetCompilerPathFunc(executionContext.CompilerType);
            var testedCodePath = FileHelpers.BuildPath(
                this.NUnitLiteConsoleAppDirectory,
                UnitTestStrategiesHelper.TestedCodeFileNameWithExtension);
            var originalTestsPassed = -1;

            var tests = executionContext.Input.Tests.OrderBy(x => x.IsTrialTest).ThenBy(x => x.OrderBy).ToList();

            for (var i = 0; i < tests.Count; i++)
            {
                var test = tests[i];

                this.SaveSetupFixture(this.NUnitLiteConsoleAppDirectory);

                File.WriteAllText(testedCodePath, test.Input);

                // Compiling
                var compilerResult = this.Compile(
                    executionContext.CompilerType,
                    compilerPath,
                    executionContext.AdditionalCompilerArguments,
                    consoleRunnerPath);

                result.IsCompiledSuccessfully = compilerResult.IsCompiledSuccessfully;
                result.CompilerComment        = compilerResult.CompilerComment;

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

                // Delete tests before execution so the user can't acces them
                FileHelpers.DeleteFiles(testedCodePath, this.SetupFixturePath);

                var arguments = new List <string> {
                    compilerResult.OutputFile
                };
                arguments.AddRange(additionalExecutionArgumentsArray);

                var processExecutionResult = executor.Execute(
                    compilerPath,
                    string.Empty,
                    executionContext.TimeLimit,
                    executionContext.MemoryLimit,
                    arguments,
                    workingDirectory: null,
                    useProcessTime: false,
                    useSystemEncoding: true);

                if (!string.IsNullOrWhiteSpace(processExecutionResult.ErrorOutput))
                {
                    throw new InvalidProcessExecutionOutputException(processExecutionResult.ErrorOutput);
                }

                var testResultsRegex = new Regex(TestResultsRegex);

                var processExecutionTestResult = UnitTestStrategiesHelper.GetTestResult(
                    processExecutionResult.ReceivedOutput,
                    testResultsRegex,
                    originalTestsPassed,
                    i == 0,
                    this.ExtractTotalAndPassedTestsCount);

                var message = processExecutionTestResult.message;
                originalTestsPassed = processExecutionTestResult.originalTestsPassed;

                var testResult = this.CheckAndGetTestResult(test, processExecutionResult, checker, message);
                result.Results.Add(testResult);

                if (i < tests.Count - 1)
                {
                    // Recreate NUnitLite Console App .csproj file, deleted after compilation, to compile again
                    this.CreateNUnitLiteConsoleAppCsProjFile(this.nUnitLiteConsoleAppCsProjTemplate);
                }
            }

            return(result);
        }