twister: increase resilience when running outside of git.

While the code already includes some provisions to allow running outside
of a git checkout, attempting that right now causes a failure to generate
the XML report, as the JSON report creates a null `zephyr_version` field.

The reason for that is that the original code doesn't set `self.version`
when the subprocess returns a non-zero status (and also doesn't log.)

Instead of having to set `self.version` for all failure cases, initialize
it to the failure state, and log if it hasn't changed from that.

Signed-off-by: Diego Elio Pettenò <flameeyes@meta.com>
This commit is contained in:
Diego Elio Pettenò 2023-08-22 12:39:38 +01:00 committed by Carles Cufí
commit d9fe63b188
2 changed files with 30 additions and 17 deletions

View file

@ -786,9 +786,9 @@ def parse_arguments(parser, args, options = None):
class TwisterEnv: class TwisterEnv:
def __init__(self, options=None) -> None: def __init__(self, options=None) -> None:
self.version = None self.version = "Unknown"
self.toolchain = None self.toolchain = None
self.commit_date = None self.commit_date = "Unknown"
self.run_date = None self.run_date = None
self.options = options self.options = options
@ -834,20 +834,21 @@ class TwisterEnv:
if _version: if _version:
self.version = _version self.version = _version
logger.info(f"Zephyr version: {self.version}") logger.info(f"Zephyr version: {self.version}")
else:
self.version = "Unknown"
logger.error("Could not determine version")
except OSError: except OSError:
logger.info("Cannot read zephyr version.") logger.exception("Failure while reading Zephyr version.")
subproc = subprocess.run(["git", "show", "-s", "--format=%cI", "HEAD"], if self.version == "Unknown":
stdout=subprocess.PIPE, logger.warning("Could not determine version")
universal_newlines=True,
cwd=ZEPHYR_BASE) try:
if subproc.returncode == 0: subproc = subprocess.run(["git", "show", "-s", "--format=%cI", "HEAD"],
self.commit_date = subproc.stdout.strip() stdout=subprocess.PIPE,
else: universal_newlines=True,
self.commit_date = "Unknown" cwd=ZEPHYR_BASE)
if subproc.returncode == 0:
self.commit_date = subproc.stdout.strip()
except OSError:
logger.exception("Failure while reading head commit date.")
@staticmethod @staticmethod
def run_cmake_script(args=[]): def run_cmake_script(args=[]):

View file

@ -368,11 +368,18 @@ TESTDATA_4 = [
'Unknown', 'Unknown',
'dummy stdout date' 'dummy stdout date'
), ),
(
mock.Mock(returncode=1, stdout='dummy stdout version'),
mock.Mock(returncode=0, stdout='dummy stdout date'),
['Could not determine version'],
'Unknown',
'dummy stdout date'
),
( (
OSError, OSError,
mock.Mock(returncode=1), mock.Mock(returncode=1),
['Cannot read zephyr version.'], ['Could not determine version'],
None, 'Unknown',
'Unknown' 'Unknown'
), ),
] ]
@ -382,7 +389,12 @@ TESTDATA_4 = [
'git_describe_return, git_show_return, expected_logs,' \ 'git_describe_return, git_show_return, expected_logs,' \
' expected_version, expected_commit_date', ' expected_version, expected_commit_date',
TESTDATA_4, TESTDATA_4,
ids=['valid', 'no zephyr version on describe', 'error on git describe'] ids=[
'valid',
'no zephyr version on describe',
'error on git describe',
'execution error on git describe',
]
) )
def test_twisterenv_check_zephyr_version( def test_twisterenv_check_zephyr_version(
caplog, caplog,