Skip to content

Commit

Permalink
Test the wasm library in pull requests
Browse files Browse the repository at this point in the history
  • Loading branch information
cormacrelf committed Sep 18, 2020
1 parent ef349c1 commit 3e6279b
Show file tree
Hide file tree
Showing 9 changed files with 4,967 additions and 43 deletions.
95 changes: 70 additions & 25 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@ jobs:
name: CSL Test Suite Regressions
runs-on: ubuntu-18.04
steps:
- uses: actions/checkout@v1
- uses: actions/checkout@v2
with:
submodules: recursive

- name: Extract branch name
shell: bash
run: |
Expand All @@ -29,13 +30,12 @@ jobs:
GITHUB_HEAD=${GITHUB_HEAD#refs/heads/}
GITHUB_HEAD=$(echo ${GITHUB_HEAD#refs/} | tr '/' '-')
echo "GITHUB_HEAD = ${GITHUB_HEAD}"
echo "##[set-output name=head;]${GITHUB_HEAD}"
echo "::set-output name=head::${GITHUB_HEAD}"
GITHUB_BASE=${GITHUB_BASE#refs/heads/}
GITHUB_BASE=$(echo ${GITHUB_BASE#refs/} | tr '/' '-')
echo "GITHUB_BASE = ${GITHUB_BASE}"
echo "##[set-output name=base;]${GITHUB_BASE}"
echo "::set-output name=base::${GITHUB_BASE}"
id: branch
- name: Download base output
Expand All @@ -48,29 +48,20 @@ jobs:
run: |
mkdir -p .snapshots/branches
aws s3 cp s3://citeproc-rs-test-results/.snapshots/branches/$GITHUB_BASE .snapshots/branches/$GITHUB_BASE
- uses: hecrj/setup-rust-action@v1
with:
rust-version: nightly
- name: Create ~/.cargo directory
run: |
mkdir -p ~/.cargo/registry
mkdir -p ~/.cargo/git
mkdir -p ./target
- name: Cache cargo registry
uses: actions/cache@v1
with:
path: ~/.cargo/registry
key: ${{ runner.os }}-cargo-registry-${{ hashFiles('**/Cargo.lock') }}
- name: Cache cargo index
uses: actions/cache@v1
- name: Install latest nightly
uses: actions-rs/toolchain@v1
with:
path: ~/.cargo/git
key: ${{ runner.os }}-cargo-index-${{ hashFiles('**/Cargo.lock') }}
- name: Cache cargo build
uses: actions/cache@v1
toolchain: nightly
override: true
- name: Cache cargo directories
uses: actions/cache@v2
with:
path: target
key: ${{ runner.os }}-cargo-build-target-${{ hashFiles('**/Cargo.lock') }}
path: |
~/.cargo/registry
~/.cargo/git
target
key: ${{ runner.os }}-cargo-${{ hashFiles('**/Cargo.lock') }}
- name: "Build tools package"
run: cargo build --package tools
- name: "Pull locales"
Expand All @@ -96,3 +87,57 @@ jobs:
aws s3 cp .snapshots/current s3://citeproc-rs-test-results/.snapshots/branches/$GITHUB_HEAD
aws s3 cp .snapshots/current s3://citeproc-rs-test-results/.snapshots/commits/$GITHUB_SHA
wasm_tests:
name: "Test the WASM package from a JS environment"
runs-on: ubuntu-18.04
steps:
- uses: actions/checkout@v2
- name: Install latest nightly
uses: actions-rs/toolchain@v1
with:
toolchain: nightly
override: true
- name: Cache cargo directories
uses: actions/cache@v2
with:
path: |
~/.cargo/registry
~/.cargo/git
target
# use "-wasm-target" to cache these separately from the CI workflow
key: ${{ runner.os }}-cargo-wasm-target-${{ hashFiles('**/Cargo.lock') }}

- name: Install wasm-pack
run: curl https://rustwasm.github.io/wasm-pack/installer/init.sh -sSf | sh -s -- -f

- name: Build WASM
working-directory: crates/wasm
run: wasm-pack build --target nodejs --out-dir pkg-nodejs --scope citeproc-rs

- name: Get yarn cache directory path
id: yarn-cache-dir-path
run: echo "::set-output name=dir::$(yarn cache dir)"
- name: Restore yarn cache
uses: actions/cache@v2
id: yarn-cache
with:
path: ${{ steps.yarn-cache-dir-path.outputs.dir }}
key: ${{ runner.os }}-yarn-${{ hashFiles('**/yarn.lock') }}
restore-keys: |
${{ runner.os }}-yarn-
- name: Yarn install
working-directory: crates/wasm/js-tests
run: yarn install

- name: Run tests
working-directory: crates/wasm/js-tests
run: yarn test

# otherwise, the yarn cache will contain a different @citeproc-rs/wasm
# entry every time.
- name: Clean yarn cache dir of wasm output
run: |
cd "$(yarn cache dir)"
ls | grep citeproc-rs-wasm | xargs rm -rf
1 change: 1 addition & 0 deletions crates/wasm/js-tests/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules
6 changes: 6 additions & 0 deletions crates/wasm/js-tests/babel.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module.exports = {
presets: [
['@babel/preset-env', {targets: {node: 'current'}}],
'@babel/preset-typescript',
],
};
83 changes: 83 additions & 0 deletions crates/wasm/js-tests/index.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
import { Driver, UpdateSummary } from '@citeproc-rs/wasm';

const mkStyle = (inner: string) => {
return `
<style class="note">
<citation>
<layout>
${inner}
</layout>
</citation>
</style>
`;
}

const mkLocale = (lang: string, terms: { [key: string]: string }) => {
return `
<?xml version="1.0" encoding="utf-8"?>
<locale xml:lang="${lang}">
<terms>
${ Object.entries(terms).map((k,v) => `<term name="${k}">${v}</term>`).join("\n") }
</terms>
</locale>
`;
}

class Fetcher {
constructor(private callback: (lang: string) => void, private factory: (lang: string) => string) { }
async fetchLocale(lang: string) {
this.callback(lang);
return this.factory(lang);
}
}

const boringFetcher = new Fetcher(() => {}, (lang: string) => mkLocale(lang, {}));
const withDriver = (cfg: any, callback: (driver: Driver) => void) => {
let style = cfg.style || mkStyle('<text variable="title" />');
let fetcher = cfg.fetcher || boringFetcher;
let fmt = cfg.format || "plain";
let driver = Driver.new(style, fetcher, fmt);
callback(driver);
driver.free();
};
const oneOneOne = (driver: Driver, r?: any) => {
let refr = {
type: "book",
title: "TEST_TITLE",
...r,
id: "citekey"
}
driver.insertReference(refr);
driver.initClusters([{id: 1, cites: [{id: "citekey"}]}]);
driver.setClusterOrder([{ id: 1 }]);
};

test('boots', () => {
withDriver({}, driver => {
expect(driver).not.toBeNull();
});
});

test('returns a single cluster, single cite, single ref', () => {
withDriver({}, driver => {
expect(driver).not.toBeNull();
oneOneOne(driver);
driver.insertReference({ id: "citekey", type: "book", title: "TEST_TITLE" });
driver.initClusters([{id: 1, cites: [{id: "citekey"}]}]);
driver.setClusterOrder([{ id: 1 }]);
let res = driver.builtCluster(1);
expect(res).toBe("TEST_TITLE");
});
});

test('gets an update when ref changes', () => {
withDriver({}, driver => {
let updates: UpdateSummary;
oneOneOne(driver);
updates = driver.batchedUpdates();
expect(updates.clusters).toContainEqual([1, "TEST_TITLE"]);
driver.insertReference({ id: "citekey", type: "book", title: "TEST_TITLE_2" });
updates = driver.batchedUpdates();
expect(updates.clusters).toContainEqual([1, "TEST_TITLE_2"]);
});
})
Loading

0 comments on commit 3e6279b

Please sign in to comment.