Files
PX4-Autopilot/Tools/test_check_submodules.py
T
Benjamin Cabé 700452758a build(git): check only the submodules a build needs (#29095)
* build(git): check only the submodules a build needs

Tools/check_submodules.sh fetches every missing submodule, so a build that uses a handful of PX4 libraries still clones NuttX, the simulators and the middleware. Paths given as arguments now limit the check, and the configure step passes PX4_GIT_SUBMODULES when it is set. Builds that do not set it check all submodules as before. A path that is not a submodule, or a submodule the build uses but that is not checked out, stops the configure instead of failing later in the build.

Assisted-by: Claude:opus-5.5
Signed-off-by: Benjamin Cabé <benjamin@zephyrproject.org>

* docs(dev_setup): document PX4_GIT_SUBMODULES

The submodule check can be limited to the submodules a build needs. List the variable next to GIT_SUBMODULES_ARE_EVIL, the other way to control the check.

Assisted-by: Claude:opus-5.5
Signed-off-by: Benjamin Cabé <benjamin@zephyrproject.org>

* test(git): cover the submodule check limited to given paths

A second top-level submodule gives the tests one to leave out, so they check that paths given to check_submodules.sh limit both the fetch of missing submodules and the report of changed ones, and that a path that is not a submodule fails the check.

Assisted-by: Claude:opus-5.5
Signed-off-by: Benjamin Cabé <benjamin@zephyrproject.org>

---------

Signed-off-by: Benjamin Cabé <benjamin@zephyrproject.org>
Signed-off-by: Benjamin Cabé <benjamin@zephyrproject.org>
2026-10-23 12:14:57 -08:00

145 lines
5.8 KiB
Python

#!/usr/bin/env python3
"""Tests for check_submodules.sh against throwaway git repositories."""
import os
import shutil
import subprocess
import tempfile
import unittest
SCRIPT = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'check_submodules.sh')
GIT_ENV = {
'GIT_AUTHOR_NAME': 'test', 'GIT_AUTHOR_EMAIL': 'test@example.com',
'GIT_COMMITTER_NAME': 'test', 'GIT_COMMITTER_EMAIL': 'test@example.com',
# submodules are cloned from local paths, and test commits are never signed
'GIT_CONFIG_COUNT': '2',
'GIT_CONFIG_KEY_0': 'protocol.file.allow', 'GIT_CONFIG_VALUE_0': 'always',
'GIT_CONFIG_KEY_1': 'commit.gpgsign', 'GIT_CONFIG_VALUE_1': 'false',
}
class CheckSubmodulesTest(unittest.TestCase):
def setUp(self):
self.tmp = tempfile.mkdtemp()
self.addCleanup(shutil.rmtree, self.tmp)
self.env = {k: v for k, v in os.environ.items()
if k not in ('CI', 'GIT_SUBMODULES_ARE_EVIL') and not k.startswith('GIT_')}
self.env.update(GIT_ENV)
# lib, with a nested submodule inner, is a submodule of the checkout at
# ext/lib, next to other at ext/other
inner = self.repo('inner')
lib = self.repo('lib')
self.git(lib, 'submodule', 'add', '-q', inner, 'inner')
self.git(lib, 'commit', '-qm', 'add inner')
other = self.repo('other')
top = self.repo('top')
self.git(top, 'submodule', 'add', '-q', lib, 'ext/lib')
self.git(top, 'submodule', 'add', '-q', other, 'ext/other')
self.git(top, 'commit', '-qm', 'add lib and other')
self.checkout = os.path.join(self.tmp, 'checkout')
self.git(self.tmp, 'clone', '-q', top, self.checkout)
os.mkdir(os.path.join(self.checkout, 'Tools'))
shutil.copy(SCRIPT, os.path.join(self.checkout, 'Tools'))
self.lib = os.path.join(self.checkout, 'ext', 'lib')
self.inner = os.path.join(self.lib, 'inner')
self.other = os.path.join(self.checkout, 'ext', 'other')
def repo(self, name):
path = os.path.join(self.tmp, name)
self.git(self.tmp, 'init', '-q', name)
self.git(path, 'commit', '-q', '--allow-empty', '-m', name)
return path
def git(self, cwd, *args):
return subprocess.run(['git', *args], cwd=cwd, env=self.env, check=True,
capture_output=True, text=True).stdout.strip()
def check(self, *paths, **env):
return subprocess.run(['Tools/check_submodules.sh', *paths], cwd=self.checkout,
env={**self.env, **env}, capture_output=True, text=True)
def fetched(self, path):
return os.path.exists(os.path.join(path, '.git'))
def change(self, path):
"""Commit in a submodule, as a developer testing a submodule change would."""
self.git(path, 'commit', '-q', '--allow-empty', '-m', 'change')
return self.git(path, 'rev-parse', 'HEAD')
def test_missing_submodules_are_fetched(self):
result = self.check()
self.assertEqual(result.returncode, 0, result.stdout)
self.assertTrue(self.fetched(self.lib))
self.assertTrue(self.fetched(self.inner))
self.assertTrue(self.fetched(self.other))
def test_recorded_commits_need_nothing(self):
self.check()
result = self.check(CI='true')
self.assertEqual(result.returncode, 0, result.stdout)
self.assertEqual(result.stdout, '')
def test_missing_nested_submodule_is_fetched(self):
self.git(self.checkout, 'submodule', 'update', '--init', '-q', 'ext/lib')
self.assertFalse(self.fetched(self.inner))
self.assertEqual(self.check().returncode, 0)
self.assertTrue(self.fetched(self.inner))
def test_changed_submodule_warns_and_is_kept(self):
self.check()
head = self.change(self.lib)
result = self.check()
self.assertEqual(result.returncode, 0, result.stdout)
self.assertIn('Warning', result.stdout)
self.assertIn('ext/lib', result.stdout)
self.assertEqual(self.git(self.lib, 'rev-parse', 'HEAD'), head)
def test_changed_submodule_fails_in_ci(self):
self.check()
head = self.change(self.inner)
result = self.check(CI='true')
self.assertEqual(result.returncode, 1)
self.assertIn('ext/lib/inner', result.stdout)
self.assertEqual(self.git(self.inner, 'rev-parse', 'HEAD'), head)
def test_missing_nested_submodule_of_changed_submodule(self):
self.git(self.checkout, 'submodule', 'update', '--init', '-q', 'ext/lib')
head = self.change(self.lib)
self.assertEqual(self.check().returncode, 0)
self.assertTrue(self.fetched(self.inner))
self.assertEqual(self.git(self.lib, 'rev-parse', 'HEAD'), head)
def test_paths_limit_the_fetch(self):
result = self.check('ext/lib')
self.assertEqual(result.returncode, 0, result.stdout)
self.assertTrue(self.fetched(self.lib))
self.assertTrue(self.fetched(self.inner))
self.assertFalse(self.fetched(self.other))
def test_paths_limit_the_changed_check(self):
self.check()
self.change(self.other)
result = self.check('ext/lib', CI='true')
self.assertEqual(result.returncode, 0, result.stdout)
self.assertEqual(result.stdout, '')
self.assertEqual(self.check('ext/other', CI='true').returncode, 1)
def test_path_not_a_submodule_fails(self):
result = self.check('ext/lib', 'ext/none')
self.assertEqual(result.returncode, 1, result.stdout)
self.assertIn('ext/none', result.stdout)
self.assertFalse(self.fetched(self.lib))
def test_git_submodules_are_evil_skips_everything(self):
result = self.check(GIT_SUBMODULES_ARE_EVIL='1')
self.assertEqual(result.returncode, 0)
self.assertFalse(self.fetched(self.lib))
if __name__ == '__main__':
unittest.main()