Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bleeding edge Unit Testing #109

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
111 changes: 96 additions & 15 deletions .github/workflows/build.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,17 @@ on:
default: 'warning'
push:
paths:
- ".github/workflows/build.yaml"
- ".github/workflows/*"
- "src/*"
- "tests/*"
- "pyproject.toml"
- "setup.py"
pull_request:
paths:
- "src/*"
- "tests/*"
- "pyproject.toml"
- "setup.py"

env:
SDCC_VERSION: 4.4.0
Expand All @@ -21,33 +27,50 @@ jobs:
Build_Windows:
runs-on: windows-latest
steps:
- name: install SDCC
run: | #install silent and then unpack missing installation libraries
# - name: Start SSH session
# uses: luchihoratiu/debug-via-ssh@main
# with:
# NGROK_AUTH_TOKEN: ${{ secrets.NGROK_AUTH_TOKEN }}
# SSH_PASS: ${{ secrets.SSH_PASS }}
- name: Install SDCC
run: |
Invoke-WebRequest -UserAgent "Wget" -Uri https://sourceforge.net/projects/sdcc/files/sdcc-win64/$env:SDCC_VERSION/sdcc-$env:SDCC_VERSION-rc3-x64-setup.exe/download -OutFile sdcc_setup.exe
ls
Start-Process -wait -FilePath "sdcc_setup.exe" -ArgumentList "/S", "/D=C:\Program Files\SDCC"
echo "Adding sdcc to PATH"
echo "Adding SDCC to PATH"
Add-Content $env:GITHUB_PATH "C:\Program Files\SDCC\bin"
- uses: actions/checkout@v4
- name: Set up Python
uses: actions/setup-python@v5
with:
python-version: '3.x'
# this mainly checks whether tests compile and work on windows. The actual test report runs on Ubuntu job
- name: Check testing framework
run: |
pip install -e .
rm tests/sim/_tsdz2.cdef # make sure cdef is generated from the source to check testing framework
pytest --collect-only
- name: Build
run: |
cd src
../tools/cygwin_64/bin/make.exe clean
../tools/cygwin_64/bin/make.exe CFLAGS=--Werror
make clean
make CFLAGS=--Werror
- uses: actions/upload-artifact@v4
with:
name: firmware_from_windows
path: bin/main.hex


Build_Linux:
runs-on: ubuntu-22.04
runs-on: ubuntu-latest
steps:
- name: install SDCC
# - name: Start SSH session
# uses: luchihoratiu/debug-via-ssh@main
# with:
# NGROK_AUTH_TOKEN: ${{ secrets.NGROK_AUTH_TOKEN }}
# SSH_PASS: ${{ secrets.SSH_PASS }}
- name: Install SDCC
run: |
cd ~
wget https://sourceforge.net/projects/sdcc/files/sdcc-linux-amd64/4.4.0/sdcc-4.4.0-amd64-unknown-linux2.5.tar.bz2/download -O sdcc-amd64.tar.bz2
ls
wget https://sourceforge.net/projects/sdcc/files/sdcc-linux-amd64/$SDCC_VERSION/sdcc-$SDCC_VERSION-amd64-unknown-linux2.5.tar.bz2/download -O sdcc-amd64.tar.bz2
sudo tar xf sdcc-amd64.tar.bz2
cd sdcc-$SDCC_VERSION/
sudo cp -r * /usr/local
Expand All @@ -65,9 +88,67 @@ jobs:
path: bin/main.hex


Tests:
runs-on: ubuntu-20.04
permissions:
contents: read
pull-requests: write
steps:
- uses: actions/checkout@v4
- name: Set up Python
uses: actions/setup-python@v5
with:
python-version: '3.x'
- name: Install dependencies
run: |
pip install -e .
pip install --upgrade pytest-md-report
pip install gcovr
- name: Run tests
env:
REPORT_OUTPUT: md_report.md
shell: bash
run: |
rm tests/sim/_tsdz2.cdef # make sure cdef is generated from the source to check testing framework

