Repository navigation
Expand file tree
/
Copy pathcli_main_test.cpp
More file actions
304 lines (280 loc) · 10.7 KB
/
Copy pathcli_main_test.cpp
File metadata and controls
304 lines (280 loc) · 10.7 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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
/** Copyright 2026 KUMAZAKI Hiroki. Licensed under Apache-2.0. */
// End-to-end tests for the `tinylamb` CLI executable (main.cpp): a SQL script
// arrives on standard input, runs as one implicit transaction, and buffered
// rows print only after the durability barrier.
#ifndef _WIN32
#include <sys/wait.h>
#include <unistd.h>
#endif
#include <array>
#include <cstdlib>
#include <filesystem>
#include <fstream>
#include <string>
#include <string_view>
#include "common/platform/process.hpp"
#include "gtest/gtest.h"
namespace {
class CliMainTest : public ::testing::Test {
public:
void SetUp() override {
dir_ = std::filesystem::temp_directory_path() /
("cli_main_test-" + std::to_string(tinylamb::platform::GetPid())) /
::testing::UnitTest::GetInstance()->current_test_info()->name();
// T2805: a failed SetUp used to surface as a mysterious "CLI errored"
// three steps later; state the environment failure at its source.
std::error_code ec;
std::filesystem::create_directories(dir_, ec);
ASSERT_FALSE(ec) << "SetUp: create_directories(" << dir_
<< ") failed: " << ec.message();
}
void TearDown() override {
// T2805: leaked directories poison the next run's paths; report the
// removal failure (a destructor may not throw, so EXPECT not ASSERT).
std::error_code ec;
std::filesystem::remove_all(dir_, ec);
EXPECT_FALSE(ec) << "TearDown: remove_all(" << dir_
<< ") failed: " << ec.message();
}
protected:
// Runs the sibling `tinylamb` executable with `sql` on stdin. Returns the
// process exit status and fills stdout/stderr.
int RunCli(std::string_view sql, std::string* out, std::string* err) {
return RunCliRaw(sql, "'" + (dir_ / "db").string() + "'", dir_ / "stdout",
out, err);
}
// T2804: variant with the full argument string (flags, arity mistakes)
// and the stdout redirect target under test control, so argument
// handling, statement splitting, and write-failure paths get pinned
// instead of only the happy path.
int RunCliRaw(std::string_view sql, const std::string& args,
const std::filesystem::path& stdout_path, std::string* out,
std::string* err) {
const std::filesystem::path exe = BinaryPath("tinylamb");
if (exe.empty()) {
*err = "cannot locate the test executable to find sibling tinylamb";
return -1;
}
if (!std::filesystem::exists(exe)) {
*err = "tinylamb executable not found next to the test binary";
return -1;
}
const std::filesystem::path input = dir_ / "input.sql";
const std::filesystem::path stderr_path = dir_ / "stderr";
{
std::ofstream in(input, std::ios::binary);
EXPECT_TRUE(in.is_open()) << "cannot create " << input;
in << sql;
EXPECT_TRUE(in.good()) << "cannot write " << input;
}
const auto shell_quote = [](const std::string& s) {
std::string out = "'";
out.reserve((s.size() * 4) + 2);
for (char c : s) {
if (c == '\'') {
out += "'\\''";
} else {
out += c;
}
}
out += "'";
return out;
};
// This harness drives the CLI through a POSIX shell so that stdin and the
// two output streams are redirected exactly as in the installation. A
// Windows port needs an equivalent launcher; the WEXITSTATUS decode below
// is already guarded so the test target at least compiles there.
const std::string command =
"/bin/sh -c " +
shell_quote(exe.string() + " " + args + " < " + input.string() + " > " +
stdout_path.string() + " 2> " + stderr_path.string());
// NOLINTNEXTLINE(cert-env33-c,concurrency-mt-unsafe): synchronous child.
const int status = std::system(command.c_str());
// /dev/full READS back as an endless NUL stream (never EOF), so only
// capture stdout when it was redirected to a regular file in dir_.
if (stdout_path.parent_path() == dir_) {
ReadFile(stdout_path, out);
} else {
out->clear();
}
ReadFile(stderr_path, err);
#ifndef _WIN32
if (WIFEXITED(status)) {
return WEXITSTATUS(status);
}
return -1;
#else
// std::system() on Windows reports the child's exit code directly; there
// is no wait(2) status word to decode with WIFEXITED/WEXITSTATUS.
return status;
#endif
}
protected: // NOLINT(readability-redundant-access-specifiers)
std::filesystem::path dir_;
private:
static std::filesystem::path BinaryPath(std::string_view name) {
const std::filesystem::path self =
tinylamb::platform::CurrentExecutablePath();
if (self.empty()) {
return {};
}
#ifdef _WIN32
return self.parent_path() / (std::string{name} + ".exe");
#else
return self.parent_path() / name;
#endif
}
static void ReadFile(const std::filesystem::path& path, std::string* out) {
std::ifstream in(path, std::ios::binary);
// An unreadable artifact must be reported here; otherwise callers see an
// empty message and blame the CLI instead of the missing output file.
if (!in.is_open()) {
ADD_FAILURE() << "cannot open " << path;
out->clear();
return;
}
out->clear();
std::array<char, 4096> buffer{};
while (in.read(buffer.data(), buffer.size()) || in.gcount() != 0) {
out->append(buffer.data(), static_cast<size_t>(in.gcount()));
}
if (in.bad() || !in.eof()) {
ADD_FAILURE() << "cannot read " << path;
out->clear();
}
}
};
TEST_F(CliMainTest, ScriptExecutesAndPrintsRowsAfterCommit) {
std::string out;
std::string err;
const int code = RunCli(
"CREATE TABLE t (k INT64, v VARCHAR);"
"INSERT INTO t VALUES (1, 'one'), (2, 'two');"
"SELECT k FROM t ORDER BY k;",
&out, &err);
EXPECT_EQ(code, 0) << err;
// Row output uses Row's operator<< format; every buffered statement's
// output (including command tags) prints only after the commit barrier.
EXPECT_NE(out.find("[1]"), std::string::npos) << out;
EXPECT_NE(out.find("[2]"), std::string::npos) << out;
EXPECT_NE(out.find("Insert Rows"), std::string::npos) << out;
}
TEST_F(CliMainTest, FailedStatementAbortsTheWholeScript) {
std::string out;
std::string err;
// The last statement fails (unknown column); nothing may be printed even
// though the earlier SELECT produced a row.
const int code = RunCli(
"CREATE TABLE t (k INT64);"
"INSERT INTO t VALUES (1);"
"SELECT k FROM t;"
"SELECT missing FROM t;",
&out, &err);
EXPECT_EQ(code, 1);
EXPECT_TRUE(out.empty()) << out;
EXPECT_FALSE(err.empty());
}
TEST_F(CliMainTest, EmptyStdinFailsWithUsage) {
std::string out;
std::string err;
const int code = RunCli("", &out, &err);
EXPECT_EQ(code, 2);
EXPECT_TRUE(out.empty());
EXPECT_NE(err.find("no SQL"), std::string::npos) << err;
}
// T2804: argument-handling coverage beyond the happy path (T2796/T2799
// behavior pins for the CLI).
TEST_F(CliMainTest, HelpFlagPrintsUsageAndExitsZero) {
std::string out;
std::string err;
const int code =
RunCliRaw("SELECT 1;", "--help", dir_ / "stdout", &out, &err);
EXPECT_EQ(code, 0) << err;
EXPECT_NE(out.find("usage:"), std::string::npos) << out;
// --help must not touch the database: no positional was given at all.
EXPECT_FALSE(std::filesystem::exists(dir_ / "db"));
}
TEST_F(CliMainTest, UnknownOptionRejectedAndNoDatabaseCreated) {
std::string out;
std::string err;
const std::string db = (dir_ / "db").string();
const int code = RunCliRaw("SELECT 1;", "--forse '" + db + "'",
dir_ / "stdout", &out, &err);
EXPECT_EQ(code, 2);
EXPECT_NE(err.find("unknown option: --forse"), std::string::npos) << err;
// The old parser handed "--forse" to Database::Create as the file name;
// a typo must leave no debris behind.
EXPECT_FALSE(std::filesystem::exists(db));
EXPECT_FALSE(std::filesystem::exists(db + ".db"));
EXPECT_FALSE(std::filesystem::exists(dir_ / "--forse.db"));
}
TEST_F(CliMainTest, TwoPositionalArgsRejected) {
std::string out;
std::string err;
const int code =
RunCliRaw("SELECT 1;", "'" + (dir_ / "db").string() + "' extra",
dir_ / "stdout", &out, &err);
EXPECT_EQ(code, 2);
EXPECT_NE(err.find("usage:"), std::string::npos) << err;
}
TEST_F(CliMainTest, ForceFlagAcceptedAndScriptRuns) {
std::string out;
std::string err;
const int code = RunCliRaw(
"CREATE TABLE f (k INT64); INSERT INTO f VALUES (1);",
"--force '" + (dir_ / "db").string() + "'", dir_ / "stdout", &out, &err);
EXPECT_EQ(code, 0) << err;
// Database::Create appends the canonical suffixes: <arg>.db + WAL.
EXPECT_TRUE(std::filesystem::exists(dir_ / "db.db")) << err;
}
TEST_F(CliMainTest, QuotedSemicolonsStayOneStatement) {
// A split that ignored quotes would cut at the ';' inside the literal
// `'a;b'`, leaving `INSERT INTO q VALUES ('a` and `b');` as two malformed
// statements that abort the whole script. Exit 0 plus the intact literal
// proves the splitter walked the quoted ';'.
std::string out;
std::string err;
const int code = RunCliRaw(
"CREATE TABLE q (v VARCHAR);"
"INSERT INTO q VALUES ('a;b');"
"SELECT v FROM q;",
"'" + (dir_ / "db").string() + "'", dir_ / "stdout", &out, &err);
EXPECT_EQ(code, 0) << err;
EXPECT_NE(out.find("\"a;b\""), std::string::npos) << out;
}
TEST_F(CliMainTest, OversizedStdinFailsInsteadOfOOM) {
std::string out;
std::string err;
// SplitSqlStatements caps the statement COUNT but not input BYTES; without
// a bounded read a huge redirect used to exhaust memory before the
// splitter ever ran. Keep one over-limit subprocess to pin the rejection
// path without paying for a second 16 MiB file write and CLI launch.
const std::string too_big =
"SELECT 1" + std::string((16 * 1024 * 1024) - 7, ' ');
EXPECT_EQ(RunCli(too_big, &out, &err), 1);
EXPECT_TRUE(out.empty()) << out;
EXPECT_NE(err.find("TooBigData"), std::string::npos) << err;
EXPECT_NE(err.find("input limit"), std::string::npos) << err;
}
TEST_F(CliMainTest, StdoutWriteFailureExitsNonZero) {
#ifndef _WIN32
if (::access("/dev/full", W_OK) != 0) {
GTEST_SKIP() << "/dev/full unavailable";
}
#else
GTEST_SKIP() << "/dev/full is a POSIX device and unavailable on Windows";
#endif
std::string out;
std::string err;
// Same script the happy-path test runs, stdout aimed at /dev/full: the
// commit succeeds but every buffered row write fails (T2794).
const int code = RunCliRaw(
"CREATE TABLE t (k INT64);"
"INSERT INTO t VALUES (1), (2), (3);"
"SELECT k FROM t ORDER BY k;",
"'" + (dir_ / "db").string() + "'", "/dev/full", &out, &err);
EXPECT_EQ(code, 1) << "stdout: " << out << " stderr: " << err;
EXPECT_NE(err.find("failed to write result output"), std::string::npos)
<< err;
}
} // namespace