diff options
-rw-r--r-- | .travis.yml | 4 | ||||
-rw-r--r-- | Dockerfile_test | 32 | ||||
-rw-r--r-- | include/tgbot/tools/FileTools.h | 9 | ||||
-rw-r--r-- | samples/echobot-curl-client/src/main.cpp | 8 | ||||
-rw-r--r-- | samples/echobot-webhook-server/src/main.cpp | 5 | ||||
-rw-r--r-- | samples/echobot/src/main.cpp | 8 | ||||
-rw-r--r-- | samples/inline-keyboard/src/main.cpp | 8 | ||||
-rw-r--r-- | samples/photo/src/main.cpp | 8 | ||||
-rw-r--r-- | src/tools/FileTools.cpp | 11 | ||||
-rw-r--r-- | test/utils.h | 16 |
10 files changed, 66 insertions, 43 deletions
diff --git a/.travis.yml b/.travis.yml index e07cafa..37629fb 100644 --- a/.travis.yml +++ b/.travis.yml @@ -4,5 +4,7 @@ services: - docker script: - - + - docker build -t reo7sp/tgbot-cpp -f Dockerfile . + - docker build -t reo7sp/tgbot-cpp-test -f Dockerfile_test . + - docker run --rm reo7sp/tgbot-cpp-test diff --git a/Dockerfile_test b/Dockerfile_test index 8d4bec0..8b16ae2 100644 --- a/Dockerfile_test +++ b/Dockerfile_test @@ -1,8 +1,21 @@ -FROM debian:stretch +FROM ubuntu:14.04 MAINTAINER Oleg Morozenkov <a@reo7sp.ru> RUN apt-get -qq update && \ - apt-get -qq install -y g++ make binutils cmake libssl-dev libboost-system-dev libboost-test-dev libcurl4-openssl-dev + apt-get -qq install -y g++ make binutils cmake libssl-dev libcurl4-openssl-dev \ + wget build-essential python-dev autotools-dev libicu-dev libbz2-dev + +WORKDIR /usr/src/boost +RUN wget -q -O boost_1_59_0.tar.gz https://sourceforge.net/projects/boost/files/boost/1.59.0/boost_1_59_0.tar.gz/download && \ + tar xzf boost_1_59_0.tar.gz && \ + rm boost_1_59_0.tar.gz && \ + cd boost_1_59_0 && \ + ./bootstrap.sh --prefix=/usr/ --with-libraries=system,test && \ + ./b2 -j $(nproc) -d0 && \ + ./b2 install && \ + cd .. && \ + rm -r boost_1_59_0 + WORKDIR /usr/src/tgbot-cpp COPY include include @@ -11,31 +24,34 @@ COPY test test COPY CMakeLists.txt ./ RUN cmake -DENABLE_TESTS=ON . && \ - make -j4 && \ + make -j$(nproc) && \ make install + COPY samples samples WORKDIR /usr/src/tgbot-cpp/samples/echobot RUN rm -rf CMakeCache.txt CMakeFiles/ && \ - cmake . && make -j4 + cmake . && make -j$(nproc) WORKDIR /usr/src/tgbot-cpp/samples/echobot-curl-client RUN rm -rf CMakeCache.txt CMakeFiles/ && \ - cmake . && make -j4 + cmake . && make -j$(nproc) WORKDIR /usr/src/tgbot-cpp/samples/echobot-webhook-server RUN rm -rf CMakeCache.txt CMakeFiles/ && \ - cmake . && make -j4 + cmake . && make -j$(nproc) WORKDIR /usr/src/tgbot-cpp/samples/inline-keyboard RUN rm -rf CMakeCache.txt CMakeFiles/ && \ - cmake . && make -j4 + cmake . && make -j$(nproc) WORKDIR /usr/src/tgbot-cpp/samples/photo RUN rm -rf CMakeCache.txt CMakeFiles/ && \ - cmake . && make -j4 + cmake . && make -j$(nproc) + WORKDIR /usr/src/tgbot-cpp ENV CTEST_OUTPUT_ON_FAILURE=1 CMD make test + diff --git a/include/tgbot/tools/FileTools.h b/include/tgbot/tools/FileTools.h index 97fb52c..e718a6c 100644 --- a/include/tgbot/tools/FileTools.h +++ b/include/tgbot/tools/FileTools.h @@ -15,16 +15,17 @@ namespace FileTools { /** * Reads whole file to string. * @param filePath Path to a file + * @throws exception of type std::ifstream::failure if reading fails * @return string with file contents */ std::string read(const std::string& filePath); /** -* Save file to disk. -* @param filePath Path to a file -* @return bool success + * Save file to disk. + * @param filePath Path to a file + * @throws exception of type std::ifstream::failure if writing fails */ -bool write(const std::string& content, const std::string& filePath); +void write(const std::string& content, const std::string& filePath); }; diff --git a/samples/echobot-curl-client/src/main.cpp b/samples/echobot-curl-client/src/main.cpp index 1f376a5..630dad8 100644 --- a/samples/echobot-curl-client/src/main.cpp +++ b/samples/echobot-curl-client/src/main.cpp @@ -8,8 +8,6 @@ using namespace std; using namespace TgBot; -bool sigintGot = false; - int main() { string token(getenv("TOKEN")); printf("Token: %s\n", token.c_str()); @@ -30,13 +28,15 @@ int main() { signal(SIGINT, [](int s) { printf("SIGINT got\n"); - sigintGot = true; + exit(0); }); + try { printf("Bot username: %s\n", bot.getApi().getMe()->username.c_str()); + bot.getApi().deleteWebhook(); TgLongPoll longPoll(bot); - while (!sigintGot) { + while (true) { printf("Long poll started\n"); longPoll.start(); } diff --git a/samples/echobot-webhook-server/src/main.cpp b/samples/echobot-webhook-server/src/main.cpp index 7b5776a..d51246d 100644 --- a/samples/echobot-webhook-server/src/main.cpp +++ b/samples/echobot-webhook-server/src/main.cpp @@ -26,6 +26,11 @@ int main() { bot.getApi().sendMessage(message->chat->id, "Your message is: " + message->text); }); + signal(SIGINT, [](int s) { + printf("SIGINT got\n"); + exit(0); + }); + try { printf("Bot username: %s\n", bot.getApi().getMe()->username.c_str()); diff --git a/samples/echobot/src/main.cpp b/samples/echobot/src/main.cpp index 802d318..966ae6f 100644 --- a/samples/echobot/src/main.cpp +++ b/samples/echobot/src/main.cpp @@ -8,8 +8,6 @@ using namespace std; using namespace TgBot; -bool sigintGot = false; - int main() { string token(getenv("TOKEN")); printf("Token: %s\n", token.c_str()); @@ -28,13 +26,15 @@ int main() { signal(SIGINT, [](int s) { printf("SIGINT got\n"); - sigintGot = true; + exit(0); }); + try { printf("Bot username: %s\n", bot.getApi().getMe()->username.c_str()); + bot.getApi().deleteWebhook(); TgLongPoll longPoll(bot); - while (!sigintGot) { + while (true) { printf("Long poll started\n"); longPoll.start(); } diff --git a/samples/inline-keyboard/src/main.cpp b/samples/inline-keyboard/src/main.cpp index 0976989..70cf7c7 100644 --- a/samples/inline-keyboard/src/main.cpp +++ b/samples/inline-keyboard/src/main.cpp @@ -8,8 +8,6 @@ using namespace std; using namespace TgBot; -bool sigintGot = false; - int main() { string token(getenv("TOKEN")); printf("Token: %s\n", token.c_str()); @@ -41,13 +39,15 @@ int main() { signal(SIGINT, [](int s) { printf("SIGINT got\n"); - sigintGot = true; + exit(0); }); + try { printf("Bot username: %s\n", bot.getApi().getMe()->username.c_str()); + bot.getApi().deleteWebhook(); TgLongPoll longPoll(bot); - while (!sigintGot) { + while (true) { printf("Long poll started\n"); longPoll.start(); } diff --git a/samples/photo/src/main.cpp b/samples/photo/src/main.cpp index 03ca437..cca31f0 100644 --- a/samples/photo/src/main.cpp +++ b/samples/photo/src/main.cpp @@ -8,8 +8,6 @@ using namespace std; using namespace TgBot; -bool sigintGot = false; - int main() { string token(getenv("TOKEN")); printf("Token: %s\n", token.c_str()); @@ -27,13 +25,15 @@ int main() { signal(SIGINT, [](int s) { printf("SIGINT got\n"); - sigintGot = true; + exit(0); }); + try { printf("Bot username: %s\n", bot.getApi().getMe()->username.c_str()); + bot.getApi().deleteWebhook(); TgLongPoll longPoll(bot); - while (!sigintGot) { + while (true) { printf("Long poll started\n"); longPoll.start(); } diff --git a/src/tools/FileTools.cpp b/src/tools/FileTools.cpp index 279f152..f18b85e 100644 --- a/src/tools/FileTools.cpp +++ b/src/tools/FileTools.cpp @@ -13,23 +13,18 @@ namespace FileTools { string read(const string& filePath) { ifstream in(filePath, ios::in | ios::binary); - if (!in) { - throw system_error(errno, system_category()); - } + in.exceptions(ifstream::failbit | ifstream::badbit); ostringstream contents; contents << in.rdbuf(); in.close(); return contents.str(); } -bool write(const string& content, const string& filePath) { +void write(const string& content, const string& filePath) { ofstream out(filePath, ios::out | ios::binary); - if (!out) { - return false; - } + out.exceptions(ofstream::failbit | ofstream::badbit); out << content; out.close(); - return true; } }; diff --git a/test/utils.h b/test/utils.h index e0db42b..9225b84 100644 --- a/test/utils.h +++ b/test/utils.h @@ -98,17 +98,21 @@ inline std::string diffS(const std::string& test, const std::string& expected) { v2.push_back(s2); } } while (r1 || r2); - return diff(v1, v2, [](const std::string& item) { - return item; - }); + + std::string (*toStringFunc)(const std::string&) = + [](const std::string& item) -> std::string { return item; }; + + return diff(v1, v2, toStringFunc); } inline std::string diffMSS(const std::unordered_map<std::string, std::string>& test, const std::unordered_map<std::string, std::string>& expected) { std::map<std::string, std::string> v1(test.begin(), test.end()); std::map<std::string, std::string> v2(expected.begin(), expected.end()); - return diff(v1, v2, [](const std::pair<const std::string, std::string>& item) { - return item.first + '=' + item.second; - }); + + std::string (*toStringFunc)(const std::pair<const std::string, std::string>&) = + [](const std::pair<const std::string, std::string>& item) -> std::string { return item.first + '=' + item.second; }; + + return diff(v1, v2, toStringFunc); } #endif //TGBOT_UTILS_H |