Skip to content

Commit

Permalink
Add format and gen to CI
Browse files Browse the repository at this point in the history
Add format and gen to CI
  • Loading branch information
sabudilovskiy authored Apr 1, 2023
2 parents 5e46047 + 650ff75 commit bc5131f
Show file tree
Hide file tree
Showing 12 changed files with 147 additions and 71 deletions.
1 change: 1 addition & 0 deletions .gen/benchs.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
benchs/hello_benchmark.cpp
30 changes: 30 additions & 0 deletions .gen/objs.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
src/views/register/view.hpp
src/views/register/view.cpp
src/views/register/Request.hpp
src/views/register/Request.cpp
src/views/login/view.hpp
src/views/login/view.cpp
src/views/login/Request.hpp
src/views/login/Request.cpp
src/views/hello/view.hpp
src/views/hello/view.cpp
src/utils/json_type.hpp
src/utils/json_type.cpp
src/utils/component_list_fwd.hpp
src/models/user_type/type.hpp
src/models/user_type/postgre.hpp
src/models/user_type/parse.hpp
src/models/user_type/parse.cpp
src/models/user/type.hpp
src/models/user/postgre.hpp
src/models/auth_token/type.hpp
src/models/auth_token/serialize.hpp
src/http/handler_parsed.hpp
src/components/controllers/user_controller_fwd.hpp
src/components/controllers/user_controller_fwd.cpp
src/components/controllers/user_controller.hpp
src/components/controllers/user_controller.cpp
src/components/controllers/token_controller_fwd.hpp
src/components/controllers/token_controller_fwd.cpp
src/components/controllers/token_controller.hpp
src/components/controllers/token_controller.cpp
1 change: 1 addition & 0 deletions .gen/service.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
service/main.cpp
1 change: 1 addition & 0 deletions .gen/unittest.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
utests/hello_test.cpp
7 changes: 7 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,13 @@ jobs:
with:
submodules: true

- name: Check format sources
run: |
make check-format
- name: Gen list of source and check diffs with commit
run: |
make gen
make check-git-status
- name: Reuse ccache directory
uses: actions/cache@v2
with:
Expand Down
8 changes: 7 additions & 1 deletion .github/workflows/docker.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,13 @@ jobs:
- uses: actions/checkout@v2
with:
submodules: true

- name: Check format sources
run: |
make check-format
- name: Check git status
run: |
make gen
make check-git-status
- name: Reuse ccache directory
uses: actions/cache@v2
with:
Expand Down
42 changes: 10 additions & 32 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,58 +9,36 @@ include(GNUInstallDirs)

add_subdirectory(third_party/userver)

#make gen
#source lists are generated in the .gen folder
file(STRINGS ${PROJECT_SOURCE_DIR}/.gen/benchs.txt benchs_src)
file(STRINGS ${PROJECT_SOURCE_DIR}/.gen/objs.txt objs_src)
file(STRINGS ${PROJECT_SOURCE_DIR}/.gen/service.txt service_src)
file(STRINGS ${PROJECT_SOURCE_DIR}/.gen/unittest.txt unittest_src)

# Common sources
add_library(${PROJECT_NAME}_objs OBJECT
src/views/hello/view.hpp
src/views/hello/view.cpp
src/components/controllers/user_controller.cpp
src/components/controllers/user_controller.hpp
src/components/controllers/user_controller_fwd.hpp
src/components/controllers/user_controller_fwd.cpp
src/components/controllers/token_controller_fwd.cpp
src/components/controllers/user_controller_fwd.cpp
src/components/controllers/token_controller.hpp
src/components/controllers/token_controller.cpp
src/models/auth_token/type.hpp
src/models/auth_token/serialize.hpp
src/http/handler_parsed.hpp
src/models/user/type.hpp
src/models/user_type/parse.cpp
src/models/user_type/parse.hpp
src/models/user_type/postgre.hpp
src/models/user_type/type.hpp
src/utils/component_list_fwd.hpp
src/utils/json_type.cpp
src/utils/json_type.hpp
src/views/login/Request.cpp
src/views/login/Request.hpp
src/views/login/view.cpp
src/views/login/view.hpp
src/views/register/Request.cpp
src/views/register/Request.hpp
src/views/register/view.cpp
src/views/register/view.hpp
${objs_src}
)
target_link_libraries(${PROJECT_NAME}_objs PUBLIC userver-core userver-postgresql)


