Skip to content

Commit

Permalink
Merge branch 'hotfix/859-pound-sign-allowed-only-at-line-start'
Browse files Browse the repository at this point in the history
  • Loading branch information
fubar-coder committed Apr 24, 2018
2 parents ef7423a + e625c18 commit f20a70c
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 19 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#region License
#region License
// Copyright (c) 2018, Fluent Migrator Project
//
// Licensed under the Apache License, Version 2.0 (the "License");
Expand All @@ -25,7 +25,7 @@ public sealed class PoundSignSingleLineComment : SingleLineComment
/// Initializes a new instance of the <see cref="PoundSignSingleLineComment"/> class.
/// </summary>
public PoundSignSingleLineComment()
: base("#")
: base("#", true)
{
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#region License
#region License
// Copyright (c) 2018, Fluent Migrator Project
//
// Licensed under the Apache License, Version 2.0 (the "License");
Expand All @@ -24,14 +24,26 @@ namespace FluentMigrator.Runner.BatchParser.RangeSearchers
/// </summary>
public class SingleLineComment : IRangeSearcher
{
private readonly bool _onlyAtBeginningOfLine;
private readonly Regex _startCodeRegex;

/// <summary>
/// Initializes a new instance of the <see cref="SingleLineComment"/> class.
/// </summary>
/// <param name="startCode">The start code for the single line comment</param>
public SingleLineComment(string startCode)
: this(startCode, false)
{
}

/// <summary>
/// Initializes a new instance of the <see cref="SingleLineComment"/> class.
/// </summary>
/// <param name="startCode">The start code for the single line comment</param>
/// <param name="onlyAtBeginningOfLine">Find the start code only at the beginning of the line</param>
public SingleLineComment(string startCode, bool onlyAtBeginningOfLine)
{
_onlyAtBeginningOfLine = onlyAtBeginningOfLine;
_startCodeRegex = new Regex(Regex.Escape(startCode), RegexOptions.CultureInvariant | RegexOptions.Compiled);
StartCodeLength = startCode.Length;
}
Expand All @@ -51,6 +63,14 @@ public int FindStartCode(ILineReader reader)
var match = _startCodeRegex.Match(reader.Line, reader.Index);
if (!match.Success)
return -1;

if (_onlyAtBeginningOfLine)
{
var skippedText = reader.Line.Substring(0, match.Index);
if (!string.IsNullOrWhiteSpace(skippedText))
return -1;
}

return match.Index;
}

Expand Down
62 changes: 47 additions & 15 deletions test/FluentMigrator.Tests/Unit/BatchParser/RangeSearcherTests.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#region License
#region License
// Copyright (c) 2018, Fluent Migrator Project
//
// Licensed under the Apache License, Version 2.0 (the "License");
Expand Down Expand Up @@ -246,80 +246,112 @@ public void TestMultiLineCommentWithMultipleLines(string input, string expected)
}
}

[TestCase(" -- qweqwe", " qweqwe")]
[TestCase(" -- qwe\nqwe", " qwe")]
public void TestDoubleDashSingleLineComment(string input, string expected)
[TestCase(" -- qweqwe", " qweqwe", " ")]
[TestCase(" -- qwe\nqwe", " qwe", " \nqwe")]
[TestCase("asd -- qwe\nqwe", " qwe", "asd \nqwe")]
public void TestDoubleDashSingleLineComment(string input, string expectedComment, string expectedOther)
{
var source = new TextReaderSource(new StringReader(input));
var reader = source.CreateReader();
Assert.IsNotNull(reader);

var content = new StringBuilder();
var writer = new StringWriter(content)
var commentContent = new StringBuilder();
var commentWriter = new StringWriter(commentContent)
{
NewLine = "\n",
};

var otherContent = new StringBuilder();
var otherWriter = new StringWriter(otherContent)
{
NewLine = "\n",
};

var addNewLine = false;
var rangeSearcher = new DoubleDashSingleLineComment();
while (reader != null)
{
if (addNewLine)
otherWriter.WriteLine();

var startIndex = rangeSearcher.FindStartCode(reader);
if (startIndex == -1)
{
otherWriter.Write(reader.ReadString(reader.Length));
addNewLine = true;
reader = reader.Advance(reader.Length);
continue;
}

otherWriter.Write(reader.ReadString(startIndex));
reader = reader.Advance(startIndex + rangeSearcher.StartCodeLength);
Assert.IsNotNull(reader);

var endInfo = rangeSearcher.FindEndCode(reader);
Assert.IsNotNull(endInfo);

var contentLength = endInfo.Index - reader.Index;
writer.Write(reader.ReadString(contentLength));
commentWriter.Write(reader.ReadString(contentLength));
addNewLine = (contentLength + rangeSearcher.EndCodeLength) == reader.Length;
reader = reader.Advance(contentLength + rangeSearcher.EndCodeLength);
}

Assert.AreEqual(expected, content.ToString());
Assert.AreEqual(expectedComment, commentContent.ToString());
Assert.AreEqual(expectedOther, otherContent.ToString());
}

