Source files not getting compiled at native tests

I am developing a program with the PlatformIO framework for an ESP32 board.
To execute some unit tests I set up a ‘native’ environment.
The program to be unit tested is ‘Syslog.cpp’ under the default source folder ‘${Project}/test’.
The mock files for the unit test are in ‘${Project}/test/mocks’.
The ‘platformio.ini’ file is:

; PlatformIO Project Configuration File
;
; Please visit documentation for the other options and examples
; https://docs.platformio.org/page/projectconf.html
  
[env]
build_src_filter =
    +<Syslog.cpp>
  
[env:nodemcu-32s]
platform = espressif32
board = nodemcu-32s
framework = arduino
monitor_speed = 921600
lib_deps =
    paulstoffregen/Time@^1.6.1
build_flags =
    -std=gnu++17
    -D__time_t_defined
    -I src
    -I include
build_src_filter =
    ${env.build_src_filter}
  
[env:native]
platform = native
build_flags =
    -std=gnu++17
    -D USING_ARDUINO_FAKE
    -D TEST
    -D UNIT_TEST
    -D__time_t_defined
    -I src
    -I include
    -I test/mocks
    ; ignore warnings pragmas for now, TODO: fix these
    -w
build_src_filter =
    ${env.build_src_filter}
    +<test/*.cpp>
    +<test/mocks/*.cpp>
lib_deps =
    throwtheswitch/Unity@^2.5.2
    fabiobatsilva/ArduinoFake@^0.4.0

This is how my folder structure looks like:

├── Changelog.md
├── LICENSE
├── README.md
├── include
│   ├── ITime.h
│   ├── IUDP.h
│   └── WProgram.h
├── keywords.txt
├── library.json
├── library.properties
├── platformio.ini
├── src
│   ├── Syslog.cpp
│   └── Syslog.h
└── test
    ├── 00_main_test.cpp
    ├── 01_unit_test.cpp
    ├── 02_set_time.cpp
    └── mocks
        ├── MockTimeLib.cpp
        ├── MockTimeLib.h
        └── MockUDP.h

It looks like according to the compiler output that the build_src_filter instructions are not honored at all. Neither my Syslog.cpp nor MockTimeLib.cpp are getting compiled.
This is the compiler output:

platformio.exe test -e native -vv
Collected 1 tests (*)

Processing * in native environment
----------------------------------------------------------------------------------------------------------------------------------------------------------------
Building...
Verbose mode can be enabled via `-v, --verbose` option
LDF: Library Dependency Finder -> https://bit.ly/configure-pio-ldf
LDF Modes: Finder ~ chain, Compatibility ~ soft
Found 2 compatible libraries
Scanning dependencies...
Dependency Graph
|-- Unity @ 2.5.2
|-- ArduinoFake @ 0.4.0
Building in test mode
Compiling .pio\build\native\unity_config_build\unity_config.o
Compiling .pio\build\native\test\00_main_test.o
Compiling .pio\build\native\test\01_unit_test.o
Compiling .pio\build\native\test\02_set_time.o
Compiling .pio\build\native\libbe1\Unity\unity.o
Compiling .pio\build\native\lib730\ArduinoFake\ArduinoFake.o
Compiling .pio\build\native\lib730\ArduinoFake\ClientFake.o
Compiling .pio\build\native\lib730\ArduinoFake\EEPROMFake.o
Compiling .pio\build\native\lib730\ArduinoFake\FunctionFake.o
Compiling .pio\build\native\lib730\ArduinoFake\PrintFake.o
Compiling .pio\build\native\lib730\ArduinoFake\SPIFake.o
Compiling .pio\build\native\lib730\ArduinoFake\SerialFake.o
Compiling .pio\build\native\lib730\ArduinoFake\StreamFake.o
Compiling .pio\build\native\lib730\ArduinoFake\WireFake.o
Compiling .pio\build\native\lib730\ArduinoFake\arduino\IPAddress.o
Compiling .pio\build\native\lib730\ArduinoFake\arduino\WString.o
Compiling .pio\build\native\lib730\ArduinoFake\arduino\noniso.o
Archiving .pio\build\native\libbe1\libUnity.a
Indexing .pio\build\native\libbe1\libUnity.a
Archiving .pio\build\native\lib730\libArduinoFake.a
Indexing .pio\build\native\lib730\libArduinoFake.a
Linking .pio\build\native\program.exe
.pio\build\native\test\00_main_test.o:00_main_test.cpp:(.text+0x41): undefined reference to `Syslog::Syslog(IUDP&, unsigned char)'
.pio\build\native\test\01_unit_test.o:01_unit_test.cpp:(.text+0x35): undefined reference to `Syslog::Syslog(IUDP&, unsigned char)'
.pio\build\native\test\02_set_time.o:02_set_time.cpp:(.text+0x5b): undefined reference to `setTime(int, int, int, int, int, int)'
.pio\build\native\test\02_set_time.o:02_set_time.cpp:(.text+0x93): undefined reference to `hour()'
.pio\build\native\test\02_set_time.o:02_set_time.cpp:(.text+0xe5): undefined reference to `minute()'
.pio\build\native\test\02_set_time.o:02_set_time.cpp:(.text+0x137): undefined reference to `second()'
.pio\build\native\test\02_set_time.o:02_set_time.cpp:(.text+0x189): undefined reference to `day()'
.pio\build\native\test\02_set_time.o:02_set_time.cpp:(.text+0x1db): undefined reference to `month()'
.pio\build\native\test\02_set_time.o:02_set_time.cpp:(.text+0x22d): undefined reference to `year()'
collect2.exe: error: ld returned 1 exit status

What am I missing? Thanks for any help.

The docs for build_src_filter say:

  • +<PATH> include template
  • -<PATH> exclude template

PATH is relative to src_dir.

What isn’t clear is that, in your case running test, is that src_dir is actually the test dir in your project root, not src.

In your test env, try the following filter:

build_src_filter =
    +<../src/Syslog.cpp>
    +<*.cpp>
    +<mocks/*.cpp>

Thank you @ardnew for this suggestion. Unfortunately, that did not change anything. I still get exactly the same output. Almost as of build_src_filter is completely ignored for test runs.

There is a test_build_src flag you can use to include the files in src.

And also a test_filter you may be able to use to include stuff in test/mocks, though I’m not certain how to use it.

Looks like you will need to reorganize your file structure a bit to achieve what you’re wanting. See the docs for unit testing test hierarchy.