Skip to content

Commit

Permalink
chore(cli): Add NEAR smoketest (#1255)
Browse files Browse the repository at this point in the history
  • Loading branch information
jozanza committed May 26, 2022
1 parent a39b48f commit 8827a96
Show file tree
Hide file tree
Showing 7 changed files with 13,303 additions and 5,875 deletions.
7 changes: 7 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -84,3 +84,10 @@ jobs:
- name: Log Grain version
run: |
grain -v
# This is to test that we didn't actually introduce multivalue
# which might happen through a binaryen optimization
- name: Run NEAR smoketest
if: matrix.os != 'windows-latest'
run: |
npm run cli test
12 changes: 12 additions & 0 deletions cli/__test__/index.gr
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/* grainc-flags --no-gc --no-bulk-memory */

import WasmI32 from "runtime/unsafe/wasmi32"
import WasmI64 from "runtime/unsafe/wasmi64"
import Env from "./nearEnv"

export let hello = () => {
let value = "Hello, World!"
let length = WasmI64.load32U(WasmI32.fromGrain(value), 4n)
let ptr = WasmI64.extendI32U(WasmI32.add(WasmI32.fromGrain(value), 8n))
Env.valueReturn(length, ptr)
}
31 changes: 31 additions & 0 deletions cli/__test__/near.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
const path = require("path");

// NOTE:
// This test suite exists to ensure wasm output does not contain multivalue functions.
// If no multivalue functions exist, Grain smart contracts should run without issue.

let worker;
afterAll(async () => {
await worker?.tearDown();
});

describe("Runs in NEAR sandbox", () => {
jest.setTimeout(60_000);
let Worker;
try {
Worker = require("near-workspaces").Worker;
} catch (err) {
// no-op
}
// near-workspaces is optional, so if it's not installed, we can skip tests
const testIf = !Worker ? test.skip : test;
testIf("it should not produce wasm multivalue function", async () => {
worker = await Worker.init();
const root = worker.rootAccount;
const contract = await root.createAndDeploy(
"grain-near-multivalue-test",
path.join(__dirname, "index.gr.wasm")
);
expect(await contract.view("hello")).toEqual("Hello, World!");
});
});
28 changes: 28 additions & 0 deletions cli/__test__/nearEnv.gr
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/* grainc-flags --no-gc --no-bulk-memory */

export foreign wasm storage_read: (
WasmI64,
WasmI64,
WasmI64
) -> WasmI64 as storageRead from "env"
export foreign wasm storage_write: (
WasmI64,
WasmI64,
WasmI64,
WasmI64,
WasmI64
) -> WasmI64 as storageWrite from "env"
export foreign wasm storage_has_key: (
WasmI64,
WasmI64
) -> WasmI64 as storageHasKey from "env"

export foreign wasm read_register: (
WasmI64,
WasmI64
) -> Void as readRegister from "env"

export foreign wasm value_return: (
WasmI64,
WasmI64
) -> Void as valueReturn from "env"
9 changes: 9 additions & 0 deletions cli/__test__/wasi.gr
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
export let fd_write: (WasmI32, WasmI32, WasmI32, WasmI32) -> WasmI32 =
(
fd,
iovs,
iovs_len,
nwritten,
) => {
0n
}
10 changes: 8 additions & 2 deletions cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,10 @@
"link": "npm link",
"format": "prettier --write .",
"check-format": "prettier --check .",
"test": "echo \"Error: no test specified\" && exit 1",
"build-pkg": "pkg ./package.json --no-bytecode --compress Brotli --output ../pkg/grain"
"build-pkg": "pkg ./package.json --no-bytecode --compress Brotli --output ../pkg/grain",
"build:test": "grain compile --wasi-polyfill __test__/wasi.gr --use-start-section __test__/index.gr",
"pretest": "npm run build:test",
"test": "jest --detectOpenHandles"
},
"pkg": {
"assets": "bin/*.js"
Expand Down Expand Up @@ -43,5 +45,9 @@
"del-cli": "^4.0.1",
"pkg": "^5.3.1",
"prettier": "^2.3.2"
},
"optionalDependencies": {
"jest": "28.1.0",
"near-workspaces": "2.0.0"
}
}
Loading

0 comments on commit 8827a96

Please sign in to comment.