[TestCase(" # qweqwe", " qweqwe")]
[TestCase(" # qwe\nqwe", " qwe")]
public void TestPoundSignSingleLineComment(string input, string expected)
[TestCase(" # qweqwe", " qweqwe", " ")]
[TestCase(" # qwe\nqwe", " qwe", " \nqwe")]
[TestCase("asd # qwe\nqwe", "", "asd # qwe\nqwe")]
public void TestPoundSignSingleLineComment(string input, string expectedComment, string expectedOther)
{
var source = new TextReaderSource(new StringReader(input));
var reader = source.CreateReader();
Assert.IsNotNull(reader);

var content = new StringBuilder();
var writer = new StringWriter(content)
var commentContent = new StringBuilder();
var commentWriter = new StringWriter(commentContent)
{
NewLine = "\n",
};

var otherContent = new StringBuilder();
var otherWriter = new StringWriter(otherContent)
{
NewLine = "\n",
};

var addNewLine = false;
var rangeSearcher = new PoundSignSingleLineComment();
while (reader != null)
{
if (addNewLine)
otherWriter.WriteLine();

var startIndex = rangeSearcher.FindStartCode(reader);
if (startIndex == -1)
{
otherWriter.Write(reader.ReadString(reader.Length));
addNewLine = true;
reader = reader.Advance(reader.Length);
continue;
}

otherWriter.Write(reader.ReadString(startIndex));
reader = reader.Advance(startIndex + rangeSearcher.StartCodeLength);
Assert.IsNotNull(reader);

var endInfo = rangeSearcher.FindEndCode(reader);
Assert.IsNotNull(endInfo);

var contentLength = endInfo.Index - reader.Index;
writer.Write(reader.ReadString(contentLength));
commentWriter.Write(reader.ReadString(contentLength));
addNewLine = (contentLength + rangeSearcher.EndCodeLength) == reader.Length;
reader = reader.Advance(contentLength + rangeSearcher.EndCodeLength);
}

Assert.AreEqual(expected, content.ToString());
Assert.AreEqual(expectedComment, commentContent.ToString());
Assert.AreEqual(expectedOther, otherContent.ToString());
}

[Test]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#region License
#region License
// Copyright (c) 2018, Fluent Migrator Project
//
// Licensed under the Apache License, Version 2.0 (the "License");
Expand Down Expand Up @@ -232,6 +232,8 @@ public void TestSqlStrippedMultiLineCommentAndSqlWithoutGo(string input, string
}

[TestCase("-- blah\nqweqwe", "\nqweqwe\n")]
[TestCase("# blah\nqweqwe", "\nqweqwe\n")]
[TestCase("qwe # blah\nqweqwe", "qwe # blah\nqweqwe\n")]
public void TestSqlStrippedSingleLineCommentAndSqlWithoutGo(string input, string expected)
{
var output = new List<string>();
Expand Down

0 comments on commit f20a70c

Please sign in to comment.