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

View file

@ -368,11 +368,18 @@ TESTDATA_4 = [
'Unknown',
'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,
mock.Mock(returncode=1),
['Cannot read zephyr version.'],
None,
['Could not determine version'],
'Unknown',
'Unknown'
),
]
@ -382,7 +389,12 @@ TESTDATA_4 = [
'git_describe_return, git_show_return, expected_logs,' \
' expected_version, expected_commit_date',
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(
caplog,