diff --git a/CMakeLists.txt b/CMakeLists.txt index 4611ac86..d34478cc 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -158,6 +158,7 @@ set (LOOT_TESTS_SRC "${CMAKE_SOURCE_DIR}/src/tests/main.cpp") set (LOOT_TESTS_HEADERS "${CMAKE_SOURCE_DIR}/src/tests/fixtures.h" "${CMAKE_SOURCE_DIR}/src/tests/api/test_api.h" "${CMAKE_SOURCE_DIR}/src/tests/backend/game/test_game_settings.h" + "${CMAKE_SOURCE_DIR}/src/tests/backend/helpers/test_git_helper.h" "${CMAKE_SOURCE_DIR}/src/tests/backend/helpers/test_helpers.h" "${CMAKE_SOURCE_DIR}/src/tests/backend/helpers/test_language.h" "${CMAKE_SOURCE_DIR}/src/tests/backend/helpers/test_yaml_set_helpers.h" diff --git a/src/tests/backend/helpers/test_git_helper.h b/src/tests/backend/helpers/test_git_helper.h new file mode 100644 index 00000000..e1ee2c91 --- /dev/null +++ b/src/tests/backend/helpers/test_git_helper.h @@ -0,0 +1,135 @@ +/* LOOT + +A load order optimisation tool for Oblivion, Skyrim, Fallout 3 and +Fallout: New Vegas. + +Copyright (C) 2014-2015 WrinklyNinja + +This file is part of LOOT. + +LOOT is free software: you can redistribute +it and/or modify it under the terms of the GNU General Public License +as published by the Free Software Foundation, either version 3 of +the License, or (at your option) any later version. + +LOOT is distributed in the hope that it will +be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with LOOT. If not, see +. +*/ + +#ifndef LOOT_TEST_BACKEND_HELPERS_LANGUAGE +#define LOOT_TEST_BACKEND_HELPERS_LANGUAGE + +#include "backend/error.h" +#include "backend/helpers/git_helper.h" +#include "tests/fixtures.h" + +using loot::GitHelper; + +#ifdef _WIN32 +boost::filesystem::path parentRepoRoot = boost::filesystem::current_path().parent_path().parent_path(); +#else +boost::filesystem::path parentRepoRoot = boost::filesystem::current_path().parent_path(); +#endif + +TEST(GitHelper, ConstructorAndDestructor) { + GitHelper * git = new GitHelper(); + EXPECT_EQ(nullptr, git->repo); + + EXPECT_EQ(2, git_libgit2_init()); + delete git; + EXPECT_EQ(0, git_libgit2_shutdown()); +} + +TEST(GitHelper, Call) { + GitHelper git; + EXPECT_NO_THROW(git.Call(0)); + EXPECT_THROW(git.Call(1), loot::error); + EXPECT_THROW(git.Call(-1), loot::error); +} + +TEST(GitHelper, SetErrorMessage) { + GitHelper git; + git.SetErrorMessage("test message"); + + try { + git.Call(1); + ADD_FAILURE() << "An exception should have been thrown."; + } + catch (loot::error& e) { + EXPECT_NE(nullptr, strstr(e.what(), "test message")); + } +} + +TEST(GitHelper, Free) { + // Initialise buffer member, it's simplest to test with. + GitHelper git; + git_buf_set(&git.buf, "foo", 4); + EXPECT_NE(nullptr, git.buf.ptr); + EXPECT_EQ(4, git.buf.size); + + git.Free(); + EXPECT_EQ(nullptr, git.buf.ptr); + EXPECT_EQ(0, git.buf.size); +} + +TEST(GitHelper, IsRepository) { + GitHelper git; + + ASSERT_TRUE(boost::filesystem::exists(parentRepoRoot)); + EXPECT_TRUE(git.IsRepository(parentRepoRoot)); + EXPECT_FALSE(git.IsRepository(boost::filesystem::current_path())); +} + +class IsFileDifferent : public ::testing::Test { + inline virtual void SetUp() { + GitHelper git; + ASSERT_TRUE(git.IsRepository(parentRepoRoot)); + ASSERT_FALSE(git.IsRepository(boost::filesystem::current_path())); + + ASSERT_TRUE(boost::filesystem::exists(parentRepoRoot / "README.md")); + + // Create a backup of CONTRIBUTING.md. + ASSERT_TRUE(boost::filesystem::exists(parentRepoRoot / "CONTRIBUTING.md")); + ASSERT_FALSE(boost::filesystem::exists(parentRepoRoot / "CONTRIBUTING.md.copy")); + ASSERT_NO_THROW(boost::filesystem::copy(parentRepoRoot / "CONTRIBUTING.md", parentRepoRoot / "CONTRIBUTING.md.copy")); + ASSERT_TRUE(boost::filesystem::exists(parentRepoRoot / "CONTRIBUTING.md.copy")); + + // Edit CONTRIBUTING.md + loot::ofstream out(parentRepoRoot / "CONTRIBUTING.md"); + out.close(); + } + + inline virtual void TearDown() { + // Restore original CONTRIBUTING.md + ASSERT_NO_THROW(boost::filesystem::remove(parentRepoRoot / "CONTRIBUTING.md")); + ASSERT_NO_THROW(boost::filesystem::rename(parentRepoRoot / "CONTRIBUTING.md.copy", parentRepoRoot / "CONTRIBUTING.md")); + ASSERT_TRUE(boost::filesystem::exists(parentRepoRoot / "CONTRIBUTING.md")); + ASSERT_FALSE(boost::filesystem::exists(parentRepoRoot / "CONTRIBUTING.md.copy")); + } +}; + +TEST_F(IsFileDifferent, InvalidRepository) { + EXPECT_THROW(loot::IsFileDifferent(boost::filesystem::current_path(), "README.md"), loot::error); +} + +TEST_F(IsFileDifferent, SameFile) { + EXPECT_FALSE(loot::IsFileDifferent(parentRepoRoot, "README.md")); +} + +TEST_F(IsFileDifferent, NewFile) { + // New files not in the index are not tracked by Git, so aren't considered + // different. + EXPECT_FALSE(loot::IsFileDifferent(parentRepoRoot, "CONTRIBUTING.md.copy")); +} + +TEST_F(IsFileDifferent, DifferentFile) { + EXPECT_TRUE(loot::IsFileDifferent(parentRepoRoot, "CONTRIBUTING.md")); +} + +#endif diff --git a/src/tests/main.cpp b/src/tests/main.cpp index 0aefa8dd..731f6556 100644 --- a/src/tests/main.cpp +++ b/src/tests/main.cpp @@ -30,6 +30,7 @@ #include "backend/metadata/test_message_content.h" #include "backend/metadata/test_plugin_dirty_info.h" #include "backend/metadata/test_tag.h" +#include "backend/helpers/test_git_helper.h" #include "backend/helpers/test_helpers.h" #include "backend/helpers/test_language.h" #include "backend/helpers/test_yaml_set_helpers.h"