Skip to content

Commit

Permalink
Fix #97 extending to async user configuration file (#99)
Browse files Browse the repository at this point in the history
* extend loadConfig to accept promises

* add tests


* update README and docs
  • Loading branch information
alxiong authored and marekkirejczyk committed Apr 4, 2019
1 parent 2828053 commit 9ab5eed
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 10 deletions.
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,10 @@ Sweeter, simpler and faster than Truffle.
* `import "openzeppelin-solidity/contracts/token/ERC20/ERC20.sol";`
* Fixtures that help write fast and maintainable test suites, e.g.:
* `const {token} = await loadFixture(standardTokenWithBalance);`
* Sub-second compilation with native and dockerized solc
* Customizable compilation options with native solc, dockerized solc and any version of solc-js loaded remotely at compiled time
* Support for promise-based configuration, e.g.:
* use native solc binary for fast compilation in CI environment
* use solc-js based on contract versions detected (async)
* Support for TypeScript
* [Documentation](https://ethereum-waffle.readthedocs.io/en/latest/)

Expand Down
12 changes: 12 additions & 0 deletions docs/source/configuration.rst
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,18 @@ Configuration can also be of type js, e.g.:

Native and dockerized solc compiler configuration is described in "Fast compilation" section.

Configuration can even be a Promise in a js, e.g.:
::

module.exports = new Promise((resolve, reject) => {
resolve({
"compiler": "native"
});
});

.. hint::
This is a powerful feature if you want to asynchronously load different compliation configurations in different environments.
For example, you can use native solc in CI for faster compilation, whereas deciding the exact solc-js version locally based on the contract versions being used, since many of those operations are asynchronous, you'll most likely be returning a Promise to waffle to handle.

Solcjs and version management
-----------------------------
Expand Down
4 changes: 2 additions & 2 deletions lib/compiler/compiler.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {Config} from '../config/config';
import defaultConfig, {Config} from '../config/config';
import {isWarningMessage} from '../utils';
import {getCompileFunction} from './getCompileFunction';
import {findInputs} from './findInputs';
Expand All @@ -9,7 +9,7 @@ import {ImportsFsEngine, resolvers} from '@resolver-engine/imports-fs';
import {gatherSources} from '@resolver-engine/imports';

export async function compileProject(configPath: string) {
await compileAndSave(loadConfig(configPath));
await compileAndSave(await loadConfig(configPath));
}

export async function compileAndSave(config: Config) {
Expand Down
20 changes: 15 additions & 5 deletions lib/config/loadConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,19 @@ function readConfigFile(configPath: string) {
return {};
}

export function loadConfig(configPath: string): Config {
return {
...defaultConfig,
...readConfigFile(configPath)
};
export async function loadConfig(configPath: string): Promise<Config> {
if (configPath) {
return new Promise(async (resolve, reject) => {
resolve({
...defaultConfig,
...await require(path.join(process.cwd(), configPath))
});
});
} else {
return new Promise((resolve, reject) => {
resolve({
...defaultConfig
});
});
}
}
6 changes: 4 additions & 2 deletions test/compiler/e2e.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import fsx from 'fs-extra';
import {join, resolve} from 'path';
import {expect} from 'chai';
import {compileProject} from '../../lib/compiler/compiler';
import defaultConfig, {Config} from '../../lib/config/config';
import {loadConfig} from '../../lib/config/loadConfig';
import {readFileContent, isFile, deepCopy} from '../../lib/utils';
import {deployContract, link, getWallets, createMockProvider} from '../../lib/waffle';
Expand All @@ -11,6 +12,7 @@ const configurations = [
'./test/projects/custom/config.js',
'./test/projects/custom/config_native.json',
'./test/projects/custom/config_docker.json',
'./test/projects/custom/config_promise.js',
'./test/projects/custom_solidity_4/config_solcjs.json',
'./test/projects/custom_solidity_4/config_docker.json'
];
Expand All @@ -25,7 +27,7 @@ const artefacts = [
'OneAndAHalf.json'
];

describe('E2E: Compiler integration', () => {
describe('E2E: Compiler integration', async () => {
describe('docker: inside out directory structure', () => {
before(async () => {
fsx.removeSync('test/projects/insideOut/build');
Expand All @@ -46,7 +48,7 @@ describe('E2E: Compiler integration', () => {
});

for (const configurationPath of configurations) {
const configuration = loadConfig(configurationPath) as any;
const configuration = await loadConfig(configurationPath) as any;
const {name, targetPath, legacyOutput} = configuration;

/* eslint-disable no-loop-func */
Expand Down
8 changes: 8 additions & 0 deletions test/projects/custom/config_promise.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
module.exports = new Promise((resolve, reject) => {
resolve({
name: "solidity 5, promise returned solcjs config",
sourcesPath: "./test/projects/custom/custom_contracts",
targetPath: "./test/projects/custom/custom_build",
npmPath: "./test/projects/custom/custom_node_modules"
});
});

0 comments on commit 9ab5eed

Please sign in to comment.