From 8c208027e2a554a3832fe3baee7232d8079fb52a Mon Sep 17 00:00:00 2001 From: Jeremy Nimmer Date: Mon, 20 May 2024 06:46:56 -0700 Subject: [PATCH] [examples] Fix some smoke test path snafus (#21452) Hard-coding paths to runfiles is incompatible with bzlmod. We should always use python.runfiles, or (in the case of acrobot) just use our simple add_test_rule pattern. --- examples/acrobot/BUILD.bazel | 16 +++++--------- .../acrobot/test/optimizer_demo_smoke_test.sh | 4 ---- .../allegro_hand/joint_control/BUILD.bazel | 3 +++ .../test/run_twisting_mug_test.py | 22 ++++++++++--------- 4 files changed, 20 insertions(+), 25 deletions(-) delete mode 100755 examples/acrobot/test/optimizer_demo_smoke_test.sh diff --git a/examples/acrobot/BUILD.bazel b/examples/acrobot/BUILD.bazel index 0a267457fa34..b8a018df88da 100644 --- a/examples/acrobot/BUILD.bazel +++ b/examples/acrobot/BUILD.bazel @@ -224,10 +224,15 @@ drake_cc_binary( drake_py_binary( name = "optimizer_demo", srcs = ["optimizer_demo.py"], + add_test_rule = True, data = [ ":spong_sim_main_cc", ":test/example_stochastic_scenario.yaml", ], + test_rule_args = [ + "--ensemble_size=3", + "--num_evaluations=3", + ], deps = [ ":acrobot_io", ":metrics", @@ -424,17 +429,6 @@ drake_cc_googletest( ], ) -sh_test( - name = "optimizer_demo_smoke_test", - srcs = ["test/optimizer_demo_smoke_test.sh"], - data = [ - ":optimizer_demo", - ], - tags = [ - "no_memcheck", # This wraps python; python is memcheck-exempt. - ], -) - drake_py_unittest( name = "spong_sim_lib_py_test", deps = [ diff --git a/examples/acrobot/test/optimizer_demo_smoke_test.sh b/examples/acrobot/test/optimizer_demo_smoke_test.sh deleted file mode 100755 index e42d71aabafa..000000000000 --- a/examples/acrobot/test/optimizer_demo_smoke_test.sh +++ /dev/null @@ -1,4 +0,0 @@ -#!/bin/bash - -${TEST_SRCDIR}/drake/examples/acrobot/optimizer_demo \ - --ensemble_size=3 --num_evaluations=3 diff --git a/examples/allegro_hand/joint_control/BUILD.bazel b/examples/allegro_hand/joint_control/BUILD.bazel index 12fa6e8b737a..4b38312ba411 100644 --- a/examples/allegro_hand/joint_control/BUILD.bazel +++ b/examples/allegro_hand/joint_control/BUILD.bazel @@ -64,6 +64,9 @@ drake_py_unittest( ":run_twisting_mug", ], flaky = True, + deps = [ + "@rules_python//python/runfiles", + ], ) add_lint_tests(enable_clang_format_lint = False) diff --git a/examples/allegro_hand/joint_control/test/run_twisting_mug_test.py b/examples/allegro_hand/joint_control/test/run_twisting_mug_test.py index 591d7322fd2a..a035313295f2 100644 --- a/examples/allegro_hand/joint_control/test/run_twisting_mug_test.py +++ b/examples/allegro_hand/joint_control/test/run_twisting_mug_test.py @@ -9,6 +9,8 @@ import time import unittest +from python.runfiles import Create as CreateRunfiles + def _unique_lcm_url(path): """Returns a unique LCM url given a path.""" @@ -19,20 +21,20 @@ def _unique_lcm_url(path): class TestRunTwistingMug(unittest.TestCase): + def _find_resource(self, basename): + respath = f"drake/examples/allegro_hand/joint_control/{basename}" + runfiles = CreateRunfiles() + result = runfiles.Rlocation(respath) + self.assertTrue(result, respath) + self.assertTrue(os.path.exists(result), respath) + return result + def setUp(self): - # Fail-fast if these are not set. - self._test_srcdir = os.environ["TEST_SRCDIR"] self._test_tmpdir = os.environ["TEST_TMPDIR"] # Find the two binaries under test. - self._sim = os.path.join( - self._test_srcdir, "drake/examples/allegro_hand/joint_control", - "allegro_single_object_simulation") - self._control = os.path.join( - self._test_srcdir, "drake/examples/allegro_hand/joint_control", - "run_twisting_mug") - assert os.path.exists(self._sim) - assert os.path.exists(self._control) + self._sim = self._find_resource("allegro_single_object_simulation") + self._control = self._find_resource("run_twisting_mug") # Let the sim and controller talk to (only) each other. self._env = dict(**os.environ)