echo "REPORT_FILE=${REPORT_OUTPUT}" >> "$GITHUB_ENV"

gcc -Wall -Wextra -Q --help=warning # print warnings that shouldbe enabled by pytest --strict

pytest --strict --coverage --md-report --md-report-flavor gfm --md-report-output "$REPORT_OUTPUT"
- name: Output reports to the job summary
if: always()
shell: bash
run: |
if [ -f "$REPORT_FILE" ]; then
echo "## Test Report" >> $GITHUB_STEP_SUMMARY
echo "" >> $GITHUB_STEP_SUMMARY
cat "$REPORT_FILE" >> $GITHUB_STEP_SUMMARY
echo "" >> $GITHUB_STEP_SUMMARY
else
echo "Report file not found."
fi
- name: Render the report to the PR when tests fail
uses: marocchino/sticky-pull-request-comment@v2
if: failure()
with:
header: test-report
recreate: true
path: ${{ env.REPORT_FILE }}
- name: Collect coverage data
run: |
echo "### Coverage Report" >> $GITHUB_STEP_SUMMARY
gcovr -r tests --print-summary >> $GITHUB_STEP_SUMMARY
echo "" >> $GITHUB_STEP_SUMMARY
- uses: actions/upload-artifact@v4
with:
name: coverage_report
path: |
tests/coverage_report.html

Compare_builds:
needs: [Build_Windows, Build_Linux]
runs-on: ubuntu-22.04
runs-on: ubuntu-latest
steps:
- name: Download Windows build
uses: actions/download-artifact@v4
Expand All @@ -81,7 +162,7 @@ jobs:
path: firmware_from_linux
- name: Compare build files
run: |
ls
echo "Comparing build files"
git diff firmware_from_windows/main.hex firmware_from_linux/main.hex --word-diff=color --ignore-space-at-eol --exit-code
git diff firmware_from_windows/main.hex firmware_from_linux/main.hex --word-diff=color --ignore-space-at-eol --exit-code
echo "Done comparing build files. Files are the same."

11 changes: 8 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
Debug

