aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/test/lib/test_repo
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--test/lib/test_repo.py48
-rwxr-xr-xtest/lib/test_repo/ci.sh24
2 files changed, 72 insertions, 0 deletions
diff --git a/test/lib/test_repo.py b/test/lib/test_repo.py
new file mode 100644
index 0000000..f12b374
--- /dev/null
+++ b/test/lib/test_repo.py
@@ -0,0 +1,48 @@
+# Copyright (c) 2023 Egor Tensin <Egor.Tensin@gmail.com>
+# This file is part of the "cimple" project.
+# For details, see https://github.com/egor-tensin/cimple.
+# Distributed under the MIT License.
+
+import logging
+import os
+import shutil
+
+from .process import run
+
+
+class Repo:
+ BRANCH = 'main'
+
+ def __init__(self, path):
+ self.path = os.path.abspath(path)
+ os.makedirs(path, exist_ok=True)
+ self.run('git', 'init', f'--initial-branch={Repo.BRANCH}')
+ self.run('git', 'config', 'user.name', 'Test User')
+ self.run('git', 'config', 'user.email', 'test@example.com')
+
+ def run(self, *args, **kwargs):
+ run(*args, cwd=self.path, **kwargs)
+
+
+class TestRepo(Repo):
+ # Prevent Pytest from discovering test cases in this class:
+ __test__ = False
+
+ DATA_DIR = 'test_repo'
+ CI_SCRIPT = 'ci.sh'
+ OUTPUT_DIR = 'output'
+
+ @staticmethod
+ def get_ci_script():
+ return os.path.join(os.path.dirname(__file__), TestRepo.DATA_DIR, TestRepo.CI_SCRIPT)
+
+ def __init__(self, path):
+ super().__init__(path)
+ shutil.copy(self.get_ci_script(), self.path)
+ self.run('git', 'add', '.')
+ self.run('git', 'commit', '-m', 'add CI script')
+ self.output_dir = os.path.join(self.path, TestRepo.OUTPUT_DIR)
+ os.makedirs(self.output_dir, exist_ok=True)
+
+ def count_ci_output_files(self):
+ return len([name for name in os.listdir(self.output_dir) if os.path.isfile(os.path.join(self.output_dir, name))])
diff --git a/test/lib/test_repo/ci.sh b/test/lib/test_repo/ci.sh
new file mode 100755
index 0000000..804cdb3
--- /dev/null
+++ b/test/lib/test_repo/ci.sh
@@ -0,0 +1,24 @@
+#!/usr/bin/env bash
+
+set -o errexit -o nounset -o pipefail
+shopt -s inherit_errexit lastpipe
+
+script_dir="$( dirname -- "${BASH_SOURCE[0]}" )"
+script_dir="$( cd -- "$script_dir" && pwd )"
+readonly script_dir
+
+ci_output_dir="$( git remote get-url origin )"
+ci_output_dir="$ci_output_dir/output"
+readonly ci_output_dir
+
+mkdir -p -- "$ci_output_dir"
+
+readonly ci_output_template=ci_XXXXXX
+
+ci_output_path="$( mktemp --tmpdir="$ci_output_dir" "$ci_output_template" )"
+readonly ci_output_path
+
+timestamp="$( date --iso-8601=ns )"
+readonly timestamp
+
+echo "A CI run happened at $timestamp" | tee -- "$ci_output_path"