Skip to content
This repository has been archived by the owner on Sep 14, 2023. It is now read-only.

Commit

Permalink
feat: integrate examples as deno tests for ci
Browse files Browse the repository at this point in the history
  • Loading branch information
ryanleecode committed Jan 25, 2023
1 parent cbc8b44 commit 84783c8
Show file tree
Hide file tree
Showing 10 changed files with 122 additions and 83 deletions.
28 changes: 0 additions & 28 deletions .github/workflows/spawn_examples.yml

This file was deleted.

1 change: 1 addition & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,4 @@ jobs:
- run: deno task codegen
- run: deno task star
- run: deno task test
- run: deno task test:examples
67 changes: 67 additions & 0 deletions _tasks/test_examples.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import { Buffer, readLines } from "../deps/std/io.ts"
import * as path from "../deps/std/path.ts"
import { writeAll } from "../deps/std/streams.ts"
import { assert } from "../deps/std/testing/asserts.ts"

if (!Deno.args[0]) {
throw new Error("specify examples directory as first CLI argument")
}

const examplesDir = path.join(Deno.cwd(), Deno.args[0])

const ignoreFile = await Deno.readTextFile(path.join(examplesDir, ".ignore"))
const ignoredFiles = new Set(ignoreFile.split("\n"))

const exampleFileNames = Array.from(Deno.readDirSync(examplesDir))
.filter((e) => e.name.match(/^.*\.ts$/g) && e.isFile && !ignoredFiles.has(e.name))
.map((f) => f.name)

Deno.test("examples", async (t) => {
await Promise.all(exampleFileNames.map((fileName) => {
return t.step({
name: fileName,
async fn() {
const task = Deno.run({
cmd: ["deno", "task", "run", `${examplesDir}/${fileName}`],
stdout: "piped",
stderr: "piped",
})

try {
const out = new Buffer()

await Promise.all([
pipeThrough(task.stdout, out),
pipeThrough(task.stderr, out),
])

const status = await task.status()

if (!status.success) {
for await (const line of readLines(out)) {
console.log(line)
}
}
assert(status.success, `task failed with status code: ${status.code}`)
} finally {
task.stdout.close()
task.stderr.close()
task.close()
}
},
sanitizeExit: false,
sanitizeOps: false,
sanitizeResources: false,
})
}))
})

async function pipeThrough(
reader: Deno.Reader,
writer: Deno.Writer,
) {
const encoder = new TextEncoder()
for await (const line of readLines(reader)) {
await writeAll(writer, encoder.encode(`${line}\n`))
}
}
1 change: 1 addition & 0 deletions deno.jsonc
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
"star": "deno task run _tasks/star.ts && deno cache --check target/star.ts",
"codegen": "deno task run cache.ts examples/mod.ts",
"test": "deno task run test_util/ctx.ts -- deno test -A -L=info --ignore=target --parallel",
"test:examples": "deno task test _tasks/test_examples.ts -- examples",
"test:update": "deno task test -- -- --update",
"bench": "deno bench -A",
"moderate": "deno task run https://deno.land/x/[email protected]/mod.ts && dprint fmt"
Expand Down
1 change: 1 addition & 0 deletions deps/std/io.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from "https://deno.land/[email protected]/io/mod.ts"
1 change: 1 addition & 0 deletions deps/std/streams.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from "https://deno.land/[email protected]/streams/mod.ts"
13 changes: 9 additions & 4 deletions effects/extrinsic.test.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,28 @@
import * as A from "../deps/std/testing/asserts.ts"
import * as T from "../test_util/mod.ts"
import * as U from "../util/mod.ts"
import { Sr25519 } from "../util/mod.ts"
import { entryRead } from "./entryRead.ts"
import { extrinsic } from "./extrinsic.ts"

const EXISTENTIAL_DEPOSIT_AMOUNT = 10_000_000_000n

