Skip to content

Commit

Permalink
Migrate tests to Ava (#799)
Browse files Browse the repository at this point in the history
* [cli] Migrate tests to Ava
* [functions] Migrate tests to Ava
* [extensions] Migrate tests to Ava
* [core] Migrate tests to Ava
* Suppress experimental node.js warnings during test runs.
* Refactor CI to improve speed, avoid 'set output'

https://github.blog/changelog/2022-10-11-github-actions-deprecating-save-state-and-set-output-commands/
  • Loading branch information
donmccurdy authored Jan 24, 2023
1 parent 2d6928b commit d3991c3
Show file tree
Hide file tree
Showing 91 changed files with 2,084 additions and 2,828 deletions.
File renamed without changes.
54 changes: 31 additions & 23 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,47 +5,55 @@ on:
pull_request:
branches: [main]
jobs:
#############################################################################
# BUILD
build:
runs-on: ubuntu-latest
strategy:
matrix:
node-version: [14.x, 18.x]
env:
CI: true
COVERAGE: ${{ matrix.node-version == '18.x' && true || false }}
steps:
# Setup.
- uses: actions/checkout@v3
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v3
with:
node-version: ${{ matrix.node-version }}
cache: yarn

# Cache.
- id: yarn-cache-dir-path
run: echo "::set-output name=dir::$(yarn cache dir)"
- uses: actions/cache@v3
id: yarn-cache
with:
path: ${{ steps.yarn-cache-dir-path.outputs.dir }}
key: ${{ runner.os }}-${{ matrix.node-version }}-yarn-${{ hashFiles('./yarn.lock') }}
restore-keys: |
${{ runner.os }}-${{ matrix.node-version }}-yarn-
# Build.
- run: yarn install --frozen-lockfile
- run: yarn dist

# Test.
- run: yarn install
- run: yarn dist
- run: yarn test
- run: yarn test --no-worker-threads # https://github.com/avajs/ava/discussions/3158
- run: yarn lint

#############################################################################
# COVERAGE
coverage:
runs-on: ubuntu-latest
env:
CI: true
NODE_VERSION: 18.x
steps:
# Setup.
- uses: actions/checkout@v3
- name: Use Node.js ${{ env.NODE_VERSION }}
uses: actions/setup-node@v3
with:
node-version: ${{ env.NODE_VERSION }}
cache: yarn

# Build.
- run: yarn install --frozen-lockfile
- run: yarn dist

# Coverage.
- name: Run coverage
if: ${{ env.COVERAGE == 'true' }}
run: |
yarn coverage
yarn coverage:report
- name: Report coverage
if: ${{ env.COVERAGE == 'true' }}
uses: codecov/codecov-action@v3
- run: yarn coverage --no-worker-threads # https://github.com/avajs/ava/discussions/3158
- run: yarn coverage:report
- uses: codecov/codecov-action@v3
with:
files: .nyc_output/coverage.lcov
files: coverage/coverage.lcov
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,4 @@ packages/**/.rts2*
docs/dist/**

# coverage
.nyc_output
coverage
21 changes: 15 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@
"watch:debug": "lerna run watch:debug --parallel --stream",
"clean:dist": "rm -rf packages/*/dist/**",
"clean:deps": "rm -rf packages/*/node_modules/**",
"test": "ts-node node_modules/tape/bin/tape packages/*/test/{*/,}*.test.ts | tap-spec",
"coverage": "nyc --reporter=lcov --reporter=text ts-node node_modules/tape/bin/tape packages/*/test/{*/,}*.test.ts",
"coverage:report": "nyc report --reporter=text-lcov > .nyc_output/coverage.lcov",
"test": "ava packages/{core,extensions,functions,cli}/test/**/*.test.ts",
"coverage": "c8 --reporter=lcov --reporter=text yarn test --tap",
"coverage:report": "c8 report --reporter=text-lcov > coverage/coverage.lcov",
"lint": "eslint \"packages/*/{src,test}/**/*.ts\"",
"validate": "scripts/validate.js",
"preversion": "yarn run test",
Expand All @@ -35,6 +35,8 @@
"@types/tape": "4.13.2",
"@typescript-eslint/eslint-plugin": "5.48.2",
"@typescript-eslint/parser": "5.48.2",
"ava": "^5.1.1",
"c8": "^7.12.0",
"canvas": "2.11.0",
"concurrently": "7.6.0",
"draco3dgltf": "1.5.5",
Expand All @@ -45,13 +47,20 @@
"meshoptimizer": "0.18.1",
"microbundle": "0.14.2",
"mikktspace": "1.1.1",
"nyc": "15.1.0",
"prettier": "2.8.3",
"source-map-support": "0.5.21",
"tap-spec": "5.0.0",
"tape": "5.6.3",
"suppress-experimental-warnings": "^1.1.3",
"ts-node": "10.9.1",
"typedoc": "0.21.2",
"typescript": "4.9.4"
},
"ava": {
"extensions": {
"ts": "module"
},
"nodeArguments": [
"--loader=ts-node/esm",
"--require=suppress-experimental-warnings"
]
}
}
29 changes: 14 additions & 15 deletions packages/cli/test/cli.test.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
require('source-map-support').install();

