Skip to content

Commit 7312884

Browse files
committed
added setup file for test
1 parent f0ca18b commit 7312884

4 files changed

Lines changed: 45 additions & 4 deletions

File tree

docker-compose.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
services:
22
postgres:
3-
image: postgres
3+
image: postgres:16-alpine
44
container_name: duron-postgres
55
environment:
66
POSTGRES_USER: duron

packages/duron/bunfig.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
[test]
2+
timeout = 20000 # 10 seconds
3+
preload = ["./test/setup.ts"]

packages/duron/test/docker.ts

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,31 @@ interface CreateContainerOptions {
1414
environment?: Record<string, string>
1515
}
1616

17-
async function createContainer({ image, containerName, ports, environment }: CreateContainerOptions) {
17+
async function ensureImageExists(image: string) {
18+
try {
19+
// Check if image exists locally
20+
const { stdout } = await execFile('docker', ['images', '-q', image])
21+
if (stdout.toString().trim()) {
22+
return // Image exists, no need to pull
23+
}
24+
} catch {
25+
// Image doesn't exist or error checking, proceed to pull
26+
}
27+
28+
// Pull the image if it doesn't exist
29+
await execFile('docker', ['pull', image])
30+
}
31+
32+
export async function createContainer({ image, containerName, ports, environment }: CreateContainerOptions) {
1833
if (containersCreated.has(containerName)) {
1934
return containersCreated.get(containerName)!
2035
}
2136

2237
const promise = (async () => {
2338
try {
39+
// Ensure the image exists before running
40+
await ensureImageExists(image)
41+
2442
await execFile('docker', [
2543
'run',
2644
'--name',
@@ -50,7 +68,7 @@ async function createContainer({ image, containerName, ports, environment }: Cre
5068
return promise
5169
}
5270

53-
async function waitForContainer(containerName: string, expectedMessage: string) {
71+
export async function waitForContainer(containerName: string, expectedMessage: string) {
5472
if (containersStarted.has(containerName)) {
5573
return containersStarted.get(containerName)!
5674
}
@@ -82,7 +100,7 @@ async function waitForContainer(containerName: string, expectedMessage: string)
82100

83101
export const getPostgresConnection = async ({ containerName, port }: { containerName: string; port: number }) => {
84102
await createContainer({
85-
image: 'postgres',
103+
image: 'postgres:16-alpine',
86104
containerName,
87105
ports: [port, 5432],
88106
environment: {

packages/duron/test/setup.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import { createContainer, waitForContainer } from './docker.js'
2+
3+
console.log('🔄 Creating postgres container...')
4+
5+
await createContainer({
6+
image: 'postgres:16-alpine',
7+
containerName: 'duron-postgres-test',
8+
ports: [5440, 5432],
9+
environment: {
10+
POSTGRES_USER: 'duron',
11+
POSTGRES_PASSWORD: 'duron',
12+
POSTGRES_DB: 'duron',
13+
},
14+
})
15+
16+
console.log('✅ Postgres container created')
17+
18+
await waitForContainer('duron-postgres-test', 'PostgreSQL init process complete')
19+
20+
console.log('✅ Postgres container started')

0 commit comments

Comments
 (0)