Skip to content

Commit

Permalink
Merge pull request #1112 from libcpr/feature/clang_tidy_format_update
Browse files Browse the repository at this point in the history
Clang-{Tidy,Format} Update
  • Loading branch information
COM8 authored Sep 22, 2024
2 parents 99f044e + b847b91 commit 225b745
Show file tree
Hide file tree
Showing 9 changed files with 154 additions and 12 deletions.
9 changes: 2 additions & 7 deletions .github/workflows/clang-format.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,9 @@ jobs:
steps:
- name: Update package list
run: sudo dnf update -y
- name: Install dependencies
run: sudo dnf install -y openssl-devel cmake git gcc clang ninja-build
- name: Install clang-tidy
- name: Install clang-format
run: sudo dnf install -y clang-tools-extra
- name: Checkout
uses: actions/checkout@v3
- name: Check format
uses: RafikFarhad/[email protected]
with:
sources: "include/**/*.hpp,include/**/*.cpp,cpr/**/*.hpp,cpr/**/*.cpp"
style: "file"
run: bash scripts/check_clang_format.sh
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,10 @@ _site/
*.swp

# VSCode
.vscode/
.vscode/*
!.vscode/tasks.json
.vs/
!.vs/tasks.json

# clangd
.cache/
Expand Down
62 changes: 62 additions & 0 deletions .vscode/tasks.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "2.0.0",
"tasks": [
{
"label": "🗑️ Delete build dir",
"type": "shell",
"command": "${workspaceFolder}/scripts/delete_build_dir.sh",
"problemMatcher": [],
"group": {
"kind": "build"
},
"presentation": {
"clear": true
},
"options": {
"cwd": "${workspaceFolder}"
}
},
{
"label": "📝 Run clang-format",
"type": "shell",
"command": "${workspaceFolder}/scripts/run_clang_format.sh",
"args": [
"cpr",
"include",
"test"
],
"problemMatcher": [],
"group": {
"kind": "build"
},
"presentation": {
"clear": true
},
"options": {
"cwd": "${workspaceFolder}"
}
},
{
"label": "📑 Check clang-format",
"type": "shell",
"command": "${workspaceFolder}/scripts/check_clang_format.sh",
"args": [
"cpr",
"include",
"test"
],
"problemMatcher": [],
"group": {
"kind": "build"
},
"presentation": {
"clear": true
},
"options": {
"cwd": "${workspaceFolder}"
}
}
]
}
2 changes: 1 addition & 1 deletion include/cpr/callback.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
#include "cprtypes.h"

#include <atomic>
#include <functional>
#include <cstdint>
#include <functional>
#include <optional>
#include <utility>

Expand Down
44 changes: 44 additions & 0 deletions scripts/check_clang_format.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#!/bin/bash

# Based on: https://gist.github.com/leilee/1d0915a583f8f29414cc21cd86e7151b
# Checks if all files are formatted based on the clang-format formatting rules.
# Execute as follows:
# ./scripts/check_clang_format.sh tests src

printf "📑 Checking if your code fulfills all clang-format rules...\n"

RET_CODE=0

function format() {
for f in $(find $@ -name '*.h' -or -name '*.hpp' -or -name '*.c' -or -name '*.cpp'); do
clang-format -i --dry-run --Werror --style=file ${f};
ret=$?
if [ $ret -ne 0 ]; then
RET_CODE=$ret
fi
done

echo "~~~ $@ directory checked ~~~";
}

# Check all of the arguments first to make sure they're all directories
for dir in "$@"; do
if [ ! -d "${dir}" ]; then
echo "${dir} is not a directory";
else
format ${dir};
fi
done

RED='\033[0;31m'
GREEN='\033[0;32m'
NC='\033[0m'

if [ $RET_CODE -eq 0 ]; then
printf "${GREEN}Everything up to standard :party: ${NC}\n"
else
printf "${RED}Not up to formatting standard :sad_face: ${NC}\n"
echo "Try running run_clang_format.sh to format all files."
fi

exit $RET_CODE
9 changes: 9 additions & 0 deletions scripts/delete_build_dir.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#!/bin/bash

printf "🗑️ Clearing your build directory...\n"
rm -rf build/*

GREEN='\033[0;32m'
NC='\033[0m'

printf "${GREEN}Done. Build directory deleted.${NC}\n"
30 changes: 30 additions & 0 deletions scripts/run_clang_format.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#!/bin/bash

# Based on: https://gist.github.com/leilee/1d0915a583f8f29414cc21cd86e7151b
# Run from the project root directory as follows:
# ./scripts/run_clang_format.sh tests src

printf "📝 Running clang-format...\n"

function format() {
for f in $(find $@ -name '*.h' -or -name '*.hpp' -or -name '*.c' -or -name '*.cpp'); do
echo "format ${f}";
clang-format -i --style=file ${f};
done

echo "~~~ $@ directory formatted ~~~";
}

# Check all of the arguments first to make sure they're all directories
for dir in "$@"; do
if [ ! -d "${dir}" ]; then
echo "${dir} is not a directory";
else
format ${dir};
fi
done

GREEN='\033[0;32m'
NC='\033[0m'

printf "${GREEN}Done. All files were formatted (if required).${NC}\n"
4 changes: 2 additions & 2 deletions test/abstractServer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -132,11 +132,11 @@ bool AbstractServer::IsConnectionActive(mg_mgr* mgr, mg_connection* conn) {
return false;
}

uint16_t AbstractServer::GetRemotePort(const mg_connection* conn) {
uint16_t AbstractServer::GetRemotePort(const mg_connection* conn) {
return (conn->rem.port >> 8) | (conn->rem.port << 8);
}

uint16_t AbstractServer::GetLocalPort(const mg_connection* conn) {
uint16_t AbstractServer::GetLocalPort(const mg_connection* conn) {
return (conn->loc.port >> 8) | (conn->loc.port << 8);
}

Expand Down
2 changes: 1 addition & 1 deletion test/resolve_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ TEST(ResolveTests, RedirectMultiple) {
Resolve resolve2{"www.example1.com", "127.0.0.1", {server->GetPort()}};

Response response = cpr::Get(url1, std::vector<Resolve>{resolve1, resolve2}, Header{{"RedirectLocation", url2.str()}});

std::string expected_text{"Hello world!"};
EXPECT_EQ(expected_text, response.text);
EXPECT_EQ(url2, response.url);
Expand Down

0 comments on commit 225b745

Please sign in to comment.