Skip to content

Commit bf177a4

Browse files
committed
fix tests
1 parent 513bda7 commit bf177a4

File tree

11 files changed

+277
-63
lines changed

11 files changed

+277
-63
lines changed

.gitignore

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,16 @@
3131
*.out
3232
*.app
3333
app/gyt
34-
tests/tests
3534
build/**/*
3635

36+
# Tests
37+
tests/tests
38+
tests/_deps
39+
tests/.ninja*
40+
tests/app/gyt
41+
tests/Testing
42+
tests/build.ninja
43+
3744
# CMake files
3845
*.cmake
3946
**/CMakeFiles/

doc/commands/branches.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# Branches
2+
3+
A branch:
4+
- is a ref under ref/heads
5+
6+
# commands that creates branches
7+
8+
`git branch -c`
9+
`git checkout -b`
10+
`git switch -c`

src/object.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ std::string GitObject::find(GitRepository &repo, const std::string &name,
9797
bool follow) {
9898
fs::path ref_path;
9999
std::string ref_name = boost::algorithm::to_lower_copy(name);
100-
if (name == "head") {
100+
if (ref_name == "head") {
101101
ref_path = repo.repo_path("HEAD");
102102
} else if (fs::exists(repo.repo_path("refs/heads/" + ref_name))) {
103103
ref_path = repo.repo_path("refs/heads/" + ref_name);
@@ -106,7 +106,7 @@ std::string GitObject::find(GitRepository &repo, const std::string &name,
106106
} else if (fs::exists(repo.repo_path("refs/remotes/" + ref_name))) {
107107
ref_path = repo.repo_path("refs/remotes/" + ref_name);
108108
} else if (fs::exists(repo.repo_path(get_commit_path(ref_name)))) {
109-
ref_path = repo.repo_path(get_commit_path(ref_name));
109+
return ref_name;
110110
} else {
111111
throw std::runtime_error(name + ": not a valid reference");
112112
}

test.sh

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,19 +20,18 @@ fi
2020
echo "Selected build type: $BUILD_TYPE"
2121
echo "Building the project... This will take a while to install dependencies for the first time."
2222

23-
mv ../tests/gitrepo/.notgit ../tests/gitrepo/.git
23+
rm -rf ../tests/gitrepo/.git
24+
cp -R ../tests/gitrepo/.notgit ../tests/gitrepo/.git
2425

2526
#Run CMake with the selected build type
26-
cmake .. -DCMAKE_BUILD_TYPE=$BUILD_TYPE -DCMAKE_EXPORT_COMPILE_COMMANDS=1 -G Ninja
27+
cmake .. -DCMAKE_BUILD_TYPE=$BUILD_TYPE -DCMAKE_EXPORT_COMPILE_COMMANDS=1 -DBUILD_TESTS=ON -G Ninja
2728

2829
#Build the project
2930
ninja
3031

3132
# test
3233
ctest --output-on-failure
3334

34-
mv ../tests/gitrepo/.git ../tests/gitrepo/.notgit
35-
3635
#symlink - so I can run it like gyt[arguments....]
3736
sudo rm /usr/local/bin/gyt
3837
sudo ln -s "$(pwd)/app/gyt" /usr/local/bin/gyt

tests/CMakeLists.txt

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
1-
add_executable(tests tests.cpp repository_t.cpp tag_t.cpp)
1+
add_executable(tests tests.cpp repository_t.cpp tag_t.cpp object_t.cpp utils/gitreposetup.cpp)
22
target_include_directories(tests PUBLIC ../ext)
33

44
target_link_libraries(tests PUBLIC boost_libraries repository commands)
55
file(COPY ${CMAKE_SOURCE_DIR}/tests/gitrepo
6-
DESTINATION ${CMAKE_BINARY_DIR}/tests
7-
FILES_MATCHING PATTERN ".*" PATTERN "*" EXCLUDE)
6+
DESTINATION ${CMAKE_BINARY_DIR}/tests)
87

98
# allow user to run tests with `make test` or `ctest`
109
include(../cmake/Catch.cmake)

tests/compile_commands.json

Lines changed: 194 additions & 0 deletions
Large diffs are not rendered by default.

tests/gitreposetup.cpp

Lines changed: 0 additions & 22 deletions
This file was deleted.

tests/object_t.cpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,17 @@
11
#include "catch2/catch.hpp"
22
#include "object.h"
33
#include "repository.h"
4+
#include "utils/gitreposetup.h"
45

56
TEST_CASE("GitObject", "[GitObject::find]") {
67
GitRepoSetup gitRepoSetup;
78
SECTION("find: resolves a commit") {
89
// Provide a path to a valid git repository
910
GitRepository repo(".");
1011

11-
REQUIRE(GitObject::find(repo, "HEAD") == FIRST_COMMIT);
12+
REQUIRE(GitObject::find(repo, "HEAD") == SECOND_COMMIT);
1213
REQUIRE(GitObject::find(repo, SECOND_COMMIT) == SECOND_COMMIT);
1314
REQUIRE(GitObject::find(repo, "tagv1.0") == SECOND_COMMIT);
14-
REQUIRE(GitObject::find(repo, "refs/heads/test_branch") == THIRD_COMMIT);
15+
REQUIRE(GitObject::find(repo, "test_branch") == THIRD_COMMIT);
1516
}
1617
}
17-
18-
TEST_CASE("GitRepository Create", "[GitRepository]") {}

tests/tag_t.cpp

Lines changed: 1 addition & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#include "catch2/catch.hpp"
33
#include "commands/tag.h"
44
#include "repository.h"
5+
#include "utils/gitreposetup.h"
56
#include <filesystem>
67
#include <fstream>
78

@@ -20,33 +21,6 @@ std::string file_contents(const fs::path &path) {
2021
throw std::runtime_error("Could not open file: " + path.string());
2122
}
2223
}
23-
const std::string FIRST_COMMIT = "1723ac93b92db1fc2c28de8e5da814136937f8c6";
24-
const std::string SECOND_COMMIT = "6c2c22e7b5b7b1682e3c14668499e84141aca0d4";
25-
const std::string THIRD_COMMIT = "ba570884af934a79081499203ba81750a945e3c5";
26-
const fs::path VALID_GIT_PATH = fs::temp_directory_path() / "gitrepo";
27-
const fs::path OLD_CWD = fs::current_path();
28-
const fs::path TEST_FILE_PATH = OLD_CWD / "gitrepo";
29-
30-
/**
31-
Uses RAII to manage the setup and teardown of a sample Git repo
32-
*/
33-
class GitRepoSetup {
34-
public:
35-
GitRepoSetup() { setup(); }
36-
~GitRepoSetup() { teardown(); }
37-
38-
void setup() {
39-
// set up a git repo with an actual commit
40-
fs::copy(TEST_FILE_PATH, VALID_GIT_PATH, fs::copy_options::recursive);
41-
42-
fs::current_path(VALID_GIT_PATH);
43-
}
44-
45-
void teardown() {
46-
fs::remove_all(VALID_GIT_PATH);
47-
fs::current_path(OLD_CWD);
48-
}
49-
};
5024

5125
TEST_CASE("tag command", "[tag]") {
5226
GitRepoSetup gitRepoSetup;

tests/utils/gitreposetup.cpp

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#include "gitreposetup.h"
2+
#include <filesystem>
3+
/**
4+
Uses RAII to manage the setup and teardown of a sample Git repo
5+
*/
6+
namespace fs = std::filesystem;
7+
8+
const std::string FIRST_COMMIT = "1723ac93b92db1fc2c28de8e5da814136937f8c6";
9+
const std::string SECOND_COMMIT = "6c2c22e7b5b7b1682e3c14668499e84141aca0d4";
10+
const std::string THIRD_COMMIT = "ba570884af934a79081499203ba81750a945e3c5";
11+
const fs::path VALID_GIT_PATH = fs::temp_directory_path() / "gitrepo";
12+
const fs::path OLD_CWD = fs::current_path();
13+
const fs::path TEST_FILE_PATH = OLD_CWD / "gitrepo";
14+
15+
GitRepoSetup::GitRepoSetup() { setup(); }
16+
GitRepoSetup::~GitRepoSetup() { teardown(); }
17+
18+
void GitRepoSetup::setup() {
19+
// set up a git repo with an actual commit
20+
fs::copy(TEST_FILE_PATH, VALID_GIT_PATH, fs::copy_options::recursive);
21+
22+
fs::current_path(VALID_GIT_PATH);
23+
}
24+
25+
void GitRepoSetup::teardown() {
26+
fs::remove_all(VALID_GIT_PATH);
27+
fs::current_path(OLD_CWD);
28+
}

tests/utils/gitreposetup.h

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#ifndef GITREPOSETUP_H
2+
#define GITREPOSETUP_H
3+
4+
#include <filesystem>
5+
6+
namespace fs = std::filesystem;
7+
/**
8+
Uses RAII to manage the setup and teardown of a sample Git repo
9+
*/
10+
extern const std::string FIRST_COMMIT;
11+
extern const std::string SECOND_COMMIT;
12+
extern const std::string THIRD_COMMIT;
13+
extern const fs::path VALID_GIT_PATH;
14+
extern const fs::path OLD_CWD;
15+
extern const fs::path TEST_FILE_PATH;
16+
17+
class GitRepoSetup {
18+
public:
19+
GitRepoSetup();
20+
~GitRepoSetup();
21+
22+
void setup();
23+
void teardown();
24+
};
25+
26+
#endif // GITREPOSETUP_H

0 commit comments

Comments
 (0)