Skip to content

Failing test: CanShowChangesForAFileWithWin1250EncodingContent #533

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions LibGit2Sharp.Tests/DiffTreeToTreeFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -560,5 +560,36 @@ public void CallingCompareWithAnUnsupportedGenericParamThrows()
Assert.Throws<LibGit2SharpException>(() => repo.Diff.Compare<string>());
}
}

[Fact]
public void CanShowChangesForAFileWithWin1250EncodingContent()
{
const string testFile = "windows1250.txt";

string repoPath = CloneStandardTestRepo();
using (var repo = new Repository(repoPath))
{
// Create the file in the workdir
string testFileFullPath = Path.Combine(repo.Info.WorkingDirectory, testFile);
Encoding win1250Encoding = Encoding.GetEncoding("windows-1250");
Assert.NotNull(win1250Encoding);
const string testFileContent = "Content in windows-1250 encoding."
+ "\u0105\u0119\u0107\u0142\u00F3\u015b\u017a\u017c"
+ "\n";
File.WriteAllText(testFileFullPath, testFileContent, win1250Encoding);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Touch(repo.Info.WorkingDirectory, testfile, testFileContent, win1250encoding);

Courtesy of M. @dahlbyk 😉


//compare changes
var changes = repo.Diff.Compare<Patch>(repo.Head.Tip.Tree,
DiffTargets.WorkingDirectory,
new[] { testFile }).FirstOrDefault();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe using .Single() here would allow us to remove the assert just below.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can it be like that gitextensions@7d1348b?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually Single() would enforce to the reader that we only expect one result 😉

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, but from the other hand we expect that patch contains the testFile (Single() doesn't check that it is file we expected, the reader will see that there is one file in the patch, but will not see that it is the right file) and we are not testing here that patch contains only files that we expect. If you would like to do that, then I would add another assertion Assert.Equal(patch.Count(), 1);

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I hear you, but the test explicitly passes in testfile to the list of paths that should be examined. As such, if one and only patch segment is returned, that should be the one matching the passed in path.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I could believe that that happen, but I prefer to ensure that it actually happen. If by accident there exists other, but very similar file (name and content) and if there will be a bug in Compare that cause that the other file will apear in patch, this test will fail and you will be wondering why. After checking byte by byte that contents should be identical you will finally find that the problem lies somewhere else.


Assert.NotNull(changes);
string expectedHunkHeader = "@@ -0,0 +1 @@\n+";
int hunkHeaderIdx = changes.Patch.IndexOf(expectedHunkHeader);
Assert.True(hunkHeaderIdx > 0, "Hunk header expected: " + expectedHunkHeader);
string fileContentFromPatch = changes.Patch.Substring(hunkHeaderIdx + expectedHunkHeader.Length);
Assert.Equal(testFileContent, fileContentFromPatch);
}
}
}
}