// make it internal to enable unit testing
        internal void ReadFrameContent(
            StackFrame frame,
            IEnumerable <string> allLines,
            int errorStartLineNumberInFile,
            int errorEndLineNumberInFile)
        {
            // Get the line boundaries in the file to be read and read all these lines at once into an array.
            var preErrorLineNumberInFile  = Math.Max(errorStartLineNumberInFile - _options.SourceCodeLineCount, 1);
            var postErrorLineNumberInFile = errorEndLineNumberInFile + _options.SourceCodeLineCount;
            var codeBlock = allLines
                            .Skip(preErrorLineNumberInFile - 1)
                            .Take(postErrorLineNumberInFile - preErrorLineNumberInFile + 1)
                            .ToArray();

            var numOfErrorLines             = (errorEndLineNumberInFile - errorStartLineNumberInFile) + 1;
            var errorStartLineNumberInArray = errorStartLineNumberInFile - preErrorLineNumberInFile;

            frame.PreContextLine = preErrorLineNumberInFile;
            frame.PreContextCode = codeBlock.Take(errorStartLineNumberInArray).ToArray();
            frame.ContextCode    = codeBlock
                                   .Skip(errorStartLineNumberInArray)
                                   .Take(numOfErrorLines)
                                   .ToArray();
            frame.PostContextCode = codeBlock
                                    .Skip(errorStartLineNumberInArray + numOfErrorLines)
                                    .ToArray();
        }
        private Task DisplayCompilationException(HttpContext context,
                                                 ICompilationException compilationException)
        {
            var model = new CompilationErrorPageModel
            {
                Options = _options,
            };

            foreach (var compilationFailure in compilationException.CompilationFailures)
            {
                var stackFrames  = new List <StackFrame>();
                var errorDetails = new ErrorDetails
                {
                    StackFrames = stackFrames
                };
                var fileContent = compilationFailure.SourceFileContent
                                  .Split(new[] { Environment.NewLine }, StringSplitOptions.None);

                foreach (var item in compilationFailure.Messages)
                {
                    var frame = new StackFrame
                    {
                        File     = compilationFailure.SourceFilePath,
                        Line     = item.StartLine,
                        Function = string.Empty
                    };

                    ReadFrameContent(frame, fileContent, item.StartLine, item.EndLine);
                    frame.ErrorDetails = item.Message;

                    stackFrames.Add(frame);
                }

                model.ErrorDetails.Add(errorDetails);
            }

            var errorPage = new CompilationErrorPage
            {
                Model = model
            };

            return(errorPage.ExecuteAsync(context));
        }
        // make it internal to enable unit testing
        internal StackFrame GetStackFrame(string function, string file, int lineNumber)
        {
            var frame = new StackFrame {
                Function = function, File = file, Line = lineNumber
            };

            if (string.IsNullOrEmpty(file))
            {
                return(frame);
            }

            IEnumerable <string> lines = null;

            if (File.Exists(file))
            {
                lines = File.ReadLines(file);
            }
            else
            {
                // Handle relative paths and embedded files
                var fileInfo = _fileProvider.GetFileInfo(file);
                if (fileInfo.Exists)
                {
                    // ReadLines doesn't accept a stream. Use ReadLines as its more efficient
                    // relative to reading lines via stream reader
                    if (!string.IsNullOrEmpty(fileInfo.PhysicalPath))
                    {
                        lines = File.ReadLines(fileInfo.PhysicalPath);
                    }
                    else
                    {
                        lines = ReadLines(fileInfo);
                    }
                }
            }

            if (lines != null)
            {
                ReadFrameContent(frame, lines, lineNumber, lineNumber);
            }

            return(frame);
        }
        // make it internal to enable unit testing
        internal void ReadFrameContent(
            StackFrame frame,
            IEnumerable<string> allLines,
            int errorStartLineNumberInFile,
            int errorEndLineNumberInFile)
        {
            // Get the line boundaries in the file to be read and read all these lines at once into an array.
            var preErrorLineNumberInFile = Math.Max(errorStartLineNumberInFile - _options.SourceCodeLineCount, 1);
            var postErrorLineNumberInFile = errorEndLineNumberInFile + _options.SourceCodeLineCount;
            var codeBlock = allLines
                .Skip(preErrorLineNumberInFile - 1)
                .Take(postErrorLineNumberInFile - preErrorLineNumberInFile + 1)
                .ToArray();

            var numOfErrorLines = (errorEndLineNumberInFile - errorStartLineNumberInFile) + 1;
            var errorStartLineNumberInArray = errorStartLineNumberInFile - preErrorLineNumberInFile;

            frame.PreContextLine = preErrorLineNumberInFile;
            frame.PreContextCode = codeBlock.Take(errorStartLineNumberInArray).ToArray();
            frame.ContextCode = codeBlock
                .Skip(errorStartLineNumberInArray)
                .Take(numOfErrorLines)
                .ToArray();
            frame.PostContextCode = codeBlock
                .Skip(errorStartLineNumberInArray + numOfErrorLines)
                .ToArray();
        }
        // make it internal to enable unit testing
        internal StackFrame GetStackFrame(string function, string file, int lineNumber)
        {
            var frame = new StackFrame { Function = function, File = file, Line = lineNumber };

            if (string.IsNullOrEmpty(file))
            {
                return frame;
            }

            IEnumerable<string> lines = null;
            if (File.Exists(file))
            {
                lines = File.ReadLines(file);
            }
            else
            {
                // Handle relative paths and embedded files
                var fileInfo = _fileProvider.GetFileInfo(file);
                if (fileInfo.Exists)
                {
                    // ReadLines doesn't accept a stream. Use ReadLines as its more efficient
                    // relative to reading lines via stream reader
                    if (!string.IsNullOrEmpty(fileInfo.PhysicalPath))
                    {
                        lines = File.ReadLines(fileInfo.PhysicalPath);
                    }
                    else
                    {
                        lines = ReadLines(fileInfo);
                    }
                }
            }

            if (lines != null)
            {
                ReadFrameContent(frame, lines, lineNumber, lineNumber);
            }

            return frame;
        }
        private Task DisplayCompilationException(HttpContext context,
                                                 ICompilationException compilationException)
        {
            var model = new CompilationErrorPageModel
            {
                Options = _options,
            };

            foreach (var compilationFailure in compilationException.CompilationFailures)
            {
                var stackFrames = new List<StackFrame>();
                var errorDetails = new ErrorDetails
                {
                    StackFrames = stackFrames
                };
                var fileContent = compilationFailure.SourceFileContent
                                                    .Split(new[] { Environment.NewLine }, StringSplitOptions.None);

                foreach (var item in compilationFailure.Messages)
                {
                    var frame = new StackFrame
                    {
                        File = compilationFailure.SourceFilePath,
                        Line = item.StartLine,
                        Function = string.Empty
                    };

                    ReadFrameContent(frame, fileContent, item.StartLine, item.EndLine);
                    frame.ErrorDetails = item.Message;

                    stackFrames.Add(frame);
                }

                model.ErrorDetails.Add(errorDetails);
            }

            var errorPage = new CompilationErrorPage
            {
                Model = model
            };

            return errorPage.ExecuteAsync(context);
        }
 private StackFrame LoadFrame(string function, string file, int lineNumber, bool showSource)
 {
     var frame = new StackFrame { Function = function, File = file, Line = lineNumber };
     if (showSource && File.Exists(file))
     {
         IEnumerable<string> code = File.ReadLines(file);
         frame.PreContextLine = Math.Max(lineNumber - _options.SourceCodeLineCount, 1);
         frame.PreContextCode = code.Skip(frame.PreContextLine - 1).Take(lineNumber - frame.PreContextLine).ToArray();
         frame.ContextCode = code.Skip(lineNumber - 1).FirstOrDefault();
         frame.PostContextCode = code.Skip(lineNumber).Take(_options.SourceCodeLineCount).ToArray();
     }
     return frame;
 }
        public void DisplaysSourceCodeLines_PreAndPostErrorLine(ErrorData errorData)
        {
            // Arrange
            var middleware = GetErrorPageMiddleware();
            var stackFrame = new StackFrame();

            // Act
            middleware.ReadFrameContent(
                stackFrame, errorData.AllLines, errorData.ErrorStartLine, errorData.ErrorEndLine);

            // Assert
            Assert.Equal(errorData.ExpectedPreContextLine, stackFrame.PreContextLine);
            Assert.Equal(errorData.ExpectedPreErrorCode, stackFrame.PreContextCode);
            Assert.Equal(errorData.ExpectedErrorCode, stackFrame.ContextCode);
            Assert.Equal(errorData.ExpectedPostErrorCode, stackFrame.PostContextCode);
        }
