Skip to content

Commit

Permalink
lint
Browse files Browse the repository at this point in the history
  • Loading branch information
saihaj committed May 1, 2024
1 parent deb0940 commit e8ac473
Show file tree
Hide file tree
Showing 23 changed files with 263 additions and 176 deletions.
10 changes: 7 additions & 3 deletions .github/workflows/napi-release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ name: release napi to npm
env:
DEBUG: napi:*
APP_NAME: lib
MACOSX_DEPLOYMENT_TARGET: "10.13"
MACOSX_DEPLOYMENT_TARGET: '10.13'
permissions:
contents: write
id-token: write
Expand Down Expand Up @@ -78,7 +78,11 @@ jobs:
if: ${{ matrix.settings.docker }}
with:
image: ${{ matrix.settings.docker }}
options: "--user 0:0 -v ${{ github.workspace }}/.cargo-cache/git/db:/usr/local/cargo/git/db -v ${{ github.workspace }}/.cargo/registry/cache:/usr/local/cargo/registry/cache -v ${{ github.workspace }}/.cargo/registry/index:/usr/local/cargo/registry/index -v ${{ github.workspace }}:/build -w /build/libs/napi"
options:
'--user 0:0 -v ${{ github.workspace }}/.cargo-cache/git/db:/usr/local/cargo/git/db -v
${{ github.workspace }}/.cargo/registry/cache:/usr/local/cargo/registry/cache -v ${{
github.workspace }}/.cargo/registry/index:/usr/local/cargo/registry/index -v ${{
github.workspace }}:/build -w /build/libs/napi'
run: ${{ matrix.settings.build }}
- name: Build
run: ${{ matrix.settings.build }}
Expand Down Expand Up @@ -157,4 +161,4 @@ jobs:
npm publish --access public
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
NPM_TOKEN: ${{ secrets.NPM_TOKEN }}
NPM_TOKEN: ${{ secrets.NPM_TOKEN }}
25 changes: 16 additions & 9 deletions packages/cli/src/commands/test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
import { Args, Command, Flags } from '@oclif/core'
import { Matchstick, loadYamlConfig, checkForMatchstickBinary, downloadLatestBinary } from '@graph-tooling/matchstick'
import {
checkForMatchstickBinary,
downloadLatestBinary,
loadYamlConfig,
Matchstick,
} from '@graph-tooling/matchstick';
import { Args, Command, Flags } from '@oclif/core';

export default class TestCommand extends Command {
static description = 'Runs rust binary for subgraph testing.';
Expand Down Expand Up @@ -35,11 +40,14 @@ export default class TestCommand extends Command {
};

async run() {
const { args: { datasource }, flags } = await this.parse(TestCommand)
const {
args: { datasource },
flags,
} = await this.parse(TestCommand);

// Load configuration from YAML file
const config = loadYamlConfig('./matchstick.yaml')
const testsDir = config.testsFolder || './tests'
const config = loadYamlConfig('./matchstick.yaml');
const testsDir = config.testsFolder || './tests';

// Matchstick binary setup
const matchstick = new Matchstick({
Expand All @@ -49,15 +57,14 @@ export default class TestCommand extends Command {
enableCoverage: flags.coverage,
enableRecompile: flags.recompile,
logs: flags.logs,
})
});

// Ensure binary is ready
if (!checkForMatchstickBinary(testsDir) || flags.force) {
await downloadLatestBinary(testsDir)
await downloadLatestBinary(testsDir);
}

// Execute tests
matchstick.run(datasource)
matchstick.run(datasource);
}
}

6 changes: 5 additions & 1 deletion packages/matchstick/.github/workflows/CI.yml
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,11 @@ jobs:
if: ${{ matrix.settings.docker }}
with:
image: ${{ matrix.settings.docker }}
options: '--user 0:0 -v ${{ github.workspace }}/.cargo-cache/git/db:/usr/local/cargo/git/db -v ${{ github.workspace }}/.cargo/registry/cache:/usr/local/cargo/registry/cache -v ${{ github.workspace }}/.cargo/registry/index:/usr/local/cargo/registry/index -v ${{ github.workspace }}:/build -w /build'
options:
'--user 0:0 -v ${{ github.workspace }}/.cargo-cache/git/db:/usr/local/cargo/git/db -v
${{ github.workspace }}/.cargo/registry/cache:/usr/local/cargo/registry/cache -v ${{
github.workspace }}/.cargo/registry/index:/usr/local/cargo/registry/index -v ${{
github.workspace }}:/build -w /build'
run: ${{ matrix.settings.build }}
- name: Build
run: ${{ matrix.settings.build }}
Expand Down
120 changes: 95 additions & 25 deletions packages/matchstick/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,60 +2,93 @@

