对tools进行了整理,Xmake也改为相对路径,在线 LiDAR-IMU 定位器实现

This commit is contained in:
mzx committed 2026-11-23 14:32:42 +08:00
1 parent 0602144ac5
commit 4502296915
70 files changed
+2652 -1298

No files matched your search

+258
View File
@@ -0,0 +1,258 @@
#!/usr/bin/env python3
"""Exercise the shared environment probes through both CLI entry points."""
import os
from pathlib import Path
import subprocess
import sys
import tempfile
import time
import unittest
from unittest.mock import patch
TOOLS = Path(__file__).resolve().parents[1]
WORKSPACE = TOOLS.parent
SCRIPTS = WORKSPACE / 'src/sentry_sim/scripts'
sys.path.insert(0, str(TOOLS))
import project_env
class EnvironmentTest(unittest.TestCase):
def setUp(self):
temp = tempfile.TemporaryDirectory(prefix='sentry env ')
self.addCleanup(temp.cleanup)
self.root = Path(temp.name)
self.env = os.environ.copy()
self.env['PYTHONDONTWRITEBYTECODE'] = '1'
for name in ('ROS_SETUP', 'ROS_INSTALL_ROOT', 'ROS_DISTRO', 'AMENT_PREFIX_PATH',
'SENTRY_BIN_DIR', 'NAV_BIN_DIR', 'SENTRY_FIELD_ROOT', 'SENTRY_MAP_YAML',
'GZ_BIN', 'GZ_FORCE_VERSION', 'BRIDGE_WAIT_TIMEOUT', 'GZ_PARTITION'):
self.env.pop(name, None)
def run_command(self, command, **overrides):
return subprocess.run(command, cwd=self.root, env={**self.env, **overrides},
capture_output=True, text=True, timeout=12)
def cli(self, *args, **overrides):
return self.run_command([sys.executable, str(TOOLS/'project_env.py'), *map(str, args)],
**overrides)
def shell(self, *args, **overrides):
return self.run_command(['bash', '-c', 'source "$1"; shift; "$@"',
'env-test', str(SCRIPTS/'env_common.sh'), *map(str, args)],
**overrides)
def executable(self, path, script='exit 0'):
path.parent.mkdir(parents=True, exist_ok=True)
path.write_text('#!/bin/bash\n' + script + '\n')
path.chmod(0o755)
return path
def expect_value(self, result, value):
self.assertEqual(result.returncode, 0, result.stderr)
self.assertEqual(result.stdout, f'{value}\n')
def expect_error(self, result, text, code=1):
self.assertEqual(result.returncode, code, result.stderr)
self.assertEqual(result.stdout, '')
self.assertIn(text, result.stderr)
def test_map_overrides(self):
field = self.root/'field with spaces'
field.mkdir()
yaml = self.root/'custom map.yaml'
yaml.touch()
for bad in (self.root/'missing.yaml', field):
with self.subTest(path=bad):
self.expect_error(self.cli('--map', SENTRY_MAP_YAML=str(bad)), 'SENTRY_MAP_YAML')
self.expect_error(self.shell('env_map_yaml', field, SENTRY_MAP_YAML=str(bad)),
'SENTRY_MAP_YAML')
self.expect_value(self.cli('--map', SENTRY_MAP_YAML=yaml.name), yaml)
self.expect_value(self.shell('env_map_yaml', field, SENTRY_MAP_YAML=yaml.name), yaml)
with patch.dict(os.environ, {'SENTRY_MAP_YAML': str(field)}):
self.assertEqual(project_env.find_map_yaml(field, explicit=yaml), yaml)
def test_map_search_order(self):
field = self.root/'field'
maps = field/'src/guodong_sentry_sim/maps'
canonical = maps/'rmul_2027/rmul_2027.yaml'
named_first = maps/'a/rmul_2027.yaml'
named_last = maps/'z/rmul_2027.yaml'
fallback = maps/'a/001.yaml'
for path in (canonical, named_first, named_last, fallback):
path.parent.mkdir(parents=True, exist_ok=True)
path.touch()
(maps/'a/000.yaml').mkdir()
def expect_map(path):
self.expect_value(self.cli('--map', SENTRY_FIELD_ROOT=str(field)), path)
self.expect_value(self.shell('env_map_yaml', field), path)
expect_map(canonical)
canonical.unlink()
expect_map(named_first)
named_first.unlink()
expect_map(named_last)
named_last.unlink()
expect_map(fallback)
fallback.unlink()
self.expect_error(self.cli('--map', SENTRY_FIELD_ROOT=str(field)), 'SENTRY_MAP_YAML')
self.expect_error(self.shell('env_map_yaml', field), 'SENTRY_MAP_YAML')
def test_field_and_ros_setup_overrides(self):
field = self.root/'field'
other = self.root/'other field'
field.mkdir()
other.mkdir()
self.expect_error(self.cli('--field', SENTRY_FIELD_ROOT=str(field/'missing')),
'SENTRY_FIELD_ROOT')
self.expect_error(self.shell('env_field_root', self.root,
SENTRY_FIELD_ROOT=str(field/'missing')), 'SENTRY_FIELD_ROOT')
self.expect_value(self.cli('--field', SENTRY_FIELD_ROOT=field.name), field)
with patch.dict(os.environ, {'SENTRY_FIELD_ROOT': str(field)}):
self.assertEqual(project_env.find_field_root(self.root, explicit=other), other)
setup = self.root/'setup with spaces.bash'
setup.write_text('export SENTRY_SETUP_TEST=loaded\n')
for invalid in (field, self.root/'missing.bash'):
self.expect_error(self.cli('--ros-setup', ROS_SETUP=str(invalid)), 'ROS_SETUP')
self.expect_error(self.shell('env_source_ros', ROS_SETUP=str(invalid)), 'ROS_SETUP')
self.expect_value(self.cli('--ros-setup', ROS_SETUP=setup.name), setup)
result = self.run_command(
['bash', '-c', 'source "$1"; env_source_ros && printf "%s\\n" "$SENTRY_SETUP_TEST"',
'env-test', str(SCRIPTS/'env_common.sh')], ROS_SETUP=setup.name)
self.expect_value(result, 'loaded')
ros2 = self.executable(self.root/'ros bin/ros2')
self.expect_value(self.cli('--ros-setup', ROS_SETUP=str(setup), AMENT_PREFIX_PATH='/fake',
PATH=f'{ros2.parent}:{self.env["PATH"]}'), '')
def test_cli_status_and_stdout(self):
self.expect_error(self.cli('--unknown'), 'unrecognized arguments', code=2)
self.expect_error(self.cli('--map', '--gz'), 'not allowed', code=2)
self.expect_error(self.cli('--gz', '--field-root', self.root), 'requires --map', code=2)
self.expect_error(self.cli('--bin-dir', SENTRY_BIN_DIR=str(self.root/'missing')),
'SENTRY_BIN_DIR')
with (patch.object(project_env, '_GZ_CANDIDATES', ()),
patch.object(project_env.shutil, 'which', return_value=None),
patch.dict(os.environ, {'GZ_BIN': ''})):
with self.assertRaisesRegex(project_env.ProbeError, '找不到 Gazebo'):
project_env.find_gz_bin()
def test_build_directory_priority_matches_shell(self):
build = self.root/'build'
unified = build/'bin'
def expect_bin(path, **overrides):
self.expect_value(self.cli(self.root, '--bin-dir', **overrides), path)
self.expect_value(self.shell('env_bin_dir', self.root, **overrides), path)
expect_bin(unified)
debug = self.executable(build/'a/first/debug/self_filter')
expect_bin(debug.parent)
release_last = self.executable(build/'z/last/release/nav_client')
release_first = self.executable(build/'a/second/release/odom_tf')
(build/'a/first/release').mkdir()
(build/'a/first/release/nav_client').touch()
expect_bin(release_first.parent)
(unified/'self_filter').mkdir(parents=True)
(unified/'nav_client').touch()
expect_bin(release_first.parent)
self.executable(unified/'cmd_vel_relay')
expect_bin(unified)
expect_bin(release_last.parent, NAV_BIN_DIR=str(release_last.parent))
expect_bin(release_first.parent, SENTRY_BIN_DIR=str(release_first.parent),
NAV_BIN_DIR=str(release_last.parent))
def test_gz_executable_validation_matches_shell(self):
fake = self.executable(self.root/'fake gazebo')
alias = self.root/'link to gz'
alias.symlink_to(fake)
denied = self.root/'non-executable'
denied.touch()
for path in (self.root, denied, self.root/'missing'):
with self.subTest(path=path):
self.expect_error(self.cli('--gz', GZ_BIN=str(path)), 'GZ_BIN')
self.expect_error(self.shell('env_gz_bin', GZ_BIN=str(path)), 'GZ_BIN')
self.expect_error(self.cli('--gz-version-args', GZ_FORCE_VERSION='12',
GZ_BIN=str(path)), 'GZ_BIN')
self.expect_value(self.cli('--gz', GZ_BIN=alias.name), alias)
self.expect_value(self.shell('env_gz_bin', GZ_BIN=alias.name), alias)
def test_version_and_explicit_override_matches_shell(self):
fake = self.executable(self.root/'fake gz', '''
if [[ "$*" != "sim --version" || -v LD_LIBRARY_PATH ]]; then exit 4; fi
touch "$SENTRY_GZ_MARKER"
echo ' 10.5.0 '
''')
marker = self.root/'probe-called'
env = dict(GZ_BIN=str(fake), LD_LIBRARY_PATH='/test/vendor', SENTRY_GZ_MARKER=str(marker))
self.expect_value(self.cli('--gz-version-args', **env), '--force-version 10')
self.assertTrue(marker.is_file())
self.expect_value(self.shell('env_gz_version_args', **env), '--force-version\n10')
marker.unlink()
self.expect_value(self.cli('--gz-version-args', GZ_FORCE_VERSION='12', **env),
'--force-version 12')
self.expect_value(self.shell('env_gz_version_args', GZ_FORCE_VERSION='12', **env),
'--force-version\n12')
self.assertFalse(marker.exists())
def test_version_nonzero_and_unparseable_warn_and_continue(self):
fake = self.root/'gz'
for script, reason in (("echo '10.0'; exit 7", '退出码 7'), ('echo unknown', '无法解析版本')):
self.executable(fake, script)
cli = self.cli('--gz-version-args', GZ_BIN=str(fake))
shell = self.shell('env_gz_version_args', GZ_BIN=str(fake))
self.expect_value(cli, '')
self.assertIn(reason, cli.stderr)
self.assertEqual(shell.returncode, 0, shell.stderr)
self.assertEqual(shell.stdout, '')
self.assertIn(reason, shell.stderr)
def test_version_timeout_stops_owned_process_group(self):
marker = self.root/'child-pids'
fake = self.executable(self.root/'slow gz', '''
trap '' TERM
sleep 30 &
printf '%s %s\\n' "$$" "$!" > "$SENTRY_GZ_MARKER"
wait
''')
start = time.monotonic()
result = self.shell('env_gz_version_args', GZ_BIN=str(fake), SENTRY_GZ_MARKER=str(marker))
elapsed = time.monotonic() - start
self.assertEqual(result.returncode, 0, result.stderr)
self.assertEqual(result.stdout, '')
self.assertIn('超时', result.stderr)
self.assertGreaterEqual(elapsed, 5)
self.assertLess(elapsed, 8)
for pid in marker.read_text().split():
try:
state = Path(f'/proc/{pid}/stat').read_text().rsplit(')', 1)[1].split()[0]
except FileNotFoundError:
continue
self.assertEqual(state, 'Z', f'Gazebo probe process {pid} is still live')
def test_bridge_does_not_start_without_clock(self):
fake_gz = self.executable(self.root/'fake gz', '''
[[ "$*" == 'topic -e -t /clock -n 1' && ! -v LD_LIBRARY_PATH ]] || exit 4
sleep 30
''')
fake_ros2 = self.executable(self.root/'bin/ros2', 'echo "bridge started"')
env = dict(BRIDGE_WAIT_TIMEOUT='0.3', GZ_BIN=str(fake_gz), LD_LIBRARY_PATH='/vendor',
PATH=f'{fake_ros2.parent}:{self.env["PATH"]}', GZ_PARTITION='isolated_test')
start = time.monotonic()
result = self.run_command(['bash', str(SCRIPTS/'bridge_after_gz.sh')], **env)
self.assertLess(time.monotonic() - start, 2)
self.expect_error(result, '等待 Gazebo /clock 超时')
self.assertIn('isolated_test', result.stderr)
self.executable(fake_gz, '''
[[ "$*" == 'topic -e -t /clock -n 1' && ! -v LD_LIBRARY_PATH ]] || exit 4
echo 'sim { sec: 1 }'
''')
result = self.run_command(['bash', str(SCRIPTS/'bridge_after_gz.sh')], **env)
self.assertEqual(result.returncode, 0, result.stderr)
self.assertIn('bridge started', result.stdout)
if __name__ == '__main__':
unittest.main(verbosity=2)