From 89b5fa839690e00eee3bf00dc97c95b9c9fe0173 Mon Sep 17 00:00:00 2001 From: Sven Strickroth Date: Sun, 9 Aug 2015 19:34:53 +0200 Subject: [PATCH] Add tests for CGitIgnoreItem Signed-off-by: Sven Strickroth --- src/Git/GitIndex.cpp | 31 +++-- src/Git/gitindex.h | 10 ++ test/UnitTests/GitIndexTest.cpp | 257 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 289 insertions(+), 9 deletions(-) diff --git a/src/Git/GitIndex.cpp b/src/Git/GitIndex.cpp index 5d801760f..fa1e7b151 100644 --- a/src/Git/GitIndex.cpp +++ b/src/Git/GitIndex.cpp @@ -858,6 +858,24 @@ int CGitIgnoreItem::FetchIgnoreList(const CString &projectroot, const CString &f return 0; } +#ifdef GTEST_INCLUDE_GTEST_GTEST_H_ +int CGitIgnoreItem::IsPathIgnored(const CStringA& patha, int& type) +{ + int pos = patha.ReverseFind('/'); + const char* base = (pos >= 0) ? ((const char*)patha + pos + 1) : patha; + + return IsPathIgnored(patha, base, type); +} +#endif + +int CGitIgnoreItem::IsPathIgnored(const CStringA& patha, const char* base, int& type) +{ + if (!m_pExcludeList) + return -1; // error or undecided + + return git_check_excluded_1(patha, patha.GetLength(), base, &type, m_pExcludeList); +} + bool CGitIgnoreList::CheckFileChanged(const CString &path) { __int64 time = 0; @@ -1149,15 +1167,10 @@ bool CGitIgnoreList::IsIgnore(const CString &path, const CString &projectroot, b } int CGitIgnoreList::CheckFileAgainstIgnoreList(const CString &ignorefile, const CStringA &patha, const char * base, int &type) { - if (m_Map.find(ignorefile) != m_Map.end()) - { - int ret = -1; - if(m_Map[ignorefile].m_pExcludeList) - ret = git_check_excluded_1(patha, patha.GetLength(), base, &type, m_Map[ignorefile].m_pExcludeList); - if (ret == 0 || ret == 1) - return ret; - } - return -1; + if (m_Map.find(ignorefile) == m_Map.end()) + return -1; // error or undecided + + return (m_Map[ignorefile].IsPathIgnored(patha, base, type)); } int CGitIgnoreList::CheckIgnore(const CString &path, const CString &projectroot, bool isDir) { diff --git a/src/Git/gitindex.h b/src/Git/gitindex.h index d343e43d2..11f40b8f5 100644 --- a/src/Git/gitindex.h +++ b/src/Git/gitindex.h @@ -270,6 +270,16 @@ public: EXCLUDE_LIST m_pExcludeList; int FetchIgnoreList(const CString &projectroot, const CString &file, bool isGlobal); + + /** + * patha: the filename to be checked whether is is ignored or not + * base: must be a pointer to the beginning of the base filename WITHIN patha + * type: DT_DIR or DT_REG + */ + int IsPathIgnored(const CStringA& patha, const char* base, int& type); +#ifdef GTEST_INCLUDE_GTEST_GTEST_H_ + int IsPathIgnored(const CStringA& patha, int& type); +#endif }; class CGitIgnoreList diff --git a/test/UnitTests/GitIndexTest.cpp b/test/UnitTests/GitIndexTest.cpp index 0f31eb842..b8b6dde22 100644 --- a/test/UnitTests/GitIndexTest.cpp +++ b/test/UnitTests/GitIndexTest.cpp @@ -20,6 +20,7 @@ #include "stdafx.h" #include "RepositoryFixtures.h" #include "gitindex.h" +#include "gitdll.h" extern CGitAdminDirMap g_AdminDirMap; // not optimal yet @@ -339,3 +340,259 @@ TEST(GitIndex, GetRangeInSortVector) EXPECT_EQ(6, start); EXPECT_EQ(6, end); } + +TEST(GitIndex, CGitIgnoreItem) +{ + CAutoTempDir tempDir; + CGitIgnoreItem ignoreItem; + + int type = DT_DIR; + EXPECT_EQ(-1, ignoreItem.IsPathIgnored("not-ignored", type)); + EXPECT_EQ(-1, ignoreItem.IsPathIgnored("subdir/not-ignored", type)); + type = DT_REG; + EXPECT_EQ(-1, ignoreItem.IsPathIgnored("not-ignored", type)); + EXPECT_EQ(-1, ignoreItem.IsPathIgnored("subdir/not-ignored", type)); + + EXPECT_EQ(-1, ignoreItem.FetchIgnoreList(tempDir.GetTempDir(), L"does-not-exist", false)); + EXPECT_EQ(-1, ignoreItem.FetchIgnoreList(tempDir.GetTempDir(), L"does-not-exist", true)); + + CString ignoreFile = tempDir.GetTempDir() + L"\\.gitignore"; + + EXPECT_TRUE(CStringUtils::WriteStringToTextFile((LPCTSTR)ignoreFile, L"")); + EXPECT_EQ(0, ignoreItem.FetchIgnoreList(tempDir.GetTempDir(), ignoreFile, false)); + EXPECT_STREQ("", ignoreItem.m_BaseDir); + EXPECT_EQ(0, ignoreItem.FetchIgnoreList(tempDir.GetTempDir(), ignoreFile, true)); + EXPECT_STREQ("", ignoreItem.m_BaseDir); + + EXPECT_TRUE(CStringUtils::WriteStringToTextFile((LPCTSTR)ignoreFile, L"#")); + EXPECT_EQ(0, ignoreItem.FetchIgnoreList(tempDir.GetTempDir(), ignoreFile, false)); + EXPECT_EQ(0, ignoreItem.FetchIgnoreList(tempDir.GetTempDir(), ignoreFile, true)); + + EXPECT_TRUE(CStringUtils::WriteStringToTextFile((LPCTSTR)ignoreFile, L"# comment")); + EXPECT_EQ(0, ignoreItem.FetchIgnoreList(tempDir.GetTempDir(), ignoreFile, false)); + EXPECT_EQ(0, ignoreItem.FetchIgnoreList(tempDir.GetTempDir(), ignoreFile, true)); + + EXPECT_TRUE(CStringUtils::WriteStringToTextFile((LPCTSTR)ignoreFile, L"\n")); + EXPECT_EQ(0, ignoreItem.FetchIgnoreList(tempDir.GetTempDir(), ignoreFile, false)); + EXPECT_EQ(0, ignoreItem.FetchIgnoreList(tempDir.GetTempDir(), ignoreFile, true)); + + EXPECT_TRUE(CStringUtils::WriteStringToTextFile((LPCTSTR)ignoreFile, L"\n#")); + EXPECT_EQ(0, ignoreItem.FetchIgnoreList(tempDir.GetTempDir(), ignoreFile, false)); + EXPECT_EQ(0, ignoreItem.FetchIgnoreList(tempDir.GetTempDir(), ignoreFile, true)); + + EXPECT_TRUE(CStringUtils::WriteStringToTextFile((LPCTSTR)ignoreFile, L"*.tmp\n")); + EXPECT_EQ(0, ignoreItem.FetchIgnoreList(tempDir.GetTempDir(), ignoreFile, false)); + type = DT_DIR; + EXPECT_EQ(-1, ignoreItem.IsPathIgnored("not-ignored", type)); + EXPECT_EQ(1, ignoreItem.IsPathIgnored("text.tmp", type)); + EXPECT_EQ(1, ignoreItem.IsPathIgnored("subdir/text.tmp", type)); + EXPECT_EQ(-1, ignoreItem.IsPathIgnored("1.tmp.1", type)); + type = DT_REG; + EXPECT_EQ(-1, ignoreItem.IsPathIgnored("not-ignored", type)); + EXPECT_EQ(1, ignoreItem.IsPathIgnored("text.tmp", type)); + EXPECT_EQ(1, ignoreItem.IsPathIgnored("subdir/text.tmp", type)); + EXPECT_EQ(-1, ignoreItem.IsPathIgnored("1.tmp.1", type)); + EXPECT_EQ(0, ignoreItem.FetchIgnoreList(tempDir.GetTempDir(), ignoreFile, true)); + type = DT_DIR; + EXPECT_EQ(-1, ignoreItem.IsPathIgnored("not-ignored", type)); + EXPECT_EQ(1, ignoreItem.IsPathIgnored("text.tmp", type)); + EXPECT_EQ(1, ignoreItem.IsPathIgnored("subdir/text.tmp", type)); + EXPECT_EQ(-1, ignoreItem.IsPathIgnored("1.tmp.1", type)); + type = DT_REG; + EXPECT_EQ(-1, ignoreItem.IsPathIgnored("not-ignored", type)); + EXPECT_EQ(1, ignoreItem.IsPathIgnored("text.tmp", type)); + EXPECT_EQ(1, ignoreItem.IsPathIgnored("subdir/text.tmp", type)); + EXPECT_EQ(-1, ignoreItem.IsPathIgnored("1.tmp.1", type)); + + EXPECT_TRUE(CStringUtils::WriteStringToTextFile((LPCTSTR)ignoreFile, L"some-file\n")); + EXPECT_EQ(0, ignoreItem.FetchIgnoreList(tempDir.GetTempDir(), ignoreFile, false)); + type = DT_DIR; + EXPECT_EQ(-1, ignoreItem.IsPathIgnored("not-ignored", type)); + EXPECT_EQ(-1, ignoreItem.IsPathIgnored("subdir/not-ignored", type)); + EXPECT_EQ(1, ignoreItem.IsPathIgnored("some-file", type)); + EXPECT_EQ(1, ignoreItem.IsPathIgnored("subdir/some-file", type)); + type = DT_REG; + EXPECT_EQ(-1, ignoreItem.IsPathIgnored("not-ignored", type)); + EXPECT_EQ(-1, ignoreItem.IsPathIgnored("subdir/not-ignored", type)); + EXPECT_EQ(1, ignoreItem.IsPathIgnored("some-file", type)); + EXPECT_EQ(1, ignoreItem.IsPathIgnored("subdir/some-file", type)); + EXPECT_EQ(0, ignoreItem.FetchIgnoreList(tempDir.GetTempDir(), ignoreFile, true)); + type = DT_DIR; + EXPECT_EQ(-1, ignoreItem.IsPathIgnored("not-ignored", type)); + EXPECT_EQ(-1, ignoreItem.IsPathIgnored("subdir/not-ignored", type)); + EXPECT_EQ(1, ignoreItem.IsPathIgnored("some-file", type)); + EXPECT_EQ(1, ignoreItem.IsPathIgnored("subdir/some-file", type)); + type = DT_REG; + EXPECT_EQ(-1, ignoreItem.IsPathIgnored("not-ignored", type)); + EXPECT_EQ(-1, ignoreItem.IsPathIgnored("subdir/not-ignored", type)); + EXPECT_EQ(1, ignoreItem.IsPathIgnored("some-file", type)); + EXPECT_EQ(1, ignoreItem.IsPathIgnored("subdir/some-file", type)); + + EXPECT_TRUE(CStringUtils::WriteStringToTextFile((LPCTSTR)ignoreFile, L"\n\nsome-file\n")); + EXPECT_EQ(0, ignoreItem.FetchIgnoreList(tempDir.GetTempDir(), ignoreFile, false)); + type = DT_DIR; + EXPECT_EQ(1, ignoreItem.IsPathIgnored("some-file", type)); + EXPECT_EQ(1, ignoreItem.IsPathIgnored("subdir/some-file", type)); + type = DT_REG; + EXPECT_EQ(1, ignoreItem.IsPathIgnored("some-file", type)); + EXPECT_EQ(1, ignoreItem.IsPathIgnored("subdir/some-file", type)); + EXPECT_EQ(0, ignoreItem.FetchIgnoreList(tempDir.GetTempDir(), ignoreFile, true)); + type = DT_DIR; + EXPECT_EQ(1, ignoreItem.IsPathIgnored("some-file", type)); + EXPECT_EQ(1, ignoreItem.IsPathIgnored("subdir/some-file", type)); + type = DT_REG; + EXPECT_EQ(1, ignoreItem.IsPathIgnored("some-file", type)); + EXPECT_EQ(1, ignoreItem.IsPathIgnored("subdir/some-file", type)); + + EXPECT_TRUE(CStringUtils::WriteStringToTextFile((LPCTSTR)ignoreFile, L"/some-file")); + EXPECT_EQ(0, ignoreItem.FetchIgnoreList(tempDir.GetTempDir(), ignoreFile, false)); + type = DT_DIR; + EXPECT_EQ(1, ignoreItem.IsPathIgnored("some-file", type)); + EXPECT_EQ(-1, ignoreItem.IsPathIgnored("subdir/some-file", type)); + type = DT_REG; + EXPECT_EQ(1, ignoreItem.IsPathIgnored("some-file", type)); + EXPECT_EQ(-1, ignoreItem.IsPathIgnored("subdir/some-file", type)); + EXPECT_EQ(0, ignoreItem.FetchIgnoreList(tempDir.GetTempDir(), ignoreFile, true)); + type = DT_DIR; + EXPECT_EQ(1, ignoreItem.IsPathIgnored("some-file", type)); + EXPECT_EQ(-1, ignoreItem.IsPathIgnored("subdir/some-file", type)); + type = DT_REG; + EXPECT_EQ(1, ignoreItem.IsPathIgnored("some-file", type)); + EXPECT_EQ(-1, ignoreItem.IsPathIgnored("subdir/some-file", type)); + + EXPECT_TRUE(CStringUtils::WriteStringToTextFile((LPCTSTR)ignoreFile, L"some-dir/")); + EXPECT_EQ(0, ignoreItem.FetchIgnoreList(tempDir.GetTempDir(), ignoreFile, false)); + type = DT_DIR; + EXPECT_EQ(1, ignoreItem.IsPathIgnored("some-dir", type)); + EXPECT_EQ(-1, ignoreItem.IsPathIgnored("some-dir/some-file", type)); + type = DT_REG; + EXPECT_EQ(-1, ignoreItem.IsPathIgnored("some-file", type)); + EXPECT_EQ(-1, ignoreItem.IsPathIgnored("some-dir", type)); + EXPECT_EQ(-1, ignoreItem.IsPathIgnored("some-dir/some-file", type)); + EXPECT_EQ(0, ignoreItem.FetchIgnoreList(tempDir.GetTempDir(), ignoreFile, true)); + type = DT_DIR; + EXPECT_EQ(1, ignoreItem.IsPathIgnored("some-dir", type)); + EXPECT_EQ(-1, ignoreItem.IsPathIgnored("some-dir/some-file", type)); + type = DT_REG; + EXPECT_EQ(-1, ignoreItem.IsPathIgnored("some-file", type)); + EXPECT_EQ(-1, ignoreItem.IsPathIgnored("some-dir", type)); + EXPECT_EQ(-1, ignoreItem.IsPathIgnored("some-dir/some-file", type)); + + EXPECT_TRUE(CStringUtils::WriteStringToTextFile((LPCTSTR)ignoreFile, L"some-*\n!some-file")); + EXPECT_EQ(0, ignoreItem.FetchIgnoreList(tempDir.GetTempDir(), ignoreFile, false)); + type = DT_DIR; + EXPECT_EQ(1, ignoreItem.IsPathIgnored("some-dir", type)); + EXPECT_EQ(0, ignoreItem.IsPathIgnored("some-file", type)); + EXPECT_EQ(-1, ignoreItem.IsPathIgnored("something", type)); + type = DT_REG; + EXPECT_EQ(1, ignoreItem.IsPathIgnored("some-dir", type)); + EXPECT_EQ(0, ignoreItem.IsPathIgnored("some-file", type)); + EXPECT_EQ(-1, ignoreItem.IsPathIgnored("something", type)); + EXPECT_EQ(0, ignoreItem.FetchIgnoreList(tempDir.GetTempDir(), ignoreFile, true)); + type = DT_DIR; + EXPECT_EQ(1, ignoreItem.IsPathIgnored("some-dir", type)); + EXPECT_EQ(0, ignoreItem.IsPathIgnored("some-file", type)); + EXPECT_EQ(-1, ignoreItem.IsPathIgnored("something", type)); + type = DT_REG; + EXPECT_EQ(1, ignoreItem.IsPathIgnored("some-dir", type)); + EXPECT_EQ(0, ignoreItem.IsPathIgnored("some-file", type)); + EXPECT_EQ(-1, ignoreItem.IsPathIgnored("something", type)); + + EXPECT_TRUE(CStringUtils::WriteStringToTextFile((LPCTSTR)ignoreFile, L"some-file\nanother/dir/*")); + EXPECT_EQ(0, ignoreItem.FetchIgnoreList(tempDir.GetTempDir(), ignoreFile, false)); + type = DT_DIR; + EXPECT_EQ(-1, ignoreItem.IsPathIgnored("some-dir", type)); + EXPECT_EQ(1, ignoreItem.IsPathIgnored("some-file", type)); + EXPECT_EQ(-1, ignoreItem.IsPathIgnored("another", type)); + EXPECT_EQ(-1, ignoreItem.IsPathIgnored("another/dir", type)); + EXPECT_EQ(1, ignoreItem.IsPathIgnored("another/dir/some", type)); + type = DT_REG; + EXPECT_EQ(-1, ignoreItem.IsPathIgnored("some-dir", type)); + EXPECT_EQ(1, ignoreItem.IsPathIgnored("some-file", type)); + EXPECT_EQ(-1, ignoreItem.IsPathIgnored("another", type)); + EXPECT_EQ(-1, ignoreItem.IsPathIgnored("another/dir", type)); + EXPECT_EQ(1, ignoreItem.IsPathIgnored("another/dir/some", type)); + EXPECT_EQ(0, ignoreItem.FetchIgnoreList(tempDir.GetTempDir(), ignoreFile, true)); + type = DT_DIR; + EXPECT_EQ(-1, ignoreItem.IsPathIgnored("some-dir", type)); + EXPECT_EQ(1, ignoreItem.IsPathIgnored("some-file", type)); + EXPECT_EQ(-1, ignoreItem.IsPathIgnored("another", type)); + EXPECT_EQ(-1, ignoreItem.IsPathIgnored("another/dir", type)); + EXPECT_EQ(1, ignoreItem.IsPathIgnored("another/dir/some", type)); + type = DT_REG; + EXPECT_EQ(-1, ignoreItem.IsPathIgnored("some-dir", type)); + EXPECT_EQ(1, ignoreItem.IsPathIgnored("some-file", type)); + EXPECT_EQ(-1, ignoreItem.IsPathIgnored("another", type)); + EXPECT_EQ(-1, ignoreItem.IsPathIgnored("another/dir", type)); + EXPECT_EQ(1, ignoreItem.IsPathIgnored("another/dir/some", type)); + + EXPECT_TRUE(::CreateDirectory(tempDir.GetTempDir() + L"\\subdir", nullptr)); + ignoreFile = tempDir.GetTempDir() + L"\\subdir\\.gitignore"; + EXPECT_TRUE(CStringUtils::WriteStringToTextFile((LPCTSTR)ignoreFile, L"/something")); + EXPECT_EQ(0, ignoreItem.FetchIgnoreList(tempDir.GetTempDir(), ignoreFile, false)); + EXPECT_STREQ("subdir/", ignoreItem.m_BaseDir); + type = DT_DIR; + EXPECT_EQ(-1, ignoreItem.IsPathIgnored("something", type)); + EXPECT_EQ(-1, ignoreItem.IsPathIgnored("something/more", type)); + EXPECT_EQ(-1, ignoreItem.IsPathIgnored("some-dir/something", type)); + EXPECT_EQ(1, ignoreItem.IsPathIgnored("subdir/something", type)); + EXPECT_EQ(-1, ignoreItem.IsPathIgnored("subdir/something/more", type)); + EXPECT_EQ(-1, ignoreItem.IsPathIgnored("subdir/some-dir/something", type)); + type = DT_REG; + EXPECT_EQ(-1, ignoreItem.IsPathIgnored("something", type)); + EXPECT_EQ(-1, ignoreItem.IsPathIgnored("something/more", type)); + EXPECT_EQ(-1, ignoreItem.IsPathIgnored("some-dir/something", type)); + EXPECT_EQ(1, ignoreItem.IsPathIgnored("subdir/something", type)); + EXPECT_EQ(-1, ignoreItem.IsPathIgnored("subdir/something/more", type)); + EXPECT_EQ(-1, ignoreItem.IsPathIgnored("subdir/some-dir/something", type)); + EXPECT_EQ(0, ignoreItem.FetchIgnoreList(tempDir.GetTempDir(), ignoreFile, true)); + EXPECT_STREQ("", ignoreItem.m_BaseDir); + type = DT_DIR; + EXPECT_EQ(1, ignoreItem.IsPathIgnored("something", type)); + EXPECT_EQ(-1, ignoreItem.IsPathIgnored("something/more", type)); + EXPECT_EQ(-1, ignoreItem.IsPathIgnored("some-dir/something", type)); + EXPECT_EQ(-1, ignoreItem.IsPathIgnored("subdir/something", type)); + EXPECT_EQ(-1, ignoreItem.IsPathIgnored("subdir/something/more", type)); + EXPECT_EQ(-1, ignoreItem.IsPathIgnored("subdir/some-dir/something", type)); + type = DT_REG; + EXPECT_EQ(1, ignoreItem.IsPathIgnored("something", type)); + EXPECT_EQ(-1, ignoreItem.IsPathIgnored("something/more", type)); + EXPECT_EQ(-1, ignoreItem.IsPathIgnored("some-dir/something", type)); + EXPECT_EQ(-1, ignoreItem.IsPathIgnored("subdir/something", type)); + EXPECT_EQ(-1, ignoreItem.IsPathIgnored("subdir/something/more", type)); + EXPECT_EQ(-1, ignoreItem.IsPathIgnored("subdir/some-dir/something", type)); + + EXPECT_TRUE(CStringUtils::WriteStringToTextFile((LPCTSTR)ignoreFile, L"something")); + EXPECT_EQ(0, ignoreItem.FetchIgnoreList(tempDir.GetTempDir(), ignoreFile, false)); + EXPECT_STREQ("subdir/", ignoreItem.m_BaseDir); + type = DT_DIR; + EXPECT_EQ(1, ignoreItem.IsPathIgnored("something", type)); + EXPECT_EQ(-1, ignoreItem.IsPathIgnored("something/more", type)); + EXPECT_EQ(1, ignoreItem.IsPathIgnored("some-dir/something", type)); + EXPECT_EQ(1, ignoreItem.IsPathIgnored("subdir/something", type)); + EXPECT_EQ(-1, ignoreItem.IsPathIgnored("subdir/something/more", type)); + EXPECT_EQ(1, ignoreItem.IsPathIgnored("subdir/some-dir/something", type)); + type = DT_REG; + EXPECT_EQ(1, ignoreItem.IsPathIgnored("something", type)); + EXPECT_EQ(-1, ignoreItem.IsPathIgnored("something/more", type)); + EXPECT_EQ(1, ignoreItem.IsPathIgnored("some-dir/something", type)); + EXPECT_EQ(1, ignoreItem.IsPathIgnored("subdir/something", type)); + EXPECT_EQ(-1, ignoreItem.IsPathIgnored("subdir/something/more", type)); + EXPECT_EQ(1, ignoreItem.IsPathIgnored("subdir/some-dir/something", type)); + EXPECT_EQ(0, ignoreItem.FetchIgnoreList(tempDir.GetTempDir(), ignoreFile, true)); + EXPECT_STREQ("", ignoreItem.m_BaseDir); + type = DT_DIR; + EXPECT_EQ(1, ignoreItem.IsPathIgnored("something", type)); + EXPECT_EQ(-1, ignoreItem.IsPathIgnored("something/more", type)); + EXPECT_EQ(1, ignoreItem.IsPathIgnored("some-dir/something", type)); + EXPECT_EQ(1, ignoreItem.IsPathIgnored("subdir/something", type)); + EXPECT_EQ(-1, ignoreItem.IsPathIgnored("subdir/something/more", type)); + EXPECT_EQ(1, ignoreItem.IsPathIgnored("subdir/some-dir/something", type)); + type = DT_REG; + EXPECT_EQ(1, ignoreItem.IsPathIgnored("something", type)); + EXPECT_EQ(-1, ignoreItem.IsPathIgnored("something/more", type)); + EXPECT_EQ(1, ignoreItem.IsPathIgnored("some-dir/something", type)); + EXPECT_EQ(1, ignoreItem.IsPathIgnored("subdir/something", type)); + EXPECT_EQ(-1, ignoreItem.IsPathIgnored("subdir/something/more", type)); + EXPECT_EQ(1, ignoreItem.IsPathIgnored("subdir/some-dir/something", type)); +} -- 2.11.4.GIT