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 24, 2023
1 parent cbc8b44 commit 7fcaec1
Show file tree
Hide file tree
Showing 12 changed files with 90 additions and 33 deletions.
28 changes: 0 additions & 28 deletions .github/workflows/spawn_examples.yml

This file was deleted.

2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,4 @@ jobs:
- run: deno lint
- run: deno task codegen
- run: deno task star
- run: deno task test
- run: deno task test:ci
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:ci": "ENV_TYPE_CI=1 deno task test",
"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/run_with_limit.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from "https://deno.land/x/[email protected]/mod.ts"
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"
12 changes: 8 additions & 4 deletions effects/extrinsic.test.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,27 @@
import * as A from "../deps/std/testing/asserts.ts"
import * as T from "../test_util/mod.ts"
import { EXISTENTIAL_DEPOSIT_AMOUNT } from "../util/constants.ts"
import * as U from "../util/mod.ts"
import { Sr25519 } from "../util/mod.ts"
import { entryRead } from "./entryRead.ts"
import { extrinsic } from "./extrinsic.ts"

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 +32,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
13 changes: 13 additions & 0 deletions examples/derp.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { Keyring } from "https://deno.land/x/[email protected]/keyring/mod.ts"
import {
cryptoWaitReady,
randomAsHex,
} from "https://deno.land/x/[email protected]/util-crypto/mod.ts"

const a = new Keyring()

await cryptoWaitReady()

const keyring = new Keyring({ type: "sr25519", ss58Format: 2 })
keyring.addFromUri(randomAsHex(32), { name: `testing` })
console.log(keyring.getPairs().map((kp) => kp.meta))
56 changes: 56 additions & 0 deletions examples/examples.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import * as path from "http://localhost:5646/@local/deps/std/path.ts"
import { makeRunWithLimit } from "../deps/run_with_limit.ts"
import { readLines } from "../deps/std/io.ts"
import { writeAll } from "../deps/std/streams.ts"
import { assert } from "../deps/std/testing/asserts.ts"

function getModuleDir(importMeta: ImportMeta): string {
return path.resolve(path.dirname(path.fromFileUrl(importMeta.url)))
}

Deno.test({
name: "examples",
ignore: Deno.env.get("ENV_TYPE_CI") ? false : true,
async fn(t) {
const { runWithLimit } = makeRunWithLimit<void>(navigator.hardwareConcurrency)

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

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

await Promise.all(exampleFileNames.map((fileName) =>
runWithLimit(async () => {
const task = Deno.run({
cmd: ["deno", "task", "run", `${getModuleDir(import.meta)}/${fileName}`],
stdout: "null",
stderr: "piped",
})

try {
await t.step({
name: fileName,
async fn() {
const encoder = new TextEncoder()
for await (const line of readLines(task.stderr)) {
await writeAll(Deno.stdout, encoder.encode(`[examples/${fileName}] ${line}\n`))
}
const status = await task.status()

assert(status.code === 0, `task failed with status code: ${status.code}`)
assert(status.success, `unsuccessful`)
},
sanitizeOps: false,
sanitizeResources: false,
sanitizeExit: false,
})
} finally {
task.stderr.close()
task.close()
}
})
))
},
})
6 changes: 6 additions & 0 deletions util/constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
/**
* Minimum deposit amount to activate an account
*
* {@link https://support.polkadot.network/support/solutions/articles/65000168651-what-is-the-existential-deposit-#:~:text=On%20the%20Polkadot%20network%2C%20an,the%20Existential%20Deposit%20(ED) What is the Existential Deposit}
*/
export const EXISTENTIAL_DEPOSIT_AMOUNT = 10_000_000_000n
1 change: 1 addition & 0 deletions util/mod.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
export * from "./branded.ts"
export * from "./case.ts"
export * from "./constants.ts"
export * from "./Counter.ts"
export * from "./error.ts"
export * from "./fetch.ts"
Expand Down

0 comments on commit 7fcaec1

Please sign in to comment.