Skip to content

Commit

Permalink
test: add wasm test
Browse files Browse the repository at this point in the history
ignore emsdk

temporarily remove wasi test

add wasi test back

--experimental-wasi-unstable-preview1

test

separate wasm test

run test in bash on windows

fix python path
  • Loading branch information
toyobayashi committed Jun 7, 2024
1 parent cb8bfc1 commit b4cad44
Show file tree
Hide file tree
Showing 7 changed files with 306 additions and 3 deletions.
31 changes: 28 additions & 3 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,12 @@ jobs:
node: 20.x
name: ${{ matrix.os }} - ${{ matrix.python }} - ${{ matrix.node }}
runs-on: ${{ matrix.os }}
env:
WASI_VERSION: '22'
WASI_VERSION_FULL: '22.0'
WASI_SDK_PATH: 'wasi-sdk-22.0'
EM_VERSION: '3.1.52'
EM_CACHE_FOLDER: 'emsdk-cache'
steps:
- name: Checkout Repository
uses: actions/checkout@v4
Expand All @@ -115,6 +121,25 @@ jobs:
python-version: ${{ matrix.python }}
env:
PYTHON_VERSION: ${{ matrix.python }} # Why do this?
- uses: seanmiddleditch/gha-setup-ninja@v4
- uses: mymindstorm/setup-emsdk@v14
with:
version: ${{ env.EM_VERSION }}
actions-cache-folder: ${{ env.EM_CACHE_FOLDER }}
- name: Install wasi-sdk (macOS or Linux)
shell: bash
if: ${{ !startsWith(matrix.os, 'windows') }}
run: |
wget -q https://github.com/WebAssembly/wasi-sdk/releases/download/wasi-sdk-${WASI_VERSION}/wasi-sdk-${WASI_VERSION_FULL}-${{ startsWith(matrix.os, 'macos') && 'macos' || 'linux' }}.tar.gz
mkdir -p $WASI_SDK_PATH
tar zxvf wasi-sdk-${WASI_VERSION_FULL}-${{ startsWith(matrix.os, 'macos') && 'macos' || 'linux' }}.tar.gz -C $WASI_SDK_PATH --strip 1
- name: Install wasi-sdk (Windows)
shell: pwsh
if: ${{ startsWith(matrix.os, 'windows') }}
run: |
Start-BitsTransfer -Source https://github.com/WebAssembly/wasi-sdk/releases/download/wasi-sdk-${env:WASI_VERSION}/wasi-sdk-${env:WASI_VERSION_FULL}.m-mingw.tar.gz
New-Item -ItemType Directory -Path ${env:WASI_SDK_PATH}
tar -zxvf wasi-sdk-${env:WASI_VERSION_FULL}.m-mingw.tar.gz -C ${env:WASI_SDK_PATH} --strip 1
- name: Install Dependencies
run: |
npm install
Expand All @@ -125,7 +150,7 @@ jobs:
echo 'GYP_MSVS_VERSION=2015' >> $Env:GITHUB_ENV
echo 'GYP_MSVS_OVERRIDE_PATH=C:\\Dummy' >> $Env:GITHUB_ENV
- name: Run Python Tests
run: python -m pytest
run: python -m pytest --ignore=${{ env.EM_CACHE_FOLDER }}
- name: Run Tests (macOS or Linux)
if: "!startsWith(matrix.os, 'windows')"
shell: bash
Expand All @@ -134,7 +159,7 @@ jobs:
FULL_TEST: ${{ (matrix.node == '20.x' && matrix.python == '3.12') && '1' || '0' }}
- name: Run Tests (Windows)
if: startsWith(matrix.os, 'windows')
shell: pwsh
run: npm run test --python="${env:pythonLocation}\\python.exe"
shell: bash # Building wasm on Windows requires using make generator, it only works in bash
run: npm run test --python="${pythonLocation}\\python.exe"
env:
FULL_TEST: ${{ (matrix.node == '20.x' && matrix.python == '3.12') && '1' || '0' }}
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,11 @@
"node": "^16.14.0 || >=18.0.0"
},
"devDependencies": {
"@emnapi/core": "^1.2.0",
"@emnapi/runtime": "^1.2.0",
"bindings": "^1.5.0",
"cross-env": "^7.0.3",
"emnapi": "^1.2.0",
"mocha": "^10.2.0",
"nan": "^2.14.2",
"require-inject": "^1.4.4",
Expand Down
62 changes: 62 additions & 0 deletions test/node_modules/hello_wasm/binding.gyp

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 21 additions & 0 deletions test/node_modules/hello_wasm/hello.c

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

50 changes: 50 additions & 0 deletions test/node_modules/hello_wasm/hello.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 16 additions & 0 deletions test/node_modules/hello_wasm/package.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

126 changes: 126 additions & 0 deletions test/test-wasm.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
'use strict'

const { describe, it } = require('mocha')
const assert = require('assert')
const path = require('path')
const cp = require('child_process')
const util = require('../lib/util')
const { platformTimeout } = require('./common')

const wasmAddonPath = path.resolve(__dirname, 'node_modules', 'hello_wasm')
const nodeGyp = path.resolve(__dirname, '..', 'bin', 'node-gyp.js')

const execFileSync = (...args) => cp.execFileSync(...args).toString().trim()

const execFile = async (cmd, env) => {
const [err,, stderr] = await util.execFile(process.execPath, cmd, {
env: {
...process.env,
NODE_GYP_NULL_LOGGER: undefined,
...env
},
encoding: 'utf-8'
})
return [err, stderr.toString().trim().split(/\r?\n/)]
}

function runWasm (hostProcess = process.execPath) {
const testCode = "console.log(require('hello_wasm').hello())"
return execFileSync(hostProcess, ['--experimental-wasi-unstable-preview1', '-e', testCode], { cwd: __dirname })
}

function executable (name) {
return name + (process.platform === 'win32' ? '.exe' : '')
}

function getWasmEnv (target) {
const env = {
GYP_CROSSCOMPILE: '1',
AR_host: 'ar',
CC_host: 'clang',
CXX_host: 'clang++'
}
if (target === 'emscripten') {
env.AR_target = 'emar'
env.CC_target = 'emcc'
env.CXX_target = 'em++'
} else if (target === 'wasi') {
env.AR_target = path.resolve(__dirname, '..', process.env.WASI_SDK_PATH, 'bin', executable('ar'))
env.CC_target = path.resolve(__dirname, '..', process.env.WASI_SDK_PATH, 'bin', executable('clang'))
env.CXX_target = path.resolve(__dirname, '..', process.env.WASI_SDK_PATH, 'bin', executable('clang++'))
} else if (target === 'wasm') {
env.AR_target = path.resolve(__dirname, '..', process.env.WASI_SDK_PATH, 'bin', executable('ar'))
env.CC_target = path.resolve(__dirname, '..', process.env.WASI_SDK_PATH, 'bin', executable('clang'))
env.CXX_target = path.resolve(__dirname, '..', process.env.WASI_SDK_PATH, 'bin', executable('clang++'))
env.CFLAGS = '--target=wasm32'
}
return env
}

describe('wasm', function () {
it('build simple node-api addon to wasm (wasm32-emscripten)', async function () {
if (!process.env.EMSDK) {
return this.skip('emsdk not found')
}
this.timeout(platformTimeout(1, { win32: 5 }))

const cmd = [
nodeGyp,
'rebuild',
'-C', wasmAddonPath,
'--loglevel=verbose',
'--arch=wasm32',
`--nodedir=${path.dirname(require.resolve('emnapi'))}`,
'--', '-f', 'make'
]
const [err, logLines] = await execFile(cmd, getWasmEnv('emscripten'))
const lastLine = logLines[logLines.length - 1]
assert.strictEqual(err, null)
assert.strictEqual(lastLine, 'gyp info ok', 'should end in ok')
assert.strictEqual(runWasm(), 'world')
})

it('build simple node-api addon to wasm (wasm32-wasip1)', async function () {
if (!process.env.WASI_SDK_PATH) {
return this.skip('wasi-sdk not found')
}
this.timeout(platformTimeout(1, { win32: 5 }))

const cmd = [
nodeGyp,
'rebuild',
'-C', wasmAddonPath,
'--loglevel=verbose',
'--arch=wasm32',
`--nodedir=${path.dirname(require.resolve('emnapi'))}`,
'--', '-f', 'make'
]
const [err, logLines] = await execFile(cmd, getWasmEnv('wasi'))
const lastLine = logLines[logLines.length - 1]
assert.strictEqual(err, null)
assert.strictEqual(lastLine, 'gyp info ok', 'should end in ok')
assert.strictEqual(runWasm(), 'world')
})

it('build simple node-api addon to wasm (wasm32-unknown-unknown)', async function () {
if (!process.env.WASI_SDK_PATH) {
return this.skip('wasi-sdk not found')
}
this.timeout(platformTimeout(1, { win32: 5 }))

const cmd = [
nodeGyp,
'rebuild',
'-C', wasmAddonPath,
'--loglevel=verbose',
'--arch=wasm32',
`--nodedir=${path.dirname(require.resolve('emnapi'))}`,
'--', '-f', 'make'
]
const [err, logLines] = await execFile(cmd, getWasmEnv('wasm'))
const lastLine = logLines[logLines.length - 1]
assert.strictEqual(err, null)
assert.strictEqual(lastLine, 'gyp info ok', 'should end in ok')
assert.strictEqual(runWasm(), 'world')
})
})

0 comments on commit b4cad44

Please sign in to comment.