import fs from 'fs';
import path from 'path';
import test from 'tape';
import test from 'ava';
import tmp from 'tmp';
import { Document, FileUtils, NodeIO } from '@gltf-transform/core';
import { ALL_EXTENSIONS } from '@gltf-transform/extensions';
import { program, programReady } from '../';
import { program, programReady } from '@gltf-transform/cli';

const draco3d = require('draco3dgltf');
const { MeshoptDecoder } = require('meshoptimizer');
Expand All @@ -27,8 +25,8 @@ test('@gltf-transform/cli::copy', async (t) => {

return program.exec(['copy', input, output]).then(async () => {
const doc2 = await io.read(output);
t.ok(doc2, 'roundtrip document');
t.equal(doc2.getRoot().listMaterials()[0].getName(), 'MyMaterial', 'roundtrip material');
t.truthy(doc2, 'roundtrip document');
t.is(doc2.getRoot().listMaterials()[0].getName(), 'MyMaterial', 'roundtrip material');
});
});

Expand All @@ -43,7 +41,7 @@ test('@gltf-transform/cli::meshopt', async (t) => {

return program.exec(['meshopt', input, output], { silent: true }).then(async () => {
const doc2 = await io.read(output);
t.ok(doc2, 'meshopt succeeds');
t.truthy(doc2, 'meshopt succeeds');
});
});

Expand All @@ -58,11 +56,11 @@ test('@gltf-transform/cli::draco', async (t) => {

return program.exec(['draco', input, output], { silent: true }).then(async () => {
const doc2 = await io.read(output);
t.ok(doc2, 'draco succeeds');
t.truthy(doc2, 'draco succeeds');
});
});

