forked from heavyai/heavydb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
OmniSQLUtilitiesTest.cpp
149 lines (126 loc) · 4.43 KB
/
OmniSQLUtilitiesTest.cpp
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
/*
* Copyright 2019 OmniSci, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "../SQLFrontend/CommandHistoryFile.h"
#include "gtest/gtest.h"
#include <boost/program_options.hpp>
#include <cstring>
#include <fstream>
#include <type_traits>
// Mocks
using GetEnvRetType = decltype(DefaultEnvResolver().getenv(""));
#ifndef _WIN32
using GetPWUIDRetType = decltype(DefaultEnvResolver().getpwuid(0));
#endif
class DefaultUnitTestResolver {
public:
template <typename... ARGS>
GetEnvRetType getenv(ARGS&&...) const {
return nullptr;
}
#ifndef _WIN32
template <typename... ARGS>
GetPWUIDRetType getpwuid(ARGS&&...) const {
return nullptr;
}
#endif
template <typename... ARGS>
const char* getpwdir(ARGS&&...) const {
return nullptr;
}
auto getuid() const { return ::getuid(); }
};
class NoHomeNoPWEntResolver : public DefaultUnitTestResolver {};
class NoHomePWEntResolver : public DefaultEnvResolver {
public:
template <typename... ARGS>
GetEnvRetType getenv(ARGS&&...) const {
return nullptr;
}
};
class HomeResolver : public DefaultEnvResolver {
public:
template <typename... ARGS>
GetEnvRetType getenv(ARGS&&... args) const {
return DefaultEnvResolver::getenv(std::forward<ARGS>(args)...);
}
#ifndef _WIN32
template <typename... ARGS>
GetPWUIDRetType getpwuid(ARGS&&...) const {
throw std::runtime_error("Unexpected getpwuid() invocation.");
}
#endif
template <typename... ARGS>
const char* getpwdir(ARGS&&...) const {
throw std::runtime_error("Unexpected getpwdir() invocation.");
}
};
// Mock-base class equivalents of CommandHistoryFile
using CommandHistoryFile_NoHomeNoPWEnt = CommandHistoryFileImpl<NoHomeNoPWEntResolver>;
using CommandHistoryFile_NoHomePWEnt = CommandHistoryFileImpl<NoHomePWEntResolver>;
using CommandHistoryFile_Home = CommandHistoryFileImpl<HomeResolver>;
// Tests
TEST(CommandHistoryFile, NoHomeEnv) {
CommandHistoryFile_NoHomeNoPWEnt cmd_file;
ASSERT_EQ(std::string(getDefaultHistoryFilename()), std::string(cmd_file));
CommandHistoryFile_NoHomePWEnt cmd_file2;
ASSERT_EQ(getHomeDirectory() + '/' + std::string(getDefaultHistoryFilename()),
std::string(cmd_file2));
}
TEST(CommandHistoryFile, HomeEnv) {
CommandHistoryFile_Home cmd_file;
ASSERT_EQ(getHomeDirectory() + '/' + std::string(getDefaultHistoryFilename()),
std::string(cmd_file));
}
TEST(CommandHistoryFile, Basic) {
CommandHistoryFile cmd_file;
ASSERT_EQ(getHomeDirectory() + '/' + std::string(getDefaultHistoryFilename()),
std::string(cmd_file));
}
TEST(CommandHistoryFile, Custom) {
CommandHistoryFile cmd_file("mutley.txt");
ASSERT_EQ(std::string("mutley.txt"), std::string(cmd_file));
}
TEST(CommandHistoryFile, BoostProgramOptionsCompatibility_DefaultOption) {
namespace po = boost::program_options;
po::options_description desc("Options");
CommandHistoryFile cmd_file;
int fake_argc = 1;
char const* fake_argv[] = {"lulz"};
desc.add_options()(
"history", po::value<CommandHistoryFile>(&cmd_file), "History filename");
po::variables_map vm;
po::store(po::command_line_parser(fake_argc, fake_argv).options(desc).run(), vm);
po::notify(vm);
ASSERT_EQ(getHomeDirectory() + '/' + std::string(getDefaultHistoryFilename()),
std::string(cmd_file));
}
TEST(CommandHistoryFile, BoostProgramOptionsCompatibility_SetOption) {
namespace po = boost::program_options;
po::options_description desc("Options");
CommandHistoryFile cmd_file;
int fake_argc = 2;
char const* fake_argv[] = {"lulz", "--history=dudley_dawson.txt"};
desc.add_options()(
"history", po::value<CommandHistoryFile>(&cmd_file), "History filename");
po::variables_map vm;
po::store(po::command_line_parser(fake_argc, fake_argv).options(desc).run(), vm);
po::notify(vm);
ASSERT_EQ(std::string("dudley_dawson.txt"), std::string(cmd_file));
}
int main(int argc, char* argv[]) {
::testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}