Skip to content

Commit

Permalink
Add test for build files
Browse files Browse the repository at this point in the history
  • Loading branch information
stwiname committed Aug 13, 2024
1 parent c87301c commit 6f6594e
Show file tree
Hide file tree
Showing 8 changed files with 113 additions and 37 deletions.
2 changes: 1 addition & 1 deletion packages/cli/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]
### Fixed
- Glob sync issue
- Glob sync issue (#2534)

## [5.2.1] - 2024-08-12
### Changed
Expand Down
39 changes: 3 additions & 36 deletions packages/cli/src/commands/build/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,10 @@
// SPDX-License-Identifier: GPL-3.0

import assert from 'assert';
import {existsSync, lstatSync, readFileSync} from 'fs';
import {existsSync, lstatSync} from 'fs';
import path from 'path';
import {Command, Flags} from '@oclif/core';
import {globSync} from 'glob';
import {runWebpack} from '../../controller/build-controller';
import {getBuildEntries, runWebpack} from '../../controller/build-controller';
import {resolveToAbsolutePath, buildManifestFromLocation, getTsManifest} from '../../utils';

export default class Build extends Command {
Expand Down Expand Up @@ -41,41 +40,9 @@ export default class Build extends Command {
);
}

// Get the output location from the project package.json main field
const pjson = JSON.parse(readFileSync(path.join(directory, 'package.json')).toString());

const defaultEntry = path.join(directory, 'src/index.ts');
const buildEntries = getBuildEntries(directory);
const outputDir = path.resolve(directory, flags.output ?? 'dist');

let buildEntries: Record<string, string> = {
index: defaultEntry,
};
globSync(path.join(directory, 'src/test/**/*.test.ts')).forEach((testFile) => {
const testName = path.basename(testFile).replace('.ts', '');
buildEntries[`test/${testName}`] = testFile;
});

globSync(path.join(directory, 'src/tests/**/*.test.ts')).forEach((testFile) => {
const testName = path.basename(testFile).replace('.ts', '');
buildEntries[`tests/${testName}`] = testFile;
});

if (pjson.exports && typeof pjson.exports !== 'string') {
buildEntries = Object.entries(pjson.exports as Record<string, string>).reduce(
(acc, [key, value]) => {
acc[key] = path.resolve(directory, value);
return acc;
},
{...buildEntries}
);
}

for (const i in buildEntries) {
if (typeof buildEntries[i] !== 'string') {
this.warn(`Ignoring entry ${i} from build.`);
delete buildEntries[i];
}
}
await runWebpack(buildEntries, directory, outputDir, isDev, true);
if (!flags.silent) {
this.log('Building and packing code ...');
Expand Down
16 changes: 16 additions & 0 deletions packages/cli/src/controller/build-controller.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// Copyright 2020-2024 SubQuery Pte Ltd authors & contributors
// SPDX-License-Identifier: GPL-3.0

import path from 'path';
import {getBuildEntries} from './build-controller';

describe('build controller', () => {
it('picks up test and export files', () => {
const dir = path.resolve(__dirname, '../../test/build');

const entries = getBuildEntries(dir);

expect(entries['test/mappingHandler.test']).toEqual(path.resolve(dir, './src/test/mappingHandler.test.ts'));
expect(entries.chaintypes).toEqual(path.resolve(dir, './src/chainTypes.ts'));
});
});
42 changes: 42 additions & 0 deletions packages/cli/src/controller/build-controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
// SPDX-License-Identifier: GPL-3.0

import assert from 'assert';
import {readFileSync} from 'fs';
import path from 'path';
import {globSync} from 'glob';
import TerserPlugin from 'terser-webpack-plugin';
import webpack, {Configuration} from 'webpack';
import {merge} from 'webpack-merge';
Expand Down Expand Up @@ -92,3 +95,42 @@ export async function runWebpack(
});
});
}

export function getBuildEntries(directory: string): Record<string, string> {
// FIXME: this is an assumption that the default entry is src/index.ts, in reality it should read from the project manifest
const defaultEntry = path.join(directory, 'src/index.ts');
let buildEntries: Record<string, string> = {
index: defaultEntry,
};

globSync(path.join(directory, 'src/test/**/*.test.ts')).forEach((testFile) => {
const testName = path.basename(testFile).replace('.ts', '');
buildEntries[`test/${testName}`] = testFile;
});

globSync(path.join(directory, 'src/tests/**/*.test.ts')).forEach((testFile) => {
const testName = path.basename(testFile).replace('.ts', '');
buildEntries[`tests/${testName}`] = testFile;
});

// Get the output location from the project package.json main field
const pjson = JSON.parse(readFileSync(path.join(directory, 'package.json')).toString());
if (pjson.exports && typeof pjson.exports !== 'string') {
buildEntries = Object.entries(pjson.exports as Record<string, string>).reduce(
(acc, [key, value]) => {
acc[key] = path.resolve(directory, value);
return acc;
},
{...buildEntries}
);
}

for (const i in buildEntries) {
if (typeof buildEntries[i] !== 'string') {
console.warn(`Ignoring entry ${i} from build.`);
delete buildEntries[i];
}
}

return buildEntries;
}
37 changes: 37 additions & 0 deletions packages/cli/test/build/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
{
"name": "polkadot-starter",
"version": "1.0.0",
"description": "This project can be used as a starting point for developing your SubQuery project",
"main": "dist/index.js",
"scripts": {
"build": "subql build",
"codegen": "subql codegen",
"start:docker": "docker-compose pull && docker-compose up --remove-orphans",
"dev": "subql codegen && subql build && docker-compose pull && docker-compose up --remove-orphans",
"prepack": "rm -rf dist && npm run build",
"test": "subql build && subql-node test"
},
"homepage": "https://github.com/subquery/subql-starter",
"repository": "github:subquery/subql-starter",
"files": [
"dist",
"schema.graphql",
"project.yaml"
],
"author": "SubQuery Team",
"license": "MIT",
"devDependencies": {
"@polkadot/api": "^11",
"@subql/cli": "latest",
"@subql/testing": "latest",
"@subql/types": "latest",
"typescript": "^5.2.2",
"@subql/common-substrate": "^4.0.1"
},
"resolutions": {
"ipfs-unixfs": "6.0.6"
},
"exports": {
"chaintypes": "src/chainTypes.ts"
}
}
Empty file.
3 changes: 3 additions & 0 deletions packages/cli/test/build/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
//Exports all handler functions
// export * from "./mappings/mappingHandlers";
import '@polkadot/api-augment';
11 changes: 11 additions & 0 deletions packages/cli/test/build/src/test/mappingHandler.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import {subqlTest} from '@subql/testing';

// See https://academy.subquery.network/build/testing.html

subqlTest(
'handleTransfer test', // Test name
191, // Block height to test at
[], // Dependent entities
[], // Expected entities
'handleEvent' // handler name
);

0 comments on commit 6f6594e

Please sign in to comment.