Repository navigation
Expand file tree
/
Copy pathtests.cpp
More file actions
86 lines (69 loc) · 2.75 KB
/
Copy pathtests.cpp
File metadata and controls
86 lines (69 loc) · 2.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#include "minigit/commit.hpp"
#include "minigit/stage.hpp"
#include <cassert>
#include <chrono>
#include <filesystem>
#include <fstream>
#include <iostream>
#include <string>
namespace fs = std::filesystem;
namespace {
class TemporaryDirectory {
public:
TemporaryDirectory() {
const auto value = std::chrono::high_resolution_clock::now().time_since_epoch().count();
path_ = fs::temp_directory_path() / ("minigit-test-" + std::to_string(value));
fs::create_directories(path_ / ".minigit" / "stage" / "data");
fs::create_directories(path_ / "data" / "notes");
}
~TemporaryDirectory() {
std::error_code ec;
fs::remove_all(path_, ec);
}
const fs::path& path() const { return path_; }
private:
fs::path path_;
};
void writeText(const fs::path& path, const std::string& text) {
fs::create_directories(path.parent_path());
std::ofstream out(path, std::ios::binary);
out << text;
}
std::string readText(const fs::path& path) {
std::ifstream in(path, std::ios::binary);
return {std::istreambuf_iterator<char>(in), std::istreambuf_iterator<char>()};
}
} // namespace
int main() {
const minigit::Commit commit{"0001", "0000", "main", "first"};
assert(minigit::serializeCommit(commit) == "0001|0000|main|first");
minigit::Commit parsed;
assert(minigit::parseCommit("0001|0000|main|first", parsed));
assert(parsed.id == "0001");
assert(parsed.parent == "0000");
assert(parsed.branch == "main");
assert(parsed.message == "first");
assert(!minigit::parseCommit("0001|0000|main", parsed));
assert(!minigit::parseCommit("0001|0000|main|first|extra", parsed));
assert(!minigit::parseCommit("0001|0000||first", parsed));
TemporaryDirectory temporary;
minigit::Stage stage(temporary.path());
std::string error;
writeText(temporary.path() / "data" / "notes" / "note.txt", "first");
assert(stage.add("data/notes/note.txt", error));
assert(!stage.empty());
assert(stage.files().size() == 1);
assert(stage.files()[0].generic_string() == "data/notes/note.txt");
assert(readText(temporary.path() / ".minigit" / "stage" / "data" / "notes" / "note.txt") == "first");
writeText(temporary.path() / "data" / "notes" / "note.txt", "second");
assert(stage.add("data/notes/note.txt", error));
assert(stage.files().size() == 1);
assert(readText(temporary.path() / ".minigit" / "stage" / "data" / "notes" / "note.txt") == "second");
assert(!stage.add("README.md", error));
assert(!stage.add("../outside.txt", error));
assert(!stage.add("data", error));
assert(stage.remove("data/notes/note.txt", error));
assert(!stage.remove("data/notes/note.txt", error));
assert(stage.empty());
std::cout << "lesson05 tests passed\n";
}