# The Service
add_executable(${PROJECT_NAME} service/main.cpp)
add_executable(${PROJECT_NAME} ${service_src})
target_link_libraries(${PROJECT_NAME} PRIVATE ${PROJECT_NAME}_objs)


# Unit Tests
add_executable(${PROJECT_NAME}_unittest
utests/hello_test.cpp
${unittest_src}
)
target_link_libraries(${PROJECT_NAME}_unittest PRIVATE ${PROJECT_NAME}_objs userver-utest)
add_google_tests(${PROJECT_NAME}_unittest)


# Benchmarks
add_executable(${PROJECT_NAME}_benchmark
benchs/hello_benchmark.cpp
${benchs_src}
)
target_link_libraries(${PROJECT_NAME}_benchmark PRIVATE ${PROJECT_NAME}_objs userver-ubench)
add_google_benchmark_tests(${PROJECT_NAME}_benchmark)
Expand Down
52 changes: 49 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -68,12 +68,58 @@ install-debug install-release: install-%: build-%
.PHONY: install
install: install-release

# Format the sources
.PHONY: format
format:
.PHONY: format-cpp
format-cpp:
@find benchs -name '*pp' -type f | xargs $(CLANG_FORMAT) -i
@find service -name '*pp' -type f | xargs $(CLANG_FORMAT) -i
@find src -name '*pp' -type f | xargs $(CLANG_FORMAT) -i
@find utests -name '*pp' -type f | xargs $(CLANG_FORMAT) -i

# Format the sources
.PHONY: format-all
format: format-cpp
@find tests -name '*.py' -type f | xargs autopep8 -i

# Check format the sources
.PHONY: check-format
check-format: check-format-cpp

.PHONY: check-git-status
check-git-status:
@echo "Checking if all files are committed to git..."
@if [ -n "$$(git status --porcelain)" ]; then \
echo "The following files are not committed:"; \
git status --short; \
echo "Please commit all changes and try again."; \
git diff --color | cat; \
exit 1; \
else \
echo "All files are committed to git."; \
fi

.PHONY: check-format-cpp
check-format-cpp:
@find benchs -name '*pp' -type f | xargs $(CLANG_FORMAT) -i --dry-run --Werror
@find service -name '*pp' -type f | xargs $(CLANG_FORMAT) -i --dry-run --Werror
@find src -name '*pp' -type f | xargs $(CLANG_FORMAT) -i --dry-run --Werror
@find utests -name '*pp' -type f | xargs $(CLANG_FORMAT) -i --dry-run --Werror
.PHONY: gen
gen:
@rm -rf .gen
@mkdir -p .gen

@find benchs -name '*pp' > .gen/benchs.txt
@LC_COLLATE=C sort .gen/benchs.txt -r -o .gen/benchs.txt

@find src -name '*pp' > .gen/objs.txt
@LC_COLLATE=C sort .gen/objs.txt -r -o .gen/objs.txt

@find service -name '*pp' > .gen/service.txt
@LC_COLLATE=C sort .gen/service.txt -r -o .gen/service.txt

@find utests -name '*pp' > .gen/unittest.txt
@LC_COLLATE=C sort .gen/unittest.txt -r -o .gen/unittest.txt

# Internal hidden targets that are used only in docker environment
--in-docker-start-debug --in-docker-start-release: --in-docker-start-%: install-%
@sed -i 's/config_vars.yaml/config_vars.docker.yaml/g' /home/user/.local/etc/timetable_vsu_backend/static_config.yaml
Expand Down
3 changes: 2 additions & 1 deletion benchs/hello_benchmark.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ void HelloBenchmark(benchmark::State& state) {

for (auto _ : state) {
const auto name = kNames[i++ % std::size(kNames)];
auto result = service_template::SayHelloTo(name, service_template::UserType::kKnown);
auto result = service_template::SayHelloTo(
name, service_template::UserType::kKnown);
benchmark::DoNotOptimize(result);
}
});
Expand Down
54 changes: 27 additions & 27 deletions service/main.cpp
Original file line number Diff line number Diff line change
@@ -1,58 +1,57 @@
#include <userver/components/minimal_server_component_list.hpp>
#include <userver/clients/dns/component.hpp>
#include <userver/clients/http/component.hpp>
#include <userver/components/minimal_server_component_list.hpp>
#include <userver/server/handlers/ping.hpp>
#include <userver/server/handlers/tests_control.hpp>
#include <userver/testsuite/testsuite_support.hpp>
#include <userver/utils/daemon_run.hpp>

