From bba74c88f4efbfd8e7921c05a33febb8a4a254ed Mon Sep 17 00:00:00 2001 From: Mathias Leppich Date: Mon, 13 Feb 2023 16:53:23 +0100 Subject: [PATCH] additional unit-tests --- test/test_repository.py | 42 ++++++++++++++++++++++++++++++++++++++--- 1 file changed, 39 insertions(+), 3 deletions(-) diff --git a/test/test_repository.py b/test/test_repository.py index d289f76eb..cf1defe72 100644 --- a/test/test_repository.py +++ b/test/test_repository.py @@ -760,17 +760,53 @@ def test_is_shallow(testrepo): assert testrepo.is_shallow -def test_repo_hashfile(testrepo): +def test_repo_hashfile_same_hash(testrepo): + data = 'Some multi-\nline text\n' + with (Path(testrepo.workdir) / 'untracked.txt').open('w') as f: + f.write(data) + + hashed_file_sha1 = testrepo.hashfile(str(Path(testrepo.workdir) / 'untracked.txt')) + hashed_data_sha1 = pygit2.hash(data) + assert hashed_file_sha1 == hashed_data_sha1 + +def test_repo_hashfile_crlf_normalization(testrepo): with (Path(testrepo.workdir) / '.gitattributes').open('w+') as f: print('*.txt eol=lf\n', file=f) + data = 'Some multi-\nline text\n' with (Path(testrepo.workdir) / 'untracked_lf.txt').open('w') as f: f.write(data) with (Path(testrepo.workdir) / 'untracked_crlf.txt').open('w') as f: f.write(data.replace('\n','\r\n')) + hashed_lf_sha1 = testrepo.hashfile(str(Path(testrepo.workdir) / 'untracked_lf.txt')) hashed_crlf_sha1 = testrepo.hashfile(str(Path(testrepo.workdir) / 'untracked_crlf.txt')) assert hashed_lf_sha1 == hashed_crlf_sha1 - hashed_sha1 = pygit2.hash(data) - assert hashed_lf_sha1 == hashed_sha1 +def test_repo_hashfile_no_normalization(testrepo): + with (Path(testrepo.workdir) / '.gitattributes').open('w+') as f: + print('*.txt -text\n', file=f) + + data = 'Some multi-\nline text\n' + with (Path(testrepo.workdir) / 'untracked_lf.txt').open('w') as f: + f.write(data) + with (Path(testrepo.workdir) / 'untracked_crlf.txt').open('w') as f: + f.write(data.replace('\n','\r\n')) + + hashed_lf_sha1 = testrepo.hashfile(str(Path(testrepo.workdir) / 'untracked_lf.txt')) + hashed_crlf_sha1 = testrepo.hashfile(str(Path(testrepo.workdir) / 'untracked_crlf.txt')) + assert hashed_lf_sha1 != hashed_crlf_sha1 + +def test_repo_hashfile_crlf_normalization_error(testrepo): + testrepo.config['core.safecrlf'] = True + with (Path(testrepo.workdir) / '.gitattributes').open('w+') as f: + print('*.txt eol=lf\n', file=f) + with (Path(testrepo.workdir) / 'untracked_crlf.txt').open('w') as f: + f.write('Some multi-\r\nline text\r\n') + + with pytest.raises(pygit2.GitError) as exc: + testrepo.hashfile(str(Path(testrepo.workdir) / 'untracked_crlf.txt')) + + assert "CRLF would be replaced by LF" in str(exc.value) + +