diff --git a/.bazelrc b/.bazelrc index 1eb348b6cb4eca1abfbc6bd375c0a13910cf48d1..0a1bc1da54d7e6a7a372a6ff8c81ee6116183369 100644 --- a/.bazelrc +++ b/.bazelrc @@ -27,3 +27,5 @@ common --extra_toolchains=@rules_zstd//zstd/toolchain/zstd:built # User-specific .bazelrc try-import %workspace%/.bazelrc.user + +build --action_env BAZEL_DO_NOT_DETECT_CPP_TOOLCHAIN=1 \ No newline at end of file diff --git a/.bazelrc.ci b/.bazelrc.ci index 2493d29127b8a89def55e1b97439b422f9bc8bde..4d385a4c83bd41582a719e508fb282aff2734b83 100644 --- a/.bazelrc.ci +++ b/.bazelrc.ci @@ -31,6 +31,7 @@ common --lockfile_mode=error # These locations are cached on the CI build:local --disk_cache=${CI_PROJECT_DIR}/.cache/bazel/disk build:local --repository_cache=${CI_PROJECT_DIR}/.cache/bazel/repo +build:local --action_env BAZEL_DO_NOT_DETECT_CPP_TOOLCHAIN=1 # Use remote cache/execution for our builds build:remote --remote_executor=${CI_REMOTE_EXECUTOR} @@ -62,3 +63,4 @@ build:remote --nolegacy_important_outputs # Describe remote executors build:remote --extra_execution_platforms=@toolchain_utils//toolchain/platform:amd64-linux + diff --git a/MODULE.bazel b/MODULE.bazel index e6744440e518a7c62072250fae392e4623bd8e5c..714aef38e0fab9ff450a0c9f5f43a5b1f8255de9 100644 --- a/MODULE.bazel +++ b/MODULE.bazel @@ -14,7 +14,8 @@ bazel_dep(name = "download_utils", version = "1.0.0-beta.2") bazel_dep(name = "rules_tar", version = "1.0.0-beta.3") bazel_dep(name = "rules_zstd", version = "1.0.0-beta.3") bazel_dep(name = "platforms", version = "0.0.10") - +bazel_dep(name = "rules_go", version = "0.49.0") +bazel_dep(name = "rules_cc", version = "0.0.9") bazel_dep(name = "hermetic_cc_toolchain", version = "3.1.0", dev_dependency = True) # Register LabGrid toolchains diff --git a/WORKSPACE b/WORKSPACE index e69de29bb2d1d6434b8b29ae775ad8c2e48c5391..c6ea7fffc6c4d9c0a715a255a9364c31b648a251 100644 --- a/WORKSPACE +++ b/WORKSPACE @@ -0,0 +1,9 @@ +workspace(name = "Catch2") + +load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive") + +http_archive( + name = "catch2", + strip_prefix = "Catch2-3.1.1", + urls = ["https://github.com/catchorg/Catch2/archive/v3.1.1.tar.gz"], +) diff --git a/labgrid/test_runner/BUILD.bazel b/labgrid/test_runner/BUILD.bazel new file mode 100644 index 0000000000000000000000000000000000000000..b348672c5e8df3bd09c83791fc4a0360d910ab9a --- /dev/null +++ b/labgrid/test_runner/BUILD.bazel @@ -0,0 +1,37 @@ +load("@rules_cc//cc:defs.bzl", "cc_binary", "cc_library") +load("@rules_go//go:def.bzl", "go_binary") +load("@rules_python//python:defs.bzl", "py_test") + +cc_library( + name = "my_lib", + srcs = ["my_lib.cpp"], + hdrs = ["my_lib.h"], +) + +cc_binary( + name = "test_binary", + srcs = ["test_binary.cpp"], + deps = [ + ":my_lib", + "@catch2//:catch2_main", + ], +) + +go_binary( + name = "runner", + srcs = ["main.go"], + data = [":test_binary"], + env = {"XML_OUTPUT_FILE": "/your_xml_output_path/"}, #when using bazel test - bazel will ignore it + pure = "on", + visibility = ["//:__subpackages__"], +) + +py_test( + name = "some-test", + srcs = ["some-test.py"], + data = [ + ":runner", + ":test_binary", + ], + deps = ["@rules_python//python/runfiles"], +) diff --git a/labgrid/test_runner/main.go b/labgrid/test_runner/main.go new file mode 100644 index 0000000000000000000000000000000000000000..9eaefd850218199d15a8ce9f6b62d187890f21a0 --- /dev/null +++ b/labgrid/test_runner/main.go @@ -0,0 +1,44 @@ +package main + +import ( + "flag" + "fmt" + "os" + "os/exec" + "path/filepath" +) + +func main() { + var redirToFile bool + binary_path := flag.String("binary", "", "binary path") + flag.BoolVar(&redirToFile, "redir", false, "redirect output to XML_OUTPUT_FILE") + flag.Parse() + binary_abs_path, _ := filepath.Abs(*binary_path) + runner_exe_path, err := os.Executable() + if err != nil { + panic(err) + } + + runner_path := filepath.Dir(runner_exe_path) + xmlOutputPath := os.Getenv("XML_OUTPUT_FILE") //the default filename of bazel XML_OUTPUT_FILE is test.xml + if xmlOutputPath == "" { + fmt.Println("XML_OUTPUT_FILE was empty") + xmlOutputPath = runner_path + } else if !filepath.IsAbs(xmlOutputPath) { + fmt.Println("XML_OUTPUT_FILE was relative") + xmlOutputPath = filepath.Join(runner_path, xmlOutputPath) + } + cmd := exec.Command("") + if redirToFile { + cmd = exec.Command(binary_abs_path, fmt.Sprintf("> %s", xmlOutputPath)) + + } else { + cmd = exec.Command(binary_abs_path) + } + cmd.Env = os.Environ() + cmd.Env = append(cmd.Env, fmt.Sprintf("XML_OUTPUT_FILE=%s", xmlOutputPath)) + err_run := cmd.Run() + if err_run != nil { + fmt.Println(err_run) + } +} diff --git a/labgrid/test_runner/my_lib.cpp b/labgrid/test_runner/my_lib.cpp new file mode 100644 index 0000000000000000000000000000000000000000..c2b164bfcc8d328dc74fbfd0e58b0e5d7ecd8bf4 --- /dev/null +++ b/labgrid/test_runner/my_lib.cpp @@ -0,0 +1,11 @@ +#include "my_lib.h" + +int add(int a, int b) +{ + return a + b; +} + +int multiply(int a, int b) +{ + return a * b; +} \ No newline at end of file diff --git a/labgrid/test_runner/my_lib.h b/labgrid/test_runner/my_lib.h new file mode 100644 index 0000000000000000000000000000000000000000..da55205aa70f814d31436a798dda01f06b88ed0b --- /dev/null +++ b/labgrid/test_runner/my_lib.h @@ -0,0 +1,2 @@ +int add(int a, int b); +int multiply(int a, int b); \ No newline at end of file diff --git a/labgrid/test_runner/some-test.py b/labgrid/test_runner/some-test.py new file mode 100644 index 0000000000000000000000000000000000000000..9e63e59f768cf5b4a296a3d8fa0c3144263122b4 --- /dev/null +++ b/labgrid/test_runner/some-test.py @@ -0,0 +1,29 @@ +import subprocess +import unittest +from python.runfiles import Runfiles +import os +import xml.etree.ElementTree as et + +class TestGoBinary(unittest.TestCase): + def test_go_binary(self): + runfiles = Runfiles.Create() + go_binary = runfiles.Rlocation("rules_labgrid/labgrid/test_runner/runner_/runner") + test_binary = runfiles.Rlocation("rules_labgrid/labgrid/test_runner/test_binary") + result = subprocess.run( + [go_binary,f"-binary={test_binary}"], + stdout=subprocess.PIPE, + stderr=subprocess.PIPE + ) + self.assertEqual(result.returncode,0,"error") + xml_output_file = os.getenv("XML_OUTPUT_FILE","not-found") + with open(xml_output_file,"r") as f: + output_real_data = f.read() + print(output_real_data) + try: + et.parse(xml_output_file) + self.assertTrue(True) + except Exception: + self.assertTrue(False,"failed to parse xml file") + +if __name__ =="__main__": + unittest.main() diff --git a/labgrid/test_runner/test_binary.cpp b/labgrid/test_runner/test_binary.cpp new file mode 100644 index 0000000000000000000000000000000000000000..d57045640011ba61b6214b242b91aaf0f373b2b0 --- /dev/null +++ b/labgrid/test_runner/test_binary.cpp @@ -0,0 +1,12 @@ +#define CATCH_CONFIG_MAIN +#include +#include "my_lib.h" + +TEST_CASE("Addition", "[add]") +{ + REQUIRE(add(2, 3) == 5); +} +TEST_CASE("Multiplication", "[multiply]") +{ + REQUIRE(multiply(2, 5) == 10); +} \ No newline at end of file