Deno.test({
name: "Balances.transfer",
fn: async (ctx) => {
const kp = Sr25519.fromSeed(crypto.getRandomValues(new Uint8Array(32)))

await ctx.step("extrinsic events", async () => {
await assertExtrinsicStatusOrder({
keypair: T.alice,
call: {
type: "Balances",
value: {
type: "transfer",
value: 12345n,
value: EXISTENTIAL_DEPOSIT_AMOUNT + 12345n,
dest: {
type: "Id",
value: T.bob.publicKey,
value: kp.publicKey,
},
},
},
Expand All @@ -28,10 +33,10 @@ Deno.test({
await ctx.step({
name: "account balance updated",
fn: async () => {
const state = await entryRead(T.westend)("System", "Account", [T.bob.publicKey]).run()
const state = await entryRead(T.westend)("System", "Account", [kp.publicKey]).run()
A.assertObjectMatch(state, {
value: {
data: { free: 10000000000012345n },
data: { free: EXISTENTIAL_DEPOSIT_AMOUNT + 12345n },
},
})
},
Expand Down
1 change: 1 addition & 0 deletions examples/.ignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ mod.ts
multisig_transfer.ts
smart_contract.ts
xcm_teleport_assets.ts
examples.test.ts
36 changes: 15 additions & 21 deletions examples/batch.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import * as C from "http://localhost:5646/@local/mod.ts"
import * as T from "http://localhost:5646/@local/test_util/mod.ts"
import * as U from "http://localhost:5646/@local/util/mod.ts"

Expand All @@ -7,6 +6,7 @@ import {
Balances,
Utility,
} from "http://localhost:5646/@local/proxy/dev:westend/@v0.9.36/pallets/mod.ts"
import { collectExtrinsicEvents } from "http://localhost:5646/@local/test_util/extrinsic.ts"

// TODO: uncomment these lines / use env upon solving `count` in zones
// const getBalances = C.Z.ls(
Expand All @@ -16,24 +16,18 @@ import {
// }),
// )

const tx = extrinsic({
sender: T.alice.address,
call: Utility.batchAll({
calls: T.users.map((pair) =>
Balances.transfer({
dest: pair.address,
value: 12345n,
})
),
}),
})
.signed(T.alice.sign)
.watch((ctx) => (status) => {
console.log(status)
if (C.rpc.known.TransactionStatus.isTerminal(status)) {
return ctx.end()
}
return
})
const root = collectExtrinsicEvents(
extrinsic({
sender: T.dave.address,
call: Utility.batchAll({
calls: T.users.map((pair) =>
Balances.transfer({
dest: pair.address,
value: 12345n,
})
),
}),
}).signed(T.dave.sign),
).next(console.log)

U.throwIfError(await tx.run())
U.throwIfError(await root.run())
56 changes: 26 additions & 30 deletions examples/polkadot_js_signer.ts
Original file line number Diff line number Diff line change
@@ -1,38 +1,34 @@
import { createTestPairs } from "https://deno.land/x/[email protected]/keyring/mod.ts"
import { TypeRegistry } from "https://deno.land/x/[email protected]/types/mod.ts"

import * as C from "http://localhost:5646/@local/mod.ts"
import { collectExtrinsicEvents } from "http://localhost:5646/@local/test_util/extrinsic.ts"

import * as T from "http://localhost:5646/@local/test_util/mod.ts"
import * as U from "http://localhost:5646/@local/util/mod.ts"
import { createTestPairs } from "https://deno.land/x/[email protected]/keyring/mod.ts"
import { TypeRegistry } from "https://deno.land/x/[email protected]/types/mod.ts"

const root = C.extrinsic(T.westend)({
sender: T.alice.address,
call: {
type: "Balances",
value: {
type: "transfer",
value: 12345n,
dest: T.bob.address,
const root = collectExtrinsicEvents(
C.extrinsic(T.westend)({
sender: T.dave.address,
call: {
type: "Balances",
value: {
type: "transfer",
value: 12345n,
dest: T.bob.address,
},
},
},
})
.signed({
signPayload(payload) {
const tr = new TypeRegistry()
tr.setSignedExtensions(payload.signedExtensions)
return Promise.resolve(
tr
.createType("ExtrinsicPayload", payload, { version: payload.version })
.sign(createTestPairs().alice!),
)
},
})
.watch((ctx) => (status) => {
console.log(status)
if (C.rpc.known.TransactionStatus.isTerminal(status)) {
return ctx.end()
}
return
})
.signed({
signPayload(payload) {
const tr = new TypeRegistry()
tr.setSignedExtensions(payload.signedExtensions)
return Promise.resolve(
tr
.createType("ExtrinsicPayload", payload, { version: payload.version })
.sign(createTestPairs().dave!),
)
},
}),
).next(console.log)

U.throwIfError(await root.run())

0 comments on commit 84783c8

Please sign in to comment.