Skip to content

Commit

Permalink
Added ability to comment line in AuthorsFile.
Browse files Browse the repository at this point in the history
Renamed tests to start with AuthorsFile instead of "Test"
Added tests for international author names.
  • Loading branch information
edwinf committed Aug 28, 2012
1 parent 449f258 commit db9044d
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 18 deletions.
26 changes: 14 additions & 12 deletions GitTfs/Util/AuthorsFile.cs
Expand Up @@ -38,22 +38,24 @@ public void Parse(TextReader authorsFileStream)
while (line != null)
{
lineCount++;
//regex pulled from git svn script here: https://github.com/git/git/blob/master/git-svn.perl
Regex ex = new Regex(@"^(.+?|\(no author\))\s*=\s*(.+?)\s*<(.+)>\s*$");
Match match = ex.Match(line);
if (match.Groups.Count != 4 || String.IsNullOrWhiteSpace(match.Groups[1].Value) || String.IsNullOrWhiteSpace(match.Groups[2].Value) || String.IsNullOrWhiteSpace(match.Groups[3].Value))
if (!line.StartsWith("#"))
{
throw new GitTfsException("Invalid format of Authors file on line " + lineCount + ".");
}
else
{
if (!authors.ContainsKey(match.Groups[1].Value))
//regex pulled from git svn script here: https://github.com/git/git/blob/master/git-svn.perl
Regex ex = new Regex(@"^(.+?|\(no author\))\s*=\s*(.+?)\s*<(.+)>\s*$");
Match match = ex.Match(line);
if (match.Groups.Count != 4 || String.IsNullOrWhiteSpace(match.Groups[1].Value) || String.IsNullOrWhiteSpace(match.Groups[2].Value) || String.IsNullOrWhiteSpace(match.Groups[3].Value))
{
//git svn doesn't trim, but maybe this should?
authors.Add(match.Groups[1].Value, new Author() { Name = match.Groups[2].Value, Email = match.Groups[3].Value });
throw new GitTfsException("Invalid format of Authors file on line " + lineCount + ".");
}
else
{
if (!authors.ContainsKey(match.Groups[1].Value))
{
//git svn doesn't trim, but maybe this should?
authors.Add(match.Groups[1].Value, new Author() { Name = match.Groups[2].Value, Email = match.Groups[3].Value });
}
}
}

line = authorsFileStream.ReadLine();
}
}
Expand Down
88 changes: 82 additions & 6 deletions GitTfsTest/Util/AuthorsFileUnitTest.cs
Expand Up @@ -42,7 +42,7 @@ public TestContext TestContext


[TestMethod]
public void TestEmptyFile()
public void AuthorsFileEmptyFile()
{
MemoryStream ms = new MemoryStream();
StreamReader sr = new StreamReader(ms);
Expand All @@ -53,7 +53,7 @@ public void TestEmptyFile()
}

[TestMethod]
public void TestSimpleRecord()
public void AuthorsFileSimpleRecord()
{
string author = @"Domain\Test.User = Test User <TestUser@example.com>";
AuthorsFile authFile = new AuthorsFile();
Expand All @@ -67,7 +67,7 @@ public void TestSimpleRecord()
}

[TestMethod]
public void TestCaseInsensitiveRecord()
public void AuthorsFileCaseInsensitiveRecord()
{
string author = @"DOMAIN\Test.User = Test User <TestUser@example.com>";
AuthorsFile authFile = new AuthorsFile();
Expand All @@ -81,7 +81,7 @@ public void TestCaseInsensitiveRecord()
}

