-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_cp.cpp
More file actions
70 lines (62 loc) · 2.3 KB
/
Copy pathtest_cp.cpp
File metadata and controls
70 lines (62 loc) · 2.3 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
#include <cfbox/applets.hpp>
#include <gtest/gtest.h>
#include "test_capture.hpp"
#include <cfbox/applet_config.hpp>
#if CFBOX_ENABLE_CP
using namespace cfbox::test;
TEST(CpTest, CopySingleFile) {
TempDir tmp;
auto src = tmp.write_file("src.txt", "hello world");
auto dst = (tmp.path / "dst.txt").string();
char a0[] = "cp", a1[256], a2[256];
std::snprintf(a1, sizeof(a1), "%s", src.c_str());
std::snprintf(a2, sizeof(a2), "%s", dst.c_str());
char* argv[] = {a0, a1, a2};
EXPECT_EQ(cp_main(3, argv), 0);
auto content = cfbox::io::read_all(dst);
ASSERT_TRUE(content.has_value());
EXPECT_EQ(content.value(), "hello world");
}
TEST(CpTest, CopyIntoDirectory) {
TempDir tmp;
auto src = tmp.write_file("file.txt", "data");
std::filesystem::create_directory(tmp.path / "dir");
auto dir = (tmp.path / "dir").string();
char a0[] = "cp", a1[256], a2[256];
std::snprintf(a1, sizeof(a1), "%s", src.c_str());
std::snprintf(a2, sizeof(a2), "%s", dir.c_str());
char* argv[] = {a0, a1, a2};
EXPECT_EQ(cp_main(3, argv), 0);
EXPECT_TRUE(std::filesystem::exists(tmp.path / "dir" / "file.txt"));
}
TEST(CpTest, CopyRecursive) {
TempDir tmp;
std::filesystem::create_directory(tmp.path / "srcdir");
tmp.write_file("srcdir/a.txt", "aaa");
tmp.write_file("srcdir/b.txt", "bbb");
auto srcdir = (tmp.path / "srcdir").string();
auto dstdir = (tmp.path / "dstdir").string();
char a0[] = "cp", a1[] = "-r", a2[256], a3[256];
std::snprintf(a2, sizeof(a2), "%s", srcdir.c_str());
std::snprintf(a3, sizeof(a3), "%s", dstdir.c_str());
char* argv[] = {a0, a1, a2, a3};
EXPECT_EQ(cp_main(4, argv), 0);
EXPECT_TRUE(std::filesystem::exists(tmp.path / "dstdir" / "a.txt"));
EXPECT_TRUE(std::filesystem::exists(tmp.path / "dstdir" / "b.txt"));
}
TEST(CpTest, MissingOperands) {
char a0[] = "cp";
char* argv[] = {a0};
EXPECT_NE(cp_main(1, argv), 0);
}
TEST(CpTest, SourceNotExist) {
TempDir tmp;
auto fake = (tmp.path / "nonexistent").string();
auto dst = (tmp.path / "dst").string();
char a0[] = "cp", a1[256], a2[256];
std::snprintf(a1, sizeof(a1), "%s", fake.c_str());
std::snprintf(a2, sizeof(a2), "%s", dst.c_str());
char* argv[] = {a0, a1, a2};
EXPECT_NE(cp_main(3, argv), 0);
}
#endif // CFBOX_ENABLE_CP