From 6ac1227a8cb38e599442cb3fb98452fbb24f8b6c Mon Sep 17 00:00:00 2001 From: Brian Sharon Date: Thu, 25 Jan 2024 16:22:28 -0800 Subject: [PATCH] Clean up the project structure (#23) * Move source files to `src` directory * Create a lib directory, move a header into it * Set up a test directory, with a sample test * Add test action * Fix check command; update badge list in README --- .github/workflows/test.yml | 31 +++++++++++++++++++ README.md | 3 +- lib/factorial/include/factorial.hpp | 5 +++ .../heatpump/include}/HeatpumpSettings.hpp | 0 platformio.ini | 17 +++++++--- src/{mitsubishi2mqtt => }/config.hpp | 0 src/{mitsubishi2mqtt => }/html_common.hpp | 0 src/{mitsubishi2mqtt => }/html_init.hpp | 0 src/{mitsubishi2mqtt => }/html_menu.hpp | 0 src/{mitsubishi2mqtt => }/html_metrics.hpp | 0 src/{mitsubishi2mqtt => }/html_pages.hpp | 0 .../javascript_common.hpp | 0 src/{mitsubishi2mqtt => }/languages/da-DA.h | 0 src/{mitsubishi2mqtt => }/languages/de-DE.h | 0 src/{mitsubishi2mqtt => }/languages/en-GB.h | 0 src/{mitsubishi2mqtt => }/languages/es-ES.h | 0 src/{mitsubishi2mqtt => }/languages/fr-FR.h | 0 src/{mitsubishi2mqtt => }/languages/it-IT.h | 0 src/{mitsubishi2mqtt => }/languages/ja-JP.h | 0 src/{mitsubishi2mqtt => }/languages/zh-CN.h | 0 src/{mitsubishi2mqtt => }/logger.cpp | 0 src/{mitsubishi2mqtt => }/logger.hpp | 0 src/{mitsubishi2mqtt => }/mitsubishi2mqtt.cpp | 2 +- src/{mitsubishi2mqtt => }/mitsubishi2mqtt.hpp | 0 test/native/test_factorial/factorial.cpp | 26 ++++++++++++++++ 25 files changed, 77 insertions(+), 7 deletions(-) create mode 100644 .github/workflows/test.yml create mode 100644 lib/factorial/include/factorial.hpp rename {src/mitsubishi2mqtt => lib/heatpump/include}/HeatpumpSettings.hpp (100%) rename src/{mitsubishi2mqtt => }/config.hpp (100%) rename src/{mitsubishi2mqtt => }/html_common.hpp (100%) rename src/{mitsubishi2mqtt => }/html_init.hpp (100%) rename src/{mitsubishi2mqtt => }/html_menu.hpp (100%) rename src/{mitsubishi2mqtt => }/html_metrics.hpp (100%) rename src/{mitsubishi2mqtt => }/html_pages.hpp (100%) rename src/{mitsubishi2mqtt => }/javascript_common.hpp (100%) rename src/{mitsubishi2mqtt => }/languages/da-DA.h (100%) rename src/{mitsubishi2mqtt => }/languages/de-DE.h (100%) rename src/{mitsubishi2mqtt => }/languages/en-GB.h (100%) rename src/{mitsubishi2mqtt => }/languages/es-ES.h (100%) rename src/{mitsubishi2mqtt => }/languages/fr-FR.h (100%) rename src/{mitsubishi2mqtt => }/languages/it-IT.h (100%) rename src/{mitsubishi2mqtt => }/languages/ja-JP.h (100%) rename src/{mitsubishi2mqtt => }/languages/zh-CN.h (100%) rename src/{mitsubishi2mqtt => }/logger.cpp (100%) rename src/{mitsubishi2mqtt => }/logger.hpp (100%) rename src/{mitsubishi2mqtt => }/mitsubishi2mqtt.cpp (99%) rename src/{mitsubishi2mqtt => }/mitsubishi2mqtt.hpp (100%) create mode 100644 test/native/test_factorial/factorial.cpp diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml new file mode 100644 index 0000000..139afa3 --- /dev/null +++ b/.github/workflows/test.yml @@ -0,0 +1,31 @@ +name: Test + +on: + push: + branches: + - main + pull_request: + +jobs: + test: + runs-on: ubuntu-latest + + steps: + - uses: actions/checkout@v3 + + - uses: actions/cache@v3 + with: + path: | + ~/.cache/pip + ~/.platformio/.cache + key: ${{ runner.os }}-pio + - uses: actions/setup-python@v4 + with: + python-version: '3.9' + + - name: Install PlatformIO Core + run: pip install --upgrade platformio + + - name: Run tests + run: | + pio test -e native diff --git a/README.md b/README.md index 4e4518e..bbd58ff 100644 --- a/README.md +++ b/README.md @@ -3,8 +3,9 @@ Use MQTT and ESP8266/ESP32 module to control Mitsubishi HVAC unit. It use SwiCago libraries: https://github.com/SwiCago/HeatPump [![.github/workflows/build.yml](https://github.com/floatplane/mitsubishi2MQTT/actions/workflows/build.yml/badge.svg)](https://github.com/floatplane/mitsubishi2MQTT/actions/workflows/build.yml) -[![.github/workflows/clangformat.yml](https://github.com/floatplane/mitsubishi2MQTT/actions/workflows/clangformat.yml/badge.svg)](https://github.com/floatplane/mitsubishi2MQTT/actions/workflows/clangformat.yml) +[![.github/workflows/test.yml](https://github.com/floatplane/mitsubishi2MQTT/actions/workflows/test.yml/badge.svg)](https://github.com/floatplane/mitsubishi2MQTT/actions/workflows/test.yml) [![.github/workflows/static_analysis.yml](https://github.com/floatplane/mitsubishi2MQTT/actions/workflows/static_analysis.yml/badge.svg)](https://github.com/floatplane/mitsubishi2MQTT/actions/workflows/static_analysis.yml) +[![.github/workflows/clangformat.yml](https://github.com/floatplane/mitsubishi2MQTT/actions/workflows/clangformat.yml/badge.svg)](https://github.com/floatplane/mitsubishi2MQTT/actions/workflows/clangformat.yml) *** Features: diff --git a/lib/factorial/include/factorial.hpp b/lib/factorial/include/factorial.hpp new file mode 100644 index 0000000..a582f5f --- /dev/null +++ b/lib/factorial/include/factorial.hpp @@ -0,0 +1,5 @@ +#pragma once + +// TODO: delete this after some other tests are set up +// NOLINTNEXTLINE(misc-no-recursion) +inline int factorial(int number) { return number <= 1 ? 1 : factorial(number - 1) * number; } \ No newline at end of file diff --git a/src/mitsubishi2mqtt/HeatpumpSettings.hpp b/lib/heatpump/include/HeatpumpSettings.hpp similarity index 100% rename from src/mitsubishi2mqtt/HeatpumpSettings.hpp rename to lib/heatpump/include/HeatpumpSettings.hpp diff --git a/platformio.ini b/platformio.ini index 504a3db..0210428 100644 --- a/platformio.ini +++ b/platformio.ini @@ -9,11 +9,10 @@ ; https://docs.platformio.org/page/projectconf.html [platformio] -src_dir = src/mitsubishi2mqtt +src_dir = src default_envs = [env] -framework = arduino lib_deps = bblanchon/ArduinoJson @ ^6.21.3 knolleary/PubSubClient @ ^2.8 @@ -22,9 +21,9 @@ lib_deps = check_tool = clangtidy, cppcheck check_flags = clangtidy: --checks '-*,bugprone-*,clang-analyzer-*,misc-*,performance-*,portability-*,readability-*,-readability-magic-numbers,google-*' - cppcheck: --enable=all --inline-suppr --std=c++20 --suppress=*:*/.pio/* --suppress=missingIncludeSystem + cppcheck: --enable=all --inline-suppr --std=c++20 --suppress=*:*/.pio/* --suppress=unmatchedSuppression:*/.pio/* --suppress=missingIncludeSystem check_skip_packages = yes -check_src_filters = +<**/*> +check_src_filters = + + build_flags = -Wall build_src_flags = !echo '-D COMMIT_HASH=\\"'$(git rev-parse --short HEAD)'\\"' @@ -33,14 +32,20 @@ platform_packages = platformio/tool-clangtidy@^1.150005.0 platformio/tool-cppcheck@^1.21100.230717 +[env:native] +platform = native +test_framework = doctest + [env:ESP8266-ESP01] -board = esp01_1m platform = espressif8266 +framework = arduino +board = esp01_1m monitor_speed = 115200 board_build.ldscript = eagle.flash.1m64.ld [env:WEMOS_D1_Mini] platform = espressif8266 +framework = arduino board = d1_mini monitor_speed = 115200 upload_speed = 460800 @@ -48,6 +53,7 @@ board_build.ldscript = eagle.flash.4m2m.ld [env:WEMOS_D1_Mini_Testing] platform = ${env:WEMOS_D1_Mini.platform} +framework = ${env:WEMOS_D1_Mini.framework} board = ${env:WEMOS_D1_Mini.board} monitor_speed = ${env:WEMOS_D1_Mini.monitor_speed} upload_speed = ${env:WEMOS_D1_Mini.upload_speed} @@ -57,6 +63,7 @@ build_flags = ${env:WEMOS_D1_Mini.build_flags} -D ENABLE_LOGGING [env:ESP32DEV] platform = espressif32 +framework = arduino board = esp32dev lib_deps = ArduinoJson @6.20.0 diff --git a/src/mitsubishi2mqtt/config.hpp b/src/config.hpp similarity index 100% rename from src/mitsubishi2mqtt/config.hpp rename to src/config.hpp diff --git a/src/mitsubishi2mqtt/html_common.hpp b/src/html_common.hpp similarity index 100% rename from src/mitsubishi2mqtt/html_common.hpp rename to src/html_common.hpp diff --git a/src/mitsubishi2mqtt/html_init.hpp b/src/html_init.hpp similarity index 100% rename from src/mitsubishi2mqtt/html_init.hpp rename to src/html_init.hpp diff --git a/src/mitsubishi2mqtt/html_menu.hpp b/src/html_menu.hpp similarity index 100% rename from src/mitsubishi2mqtt/html_menu.hpp rename to src/html_menu.hpp diff --git a/src/mitsubishi2mqtt/html_metrics.hpp b/src/html_metrics.hpp similarity index 100% rename from src/mitsubishi2mqtt/html_metrics.hpp rename to src/html_metrics.hpp diff --git a/src/mitsubishi2mqtt/html_pages.hpp b/src/html_pages.hpp similarity index 100% rename from src/mitsubishi2mqtt/html_pages.hpp rename to src/html_pages.hpp diff --git a/src/mitsubishi2mqtt/javascript_common.hpp b/src/javascript_common.hpp similarity index 100% rename from src/mitsubishi2mqtt/javascript_common.hpp rename to src/javascript_common.hpp diff --git a/src/mitsubishi2mqtt/languages/da-DA.h b/src/languages/da-DA.h similarity index 100% rename from src/mitsubishi2mqtt/languages/da-DA.h rename to src/languages/da-DA.h diff --git a/src/mitsubishi2mqtt/languages/de-DE.h b/src/languages/de-DE.h similarity index 100% rename from src/mitsubishi2mqtt/languages/de-DE.h rename to src/languages/de-DE.h diff --git a/src/mitsubishi2mqtt/languages/en-GB.h b/src/languages/en-GB.h similarity index 100% rename from src/mitsubishi2mqtt/languages/en-GB.h rename to src/languages/en-GB.h diff --git a/src/mitsubishi2mqtt/languages/es-ES.h b/src/languages/es-ES.h similarity index 100% rename from src/mitsubishi2mqtt/languages/es-ES.h rename to src/languages/es-ES.h diff --git a/src/mitsubishi2mqtt/languages/fr-FR.h b/src/languages/fr-FR.h similarity index 100% rename from src/mitsubishi2mqtt/languages/fr-FR.h rename to src/languages/fr-FR.h diff --git a/src/mitsubishi2mqtt/languages/it-IT.h b/src/languages/it-IT.h similarity index 100% rename from src/mitsubishi2mqtt/languages/it-IT.h rename to src/languages/it-IT.h diff --git a/src/mitsubishi2mqtt/languages/ja-JP.h b/src/languages/ja-JP.h similarity index 100% rename from src/mitsubishi2mqtt/languages/ja-JP.h rename to src/languages/ja-JP.h diff --git a/src/mitsubishi2mqtt/languages/zh-CN.h b/src/languages/zh-CN.h similarity index 100% rename from src/mitsubishi2mqtt/languages/zh-CN.h rename to src/languages/zh-CN.h diff --git a/src/mitsubishi2mqtt/logger.cpp b/src/logger.cpp similarity index 100% rename from src/mitsubishi2mqtt/logger.cpp rename to src/logger.cpp diff --git a/src/mitsubishi2mqtt/logger.hpp b/src/logger.hpp similarity index 100% rename from src/mitsubishi2mqtt/logger.hpp rename to src/logger.hpp diff --git a/src/mitsubishi2mqtt/mitsubishi2mqtt.cpp b/src/mitsubishi2mqtt.cpp similarity index 99% rename from src/mitsubishi2mqtt/mitsubishi2mqtt.cpp rename to src/mitsubishi2mqtt.cpp index f56dd53..cf251f1 100644 --- a/src/mitsubishi2mqtt/mitsubishi2mqtt.cpp +++ b/src/mitsubishi2mqtt.cpp @@ -38,7 +38,7 @@ ESP8266WebServer server(80); // ESP8266 web #include // for rounding to Fahrenheit values // #include // for LED status (Using a Wemos D1-Mini) -#include "HeatpumpSettings.hpp" +#include // #include "config.hpp" // config file #include "html_common.hpp" // common code HTML (like header, footer) #include "html_init.hpp" // code html for initial config diff --git a/src/mitsubishi2mqtt/mitsubishi2mqtt.hpp b/src/mitsubishi2mqtt.hpp similarity index 100% rename from src/mitsubishi2mqtt/mitsubishi2mqtt.hpp rename to src/mitsubishi2mqtt.hpp diff --git a/test/native/test_factorial/factorial.cpp b/test/native/test_factorial/factorial.cpp new file mode 100644 index 0000000..0fb2fe3 --- /dev/null +++ b/test/native/test_factorial/factorial.cpp @@ -0,0 +1,26 @@ +#define DOCTEST_CONFIG_IMPLEMENT // REQUIRED: Enable custom main() +#include + +#include + +TEST_CASE("testing the factorial function") { + CHECK(factorial(0) == 1); + CHECK(factorial(1) == 1); + CHECK(factorial(2) == 2); + CHECK(factorial(3) == 6); + CHECK(factorial(10) == 3628800); +} + +int main(int argc, char **argv) { + doctest::Context context; + + // BEGIN:: PLATFORMIO REQUIRED OPTIONS + context.setOption("success", true); // Report successful tests + context.setOption("no-exitcode", true); // Do not return non-zero code on failed test case + // END:: PLATFORMIO REQUIRED OPTIONS + + // YOUR CUSTOM DOCTEST OPTIONS + + context.applyCommandLine(argc, argv); + return context.run(); +} \ No newline at end of file