Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .envrc.sample
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,6 @@ eval "$(devbox generate direnv --print-envrc)"
# Use POSTMARK_API_TEST if you don't need to send real emails.
# https://postmarkapp.com/support/article/1213-best-practices-for-testing-your-emails-through-postmark
export POSTMARK_SERVER_TOKEN=POSTMARK_API_TEST

# SQLite database path
export REGISTRY_DB_PATH=./db/registry.db
2 changes: 0 additions & 2 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,3 @@ jobs:
timeout_minutes: 1
max_attempts: 3
command: devbox run test
env:
POSTMARK_SERVER_TOKEN: ${{ vars.POSTMARK_SERVER_TOKEN }}
4 changes: 2 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
.envrc
.gren
db/*.db*
dist/app
dist/test
dist/*
log/*
node_modules
Empty file added log/.gitkeep
Empty file.
1 change: 1 addition & 0 deletions process-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ version: "0.5"
processes:
server:
command: make server
log_location: ${SERVER_LOG_PATH:-./log/server.log}
12 changes: 12 additions & 0 deletions scripts/test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,18 @@
# fail immediately if app doesn't build
make dist/app || exit 1

TEST_DB_PATH=./db/test.db
TEST_LOG_PATH=./log/test.log
TEST_POSTMARK_TOKEN=POSTMARK_API_TEST

export REGISTRY_DB_PATH="$TEST_DB_PATH"
export SERVER_LOG_PATH="$TEST_LOG_PATH"
export POSTMARK_SERVER_TOKEN="$TEST_POSTMARK_TOKEN"

# clear leftover state from previous runs
rm -f "$TEST_DB_PATH"
rm -f "$TEST_LOG_PATH"

# start app in background and run all tests
devbox services up -b && \
npx -y wait-on tcp:3000 -t 5s && \
Expand Down
48 changes: 31 additions & 17 deletions src/Main.gren
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import Crypto
import Sqlite
import Dict
import FileSystem
import FileSystem.Path as Path
import HttpClient
import HttpServer exposing (Request, ServerError(..), Method(..))
import HttpServer.Response as Response exposing (Response)
Expand Down Expand Up @@ -71,6 +72,11 @@ init env =
, apiToken = token
}
)

maybeDbLocation =
envVars
|> Dict.get "REGISTRY_DB_PATH"
|> Maybe.map (\path -> Sqlite.File (Path.fromPosixString path))
in
Node.startProgram
{ model =
Expand All @@ -82,23 +88,31 @@ init env =
, postmark = postmark
}
, command =
let
initDb =
Registry.Db.open fsPerm
|> Task.andThen
(\db ->
Registry.Db.migrate db
|> Task.map (\_ -> db)
)
|> Task.attempt DbMigrationResult
in
Cmd.batch
[ initDb
, HttpServer.createServer serverPermission serverConfig
|> Task.attempt CreateServerResult
, Crypto.getSecureContext
|> Task.attempt SecureContextResult
]
when maybeDbLocation is
Nothing ->
"REGISTRY_DB_PATH environment variable is not set"
|> print env.stderr
|> Task.map (\_ -> Node.exitWithCode 1)
|> Task.executeCmd

Just dbLocation ->
let
initDb =
Registry.Db.open fsPerm dbLocation
|> Task.andThen
(\db ->
Registry.Db.migrate db
|> Task.map (\_ -> db)
)
|> Task.attempt DbMigrationResult
in
Cmd.batch
[ initDb
, HttpServer.createServer serverPermission serverConfig
|> Task.attempt CreateServerResult
, Crypto.getSecureContext
|> Task.attempt SecureContextResult
]
}


Expand Down
12 changes: 3 additions & 9 deletions src/Registry/Db.gren
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,13 @@ module Registry.Db exposing


import FileSystem
import FileSystem.Path as Path
import Sqlite
import Task exposing (Task)


open : FileSystem.Permission -> Task Sqlite.Error Sqlite.Database
open fsPerm =
let
-- TODO: get db path from environment
dbPath =
"./db/registry.db"
in
Sqlite.open fsPerm Sqlite.defaultOptions (Sqlite.File (Path.fromPosixString dbPath))
open : FileSystem.Permission -> Sqlite.Location -> Task Sqlite.Error Sqlite.Database
open fsPerm location =
Sqlite.open fsPerm Sqlite.defaultOptions location


migrate : Sqlite.Database -> Task Sqlite.Error (Array Sqlite.ExecutionSummary)
Expand Down
15 changes: 14 additions & 1 deletion src/Test/Main.gren
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
module Test.Main exposing (main)

import Dict
import Expect
import FileSystem
import FileSystem.Path as Path
import HttpClient
import Init
import Node
import Registry.Db as Db
import Sqlite
import Task
import Test
import Test.E2E
Expand All @@ -17,10 +20,20 @@ main =
Node.defineSimpleProgram <| \env ->
Init.await HttpClient.initialize <| \httpPerm ->
Init.await FileSystem.initialize <| \fsPerm ->
Init.awaitTask Node.getEnvironmentVariables <| \envVars ->
let
openDb =
when Dict.get "REGISTRY_DB_PATH" envVars is
Just path ->
Db.open fsPerm (Sqlite.File (Path.fromPosixString path))

Nothing ->
Task.fail (Sqlite.GenericError "REGISTRY_DB_PATH environment variable is not set")
in
Effectful.run env <|
Effectful.concat
[ Effectful.wrap <| Test.describe "Unit tests" Test.Unit.tests
, await "Init DB for E2E tests" (Db.open fsPerm) <| \db ->
, await "Init DB for E2E tests" openDb <| \db ->
await "Run migrations for E2E tests" (Db.migrate db) <| \_ ->
Effectful.describe "E2E tests" <|
Test.E2E.tests
Expand Down
Loading