#include <filesystem>
#include <fstream>
#include <sstream>
#include <utility>
#include "../src/components/controllers/token_controller_fwd.hpp"
#include "../src/components/controllers/user_controller_fwd.hpp"
#include "../src/views/hello/view.hpp"
#include "../src/views/login/view.hpp"
#include "../src/views/register/view.hpp"
#include "userver/formats/json/value.hpp"
#include "userver/logging/log.hpp"
#include "userver/storages/postgres/component.hpp"
#include "../src/views/login/view.hpp"
#include "../src/views/register/view.hpp"
#include "../src/components/controllers/token_controller_fwd.hpp"
#include "../src/components/controllers/user_controller_fwd.hpp"
#include <fstream>
#include <filesystem>
#include <sstream>
#include <utility>

namespace fs = std::filesystem;

void LogAllFiles(fs::path path){
void LogAllFiles(fs::path path) {
std::stringstream buffer;
LOG_ERROR() << path;
for (const auto & entry : fs::directory_iterator(path))
buffer << entry.path() << ' ';
for (const auto& entry : fs::directory_iterator(path))
buffer << entry.path() << ' ';
LOG_ERROR() << buffer.str();
}

void LogCommands(int argc, char** argv){
void LogCommands(int argc, char** argv) {
std::stringstream buffer;
if (argc == 0){
if (argc == 0) {
LOG_ERROR() << "No commands";
}
for (int i = 0; i < argc; i++){
for (int i = 0; i < argc; i++) {
buffer << argv[i] << ' ';
}
LOG_ERROR() << fmt::format("Commands is {}", buffer.str());
}

void LogFile(std::string_view path){
void LogFile(std::string_view path) {
std::string path_str(path);
std::ifstream file(path_str);
std::string temp;
if (!file.is_open()){
if (!file.is_open()) {
std::stringstream buffer;
buffer << strerror(errno);
LOG_ERROR() << fmt::format("{} ifstream fail: {} ", path, buffer.str());
}
else if (file.eof()){
} else if (file.eof()) {
LOG_ERROR() << fmt::format("{} is empty", path);
}
while (std::getline(file, temp)){
while (std::getline(file, temp)) {
LOG_ERROR() << temp;
}
file.close();
Expand All @@ -68,13 +67,14 @@ int main(int argc, char* argv[]) {
// LogFile("config_vars.docker.yaml");
using namespace timetable_vsu_backend::components;
using namespace timetable_vsu_backend::views;
auto component_list = userver::components::MinimalServerComponentList()
.Append<userver::server::handlers::Ping>()
.Append<userver::components::TestsuiteSupport>()
.Append<userver::components::HttpClient>()
.Append<userver::clients::dns::Component>()
.Append<userver::components::Postgres>("postgres-db-1")
.Append<userver::server::handlers::TestsControl>();
auto component_list =
userver::components::MinimalServerComponentList()
.Append<userver::server::handlers::Ping>()
.Append<userver::components::TestsuiteSupport>()
.Append<userver::components::HttpClient>()
.Append<userver::clients::dns::Component>()
.Append<userver::components::Postgres>("postgres-db-1")
.Append<userver::server::handlers::TestsControl>();
service_template::AppendHello(component_list);
timetable_vsu_backend::views::login::Append(component_list);
timetable_vsu_backend::views::register_::Append(component_list);
Expand Down
17 changes: 11 additions & 6 deletions utests/hello_test.cpp
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
#include "../src/views/hello/view.hpp"
#include <vector>
#include <userver/utest/utest.hpp>

#include <vector>
#include "../src/views/hello/view.hpp"

UTEST(SayHelloTo, Basic) {
EXPECT_EQ(service_template::SayHelloTo("Developer", service_template::UserType::kKnown), "Hi again, Developer!\n");
EXPECT_EQ(service_template::SayHelloTo("", service_template::UserType::kFirstTime), "Hello, unknown user!\n");
EXPECT_EQ(service_template::SayHelloTo("Developer", service_template::UserType::kFirstTime), "Hello, Developer!\n");
EXPECT_EQ(service_template::SayHelloTo("Developer",
service_template::UserType::kKnown),
"Hi again, Developer!\n");
EXPECT_EQ(
service_template::SayHelloTo("", service_template::UserType::kFirstTime),
"Hello, unknown user!\n");
EXPECT_EQ(service_template::SayHelloTo(
"Developer", service_template::UserType::kFirstTime),
"Hello, Developer!\n");
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"folders": [
{
"path": "../",
"path": ".",
"name" : "vsu_timetable"
},
],
Expand Down

0 comments on commit bc5131f

Please sign in to comment.