test('@gltf-transform/cli::validate', async (_t) => {
test('@gltf-transform/cli::validate', async (t) => {
await programReady;
const io = new NodeIO();
const input = tmp.tmpNameSync({ postfix: '.glb' });
Expand All @@ -73,10 +71,11 @@ test('@gltf-transform/cli::validate', async (_t) => {
doc.createMaterial('MyMaterial').setBaseColorFactor([1, 0, 0, 1]);
await io.write(input, doc);

return program.exec(['validate', input], { silent: true });
await program.exec(['validate', input], { silent: true });
t.pass();
});

test('@gltf-transform/cli::inspect', async (_t) => {
test('@gltf-transform/cli::inspect', async (t) => {
await programReady;
const io = new NodeIO();
const input = tmp.tmpNameSync({ postfix: '.glb' });
Expand All @@ -90,7 +89,8 @@ test('@gltf-transform/cli::inspect', async (_t) => {
doc.createAnimation();
await io.write(input, doc);

return program.exec(['inspect', input], { silent: true });
await program.exec(['inspect', input], { silent: true });
t.pass();
});

test('@gltf-transform/cli::merge', async (t) => {
Expand Down Expand Up @@ -127,7 +127,6 @@ test('@gltf-transform/cli::merge', async (t) => {
const root = doc.getRoot();
const sceneNames = root.listScenes().map((s) => s.getName());
const texName = root.listTextures()[0].getName();
t.deepEquals(sceneNames, ['SceneA', FileUtils.basename(inputB)], 'merge scenes');
t.equals(texName, FileUtils.basename(inputC), 'merge textures');
t.end();
t.deepEqual(sceneNames, ['SceneA', FileUtils.basename(inputB)], 'merge scenes');
t.is(texName, FileUtils.basename(inputC), 'merge textures');
});
18 changes: 7 additions & 11 deletions packages/cli/test/ktxfix.test.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
require('source-map-support').install();

import fs from 'fs';
import path from 'path';
import { KHR_DF_PRIMARIES_BT709, KHR_DF_PRIMARIES_UNSPECIFIED, read } from 'ktx-parse';
import test from 'tape';
import test from 'ava';
import { Document, Logger, Texture } from '@gltf-transform/core';
import { ktxfix } from '../';
import { ktxfix } from '@gltf-transform/cli';

test('@gltf-transform/cli::ktxfix', async (t) => {
const doc = new Document().setLogger(new Logger(Logger.Verbosity.SILENT));
Expand All @@ -15,28 +13,26 @@ test('@gltf-transform/cli::ktxfix', async (t) => {
.setMimeType('image/ktx2')
.setImage(fs.readFileSync(path.join(__dirname, 'in', 'test.ktx2')));

t.equals(getColorPrimaries(texture), KHR_DF_PRIMARIES_BT709, 'initial - sRGB');
t.is(getColorPrimaries(texture), KHR_DF_PRIMARIES_BT709, 'initial - sRGB');

await doc.transform(ktxfix());

t.equals(getColorPrimaries(texture), KHR_DF_PRIMARIES_BT709, 'unused - no change');
t.is(getColorPrimaries(texture), KHR_DF_PRIMARIES_BT709, 'unused - no change');

material.setOcclusionTexture(texture);
await doc.transform(ktxfix());

t.equals(getColorPrimaries(texture), KHR_DF_PRIMARIES_UNSPECIFIED, 'occlusion - unspecified');
t.is(getColorPrimaries(texture), KHR_DF_PRIMARIES_UNSPECIFIED, 'occlusion - unspecified');

texture.detach();
await doc.transform(ktxfix());

t.equals(getColorPrimaries(texture), KHR_DF_PRIMARIES_UNSPECIFIED, 'unused - no change');
t.is(getColorPrimaries(texture), KHR_DF_PRIMARIES_UNSPECIFIED, 'unused - no change');

material.setBaseColorTexture(texture);
await doc.transform(ktxfix());

t.equals(getColorPrimaries(texture), KHR_DF_PRIMARIES_BT709, 'base color - sRGB');

t.end();
t.is(getColorPrimaries(texture), KHR_DF_PRIMARIES_BT709, 'base color - sRGB');
});

function getColorPrimaries(texture: Texture): number {
Expand Down
24 changes: 8 additions & 16 deletions packages/cli/test/toktx.test.ts
Original file line number Diff line number Diff line change
@@ -1,48 +1,40 @@
require('source-map-support').install();

import fs from 'fs/promises';
import test from 'tape';
import test from 'ava';
import { Document, Logger, TextureChannel, vec2 } from '@gltf-transform/core';
import { KHRMaterialsClearcoat } from '@gltf-transform/extensions';
import { Mode, mockCommandExists, mockSpawn, toktx, mockWaitExit } from '../';
import { Mode, mockCommandExists, mockSpawn, toktx, mockWaitExit } from '@gltf-transform/cli';
import type { ChildProcess } from 'child_process';

const { R, G } = TextureChannel;

test('@gltf-transform/cli::toktx | resize', async (t) => {
t.equals(await getParams({ mode: Mode.ETC1S }, [508, 508]), '--genmipmap --bcmp', '508x508 → no change');
t.is(await getParams({ mode: Mode.ETC1S }, [508, 508]), '--genmipmap --bcmp', '508x508 → no change');

t.equals(
await getParams({ mode: Mode.ETC1S }, [507, 509]),
'--genmipmap --bcmp --resize 508x512',
'507x509 → 508x512'
);
t.is(await getParams({ mode: Mode.ETC1S }, [507, 509]), '--genmipmap --bcmp --resize 508x512', '507x509 → 508x512');

t.equals(
t.is(
await getParams({ mode: Mode.ETC1S, powerOfTwo: true }, [508, 508]),
'--genmipmap --bcmp --resize 512x512',
'508x508+powerOfTwo → 512x512'
);

t.equals(
t.is(
await getParams({ mode: Mode.ETC1S, powerOfTwo: true }, [5, 3]),
'--genmipmap --bcmp --resize 4x4',
'5x3+powerOfTwo → 4x4'
);

t.equals(
t.is(
await getParams({ mode: Mode.ETC1S }, [508, 508], R),
'--genmipmap --bcmp --assign_oetf linear --assign_primaries none --target_type R',
'channels → R'
);

t.equals(
t.is(
await getParams({ mode: Mode.ETC1S }, [508, 508], G),
'--genmipmap --bcmp --assign_oetf linear --assign_primaries none --target_type RG',
'channels → RG'
);

t.end();
});

async function getParams(options: Record<string, unknown>, size: vec2, channels = 0): Promise<string> {
Expand Down
11 changes: 5 additions & 6 deletions packages/cli/test/util.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import test from 'tape';
import { formatBytes, formatHeader, formatParagraph } from '../';
import test from 'ava';
import { formatBytes, formatHeader, formatParagraph } from '@gltf-transform/cli';

const HEADER = `
HELLO
Expand All @@ -15,8 +15,7 @@ chocolate cake. Liquorice jelly-o pie jujubes fruitcake chocolate bar jelly-o
tart. Marshmallow icing tart tootsie roll brownie dragée.`.trim();

test('@gltf-transform/cli::util', (t) => {
t.equals(formatBytes(1000), '1 KB', 'formatBytes');
t.equals(formatHeader('Hello'), HEADER, 'formatHeader');
t.equals(formatParagraph(TEXT), PARAGRAPH, 'formatParagraph');
t.end();
t.is(formatBytes(1000), '1 KB', 'formatBytes');
t.is(formatHeader('Hello'), HEADER, 'formatHeader');
t.is(formatParagraph(TEXT), PARAGRAPH, 'formatParagraph');
});
5 changes: 5 additions & 0 deletions packages/cli/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,14 @@
"src/**/*",
"../global.d.ts"
],
"paths": {
"@gltf-transform/cli": ["./"]
},
"compilerOptions": {
"outDir": "dist",
"strict": true,
"importsNotUsedAsValues": "error",
"module": "ES2020",
"target": "ES2020"
}
}
Loading

0 comments on commit d3991c3

Please sign in to comment.