PlatformIO Community

Pio test native UNIT_TEST guard still including arduino code


#1

I’m running the tests with the command platformio test -e native -f native. If I comment out the FastLED.h include and all the code referencing it from main.cpp my tests run and pass as the timer.cpp doesn’t reference any arduino/avr libraries. However, even with the #ifndef UNIT_TEST guard in main.cpp, with the FastLED code referenced in main.cpp it tries to compile FastLED and throws a bunch of errors. I understand why it would throw those errors if I was trying to run the native tests referencing arduino/avr libraries. But why is it not excluding the code in my main.cpp file? Am I not structuring the project correctly?

My platformio.ini

[env:uno]
platform = atmelavr
board = uno
framework = arduino

[env:native]
platform = native
test_ignore = arduino

Project Structure

<project_root>/
    lib/
        time/
            src/
                time.h
                time.cpp
    src/
        main.cpp
    test/
        arduino/
            test_uno.cpp
        native/
            test_time.cpp


//main.cpp
#ifndef UNIT_TEST

#include <Arduino.h>
#include <time.h>
#include <FastLED.h>
#include <stdint.h>

uint8_t hours;
uint8_t minutes;
uint8_t seconds;
CRGB leds[19];

#define get_bit(n, k) ((n & (1 << k)) >> k)

void update_leds(CRGB *led_arr, uint32_t value){
  // ...snip
}

void setup() {
  FastLED.addLeds<WS2812B, 3, GRB>(leds, 19);
  time_init(&hours, &minutes, &seconds);
}

void loop() {
  tick(&hours, &minutes, &seconds);
  uint32_t converted_time = convert_time(hours, minutes, seconds);
  update_leds(leds, converted_time);
  delay(1000);
}

#endif


//test_time.cpp    
#ifdef UNIT_TEST

#include <time.h>
#include <unity.h>
#include <stdio.h>
#include <stdint.h>

// ... snip tests defined here

int main(int argc, char **argv) {
    UNITY_BEGIN();
    //RUN_TEST here
    UNITY_END();
    return 0;
}

#endif

#2

You could explicitly ignore the FastLED library for your native environment with lib_ignore = FastLED.

The UNIT_TEST guard in your main.cpp is not evaluated by the Library Dependency Finder (LDF) in its default mode “chain”. Therfore the FastLED lib will be compiled by the build system but run into a bunch of errors because of the missing arduino framework. You could change the Dependency Finder Mode to chain+ which will consider the preprocessor syntax and your #ifndef guard

Hopefully this helps :slight_smile:


#3

Changing the lib_ldf_mode to chain+ was exactly what it was! Is there a specific reason why the preprocessor syntax is ignored by default?