66import subprocess
77import time
88import secrets
9+ import os
910
1011
1112def get_test_redis_url (port : int ):
@@ -28,6 +29,11 @@ def get_test_db_info():
2829 }
2930
3031
32+ def is_ci_environment ():
33+ """Check if we're running in a CI environment where Docker may not be available."""
34+ return os .environ .get ("CI" ) == "true" or os .environ .get ("GITHUB_ACTIONS" ) == "true"
35+
36+
3137def _short_random_string () -> str :
3238 """Returns a random string of 6 lowercase letters."""
3339 return "" .join (random .choice (string .ascii_lowercase ) for i in range (6 ))
@@ -50,7 +56,44 @@ def _execute_sql(url: str, sql: str):
5056@pytest .fixture (scope = "session" )
5157def db_server ():
5258 """Starts a DB server and creates a template DB once per session."""
53-
59+
60+ # In CI environments, use the service containers provided by GitHub Actions
61+ if is_ci_environment ():
62+ # GitHub Actions provides PostgreSQL service container
63+ db_url = "postgresql://postgres:test@localhost:5432"
64+
65+ # Create a template database and load data
66+ template_db = f"kernelboard_template_{ _short_random_string ()} "
67+ _execute_sql (db_url , f"CREATE DATABASE { template_db } " )
68+
69+ # Load data.sql into the template database using psql
70+ result = subprocess .run (
71+ [
72+ "psql" ,
73+ "-h" ,
74+ "localhost" ,
75+ "-U" ,
76+ "postgres" ,
77+ "-p" ,
78+ "5432" ,
79+ "-d" ,
80+ template_db ,
81+ "-f" ,
82+ "tests/data.sql" ,
83+ ],
84+ env = {"PGPASSWORD" : "test" },
85+ )
86+
87+ if result .returncode != 0 :
88+ pytest .exit ("Error loading data.sql in CI" , returncode = 1 )
89+
90+ yield {"db_url" : db_url , "db_name" : template_db }
91+
92+ # Cleanup: drop the template database
93+ _execute_sql (db_url , f"DROP DATABASE { template_db } " )
94+ return
95+
96+ # Original Docker-based logic for local development
5497 container_name = f"kernelboard_db_{ _short_random_string ()} "
5598
5699 test_db = get_test_db_info ()
@@ -166,7 +209,14 @@ def db_server():
166209def redis_server ():
167210 """
168211 Starts a Redis Docker container for the test session.
212+ In CI environments, uses the service container provided by GitHub Actions.
169213 """
214+
215+ # In CI environments, use the service containers provided by GitHub Actions
216+ if is_ci_environment ():
217+ yield "redis://localhost:6379/0"
218+ return
219+
170220 container_name = f"kernelboard_redis_{ _short_random_string ()} "
171221 port = get_test_redis_port ()
172222 redis_url = get_test_redis_url (port )
@@ -301,6 +351,12 @@ def runner(app):
301351
302352@pytest .fixture (autouse = True )
303353def set_env (monkeypatch ):
354+ # In CI environments, rely on environment variables set by GitHub Actions
355+ if is_ci_environment ():
356+ # Don't override environment variables in CI mode - use what's already set
357+ return
358+
359+ # For local development, set the traditional test environment variables
304360 monkeypatch .setenv ("DATABASE_URL" , get_test_db_info ()["db_url" ])
305361 monkeypatch .setenv ("DISCORD_CLIENT_ID" , "test" )
306362 monkeypatch .setenv ("DISCORD_CLIENT_SECRET" , "test" )
0 commit comments