__pycache__
**/*.launch
.vscode
.*
!.github
tools/Java_Configurator_Source/
!tools/Java_Configurator_Source/src
*.hex
*.bin
*.elf
*.dump
*.ctu-info
src/Result.log
bin/
experimental settings/
releases/
releases/
tests/sim/*
!_tsdz2.cdef
3 changes: 2 additions & 1 deletion .vscode/extensions.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
// List of extensions which should be recommended for users of this workspace.
"recommendations": [
"ms-vscode.cpptools",
"cl.stm8-debug"
"ms-python.python",
"cl.stm8-debug",
]
}
27 changes: 26 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
[![Build Action](../../actions/workflows/build.yaml/badge.svg)](../../actions/workflows/build.yaml)
![GitHub issues](https://img.shields.io/github/issues/emmebrusa/TSDZ2-Smart-EBike-1) [![Build Action](../../actions/workflows/build.yaml/badge.svg)](../../actions/workflows/build.yaml)

This repository is updated by mbrusa.

Expand Down Expand Up @@ -53,7 +53,32 @@ This project is being developed and maintained for free by a community of users.
- install [cpptools](https://marketplace.visualstudio.com/items?itemName=ms-vscode.cpptools)
- press F5 in VScode to build, flash, and debug (`STM8-gdb` profile)

## Testing
### Setup
Initialize virtual environment (optional)::

`py -m venv .venv`

Enable virtual environment or let VScode to do it automatically:

`.venv\Scripts\activate` or `source .venv\Scripts\activate`

Install dependencies:

`pip install .`

### Usage

Run tests:

`pytest`

Any changes should have a corresponding unit test added, unless unfeasible.

Calculate coverage and generate html report (probably will not work on Windows):
`pytest --coverage`

Tests with coverage are executed in the CI as well.

### Compile the firmware manually
- `cd src/` and use `make` or `compile.bat` to compile the firmware.
Expand Down
31 changes: 31 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
[build-system]
requires = ["hatchling"]
build-backend = "hatchling.build"

[tool.hatch.build.targets.wheel]
packages = ["tests"]

[project]
name = "TSDZ2-Smart-EBike"
version = "20.1C.6"
description = "TSDZ2 Open Source Firmware adapted to VLCD5-VLCD6-XH18 displays."
requires-python = ">=3.7"

dependencies = [
"pytest",
"cffi >=1.15.0",
"setuptools",
"numpy",
"hypothesis",
]

[project.urls]
"Homepage" = "https://github.com/emmebrusa/TSDZ2-Smart-EBike-1"

[tool.pyright]
extraPaths = ["tests"]

[tool.pytest.ini_options]
addopts = [
# "--ignore=tests/example",
]
6 changes: 3 additions & 3 deletions src/STM8S_StdPeriph_Lib/inc/stm8s.h
Original file line number Diff line number Diff line change
Expand Up @@ -241,12 +241,12 @@ typedef uint16_t u16;
typedef uint8_t u8;


typedef enum {RESET = 0, SET = !RESET} FlagStatus, ITStatus, BitStatus, BitAction;
typedef enum {RESET = 0, SET = 1} FlagStatus, ITStatus, BitStatus, BitAction;

typedef enum {DISABLE = 0, ENABLE = !DISABLE} FunctionalState;
typedef enum {DISABLE = 0, ENABLE = 1} FunctionalState;
#define IS_FUNCTIONALSTATE_OK(STATE) (((STATE) == DISABLE) || ((STATE) == ENABLE))

typedef enum {ERROR = 0, SUCCESS = !ERROR} ErrorStatus;
typedef enum {ERROR = 0, SUCCESS = 1} ErrorStatus;


/**
Expand Down
1 change: 0 additions & 1 deletion src/adc.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
*/

#include <stdint.h>
#include <stdio.h>
#include "stm8s.h"
#include "pins.h"
#include "stm8s_adc1.h"
Expand Down
6 changes: 3 additions & 3 deletions src/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
#define WHEEL_MAX_SPEED 25
#define ENABLE_LIGHTS 1
#define ENABLE_WALK_ASSIST 1
#define ENABLE_BRAKE_SENSOR 0
#define ENABLE_BRAKE_SENSOR 1
#define ENABLE_THROTTLE 0
#define ENABLE_TEMPERATURE_LIMIT 0
#define ENABLE_STREET_MODE_ON_STARTUP 1
Expand Down Expand Up @@ -111,7 +111,7 @@
#define WALK_ASSIST_LEVEL_3 40
#define WALK_ASSIST_LEVEL_4 45
#define WALK_ASSIST_THRESHOLD_SPEED_X10 60
#define WALK_ASSIST_DEBOUNCE_ENABLED 0
#define WALK_ASSIST_DEBOUNCE_ENABLED 1
#define WALK_ASSIST_DEBOUNCE_TIME 60
#define CRUISE_TARGET_SPEED_LEVEL_1 15
#define CRUISE_TARGET_SPEED_LEVEL_2 18
Expand All @@ -126,7 +126,7 @@
#define ASSIST_THROTTLE_MAX_VALUE 255
#define STREET_MODE_WALK_ENABLED 1
#define DATA_DISPLAY_ON_STARTUP 1
#define FIELD_WEAKENING_ENABLED 0
#define FIELD_WEAKENING_ENABLED 1
#define PEDAL_TORQUE_ADC_OFFSET_ADJ 20
#define PEDAL_TORQUE_ADC_RANGE_ADJ 20
#define PEDAL_TORQUE_ADC_ANGLE_ADJ 36
Expand Down
19 changes: 9 additions & 10 deletions src/ebike_app.c
Original file line number Diff line number Diff line change
Expand Up @@ -721,7 +721,7 @@ static void apply_power_assist(void)
ui8_adc_battery_current_target = ui8_adc_battery_current_max;
}
else {
ui8_adc_battery_current_target = ui16_adc_battery_current_target;
ui8_adc_battery_current_target = (uint8_t)ui16_adc_battery_current_target;
}

