From 304d809590bd25f53a373029ec37006a46b9699f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pablo=20Barb=C3=A1chano?= Date: Wed, 30 Aug 2023 20:55:31 +0200 Subject: [PATCH] tests: allow running outside of a git repository MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Make the git information for metrics optional. Useful when running outside of a classic git repository, for example extracted from a tarball or a git worktree. Signed-off-by: Pablo Barbáchano --- tests/framework/properties.py | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/tests/framework/properties.py b/tests/framework/properties.py index d934e5d62962..036d3743b424 100644 --- a/tests/framework/properties.py +++ b/tests/framework/properties.py @@ -72,13 +72,19 @@ def __init__(self): self.os = get_os_version() self.host_os = get_host_os() self.libc_ver = "-".join(platform.libc_ver()) - self.git_commit_id = run_cmd("git rev-parse HEAD") - self.git_branch = run_cmd("git show -s --pretty=%D HEAD") - self.git_origin_url = run_cmd("git config --get remote.origin.url") self.rust_version = run_cmd("rustc --version |awk '{print $2}'") self.buildkite_pipeline_slug = os.environ.get("BUILDKITE_PIPELINE_SLUG") self.buildkite_build_number = os.environ.get("BUILDKITE_BUILD_NUMBER") + if self._in_git_repo(): + self.git_commit_id = run_cmd("git rev-parse HEAD") + self.git_branch = run_cmd("git show -s --pretty=%D HEAD") + self.git_origin_url = run_cmd("git config --get remote.origin.url") + else: + self.git_commit_id = None + self.git_branch = None + self.git_origin_url = None + self.environment = self._detect_environment() if self.is_ec2: self.instance = imdsv2_get("/meta-data/instance-type") @@ -105,6 +111,12 @@ def _detect_environment(self): except Exception: return "local" + def _in_git_repo(self): + try: + run_cmd("git rev-parse --show-toplevel") + except subprocess.CalledProcessError: + return False + return True + global_props = GlobalProps() -# TBD could do a props fixture for tests to use...