OmniSciDB  04ee39c94c
OmniSQLUtilitiesTest.cpp
Go to the documentation of this file.
1 /*
2  * Copyright 2019 OmniSci, Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include "../SQLFrontend/CommandHistoryFile.h"
18 #include "gtest/gtest.h"
19 
20 #include <boost/program_options.hpp>
21 #include <cstring>
22 #include <fstream>
23 
24 #include <type_traits>
25 // Mocks
26 
27 using GetEnvRetType = decltype(DefaultEnvResolver().getenv(""));
28 using GetPWUIDRetType = decltype(DefaultEnvResolver().getpwuid(0));
29 
31  public:
32  template <typename... ARGS>
33  GetEnvRetType getenv(ARGS&&...) const {
34  return nullptr;
35  }
36  template <typename... ARGS>
37  GetPWUIDRetType getpwuid(ARGS&&...) const {
38  return nullptr;
39  }
40  auto getuid() const { return ::getuid(); }
41 };
42 
44 
46  public:
47  template <typename... ARGS>
48  GetPWUIDRetType getpwuid(ARGS&&... args) const {
49  return ::getpwuid(std::forward<ARGS>(args)...);
50  }
51 };
52 
54  public:
55  template <typename... ARGS>
56  GetEnvRetType getenv(ARGS&&... args) const {
57  return DefaultEnvResolver::getenv(std::forward<ARGS>(args)...);
58  }
59  template <typename... ARGS>
60  GetPWUIDRetType getpwuid(ARGS&&...) const {
61  throw std::runtime_error("Unexpected getpwuid() invocation.");
62  }
63 };
64 
65 // Mock-base class equivalents of CommandHistoryFile
69 
70 // Tests
71 TEST(CommandHistoryFile, NoHomeEnv) {
73  ASSERT_EQ(std::string(getDefaultHistoryFilename()), std::string(cmd_file));
74 
76  ASSERT_EQ(std::string(getpwuid(getuid())->pw_dir) + '/' +
77  std::string(getDefaultHistoryFilename()),
78  std::string(cmd_file2));
79 }
80 
82  CommandHistoryFile_Home cmd_file;
83  ASSERT_EQ(std::string(getpwuid(getuid())->pw_dir) + '/' +
84  std::string(getDefaultHistoryFilename()),
85  std::string(cmd_file));
86 }
87 
89  CommandHistoryFile cmd_file;
90  ASSERT_EQ(std::string(getpwuid(getuid())->pw_dir) + '/' +
91  std::string(getDefaultHistoryFilename()),
92  std::string(cmd_file));
93 }
94 
96  CommandHistoryFile cmd_file("mutley.txt");
97  ASSERT_EQ(std::string("mutley.txt"), std::string(cmd_file));
98 }
99 
100 TEST(CommandHistoryFile, BoostProgramOptionsCompatibility_DefaultOption) {
101  namespace po = boost::program_options;
102  po::options_description desc("Options");
103  CommandHistoryFile cmd_file;
104 
105  int fake_argc = 1;
106  char const* fake_argv[] = {"lulz"};
107 
108  desc.add_options()(
109  "history", po::value<CommandHistoryFile>(&cmd_file), "History filename");
110  po::variables_map vm;
111  po::store(po::command_line_parser(fake_argc, fake_argv).options(desc).run(), vm);
112  po::notify(vm);
113 
114  ASSERT_EQ(std::string(getpwuid(getuid())->pw_dir) + '/' +
115  std::string(getDefaultHistoryFilename()),
116  std::string(cmd_file));
117 }
118 
119 TEST(CommandHistoryFile, BoostProgramOptionsCompatibility_SetOption) {
120  namespace po = boost::program_options;
121  po::options_description desc("Options");
122  CommandHistoryFile cmd_file;
123 
124  int fake_argc = 2;
125  char const* fake_argv[] = {"lulz", "--history=dudley_dawson.txt"};
126 
127  desc.add_options()(
128  "history", po::value<CommandHistoryFile>(&cmd_file), "History filename");
129  po::variables_map vm;
130  po::store(po::command_line_parser(fake_argc, fake_argv).options(desc).run(), vm);
131  po::notify(vm);
132 
133  ASSERT_EQ(std::string("dudley_dawson.txt"), std::string(cmd_file));
134 }
135 
136 int main(int argc, char* argv[]) {
137  ::testing::InitGoogleTest(&argc, argv);
138  return RUN_ALL_TESTS();
139 }
int main(int argc, char *argv[])
auto const * getenv(char const *env_var_name) const
constexpr char const *const getDefaultHistoryFilename()
GetPWUIDRetType getpwuid(ARGS &&...) const
GetEnvRetType getenv(ARGS &&...) const
GetEnvRetType getenv(ARGS &&... args) const
decltype(DefaultEnvResolver().getpwuid(0)) GetPWUIDRetType
GetPWUIDRetType getpwuid(ARGS &&... args) const
GetPWUIDRetType getpwuid(ARGS &&...) const
decltype(DefaultEnvResolver().getenv("")) GetEnvRetType
static bool run
TEST(CommandHistoryFile, NoHomeEnv)