From 75d8390704c0ccaaf8a97c267127bd99cce5d460 Mon Sep 17 00:00:00 2001
From: Lucas Saavedra Vaz <32426024+lucasssvaz@users.noreply.github.com>
Date: Wed, 5 Mar 2025 09:11:02 -0300
Subject: [PATCH] ci(hw): Fix files being overwritten

---
 .github/scripts/sketch_utils.sh   |  4 ++--
 .github/scripts/tests_run.sh      | 10 +++++-----
 .github/workflows/tests_build.yml | 24 ++++++++++++------------
 .github/workflows/tests_hw.yml    | 16 +++++++++++++---
 .github/workflows/tests_qemu.yml  |  2 +-
 .github/workflows/tests_wokwi.yml |  2 +-
 docs/en/contributing.rst          |  4 ++--
 7 files changed, 36 insertions(+), 26 deletions(-)

diff --git a/.github/scripts/sketch_utils.sh b/.github/scripts/sketch_utils.sh
index 00d7d1bc232..e536da50111 100755
--- a/.github/scripts/sketch_utils.sh
+++ b/.github/scripts/sketch_utils.sh
@@ -244,7 +244,7 @@ function build_sketch { # build_sketch <ide_path> <user_path> <path-to-ino> [ext
         build_dir="$ARDUINO_BUILD_DIR"
     elif [ "$len" -eq 1 ]; then
         # build_dir="$sketchdir/build"
-        build_dir="$HOME/.arduino/tests/$sketchname/build.tmp"
+        build_dir="$HOME/.arduino/tests/$target/$sketchname/build.tmp"
     fi
 
     output_file="$HOME/.arduino/cli_compile_output.txt"
@@ -254,7 +254,7 @@ function build_sketch { # build_sketch <ide_path> <user_path> <path-to-ino> [ext
     for i in $(seq 0 $((len - 1))); do
         if [ "$len" -ne 1 ]; then
             # build_dir="$sketchdir/build$i"
-            build_dir="$HOME/.arduino/tests/$sketchname/build$i.tmp"
+            build_dir="$HOME/.arduino/tests/$target/$sketchname/build$i.tmp"
         fi
         rm -rf "$build_dir"
         mkdir -p "$build_dir"
diff --git a/.github/scripts/tests_run.sh b/.github/scripts/tests_run.sh
index 274666c8a44..1c4bee79742 100755
--- a/.github/scripts/tests_run.sh
+++ b/.github/scripts/tests_run.sh
@@ -27,9 +27,9 @@ function run_test {
     fi
 
     if [ "$len" -eq 1 ]; then
-        sdkconfig_path="$HOME/.arduino/tests/$sketchname/build.tmp/sdkconfig"
+        sdkconfig_path="$HOME/.arduino/tests/$target/$sketchname/build.tmp/sdkconfig"
     else
-        sdkconfig_path="$HOME/.arduino/tests/$sketchname/build0.tmp/sdkconfig"
+        sdkconfig_path="$HOME/.arduino/tests/$target/$sketchname/build0.tmp/sdkconfig"
     fi
 
     if [ -f "$sketchdir"/ci.json ]; then
@@ -45,7 +45,7 @@ function run_test {
     fi
 
     if [ ! -f "$sdkconfig_path" ]; then
-        printf "\033[93mSketch %s not built\nMight be due to missing target requirements or build failure\033[0m\n" "$sketchname"
+        printf "\033[93mSketch %s build not found in %s\nMight be due to missing target requirements or build failure\033[0m\n" "$(dirname "$sdkconfig_path")" "$sketchname"
         printf "\n\n\n"
         return 0
     fi
@@ -60,7 +60,7 @@ function run_test {
 
     if [ "$len" -eq 1 ]; then
         # build_dir="$sketchdir/build"
-        build_dir="$HOME/.arduino/tests/$sketchname/build.tmp"
+        build_dir="$HOME/.arduino/tests/$target/$sketchname/build.tmp"
         report_file="$sketchdir/$target/$sketchname.xml"
     fi
 
@@ -83,7 +83,7 @@ function run_test {
 
         if [ "$len" -ne 1 ]; then
             # build_dir="$sketchdir/build$i"
-            build_dir="$HOME/.arduino/tests/$sketchname/build$i.tmp"
+            build_dir="$HOME/.arduino/tests/$target/$sketchname/build$i.tmp"
             report_file="$sketchdir/$target/$sketchname$i.xml"
         fi
 
diff --git a/.github/workflows/tests_build.yml b/.github/workflows/tests_build.yml
index 7a5a2959657..7a10c95ed22 100644
--- a/.github/workflows/tests_build.yml
+++ b/.github/workflows/tests_build.yml
@@ -26,10 +26,10 @@ jobs:
         with:
           key: tests-${{ env.id }}-bin
           path: |
-            ~/.arduino/tests/**/build*.tmp/*.bin
-            ~/.arduino/tests/**/build*.tmp/*.elf
-            ~/.arduino/tests/**/build*.tmp/*.json
-            ~/.arduino/tests/**/build*.tmp/sdkconfig
+            ~/.arduino/tests/${{ inputs.chip }}/**/build*.tmp/*.bin
+            ~/.arduino/tests/${{ inputs.chip }}/**/build*.tmp/*.elf
+            ~/.arduino/tests/${{ inputs.chip }}/**/build*.tmp/*.json
+            ~/.arduino/tests/${{ inputs.chip }}/**/build*.tmp/sdkconfig
 
       - name: Evaluate if tests should be built
         id: check-build
@@ -73,10 +73,10 @@ jobs:
         with:
           key: tests-${{ env.id }}-bin
           path: |
-            ~/.arduino/tests/**/build*.tmp/*.bin
-            ~/.arduino/tests/**/build*.tmp/*.elf
-            ~/.arduino/tests/**/build*.tmp/*.json
-            ~/.arduino/tests/**/build*.tmp/sdkconfig
+            ~/.arduino/tests/${{ inputs.chip }}/**/build*.tmp/*.bin
+            ~/.arduino/tests/${{ inputs.chip }}/**/build*.tmp/*.elf
+            ~/.arduino/tests/${{ inputs.chip }}/**/build*.tmp/*.json
+            ~/.arduino/tests/${{ inputs.chip }}/**/build*.tmp/sdkconfig
 
       - name: Upload ${{ inputs.chip }} ${{ inputs.type }} binaries as artifacts
         uses: actions/upload-artifact@v4
@@ -84,7 +84,7 @@ jobs:
           name: tests-bin-${{ inputs.chip }}-${{ inputs.type }}
           overwrite: true
           path: |
-            ~/.arduino/tests/**/build*.tmp/*.bin
-            ~/.arduino/tests/**/build*.tmp/*.elf
-            ~/.arduino/tests/**/build*.tmp/*.json
-            ~/.arduino/tests/**/build*.tmp/sdkconfig
+            ~/.arduino/tests/${{ inputs.chip }}/**/build*.tmp/*.bin
+            ~/.arduino/tests/${{ inputs.chip }}/**/build*.tmp/*.elf
+            ~/.arduino/tests/${{ inputs.chip }}/**/build*.tmp/*.json
+            ~/.arduino/tests/${{ inputs.chip }}/**/build*.tmp/sdkconfig
diff --git a/.github/workflows/tests_hw.yml b/.github/workflows/tests_hw.yml
index 76480ed7c0e..6c15ba79a7f 100644
--- a/.github/workflows/tests_hw.yml
+++ b/.github/workflows/tests_hw.yml
@@ -22,13 +22,18 @@ defaults:
 jobs:
   hardware-test:
     name: Hardware ${{ inputs.chip }} ${{ inputs.type }} tests
-    runs-on: [arduino, "${{ inputs.chip }}"]
+    runs-on: ["arduino", "${{ inputs.chip }}"]
     env:
       id: ${{ github.event.pull_request.number || github.ref }}-${{ github.event.pull_request.head.sha || github.sha }}-${{ inputs.chip }}-${{ inputs.type }}
     container:
       image: python:3.10.1-bullseye
-      options: --privileged
+      options: --privileged --device-cgroup-rule="c 188:* rmw" --device-cgroup-rule="c 166:* rmw"
     steps:
+      - name: Clean workspace
+        run: |
+          rm -rf ./*
+          rm -rf ~/.arduino/tests
+
       - name: Check if already passed
         id: cache-results
         if: github.event.pull_request.number != null
@@ -81,7 +86,12 @@ jobs:
         with:
           name: tests-bin-${{ inputs.chip }}-${{ inputs.type }}
           path: |
-            ~/.arduino/tests
+            ~/.arduino/tests/${{ inputs.chip }}
+
+      - name: List binaries
+        if: ${{ steps.check-tests.outputs.enabled == 'true' }}
+        run: |
+          ls -laR ~/.arduino/tests
 
       - name: Run Tests
         if: ${{ steps.check-tests.outputs.enabled == 'true' }}
diff --git a/.github/workflows/tests_qemu.yml b/.github/workflows/tests_qemu.yml
index 6675909c9df..6c5934ce69a 100644
--- a/.github/workflows/tests_qemu.yml
+++ b/.github/workflows/tests_qemu.yml
@@ -117,7 +117,7 @@ jobs:
         with:
           name: tests-bin-${{ inputs.chip }}-${{ inputs.type }}
           path: |
-            ~/.arduino/tests
+            ~/.arduino/tests/${{ inputs.chip }}
 
       - name: Run Tests
         if: ${{ steps.check-tests.outputs.enabled == 'true' }}
diff --git a/.github/workflows/tests_wokwi.yml b/.github/workflows/tests_wokwi.yml
index c254d5fa153..4e5d3ceca51 100644
--- a/.github/workflows/tests_wokwi.yml
+++ b/.github/workflows/tests_wokwi.yml
@@ -276,7 +276,7 @@ jobs:
           run-id: ${{ github.event.workflow_run.id }}
           name: tests-bin-${{ matrix.chip }}-${{ matrix.type }}
           path: |
-            ~/.arduino/tests
+            ~/.arduino/tests/${{ matrix.chip }}
 
       - name: Run Tests
         if: ${{ steps.check-tests.outputs.enabled == 'true' }}
diff --git a/docs/en/contributing.rst b/docs/en/contributing.rst
index fb7843f1fb6..4093c60ec64 100644
--- a/docs/en/contributing.rst
+++ b/docs/en/contributing.rst
@@ -318,7 +318,7 @@ ESP32-C3 target, you would run:
 
     ./.github/scripts/tests_build.sh -s uart -t esp32c3
 
-You should see the output of the build process and the test binary should be generated in the ``~/.arduino/tests/<test_name>/build.tmp`` folder.
+You should see the output of the build process and the test binary should be generated in the ``~/.arduino/tests/<target_chip>/<test_name>/build.tmp`` folder.
 
 Now that the test is built, you can run it in the target board. Connect the target board to your computer and run:
 
@@ -339,7 +339,7 @@ The test will run on the target board and you should see the output of the test
     lucassvaz@Lucas--MacBook-Pro esp32 % ./.github/scripts/tests_run.sh -s uart -t esp32c3
     Sketch uart test type: validation
     Running test: uart -- Config: Default
-    pytest tests --build-dir /Users/lucassvaz/.arduino/tests/uart/build.tmp -k test_uart --junit-xml=/Users/lucassvaz/Espressif/Arduino/hardware/espressif/esp32/tests/validation/uart/esp32c3/uart.xml --embedded-services esp,arduino
+    pytest tests --build-dir /Users/lucassvaz/.arduino/tests/esp32c3/uart/build.tmp -k test_uart --junit-xml=/Users/lucassvaz/Espressif/Arduino/hardware/espressif/esp32/tests/validation/uart/esp32c3/uart.xml --embedded-services esp,arduino
     =============================================================================================== test session starts ================================================================================================
     platform darwin -- Python 3.12.3, pytest-8.2.2, pluggy-1.5.0
     rootdir: /Users/lucassvaz/Espressif/Arduino/hardware/espressif/esp32/tests