Esempio n. 9
0
 private void ReadFrameContent(StackFrame frame,
                               IEnumerable<string> code,
                               int startLineNumber,
                               int endLineNumber)
 {
     frame.PreContextLine = Math.Max(startLineNumber - _options.SourceCodeLineCount, 1);
     frame.PreContextCode = code.Skip(frame.PreContextLine - 1).Take(startLineNumber - frame.PreContextLine).ToArray();
     frame.ContextCode = code.Skip(startLineNumber - 1).Take(1 + Math.Max(0, endLineNumber - startLineNumber));
     frame.PostContextCode = code.Skip(startLineNumber).Take(_options.SourceCodeLineCount).ToArray();
 }
Esempio n. 10
0
 private StackFrame LoadFrame(string function, string file, int lineNumber)
 {
     var frame = new StackFrame { Function = function, File = file, Line = lineNumber };
     if (File.Exists(file))
     {
         var code = File.ReadLines(file);
         ReadFrameContent(frame, code, lineNumber, lineNumber);
     }
     return frame;
 }
 private StackFrame LoadFrame(string function, string file, int lineNumber, bool showSource)
 {
     var frame = new StackFrame { Function = function, File = file, Line = lineNumber };
     if (showSource && File.Exists(file))
     {
         IEnumerable<string> code = File.ReadLines(file);
         ReadFrameContent(frame, code, lineNumber, lineNumber);
     }
     return frame;
 }