be0c12
From 6fbbf368f5a6d181b21f448255d5a4182dc2ab3a Mon Sep 17 00:00:00 2001
be0c12
From: Frantisek Sumsal <frantisek@sumsal.cz>
be0c12
Date: Mon, 29 Nov 2021 13:00:21 +0100
be0c12
Subject: [PATCH] tests: add helper function to autodetect CI environments
be0c12
be0c12
Sadly there is no standarized way to check if we're running in some
be0c12
CI environment. So let's try to gather the heuristics in one helper
be0c12
function.
be0c12
be0c12
Loosely cherry-picked from 4eb0c875f8825199a829ddc597874915fbee0a84.
be0c12
be0c12
Related: #2017033
be0c12
---
be0c12
 src/basic/string-util.h |  6 ++++++
be0c12
 src/shared/tests.c      | 42 +++++++++++++++++++++++++++++++++++++++++
be0c12
 src/shared/tests.h      |  3 +++
be0c12
 3 files changed, 51 insertions(+)
be0c12
be0c12
diff --git a/src/basic/string-util.h b/src/basic/string-util.h
be0c12
index 96a9260f93..742b566932 100644
be0c12
--- a/src/basic/string-util.h
be0c12
+++ b/src/basic/string-util.h
be0c12
@@ -32,6 +32,12 @@ static inline bool streq_ptr(const char *a, const char *b) {
be0c12
         return strcmp_ptr(a, b) == 0;
be0c12
 }
be0c12
 
be0c12
+static inline char* strstr_ptr(const char *haystack, const char *needle) {
be0c12
+        if (!haystack || !needle)
be0c12
+                return NULL;
be0c12
+        return strstr(haystack, needle);
be0c12
+}
be0c12
+
be0c12
 static inline const char* strempty(const char *s) {
be0c12
         return s ?: "";
be0c12
 }
be0c12
diff --git a/src/shared/tests.c b/src/shared/tests.c
be0c12
index 100b62b9b0..1da80d653f 100644
be0c12
--- a/src/shared/tests.c
be0c12
+++ b/src/shared/tests.c
be0c12
@@ -7,7 +7,9 @@
be0c12
 #include <util.h>
be0c12
 
be0c12
 #include "tests.h"
be0c12
+#include "env-util.h"
be0c12
 #include "path-util.h"
be0c12
+#include "strv.h"
be0c12
 
be0c12
 char* setup_fake_runtime_dir(void) {
be0c12
         char t[] = "/tmp/fake-xdg-runtime-XXXXXX", *p;
be0c12
@@ -75,3 +77,43 @@ void test_setup_logging(int level) {
be0c12
         log_parse_environment();
be0c12
         log_open();
be0c12
 }
be0c12
+
be0c12
+const char *ci_environment(void) {
be0c12
+        /* We return a string because we might want to provide multiple bits of information later on: not
be0c12
+         * just the general CI environment type, but also whether we're sanitizing or not, etc. The caller is
be0c12
+         * expected to use strstr on the returned value. */
be0c12
+        static const char *ans = (void*) UINTPTR_MAX;
be0c12
+        const char *p;
be0c12
+        int r;
be0c12
+
be0c12
+        if (ans != (void*) UINTPTR_MAX)
be0c12
+                return ans;
be0c12
+
be0c12
+        /* We allow specifying the environment with $CITYPE. Nobody uses this so far, but we are ready. */
be0c12
+        p = getenv("CITYPE");
be0c12
+        if (!isempty(p))
be0c12
+                return (ans = p);
be0c12
+
be0c12
+        if (getenv_bool("TRAVIS") > 0)
be0c12
+                return (ans = "travis");
be0c12
+        if (getenv_bool("SEMAPHORE") > 0)
be0c12
+                return (ans = "semaphore");
be0c12
+        if (getenv_bool("GITHUB_ACTIONS") > 0)
be0c12
+                return (ans = "github-actions");
be0c12
+        if (getenv("AUTOPKGTEST_ARTIFACTS") || getenv("AUTOPKGTEST_TMP"))
be0c12
+                return (ans = "autopkgtest");
be0c12
+
be0c12
+        FOREACH_STRING(p, "CI", "CONTINOUS_INTEGRATION") {
be0c12
+                /* Those vars are booleans according to Semaphore and Travis docs:
be0c12
+                 * https://docs.travis-ci.com/user/environment-variables/#default-environment-variables
be0c12
+                 * https://docs.semaphoreci.com/ci-cd-environment/environment-variables/#ci
be0c12
+                 */
be0c12
+                r = getenv_bool(p);
be0c12
+                if (r > 0)
be0c12
+                        return (ans = "unknown"); /* Some other unknown thing */
be0c12
+                if (r == 0)
be0c12
+                        return (ans = NULL);
be0c12
+        }
be0c12
+
be0c12
+        return (ans = NULL);
be0c12
+}
be0c12
diff --git a/src/shared/tests.h b/src/shared/tests.h
be0c12
index 3d696d02fd..4f8f349097 100644
be0c12
--- a/src/shared/tests.h
be0c12
+++ b/src/shared/tests.h
be0c12
@@ -5,3 +5,6 @@ char* setup_fake_runtime_dir(void);
be0c12
 bool test_is_running_from_builddir(char **exedir);
be0c12
 const char* get_testdata_dir(void);
be0c12
 void test_setup_logging(int level);
be0c12
+
be0c12
+/* Provide a convenient way to check if we're running in CI. */
be0c12
+const char *ci_environment(void);