diff --git a/.envrc.sample b/.envrc.sample index 8a918ec..8289b2c 100644 --- a/.envrc.sample +++ b/.envrc.sample @@ -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 diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index ec05bb5..54ab43c 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -20,5 +20,3 @@ jobs: timeout_minutes: 1 max_attempts: 3 command: devbox run test - env: - POSTMARK_SERVER_TOKEN: ${{ vars.POSTMARK_SERVER_TOKEN }} diff --git a/.gitignore b/.gitignore index b48e3d5..65d68a0 100644 --- a/.gitignore +++ b/.gitignore @@ -1,6 +1,6 @@ .envrc .gren db/*.db* -dist/app -dist/test +dist/* +log/* node_modules diff --git a/log/.gitkeep b/log/.gitkeep new file mode 100644 index 0000000..e69de29 diff --git a/process-compose.yml b/process-compose.yml index bf658ca..f69fe3f 100644 --- a/process-compose.yml +++ b/process-compose.yml @@ -4,3 +4,4 @@ version: "0.5" processes: server: command: make server + log_location: ${SERVER_LOG_PATH:-./log/server.log} diff --git a/scripts/test.sh b/scripts/test.sh index 271b498..2c37926 100755 --- a/scripts/test.sh +++ b/scripts/test.sh @@ -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 && \ diff --git a/src/Main.gren b/src/Main.gren index f518976..ef629ac 100644 --- a/src/Main.gren +++ b/src/Main.gren @@ -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) @@ -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 = @@ -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 + ] } diff --git a/src/Registry/Db.gren b/src/Registry/Db.gren index 4890f0d..6d13922 100644 --- a/src/Registry/Db.gren +++ b/src/Registry/Db.gren @@ -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) diff --git a/src/Test/Main.gren b/src/Test/Main.gren index a76ed70..fc7ffda 100644 --- a/src/Test/Main.gren +++ b/src/Test/Main.gren @@ -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 @@ -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