[TestMethod]
public void TestMultiLineRecord()
public void AuthorsFileMultiLineRecord()
{
string author =
@"Domain\Test.User = Test User <TestUser@example.com>
Expand All @@ -104,7 +104,7 @@ public void TestMultiLineRecord()

[TestMethod]
[ExpectedException(typeof(GitTfsException))]
public void TestMultiLineRecordWithBlankLine()
public void AuthorsFileMultiLineRecordWithBlankLine()
{
string author =
@"Domain\Test.User = Test User <TestUser@example.com>
Expand All @@ -116,12 +116,88 @@ public void TestMultiLineRecordWithBlankLine()

[TestMethod]
[ExpectedException(typeof(GitTfsException))]
public void TestBadRecord()
public void AuthorsFileTestBadRecord()
{
string author =
@"Domain\Test.User = Test User";
AuthorsFile authFile = new AuthorsFile();
authFile.Parse(new StreamReader(new MemoryStream(Encoding.ASCII.GetBytes(author))));
}

[TestMethod]
public void AuthorsFileCommentCharacterStartOfLine()
{
string author =
@"Domain\Test.User = Test User <TestUser@example.com>
#Domain\Different.User = Three Name User < TestUser@example.com >";
AuthorsFile authFile = new AuthorsFile();
authFile.Parse(new StreamReader(new MemoryStream(Encoding.ASCII.GetBytes(author))));
Assert.IsNotNull(authFile.Authors);
Assert.AreEqual<int>(1, authFile.Authors.Count);

Assert.IsTrue(authFile.Authors.ContainsKey(@"Domain\Test.User"));
Assert.IsFalse(authFile.Authors.ContainsKey(@"Domain\Different.User"));
}

[TestMethod]
public void AuthorsFileCommentCharacterMiddleOfLine()
{
string author =
@"Domain\Test.User = Test User <TestUser@example.com>
D#omain\Different.User = Three Name User < TestUser@example.com >";
AuthorsFile authFile = new AuthorsFile();
authFile.Parse(new StreamReader(new MemoryStream(Encoding.ASCII.GetBytes(author))));
Assert.IsNotNull(authFile.Authors);
Assert.AreEqual<int>(2, authFile.Authors.Count);

Assert.IsTrue(authFile.Authors.ContainsKey(@"Domain\Test.User"));
Assert.IsTrue(authFile.Authors.ContainsKey(@"D#omain\Different.User"));
}

[TestMethod]
public void AuthorsFileInternationalCharacters()
{
string author = @"DOMAIN\Blåbærsyltetøy = ÆØÅ User <ÆØÅ@example.com>";
AuthorsFile authFile = new AuthorsFile();
authFile.Parse(new StreamReader(new MemoryStream(Encoding.UTF8.GetBytes(author))));
Assert.IsNotNull(authFile.Authors);
Assert.AreEqual<int>(1, authFile.Authors.Count);
Assert.IsTrue(authFile.Authors.ContainsKey(@"domain\Blåbærsyltetøy"));
Author auth = authFile.Authors[@"domain\Blåbærsyltetøy"];
Assert.AreEqual<string>("ÆØÅ User", auth.Name);
Assert.AreEqual<string>("ÆØÅ@example.com", auth.Email);
}

[TestMethod]
public void AuthorsFileInternationalCharactersMultiLine()
{
string author = @"DOMAIN\Blåbærsyltetøy = ÆØÅ User <ÆØÅ@example.com>
differentDomain\Blåbærsyltetøy = ÆØÅ User <ÆØÅ@example.com>";
AuthorsFile authFile = new AuthorsFile();
authFile.Parse(new StreamReader(new MemoryStream(Encoding.UTF8.GetBytes(author))));
Assert.IsNotNull(authFile.Authors);
Assert.AreEqual<int>(2, authFile.Authors.Count);
Assert.IsTrue(authFile.Authors.ContainsKey(@"domain\Blåbærsyltetøy"));
Author auth = authFile.Authors[@"domain\Blåbærsyltetøy"];
Assert.AreEqual<string>("ÆØÅ User", auth.Name);
Assert.AreEqual<string>("ÆØÅ@example.com", auth.Email);

Assert.IsTrue(authFile.Authors.ContainsKey(@"differentDomain\Blåbærsyltetøy"));
auth = authFile.Authors[@"differentDomain\Blåbærsyltetøy"];
Assert.AreEqual<string>("ÆØÅ User", auth.Name);
Assert.AreEqual<string>("ÆØÅ@example.com", auth.Email);
}

[TestMethod]
public void AuthorsFileInternationalCharactersCommented()
{
string author = @"DOMAIN\Blåbærsyltetøy = ÆØÅ User <ÆØÅ@example.com>
#DifferentDomain\Blåbærsyltetøy = ÆØÅ User <ÆØÅ@example.com>";
AuthorsFile authFile = new AuthorsFile();
authFile.Parse(new StreamReader(new MemoryStream(Encoding.UTF8.GetBytes(author))));
Assert.IsNotNull(authFile.Authors);
Assert.AreEqual<int>(1, authFile.Authors.Count);
Assert.IsTrue(authFile.Authors.ContainsKey(@"domain\Blåbærsyltetøy"));
}
}
}

0 comments on commit db9044d

Please sign in to comment.