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

Commit

Permalink
feat: examples node testing (#1031)
Browse files Browse the repository at this point in the history
Co-authored-by: Harry Solovay <[email protected]>
Co-authored-by: tjjfvi <[email protected]>
  • Loading branch information
3 people authored Jun 14, 2023
1 parent 997c33f commit 7dea826
Show file tree
Hide file tree
Showing 26 changed files with 192 additions and 57 deletions.
31 changes: 30 additions & 1 deletion .github/workflows/checks.yml
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ jobs:
path: ~/.cache/deno
key: cache-${{ env.CAPI_SHA }}
- run: deno run -A _tasks/use_remote.ts
- run: deno task test:eg
- run: deno task test:eg:deno
- name: Archive target
uses: actions/upload-artifact@v3
if: failure() || cancelled()
Expand All @@ -149,3 +149,32 @@ jobs:
target
!target/**/db/full/*
!target/**/paritydb/full/*
examples-node:
name: Examples (Node)
runs-on: ubuntu-latest
timeout-minutes: 15
needs: cache
steps:
- uses: actions/checkout@v3
- uses: denoland/setup-deno@61fe2df320078202e33d7d5ad347e7dcfa0e8f31 # v1.1.0
with:
deno-version: v1.33.3
- uses: actions/cache@v3
with:
path: ~/.cache/deno
key: cache-${{ env.CAPI_SHA }}
- run: deno run -A _tasks/use_remote.ts
- run: deno task dnt --server ${{ env.CAPI_SHA }} --examples
- uses: actions/setup-node@v3
with:
node-version: "20"
- run: deno task test:eg:node
- name: Archive target
uses: actions/upload-artifact@v3
if: failure() || cancelled()
with:
name: examples-node-target
path: |
target
!target/**/db/full/*
!target/**/paritydb/full/*
4 changes: 1 addition & 3 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,10 @@ jobs:
registry-url: "https://registry.npmjs.org"
- name: Build NPM Package
run: deno task dnt --version ${{steps.get_tag_version.outputs.tag_version}} --server ${{steps.get_tag_version.outputs.tag_version}}
- run: npm pack
working-directory: "./target/npm"
- uses: actions/upload-artifact@v3
with:
name: package
path: "./target/npm/*.tgz"
path: "./target/npm/artifacts/capi*.tgz"
- uses: octokit/[email protected]
if: startsWith(github.ref, 'refs/tags/')
with:
Expand Down
129 changes: 108 additions & 21 deletions _tasks/dnt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,30 @@ import importMap from "../import_map.json" assert { type: "json" }
import * as nets from "../nets.ts"
import { normalizePackageName } from "../util/normalize.ts"

const { version: packageVersion, server: serverVersion } = flags.parse(Deno.args, {
string: ["version", "server"],
default: {
version: "v0.0.0-local",
const {
version: packageVersion,
server: serverVersion,
examples: buildExamples,
} = flags.parse(
Deno.args,
{
string: ["version", "server"],
boolean: ["examples"],
default: {
version: "v0.0.0-local",
},
},
})
)

const server = serverVersion ? `https://capi.dev/@${serverVersion}/` : "http://localhost:4646/"
const hash = new URL(importMap.imports["@capi/"]).pathname.slice(1, -1)
const hash = new URL(importMap.imports["@capi/"]).pathname.split("/").at(-2)!

const outDir = path.join("target", "npm")
const outDir = path.join(Deno.cwd(), "target", "npm")
const capiOutDir = path.join(outDir, "capi")
const examplesOutDir = path.join(outDir, "capi-examples")

await fs.emptyDir(outDir)
await Deno.mkdir(capiOutDir)

const entryPoints: EntryPoint[] = []
const mappings: Record<string, string> = {}
Expand Down Expand Up @@ -47,6 +58,12 @@ for (const pathname of allFiles) {
}
}

const capiCodegenDeps = Object.fromEntries(
Object.keys(nets).map((key) => normalizePackageName(key)).map((
packageName,
) => [`@capi/${packageName}`, `${server}${hash}/${packageName}.tar`]),
)

await Promise.all([
build({
package: {
Expand All @@ -57,12 +74,7 @@ await Promise.all([
license: "Apache-2.0",
repository: "github:paritytech/capi",
dependencies: {
...Object.fromEntries(
Object.keys(nets).map((key) => {
const name = normalizePackageName(key)
return [`@capi/${name}`, `${server}${hash}/${name}.tar`]
}),
),
...capiCodegenDeps,
"ts-node": "^10.9.1",
},
},
Expand Down Expand Up @@ -110,11 +122,12 @@ await Promise.all([
"node:net": "node:net",
"node:http": "node:http",
"node:stream": "node:stream",
"node:fs": "node:fs",
"node:fs/promises": "node:fs/promises",
"https://raw.githubusercontent.com/paritytech/capi-binary-builds/f5baeca/streamToFile.ts":
"https://raw.githubusercontent.com/paritytech/capi-binary-builds/f5baeca/streamToFile.node.ts",
},
outDir,
outDir: capiOutDir,
shims: {
deno: true,
custom: [{
Expand All @@ -131,25 +144,25 @@ await Promise.all([
test: false,
typeCheck: false,
}),
fs.copy("LICENSE", path.join(outDir, "LICENSE")),
fs.copy("Readme.md", path.join(outDir, "Readme.md")),
fs.copy("server/static/", path.join(outDir, "esm/server/static/")),
fs.copy("LICENSE", path.join(capiOutDir, "LICENSE")),
fs.copy("Readme.md", path.join(capiOutDir, "Readme.md")),
fs.copy("server/static/", path.join(capiOutDir, "esm/server/static/")),
])

await Promise.all([
fs.copy(
"target/npm/src/rune/_empty.d.ts",
"target/npm/types/rune/_empty.d.ts",
path.join(capiOutDir, "src/rune/_empty.d.ts"),
path.join(capiOutDir, "types/rune/_empty.d.ts"),
{ overwrite: true },
),
editFile(
"target/npm/esm/main.js",
path.join(capiOutDir, "esm/main.js"),
(content) =>
content
.replace(/^#!.+/, "#!/usr/bin/env -S node --loader ts-node/esm"),
),
editFile(
"target/npm/esm/_dnt.shims.js",
path.join(capiOutDir, "esm/_dnt.shims.js"),
(content) =>
content
.replace(/"@deno\/shim-deno"/g, `"./deps/shims/Deno.node.js"`),
Expand All @@ -159,3 +172,77 @@ await Promise.all([
async function editFile(path: string, modify: (content: string) => string) {
await Deno.writeTextFile(path, modify(await Deno.readTextFile(path)))
}

await new Deno.Command("npm", {
args: ["pack", "--pack-destination", outDir],
cwd: capiOutDir,
}).output()

if (buildExamples) {
const exampleEntryPoints: EntryPoint[] = []
for await (
const { path } of fs.walkSync(".", {
exts: [".eg.ts"],
includeDirs: false,
})
) {
exampleEntryPoints.push({
name: path,
path: `./${path}`,
})
}

await build({
package: {
name: "capi-examples",
version: packageVersion,
type: "module",
devDependencies: {
"ts-node": "^10.9.1",
},
dependencies: {
...capiCodegenDeps,
capi: `file:../capi-${packageVersion}.tgz`,
},
},
compilerOptions: {
importHelpers: true,
sourceMap: true,
target: "ES2021",
lib: ["es2022.error", "dom.iterable"],
},
entryPoints: exampleEntryPoints,
mappings: {
"https://deno.land/x/[email protected]/keyring/mod.ts": {
name: "@polkadot/keyring",
version: "12.2.1",
},
"https://deno.land/x/[email protected]/types/mod.ts": {
name: "@polkadot/types",
version: "10.7.2",
},
},
importMap: "_tasks/dnt_examples_import_map.json",
outDir: examplesOutDir,
scriptModule: false,
declaration: false,
shims: { deno: true },
test: false,
typeCheck: false,
})

await Promise.all(
[
fs.copy(
"examples/ink/erc20.json",
path.join(examplesOutDir, "esm/examples/ink/erc20.json"),
{ overwrite: true },
),
fs.copy(
"examples/ink/erc20.wasm",
path.join(examplesOutDir, "esm/examples/ink/erc20.wasm"),
{ overwrite: true },
),
],
)
}
5 changes: 5 additions & 0 deletions _tasks/dnt_examples_import_map.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"imports": {
"asserts": "../deps/std/testing/asserts.ts"
}
}
4 changes: 3 additions & 1 deletion deno.jsonc
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,9 @@
"dnt": "deno task run _tasks/dnt.ts",
"test": "deno task capi serve -- deno test -A -L=info --ignore=target --parallel -r=http://localhost:4646/",
"test:update": "deno task test -- --update",
"test:eg": "deno task run https://deno.land/x/[email protected]/test.ts 'examples/**/*.eg.ts'",
"test:eg": "deno task run https://deno.land/x/[email protected]/main.ts",
"test:eg:deno": "deno task test:eg test deno 'examples/**/*.eg.ts'",
"test:eg:node": "deno task test:eg test node 'target/npm/capi-examples/esm/examples/**/*.eg.js'",
"moderate": "deno run -A https://deno.land/x/[email protected]/mod.ts --exclude '*.test.ts' '*.node.ts' && dprint fmt",
"capi": "deno run -A main.ts",
"cache": "deno task capi serve -- deno cache -r=http://localhost:4646/",
Expand Down
3 changes: 3 additions & 0 deletions deps/shims/Deno.node.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import * as fs from "node:fs"
import { Command } from "./command.node.ts"

declare const process: { env: Record<string, string> }
Expand All @@ -19,6 +20,8 @@ let _deno = {
},
toObject: () => ({ ...process.env }),
},
readFileSync: (path: string | URL): Uint8Array => fs.readFileSync(path),
readTextFileSync: (path: string | URL): string => fs.readFileSync(path, "utf8"),
} as any as typeof globalThis.Deno