[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)

👋 Welcome to **Matchstick** - a unit testing framework for The Graph protocol. Try out your mapping logic in a sandboxed environment and ensure your handlers run correctly when deploying your awesome subgraph!
👋 Welcome to **Matchstick** - a unit testing framework for The Graph protocol. Try out your mapping
logic in a sandboxed environment and ensure your handlers run correctly when deploying your awesome
subgraph!

## Quick Start 🚀

**Matchstick** relies on a helper library - [matchstick-as](https://github.com/LimeChain/matchstick-as "matchstick-as"), written in **AssemblyScript** and used as an import in the unit tests.
**Matchstick** relies on a helper library -
[matchstick-as](https://github.com/LimeChain/matchstick-as 'matchstick-as'), written in
**AssemblyScript** and used as an import in the unit tests.

### Configuration ⚙️
Matchstick can be configured to use a custom tests and libs folder via `matchstick.yaml` config file:

Matchstick can be configured to use a custom tests and libs folder via `matchstick.yaml` config
file:

- To change the default tests location (./tests), add `testsFolder: path/to/tests_folder`

- To change the default libs location (./node_modules), add `libsFolder: path/to/node_modules`

- To change the default manifest location (./subgraph.yaml), add `manifestPath: path/to/subgraph.yaml`
- To change the default manifest location (./subgraph.yaml), add
`manifestPath: path/to/subgraph.yaml`

### Docker 🐳
The quickest way to use **Matchstick** "out of the box" is to build and run an ubuntu-based Docker container with a **Matchstick** image. Steps:

The quickest way to use **Matchstick** "out of the box" is to build and run an ubuntu-based Docker
container with a **Matchstick** image. Steps:

- Install [Docker](https://docs.docker.com/get-docker/) if you don't have it already.

- Create a file named `Dockerfile` in the root folder of your subgraph project, and paste [the contents of this file](https://github.com/LimeChain/demo-subgraph/blob/main/Dockerfile) there. Replace `<MATCHSTICK_VERSION>` placeholder with the desired matchstick version. You can find all available versions [here](https://github.com/LimeChain/matchstick/releases)
- Create a file named `Dockerfile` in the root folder of your subgraph project, and paste
[the contents of this file](https://github.com/LimeChain/demo-subgraph/blob/main/Dockerfile)
there. Replace `<MATCHSTICK_VERSION>` placeholder with the desired matchstick version. You can
find all available versions [here](https://github.com/LimeChain/matchstick/releases)

- Build a **Matchstick** image using the following command:

```
docker build -t matchstick .
```

- The build step might take a while, but once that's done we can quickly run our tests like this:
- The build step might take a while, but once that's done we can quickly run our tests like this:

```
docker run -it --rm --mount type=bind,source=<absolute/path/to/project>,target=/matchstick matchstick
```

❗ If you want to pass arguments to **Matchstick** (for instance to test only a specific datasource or to generate a test coverage report) you can do so like this:
❗ If you want to pass arguments to **Matchstick** (for instance to test only a specific datasource
or to generate a test coverage report) you can do so like this:

```
docker run -e ARGS="gravity" -it --rm --mount type=bind,source=<absolute/path/to/project>,target=/matchstick matchstick
```

**Note:** The command will mount the project folder in the container, so you don't need to rebuild the image after every change to your code. Also any changes that happen to files during the run will persist on the host machine as well. [More info about docker bind mounts](https://docs.docker.com/storage/bind-mounts/)
**Note:** The command will mount the project folder in the container, so you don't need to
rebuild the image after every change to your code. Also any changes that happen to files during the
run will persist on the host machine as well.
[More info about docker bind mounts](https://docs.docker.com/storage/bind-mounts/)

After that you can go straight to [the final setup step](https://github.com/LimeChain/matchstick/tree/dockerize#install-dependencies) and you'll be all set to start writing your first unit test.
After that you can go straight to
[the final setup step](https://github.com/LimeChain/matchstick/tree/dockerize#install-dependencies)
and you'll be all set to start writing your first unit test.

❗ If you have previously ran `graph test` you may encounter the following error during `docker build`: `error from sender: failed to xattr node_modules/binary-install-raw/bin/binary-<platform>: permission denied`. In this case create a file named `.dockerignore` in the root folder and add `node_modules/binary-install-raw/bin`
❗ If you have previously ran `graph test` you may encounter the following error during
`docker build`:
`error from sender: failed to xattr node_modules/binary-install-raw/bin/binary-<platform>: permission denied`.
In this case create a file named `.dockerignore` in the root folder and add
`node_modules/binary-install-raw/bin`

❗ Although using the Docker approach is easy, we highly recommend using **Matchstick** via OS-specific binary (which is downloaded automatically when you run `graph test`). The Docker approach should only be considered if for some reason you cannot get `graph test` to work, or if you just want to quickly try something out.
❗ Although using the Docker approach is easy, we highly recommend using **Matchstick** via
OS-specific binary (which is downloaded automatically when you run `graph test`). The Docker
approach should only be considered if for some reason you cannot get `graph test` to work, or if you
just want to quickly try something out.

### OS-specific release binaries ⚙️
The release binary comes in two flavours - for **МacOS** and **Linux**. To add **Matchstick** to your subgraph project just open up a terminal, navigate to the root folder of your project and simply run `graph test` - it downloads the latest **Matchstick** binary and runs the specified test or all tests in a test folder (or all existing tests if no datasource flag is specified). Example usage: `graph test gravity`.

The release binary comes in two flavours - for **МacOS** and **Linux**. To add **Matchstick** to
your subgraph project just open up a terminal, navigate to the root folder of your project and
simply run `graph test` - it downloads the latest **Matchstick** binary and runs the specified test
or all tests in a test folder (or all existing tests if no datasource flag is specified). Example
usage: `graph test gravity`.

❗ If you don't have Postgres installed, you will need to install it. Instructions for that below:
❗❗❗ Since graph-node depends on diesel (and diesel requires a local postgres installation) we highly advise using the commands below as adding it in any other way may cause unexpected errors!
❗❗❗ Since graph-node depends on diesel (and diesel requires a local postgres installation) we
highly advise using the commands below as adding it in any other way may cause unexpected errors!

#### MacOS 

❗ Postgres installation command:

```
Expand All @@ -69,45 +102,82 @@ ln -sf /usr/local/opt/postgresql@14/lib/postgresql@14/libpq.5.dylib /usr/local/o
```

#### Linux 🐧

❗ Postgres installation command (depends on your distro):

```
sudo apt install postgresql
```

#### WSL (Windows Subsystem for Linux) 🤖
You can use Matchstick on WSL both using the Docker approach and the binary approach. As WSL can be a bit tricky, here's a few tips in case you encounter issues like `static BYTES = Symbol("Bytes") SyntaxError: Unexpected token =` or `<PROJECT_PATH>/node_modules/gluegun/build/index.js:13 throw up;`, or anything else that looks off:
#### WSL (Windows Subsystem for Linux) 🤖

Please make sure you're on a newer version of Node.js (graph-cli doesn't support **v10.19.0** anymore, and that is still the default version for new Ubuntu images on WSL. For instance Matchstick is confirmed to be working on WSL with **v18.1.0**, you can switch to it either via **nvm** or if you update your global Node.js). Don't forget to delete `node_modules` and to run `npm install` again after updating you nodejs! Then, make sure you have **libpq** installed, you can do that by running `sudo apt-get install libpq-dev`. And finally, do not use `graph test` (which uses your global installation of graph-cli and for some reason that looks like it's broken on WSL currently), instead use `yarn test` or `npm run test` (that will use the local, project-level instance of graph-cli, which works like a charm. For that you would of course need to have a "test" script in your package.json file which can be something as simple as `"test": "graph test"`).
You can use Matchstick on WSL both using the Docker approach and the binary approach. As WSL can be
a bit tricky, here's a few tips in case you encounter issues like
`static BYTES = Symbol("Bytes") SyntaxError: Unexpected token =` or
`<PROJECT_PATH>/node_modules/gluegun/build/index.js:13 throw up;`, or anything else that looks off:

Please make sure you're on a newer version of Node.js (graph-cli doesn't support **v10.19.0**
anymore, and that is still the default version for new Ubuntu images on WSL. For instance Matchstick
is confirmed to be working on WSL with **v18.1.0**, you can switch to it either via **nvm** or if
you update your global Node.js). Don't forget to delete `node_modules` and to run `npm install`
again after updating you nodejs! Then, make sure you have **libpq** installed, you can do that by
running `sudo apt-get install libpq-dev`. And finally, do not use `graph test` (which uses your
global installation of graph-cli and for some reason that looks like it's broken on WSL currently),
instead use `yarn test` or `npm run test` (that will use the local, project-level instance of
graph-cli, which works like a charm. For that you would of course need to have a "test" script in
your package.json file which can be something as simple as `"test": "graph test"`).

### Install dependencies
In order to use the test helper methods and run the tests, you will need to install the following dependencies:

In order to use the test helper methods and run the tests, you will need to install the following
dependencies:

```
yarn add --dev matchstick-as
```

Now you can jump straight to the [examples](https://github.com/LimeChain/demo-subgraph#readme "examples of tests") in our [demo-subgraph](https://github.com/LimeChain/demo-subgraph "demo-subgraph") and start your journey in Subgraph unit testing!
Now you can jump straight to the
[examples](https://github.com/LimeChain/demo-subgraph#readme 'examples of tests') in our
[demo-subgraph](https://github.com/LimeChain/demo-subgraph 'demo-subgraph') and start your journey
in Subgraph unit testing!

## Building from source

### Prerequisites

To build and run **Matchstick** you need to have the following installed on your system:

- Rust - [How to install Rust](https://www.rust-lang.org/en-US/install.html "How to install Rust")
- Rust - [How to install Rust](https://www.rust-lang.org/en-US/install.html 'How to install Rust')
- PostgreSQL – [PostgreSQL Downloads](https://www.postgresql.org/download/)

### Setup
Clone this repository and run `cargo build`. If that executes successfully congratulations 🎉 you're all set.

**NOTE:** *You may encounter an error, related to missing `libpq` dependencies on your system. In that case - install the missing dependencies (listed in the error log) with your package manager.*
Clone this repository and run `cargo build`. If that executes successfully congratulations 🎉 you're
all set.

**NOTE:** _You may encounter an error, related to missing `libpq` dependencies on your system. In
that case - install the missing dependencies (listed in the error log) with your package manager._

## Next steps 🎯
There is a lot of room for improvements to **Matchstick**. We're trying to gather as much feedback from subgraph developers as we can, to understand how we can solve the problems they face when building subgraphs, as well as how we can make the overall testing process as smooth and streamlined as possible.

There's a GitHub project board where we keep track of day to day work which you can check out [here](https://github.com/LimeChain/matchstick/projects/1 "here").
There is a lot of room for improvements to **Matchstick**. We're trying to gather as much feedback
from subgraph developers as we can, to understand how we can solve the problems they face when
building subgraphs, as well as how we can make the overall testing process as smooth and streamlined
as possible.

There's a GitHub project board where we keep track of day to day work which you can check out
[here](https://github.com/LimeChain/matchstick/projects/1 'here').

You can check out the full list of tasks [here](https://github.com/LimeChain/matchstick/projects/2).

## Technologies used 💻

![diagram-resized](https://user-images.githubusercontent.com/32264020/128724602-81699397-1bb9-4e54-94f5-bb0f40c2a38b.jpg)

The **Matchstick** framework is built in **Rust** and acts as a wrapper for the generated WebAssembly module that contains the mappings and the unit tests. It passes the host function implementations down to the module, to be used in the tests (and in the mappings if needed). The framework also acts as a proxy for structs defined in the [graph-node repo](https://github.com/graphprotocol/graph-node/tree/master/graph "graph-node repo"), because it needs to pass down all the usual imports, as well as a few bonus/mocked ones glued on top.
The **Matchstick** framework is built in **Rust** and acts as a wrapper for the generated
WebAssembly module that contains the mappings and the unit tests. It passes the host function
implementations down to the module, to be used in the tests (and in the mappings if needed). The
framework also acts as a proxy for structs defined in the
[graph-node repo](https://github.com/graphprotocol/graph-node/tree/master/graph 'graph-node repo'),
because it needs to pass down all the usual imports, as well as a few bonus/mocked ones glued on
top.
11 changes: 5 additions & 6 deletions packages/matchstick/__test__/index.spec.mjs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import test from 'ava'
import test from 'ava';
import { sum } from '../index.js';

import { sum } from '../index.js'

test('sum from native', (t) => {
t.is(sum(1, 2), 3)
})
test('sum from native', t => {
t.is(sum(1, 2), 3);
});
25 changes: 15 additions & 10 deletions packages/matchstick/mocks/as/mock-includes.test.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,22 @@
import { handleNewGravatars, createNewGravatarEvent, trySaveGravatarFromContract, saveGravatarFromContract } from "./utils"
import { test, log } from 'matchstick-as/assembly/index'
import { handleCreateGravatar, handleNewGravatar } from "../src/gravity"
import { log, test } from 'matchstick-as/assembly/index';

Check failure on line 1 in packages/matchstick/mocks/as/mock-includes.test.ts

View workflow job for this annotation

GitHub Actions / Lint

'log' is defined but never used. Allowed unused vars must match /^_/u
import { handleCreateGravatar, handleNewGravatar } from '../src/gravity';

Check failure on line 2 in packages/matchstick/mocks/as/mock-includes.test.ts

View workflow job for this annotation

GitHub Actions / Lint

'handleCreateGravatar' is defined but never used. Allowed unused vars must match /^_/u

Check failure on line 2 in packages/matchstick/mocks/as/mock-includes.test.ts

View workflow job for this annotation

GitHub Actions / Lint

'handleNewGravatar' is defined but never used. Allowed unused vars must match /^_/u
import {
createNewGravatarEvent,

Check failure on line 4 in packages/matchstick/mocks/as/mock-includes.test.ts

View workflow job for this annotation

GitHub Actions / Lint

'createNewGravatarEvent' is defined but never used. Allowed unused vars must match /^_/u
handleNewGravatars,

Check failure on line 5 in packages/matchstick/mocks/as/mock-includes.test.ts

View workflow job for this annotation

GitHub Actions / Lint

'handleNewGravatars' is defined but never used. Allowed unused vars must match /^_/u
saveGravatarFromContract,

Check failure on line 6 in packages/matchstick/mocks/as/mock-includes.test.ts

View workflow job for this annotation

GitHub Actions / Lint

'saveGravatarFromContract' is defined but never used. Allowed unused vars must match /^_/u
trySaveGravatarFromContract,

Check failure on line 7 in packages/matchstick/mocks/as/mock-includes.test.ts

View workflow job for this annotation

GitHub Actions / Lint

'trySaveGravatarFromContract' is defined but never used. Allowed unused vars must match /^_/u
} from './utils';

test("0", () => {})
test('0', () => {});

Check failure on line 10 in packages/matchstick/mocks/as/mock-includes.test.ts

View workflow job for this annotation

GitHub Actions / Lint

Unexpected empty arrow function

test("1", () => {})
test('1', () => {});

Check failure on line 12 in packages/matchstick/mocks/as/mock-includes.test.ts

View workflow job for this annotation

GitHub Actions / Lint

Unexpected empty arrow function

test("2", () => {})
test('2', () => {});

Check failure on line 14 in packages/matchstick/mocks/as/mock-includes.test.ts

View workflow job for this annotation

GitHub Actions / Lint

Unexpected empty arrow function

test("3", () => {})
test('3', () => {});

test("4", () => {})
test('4', () => {});

test("5", () => {})
test('5', () => {});

test("6", () => {})
test('6', () => {});
2 changes: 1 addition & 1 deletion packages/matchstick/mocks/as/utils.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
// Mock file
import { Gravatar } from '../generated/schema'
import { Gravatar } from '../generated/schema';
1 change: 0 additions & 1 deletion packages/matchstick/mocks/yamls/matchstick_empty.yaml
Original file line number Diff line number Diff line change
@@ -1 +0,0 @@

1 change: 0 additions & 1 deletion packages/matchstick/mocks/yamls/subgraph_invalid.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ description: Demo Subgraph showcasing unit testing with Matchstick!
repository: https://github.com/LimeChain/demo-subgraph
schema:
file: ./schema.graphql
invalid
dataSources:
- kind: ethereum/contract
name: Gravity
Expand Down
18 changes: 9 additions & 9 deletions packages/matchstick/npm/android-arm-eabi/package.json
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
{
"name": "matchstick-android-arm-eabi",
"version": "0.0.0",
"os": [
"android"
],
"license": "MIT",
"engines": {
"node": ">= 10"
},
"cpu": [
"arm"
],
"os": [
"android"
],
"main": "matchstick.android-arm-eabi.node",
"files": [
"matchstick.android-arm-eabi.node"
],
"license": "MIT",
"engines": {
"node": ">= 10"
}
}
]
}

0 comments on commit e8ac473

Please sign in to comment.