#if STARTUP_ASSIST_ENABLED
Expand Down Expand Up @@ -784,7 +784,7 @@ static void apply_torque_assist(void)
ui8_adc_battery_current_target = ui8_adc_battery_current_max;
}
else {
ui8_adc_battery_current_target = ui16_adc_battery_current_target_torque_assist;
ui8_adc_battery_current_target = (uint8_t)ui16_adc_battery_current_target_torque_assist;
}

#if STARTUP_ASSIST_ENABLED
Expand Down Expand Up @@ -834,7 +834,7 @@ static void apply_cadence_assist(void)
ui8_adc_battery_current_target = ui8_adc_battery_current_max;
}
else {
ui8_adc_battery_current_target = ui16_adc_battery_current_target_cadence_assist;
ui8_adc_battery_current_target = (uint8_t)ui16_adc_battery_current_target_cadence_assist;
}

// set duty cycle target
Expand Down Expand Up @@ -895,7 +895,7 @@ static void apply_emtb_assist(void)
ui8_adc_battery_current_target = ui8_adc_battery_current_max;
}
else {
ui8_adc_battery_current_target = ui16_adc_battery_current_target_eMTB_assist;
ui8_adc_battery_current_target = (uint8_t)ui16_adc_battery_current_target_eMTB_assist;
}

#if STARTUP_ASSIST_ENABLED
Expand Down Expand Up @@ -983,7 +983,7 @@ static void apply_hybrid_assist(void)
ui8_adc_battery_current_target = ui8_adc_battery_current_max;
}
else {
ui8_adc_battery_current_target = ui16_adc_battery_current_target;
ui8_adc_battery_current_target = (uint8_t)ui16_adc_battery_current_target;
}

#if STARTUP_ASSIST_ENABLED
Expand Down Expand Up @@ -1404,7 +1404,7 @@ static void apply_temperature_limiting(void)
}
else {
// adjust target current if motor over temperature limit
ui8_adc_battery_current_target = map_ui16((uint16_t) ui16_motor_temperature_filtered_x10,
ui8_adc_battery_current_target = (uint8_t)map_ui16((uint16_t) ui16_motor_temperature_filtered_x10,
(uint16_t) ((uint8_t)ui8_motor_temperature_min_value_to_limit_array[TEMPERATURE_SENSOR_TYPE] * (uint8_t)10U),
(uint16_t) ((uint8_t)ui8_motor_temperature_max_value_to_limit_array[TEMPERATURE_SENSOR_TYPE] * (uint8_t)10U),
ui8_adc_battery_current_target,
Expand Down Expand Up @@ -1622,9 +1622,8 @@ static void get_pedal_torque(void)
}


struct_configuration_variables* get_configuration_variables(void)
{
return &m_configuration_variables;
struct_configuration_variables* get_configuration_variables(void) {
return (struct_configuration_variables*) &m_configuration_variables;
}


Expand Down Expand Up @@ -2899,7 +2898,7 @@ static void uart_send_package(void)

// reserved for VLCD5, torque sensor value TE and TE1
#if ENABLE_VLCD5
ui8_tx_buffer[3] = ui16_adc_pedal_torque_offset_init;
ui8_tx_buffer[3] = (uint8_t)ui16_adc_pedal_torque_offset_init;
if (ui16_adc_pedal_torque > ui16_adc_pedal_torque_offset_init) {
ui8_tx_buffer[4] = ui16_adc_pedal_torque - ui16_adc_pedal_torque_offset_init;
}
Expand Down
Loading