export const Deno = new Proxy(
Expand Down
2 changes: 1 addition & 1 deletion examples/blocks.eg.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

import { $eventRecord, metadata, polkadot } from "@capi/polkadot"
import { $, $extrinsic, known, Rune } from "capi"
import { babeBlockAuthor } from "capi/patterns/consensus/mod.ts"
import { babeBlockAuthor } from "capi/patterns/consensus"

/// Reference the latest block hash.
const blockHash = polkadot.blockHash()
Expand Down
2 changes: 1 addition & 1 deletion examples/dev/storage_sizes.eg.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

import { polkadotDev } from "@capi/polkadot-dev"
import { $ } from "capi"
import { storageSizes } from "capi/patterns/storage_sizes.ts"
import { storageSizes } from "capi/patterns/storage_sizes"

/// Use the storageSizes factory to produce a Rune. Then execute it.
const sizes = await storageSizes(polkadotDev).run()
Expand Down
6 changes: 3 additions & 3 deletions examples/ink/deploy.eg.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@

import { contractsDev } from "@capi/contracts-dev"
import { $, createDevUsers, hex, Sr25519, ss58 } from "capi"
import { InkMetadataRune } from "capi/patterns/ink/mod.ts"
import { signature } from "capi/patterns/signature/polkadot.ts"
import { InkMetadataRune } from "capi/patterns/ink"
import { signature } from "capi/patterns/signature/polkadot"

/// Initialize an `InkMetadataRune` with the raw Ink metadata text.
const metadata = InkMetadataRune.fromMetadataText(
Deno.readTextFileSync(new URL(import.meta.resolve("./erc20.json"))),
Deno.readTextFileSync(new URL("./erc20.json", import.meta.url)),
)

/// Given that other examples may utilize this script, we'll allow the
Expand Down
6 changes: 3 additions & 3 deletions examples/ink/interact.eg.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
import { contractsDev } from "@capi/contracts-dev"
import { assert } from "asserts"
import { $, createDevUsers, hex } from "capi"
import { InkMetadataRune } from "capi/patterns/ink/mod.ts"
import { signature } from "capi/patterns/signature/polkadot.ts"
import { InkMetadataRune } from "capi/patterns/ink"
import { signature } from "capi/patterns/signature/polkadot"

/// Get two test users. Alexa will deploy, Billy will be the recipient of an erc20
/// token transfer.
Expand All @@ -23,7 +23,7 @@ await import("./deploy.eg.ts")
const address = Deno.env.get("CONTRACT_SS58_ADDRESS")!

/// Initialize an `InkMetadataRune` with the raw Ink metadata text.
export const metadata = InkMetadataRune.fromMetadataText(
const metadata = InkMetadataRune.fromMetadataText(
Deno.readTextFileSync(new URL("./erc20.json", import.meta.url)),
)

Expand Down
4 changes: 2 additions & 2 deletions examples/misc/identity.eg.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@

import { polkadotDev } from "@capi/polkadot-dev"
import { $, createDevUsers, is } from "capi"
import { IdentityInfoTranscoders } from "capi/patterns/identity.ts"
import { signature } from "capi/patterns/signature/polkadot.ts"
import { IdentityInfoTranscoders } from "capi/patterns/identity"
import { signature } from "capi/patterns/signature/polkadot"

const { alexa } = await createDevUsers()

Expand Down
2 changes: 1 addition & 1 deletion examples/misc/indices.eg.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import { polkadotDev } from "@capi/polkadot-dev"
import { assertEquals } from "asserts"
import { createDevUsers, is } from "capi"
import { signature } from "capi/patterns/signature/polkadot.ts"
import { signature } from "capi/patterns/signature/polkadot"

const { alexa } = await createDevUsers()

Expand Down
4 changes: 2 additions & 2 deletions examples/multisig/basic.eg.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
import { polkadotDev } from "@capi/polkadot-dev"
import { assert } from "asserts"
import { $, createDevUsers, is } from "capi"
import { MultisigRune } from "capi/patterns/multisig/mod.ts"
import { signature } from "capi/patterns/signature/polkadot.ts"
import { MultisigRune } from "capi/patterns/multisig"
import { signature } from "capi/patterns/signature/polkadot"

const { alexa, billy, carol, david } = await createDevUsers()

Expand Down
6 changes: 3 additions & 3 deletions examples/multisig/stash.eg.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@
import { MultiAddress, polkadotDev } from "@capi/polkadot-dev"
import { assert } from "asserts"
import { createDevUsers, is } from "capi"
import { MultisigRune } from "capi/patterns/multisig/mod.ts"
import { filterPureCreatedEvents } from "capi/patterns/proxy/mod.ts"
import { signature } from "capi/patterns/signature/polkadot.ts"
import { MultisigRune } from "capi/patterns/multisig"
import { filterPureCreatedEvents } from "capi/patterns/proxy"
import { signature } from "capi/patterns/signature/polkadot"

const { alexa, billy, carol } = await createDevUsers()

Expand Down
4 changes: 2 additions & 2 deletions examples/multisig/virtual.eg.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@
import { MultiAddress, polkadotDev } from "@capi/polkadot-dev"
import { assert } from "asserts"
import { $, createDevUsers, is, Rune, Sr25519 } from "capi"
import { VirtualMultisigRune } from "capi/patterns/multisig/mod.ts"
import { signature } from "capi/patterns/signature/polkadot.ts"
import { VirtualMultisigRune } from "capi/patterns/multisig"
import { signature } from "capi/patterns/signature/polkadot"
import { parse } from "../../deps/std/flags.ts"

const { alexa, billy, carol, david } = await createDevUsers()
Expand Down
4 changes: 2 additions & 2 deletions examples/nfts.eg.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ import {
} from "@capi/rococo-dev-westmint"
import { assertEquals } from "asserts"
import { $, createDevUsers, is, Rune } from "capi"
import { DefaultCollectionSetting, DefaultItemSetting } from "capi/patterns/nfts.ts"
import { signature } from "capi/patterns/signature/statemint.ts"
import { DefaultCollectionSetting, DefaultItemSetting } from "capi/patterns/nfts"
import { signature } from "capi/patterns/signature/statemint"

/// Create two dev users. Alexa will mint and list the NFT. Billy will purchase it.
const { alexa, billy } = await createDevUsers()
Expand Down
Loading

0 comments on commit 7dea826

Please sign in to comment.