feat: upgrade purs-nix and change mock server to httpurple

This commit is contained in:
klarkc 2024-07-11 14:26:00 -03:00
parent 483453d463
commit ddaad01eed
No known key found for this signature in database
GPG Key ID: A9688C165077FB02
3 changed files with 142 additions and 271 deletions

View File

@ -1,26 +1,5 @@
{
"nodes": {
"deadnix": {
"inputs": {
"fenix": "fenix",
"naersk": "naersk",
"nixpkgs": "nixpkgs_5",
"utils": "utils"
},
"locked": {
"lastModified": 1656370114,
"narHash": "sha256-XBbSWeBuF6Ck0jc634yAp2hjPXWM2JyRDPCdK0dh3w4=",
"owner": "astro",
"repo": "deadnix",
"rev": "9f450f7250ad7680cb5f12ce5985cc18496c2d5f",
"type": "github"
},
"original": {
"owner": "astro",
"repo": "deadnix",
"type": "github"
}
},
"docs-search": {
"flake": false,
"locked": {
@ -37,32 +16,13 @@
"type": "github"
}
},
"fenix": {
"inputs": {
"nixpkgs": "nixpkgs_3",
"rust-analyzer-src": "rust-analyzer-src"
},
"locked": {
"lastModified": 1655533500,
"narHash": "sha256-qJJmLVoMYfDLywI9MNL7sb0W/GsKQF9HDatdHm1tSl0=",
"owner": "nix-community",
"repo": "fenix",
"rev": "b6630603af13df17d0dd4df8629e9a24e6ba0fbd",
"type": "github"
},
"original": {
"owner": "nix-community",
"repo": "fenix",
"type": "github"
}
},
"flake-utils": {
"locked": {
"lastModified": 1618217525,
"narHash": "sha256-WGrhVczjXTiswQaoxQ+0PTfbLNeOQM6M36zvLn78AYg=",
"lastModified": 1644229661,
"narHash": "sha256-1YdnJAsNy69bpcjuoKdOYQX0YxZBiCYZo4Twxerqv7k=",
"owner": "numtide",
"repo": "flake-utils",
"rev": "c6169a2772643c4a93a0b5ac1c61e296cba68544",
"rev": "3cecb5b042f7f209c56ffd8371b2711a290ec797",
"type": "github"
},
"original": {
@ -101,6 +61,21 @@
"type": "github"
}
},
"flake-utils_4": {
"locked": {
"lastModified": 1618217525,
"narHash": "sha256-WGrhVczjXTiswQaoxQ+0PTfbLNeOQM6M36zvLn78AYg=",
"owner": "numtide",
"repo": "flake-utils",
"rev": "c6169a2772643c4a93a0b5ac1c61e296cba68544",
"type": "github"
},
"original": {
"owner": "numtide",
"repo": "flake-utils",
"type": "github"
}
},
"get-flake": {
"locked": {
"lastModified": 1644686428,
@ -116,6 +91,44 @@
"type": "github"
}
},
"httpurple-argonaut": {
"flake": false,
"locked": {
"lastModified": 1655138308,
"narHash": "sha256-tWTgc2gERJLkv+XzK4HtqJJbqvw10svVu1cmXFADtls=",
"owner": "sigma-andex",
"repo": "purescript-httpurple-argonaut",
"rev": "3187fba4fe0682849b4acbf215e6a6fc1f6493de",
"type": "github"
},
"original": {
"owner": "sigma-andex",
"repo": "purescript-httpurple-argonaut",
"type": "github"
}
},
"lint-utils": {
"inputs": {
"flake-utils": "flake-utils",
"nixpkgs": [
"purs-nix",
"nixpkgs"
]
},
"locked": {
"lastModified": 1707777931,
"narHash": "sha256-PsPb5xMBZ9dPDP04o9vqKEUIEG80Z84/74fPuOMs0ZI=",
"owner": "homotopic",
"repo": "lint-utils",
"rev": "5f11e3e51d8f1aa4ed62a89e90f05953931e105a",
"type": "github"
},
"original": {
"owner": "homotopic",
"repo": "lint-utils",
"type": "github"
}
},
"make-shell": {
"locked": {
"lastModified": 1634940815,
@ -148,31 +161,13 @@
"type": "github"
}
},
"naersk": {
"inputs": {
"nixpkgs": "nixpkgs_4"
},
"locked": {
"lastModified": 1655042882,
"narHash": "sha256-9BX8Fuez5YJlN7cdPO63InoyBy7dm3VlJkkmTt6fS1A=",
"owner": "nix-community",
"repo": "naersk",
"rev": "cddffb5aa211f50c4b8750adbec0bbbdfb26bb9f",
"type": "github"
},
"original": {
"owner": "nix-community",
"repo": "naersk",
"type": "github"
}
},
"nixpkgs": {
"locked": {
"lastModified": 1703499205,
"narHash": "sha256-lF9rK5mSUfIZJgZxC3ge40tp1gmyyOXZ+lRY3P8bfbg=",
"lastModified": 1704161960,
"narHash": "sha256-QGua89Pmq+FBAro8NriTuoO/wNaUtugt29/qqA8zeeM=",
"owner": "NixOS",
"repo": "nixpkgs",
"rev": "e1fa12d4f6c6fe19ccb59cac54b5b3f25e160870",
"rev": "63143ac2c9186be6d9da6035fa22620018c85932",
"type": "github"
},
"original": {
@ -183,66 +178,6 @@
}
},
"nixpkgs_2": {
"locked": {
"lastModified": 1646506091,
"narHash": "sha256-sWNAJE2m+HOh1jtXlHcnhxsj6/sXrHgbqVNcVRlveK4=",
"owner": "NixOS",
"repo": "nixpkgs",
"rev": "3e644bd62489b516292c816f70bf0052c693b3c7",
"type": "github"
},
"original": {
"owner": "NixOS",
"ref": "nixpkgs-unstable",
"repo": "nixpkgs",
"type": "github"
}
},
"nixpkgs_3": {
"locked": {
"lastModified": 1655400192,
"narHash": "sha256-49OBVVRgb9H/PSmNT9W61+NRdDbuSJVuDDflwXlaUKU=",
"owner": "nixos",
"repo": "nixpkgs",
"rev": "3d7435c638baffaa826b85459df0fff47f12317d",
"type": "github"
},
"original": {
"owner": "nixos",
"ref": "nixos-unstable",
"repo": "nixpkgs",
"type": "github"
}
},
"nixpkgs_4": {
"locked": {
"lastModified": 1655481042,
"narHash": "sha256-XHbcywq2vIQ5CeH1OK3TN793jkiNAAZsSctS1PFgseo=",
"owner": "NixOS",
"repo": "nixpkgs",
"rev": "103a4c0ae46afa9cf008c30744175315ca38e9f9",
"type": "github"
},
"original": {
"id": "nixpkgs",
"type": "indirect"
}
},
"nixpkgs_5": {
"locked": {
"lastModified": 1655481042,
"narHash": "sha256-XHbcywq2vIQ5CeH1OK3TN793jkiNAAZsSctS1PFgseo=",
"owner": "NixOS",
"repo": "nixpkgs",
"rev": "103a4c0ae46afa9cf008c30744175315ca38e9f9",
"type": "github"
},
"original": {
"id": "nixpkgs",
"type": "indirect"
}
},
"nixpkgs_6": {
"locked": {
"lastModified": 1656549732,
"narHash": "sha256-eILutFZGjfk2bEzfim8S/qyYc//0S1KsCeO+OWbtoR0=",
@ -291,17 +226,16 @@
},
"ps-tools": {
"inputs": {
"deadnix": "deadnix",
"make-shell": "make-shell_2",
"nixpkgs": "nixpkgs_6",
"utils": "utils_2"
"nixpkgs": "nixpkgs_2",
"utils": "utils"
},
"locked": {
"lastModified": 1691537451,
"narHash": "sha256-9dnX6E7eWVm8xhHDeA3AmkvhqcWRo6RJ4z6Vrn65cFs=",
"lastModified": 1704567308,
"narHash": "sha256-WbFPIkKLtyQOPBUjintckKIYnfs7MvIbmfVsLRSAPlc=",
"owner": "purs-nix",
"repo": "purescript-tools",
"rev": "675cd35bc9255f9c2b1f6f6784ef41b9773202e8",
"rev": "ac626313141cbee78f06eb3c5e90359f695aef9b",
"type": "github"
},
"original": {
@ -314,91 +248,44 @@
"inputs": {
"docs-search": "docs-search",
"get-flake": "get-flake",
"lint-utils": "lint-utils",
"make-shell": "make-shell",
"nixpkgs": "nixpkgs_2",
"nixpkgs": "nixpkgs",
"parsec": "parsec",
"ps-tools": "ps-tools",
"utils": "utils_3"
"utils": "utils_2"
},
"locked": {
"lastModified": 1691603627,
"narHash": "sha256-bsJh6PCDF9+hSuk/dbTtSXdma0VvjFejN6weHPt7cyU=",
"lastModified": 1711996073,
"narHash": "sha256-NDV8q9qtpKjHgIcMVBgdNqEfhpfK1d19rHYNa7yq0Qs=",
"owner": "purs-nix",
"repo": "purs-nix",
"rev": "8729b7fbb02822df3fb3988bae94b276e50ca6fc",
"rev": "8caa40f7fe69b9fcd05baae04c4ec9eb43953e2e",
"type": "github"
},
"original": {
"owner": "purs-nix",
"ref": "ps-0.15",
"repo": "purs-nix",
"type": "github"
}
},
"root": {
"inputs": {
"nixpkgs": "nixpkgs",
"httpurple-argonaut": "httpurple-argonaut",
"nixpkgs": [
"purs-nix",
"nixpkgs"
],
"npmlock2nix": "npmlock2nix",
"ps-tools": [
"purs-nix",
"ps-tools"
],
"purs-nix": "purs-nix",
"utils": "utils_4"
}
},
"rust-analyzer-src": {
"flake": false,
"locked": {
"lastModified": 1655507737,
"narHash": "sha256-o+AqNsjL6o2RHh4InZHQVpkmqg570YFJL4Db8mKq+fs=",
"owner": "rust-lang",
"repo": "rust-analyzer",
"rev": "12dd81092e37df28b7a3591cae9675e668927198",
"type": "github"
},
"original": {
"owner": "rust-lang",
"ref": "nightly",
"repo": "rust-analyzer",
"type": "github"
"utils": "utils_3"
}
},
"utils": {
"locked": {
"lastModified": 1653893745,
"narHash": "sha256-0jntwV3Z8//YwuOjzhV2sgJJPt+HY6KhU7VZUL0fKZQ=",
"owner": "numtide",
"repo": "flake-utils",
"rev": "1ed9fb1935d260de5fe1c2f7ee0ebaae17ed2fa1",
"type": "github"
},
"original": {
"owner": "numtide",
"repo": "flake-utils",
"type": "github"
}
},
"utils_2": {
"inputs": {
"flake-utils": "flake-utils"
},
"locked": {
"lastModified": 1656044990,
"narHash": "sha256-f01BB7CaOyntOab9XnpH9HD63rGcnu2iyL4M2ubs5F8=",
"owner": "ursi",
"repo": "flake-utils",
"rev": "f53b674a2c90f6202a2f4cd491aba121775490b5",
"type": "github"
},
"original": {
"owner": "ursi",
"ref": "8",
"repo": "flake-utils",
"type": "github"
}
},
"utils_3": {
"inputs": {
"flake-utils": "flake-utils_2"
},
@ -417,10 +304,29 @@
"type": "github"
}
},
"utils_4": {
"utils_2": {
"inputs": {
"flake-utils": "flake-utils_3"
},
"locked": {
"lastModified": 1656044990,
"narHash": "sha256-f01BB7CaOyntOab9XnpH9HD63rGcnu2iyL4M2ubs5F8=",
"owner": "ursi",
"repo": "flake-utils",
"rev": "f53b674a2c90f6202a2f4cd491aba121775490b5",
"type": "github"
},
"original": {
"owner": "ursi",
"ref": "8",
"repo": "flake-utils",
"type": "github"
}
},
"utils_3": {
"inputs": {
"flake-utils": "flake-utils_4"
},
"locked": {
"lastModified": 1658606827,
"narHash": "sha256-3kSWTDpV4+uApt/Xd0kv2m7TR1Y6A7SgYUeX86/qYuU=",

View File

@ -1,12 +1,18 @@
{
inputs =
{
nixpkgs.url = "github:NixOS/nixpkgs/nixpkgs-unstable";
npmlock2nix.url = "github:nix-community/npmlock2nix";
npmlock2nix.flake = false;
ps-tools.follows = "purs-nix/ps-tools";
purs-nix.url = "github:purs-nix/purs-nix/ps-0.15";
purs-nix.url = "github:purs-nix/purs-nix";
nixpkgs.follows = "purs-nix/nixpkgs";
utils.url = "github:ursi/flake-utils";
ps-tools.follows = "purs-nix/ps-tools";
npmlock2nix = {
url = "github:nix-community/npmlock2nix";
flake = false;
};
httpurple-argonaut = {
url = "github:sigma-andex/purescript-httpurple-argonaut";
flake = false;
};
};
outputs = { self, utils, ... }@inputs:
@ -15,25 +21,24 @@
inherit inputs;
# Limited by ps-tools
systems = [ "x86_64-linux" "x86_64-darwin" ];
make-pkgs = system: import inputs.nixpkgs {
inherit system;
# required by npmlock2nix
config.permittedInsecurePackages = [
"nodejs-16.20.2"
];
};
}
({ pkgs, system, ... }:
({ pkgs, system, ... }@ctx:
let
inherit (pkgs) nodejs;
npm = import inputs.npmlock2nix { inherit pkgs; };
node_modules = npm.v2.node_modules { src = ./.; inherit nodejs; } + /node_modules;
ps-tools = inputs.ps-tools.legacyPackages.${system};
inherit (ps-tools.for-0_15) purescript purs-tidy purescript-language-server;
inherit (ctx.ps-tools) purescript purs-tidy purescript-language-server;
purs-nix = inputs.purs-nix { inherit system; };
affjax-node_ = pkgs.lib.recursiveUpdate purs-nix.ps-pkgs.affjax-node {
purs-nix-info.foreign."Affjax.Node" = { inherit node_modules; };
};
# TODO use httpurple-argonaut from official index
httpurple-argonaut_ = purs-nix.build
{
name = "httpurple-argonaut";
src.path = inputs.httpurple-argonaut;
info.dependencies = [ "argonaut" "console" "effect" "either" "httpurple" "prelude"];
};
ps =
purs-nix.purs
{
@ -48,10 +53,8 @@
argonaut-codecs
argonaut-generic
effect
httpure
node-buffer
node-process
node-streams-aff
httpurple
httpurple-argonaut_
test-unit
parsing
];

View File

@ -1,11 +1,7 @@
module Test.Compiler.ServerMock (settings, setupSrv) where
import Prelude (Unit, ($), (==), (>>=), (<<<), unit, pure, bind, const, discard)
import Control.Apply ((*>))
import Control.Monad.Error.Class (throwError, liftMaybe)
import Control.Monad.Reader.Trans (ReaderT, ask, runReaderT)
import Control.Monad.Trans.Class (lift)
import Data.Argonaut.Core (Json, stringify)
import Prelude (Unit, ($), (<<<), unit, pure, bind, const)
import Data.Argonaut.Core (Json)
import Data.Argonaut.Decode (JsonDecodeError, printJsonDecodeError)
import Data.Argonaut.Decode.Class (class DecodeJson, decodeJson)
import Data.Argonaut.Decode.Generic (genericDecodeJson)
@ -17,19 +13,13 @@ import Data.Functor ((<$>))
import Data.Generic.Rep (class Generic)
import Data.Maybe (Maybe(Nothing))
import Data.Newtype (class Newtype, unwrap, wrap)
import Effect
import Node.HTTP.Types (ServerResponse)
import Effect (Effect)
import Effect.Aff (Aff, bracket)
import Effect.Class (liftEffect)
import Effect.Exception (Error, error)
import HTTPure.Request (Request)
import HTTPure.Response (ResponseM, Response, ok)
import HTTPure.Server (serve)
import HTTPure.Lookup ((!!))
import HTTPure.Method (Method(Post))
import HTTPure.Body (class Body)
import HTTPure.Headers (header)
import Node.Stream.Aff (write, end, fromStringUTF8)
import Node.HTTP (responseAsStream)
import HTTPurple (RouteDuplex', Method(Post), (/), ResponseHeaders, noArgs, mkRoute, serve, ok, notFound, toJson)
import HTTPurple.Json.Argonaut (jsonEncoder)
import Compiler (Settings, Code, SuccessResult)
@ -51,26 +41,6 @@ successToJson = (encodeJson :: SuccessResult_ -> Json) <<< wrap
successFromJson_ :: Json -> Either Error SuccessResult
successFromJson_ json = unwrap <$> (error <<< printJsonDecodeError) `lmap` (decodeJson json :: Either JsonDecodeError SuccessResult_)
newtype Json_ = Json_ Json
derive instance Newtype Json_ _
instance Body Json_ where
defaultHeaders _ = pure $ header "Content-Type" "application/json"
write json res = do
let stream = responseAsStream res
body' <- liftEffect $ fromStringUTF8 $ stringify $ unwrap $ json
write stream body'
end stream
type ServerMock = ReaderT Code (ReaderT Request Aff)
askRes :: ServerMock Code
askRes = ask
askReq :: ServerMock Request
askReq = lift ask
settings :: Settings
settings = { protocol: "http"
, hostname: "localhost"
@ -78,38 +48,30 @@ settings = { protocol: "http"
, parser: successFromJson_
}
validatePath :: ServerMock Unit
validatePath = do
let invalidPath = error "invalid path"
missingPath = error "missing path"
req <- askReq
p <- liftMaybe missingPath $ req.path !! 0
case p == "compile" of
true -> pure unit
false -> throwError invalidPath
data Route = Compile
validateMethod :: ServerMock Unit
validateMethod = askReq >>= case _ of
{ method: Post } -> pure unit
_ -> throwError $ error "invalid method"
derive instance Generic Route _
validate :: ServerMock Unit
validate = validatePath *> validateMethod
route :: RouteDuplex' Route
route = mkRoute
{ "Compile": "compile" / noArgs
}
answer :: ServerMock Response
answer = do
res <- askRes
let json :: Json_
json = wrap $ successToJson { js: res, warnings: Nothing }
ok json
runServerMock :: Code -> Request -> ResponseM
runServerMock res req = runReaderT (runReaderT (validate *> answer) res) req
mkRouter
:: forall a.
String
-> { method :: Method, route :: Route | a }
-> Aff { headers :: ResponseHeaders, status :: Int , writeBody :: ServerResponse -> Aff Unit }
mkRouter js = case _ of
{ route: Compile, method: Post } -> ok $ toJson jsonEncoder $ successToJson { js, warnings: Nothing }
_ -> notFound
launchServerMock :: Code -> Aff (Effect Unit)
launchServerMock res = do
close <- liftEffect $ serve settings.port (\req -> runServerMock res req) $ pure unit
pure $ close $ pure unit
launchServerMock c = liftEffect do
let router = mkRouter c
close <- serve { port: settings.port } { route, router }
pure $ close $ pure unit
setupSrv :: forall b. Code -> Aff b -> Aff b
setupSrv code act = bracket (launchServerMock code) liftEffect (const act)