This repository has been archived by the owner on Sep 26, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
0e3563b
commit b88fe7a
Showing
7 changed files
with
183 additions
and
8 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
import { execPreRequestScript } from "../preRequest" | ||
import "@relmify/jest-fp-ts" | ||
|
||
describe("execPreRequestScript", () => { | ||
test("returns the updated envirionment properly", () => { | ||
return expect( | ||
execPreRequestScript( | ||
` | ||
pw.env.set("bob", "newbob") | ||
`, | ||
[{ key: "bob", value: "oldbob" }, { key: "foo", value: "bar" }] | ||
)() | ||
).resolves.toEqualRight([ | ||
{ key: "bob", value: "newbob" }, | ||
{ key: "foo", value: "bar" } | ||
]) | ||
}) | ||
|
||
test("fails if the key is not a string", () => { | ||
return expect( | ||
execPreRequestScript( | ||
` | ||
pw.env.set(10, "newbob") | ||
`, | ||
[{ key: "bob", value: "oldbob" }, { key: "foo", value: "bar" }] | ||
)() | ||
).resolves.toBeLeft() | ||
}) | ||
|
||
test("fails if the value is not a string", () => { | ||
return expect( | ||
execPreRequestScript( | ||
` | ||
pw.env.set("bob", 10) | ||
`, | ||
[{ key: "bob", value: "oldbob" }, { key: "foo", value: "bar" }] | ||
)() | ||
).resolves.toBeLeft() | ||
}) | ||
|
||
test("fails for invalid syntax", () => { | ||
return expect( | ||
execPreRequestScript( | ||
` | ||
pw.env.set("bob", | ||
`, | ||
[{ key: "bob", value: "oldbob" }, { key: "foo", value: "bar" }] | ||
)() | ||
).resolves.toBeLeft() | ||
}) | ||
|
||
test("creates new env variable if doesn't exist", () => { | ||
return expect( | ||
execPreRequestScript( | ||
` | ||
pw.env.set("foo", "bar") | ||
`, | ||
[] | ||
)() | ||
).resolves.toEqualRight( | ||
[{ key: "foo", value: "bar" }] | ||
) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
import { pipe } from "fp-ts/lib/function"; | ||
import { chain, TaskEither, tryCatch, right, left } from "fp-ts/lib/TaskEither"; | ||
import * as qjs from "quickjs-emscripten"; | ||
import clone from "lodash/clone"; | ||
|
||
type EnvEntry = { | ||
key: string; | ||
value: string; | ||
}; | ||
|
||
export const execPreRequestScript = ( | ||
preRequestScript: string, | ||
env: EnvEntry[] | ||
): TaskEither<string, EnvEntry[]> => pipe( | ||
tryCatch( | ||
async () => await qjs.getQuickJS(), | ||
(reason) => `QuickJS initialization failed: ${reason}` | ||
), | ||
chain( | ||
(QuickJS) => { | ||
const finalEnv = clone(env) | ||
|
||
const vm = QuickJS.createVm() | ||
|
||
const pwHandle = vm.newObject() | ||
|
||
const envHandle = vm.newObject() | ||
|
||
const envSetFuncHandle = vm.newFunction("set", (keyHandle, valueHandle) => { | ||
const key = vm.dump(keyHandle) | ||
const value = vm.dump(valueHandle) | ||
|
||
if (typeof key !== "string") return { | ||
error: vm.newString("Expected key to be a string") | ||
} | ||
|
||
if (typeof value !== "string") return { | ||
error: vm.newString("Expected value to be a string") | ||
} | ||
|
||
const keyIndex = finalEnv.findIndex((env) => env.key === key) | ||
|
||
if (keyIndex === -1) { | ||
finalEnv.push({ key, value }) | ||
} else { | ||
finalEnv[keyIndex] = { key, value } | ||
} | ||
|
||
return { | ||
value: vm.undefined | ||
} | ||
}) | ||
|
||
vm.setProp(envHandle, "set", envSetFuncHandle) | ||
envSetFuncHandle.dispose() | ||
|
||
vm.setProp(pwHandle, "env", envHandle) | ||
envHandle.dispose() | ||
|
||
vm.setProp(vm.global, "pw", pwHandle) | ||
pwHandle.dispose() | ||
|
||
const evalRes = vm.evalCode(preRequestScript) | ||
|
||
if (evalRes.error) { | ||
const errorData = vm.dump(evalRes.error) | ||
evalRes.error.dispose() | ||
|
||
return left(`Script evaluation failed: ${errorData}`) | ||
} | ||
|
||
vm.dispose() | ||
|
||
return right(finalEnv) | ||
} | ||
) | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters