feat(@embark/site): add new docs for the v5 tests

This commit is contained in:
Jonathan Rainville 2019-06-11 11:58:26 -04:00
parent 9dcf236ab7
commit 9bc5bdeb5a
11 changed files with 86 additions and 28 deletions

View File

@ -1,4 +1,4 @@
/*global contract, config, it, assert*/ /*global contract, config, it, assert, web3*/
const SimpleStorage = require('Embark/contracts/SimpleStorage'); const SimpleStorage = require('Embark/contracts/SimpleStorage');
let accounts; let accounts;
@ -12,12 +12,14 @@ config({
// ] // ]
//}, //},
contracts: { contracts: {
"SimpleStorage": { deploy: {
args: [100] "SimpleStorage": {
args: [100]
}
} }
} }
}, (_err, web3_accounts) => { }, (_err, web3_accounts) => {
accounts = web3_accounts accounts = web3_accounts;
}); });
contract("SimpleStorage", function () { contract("SimpleStorage", function () {

View File

@ -6,6 +6,7 @@ const EmbarkJS = require('Embark/EmbarkJS');
config({ config({
namesystem: { namesystem: {
enabled: true,
"register": { "register": {
"rootDomain": "embark.eth", "rootDomain": "embark.eth",
"subdomains": { "subdomains": {

View File

@ -5,6 +5,9 @@ const SimpleStorage = require('Embark/contracts/SimpleStorage');
let accounts; let accounts;
config({ config({
namesystem: {
enabled: true
},
contracts: { contracts: {
deploy: { deploy: {
"SimpleStorage": { "SimpleStorage": {

View File

@ -4,7 +4,7 @@ let shelljs = require('shelljs');
import { Proxy } from './proxy'; import { Proxy } from './proxy';
import { IPC } from 'embark-core'; import { IPC } from 'embark-core';
const constants = require('embark-core/constants'); const constants = require('embark-core/constants');
import { AccountParser, dappPath, defaultHost, dockerHostSwap, embarkPath } from 'embark-utils'; import { AccountParser, dappPath, defaultHost, dockerHostSwap, embarkPath, deconstructUrl } from 'embark-utils';
export class Simulator { export class Simulator {
constructor(options) { constructor(options) {
@ -18,10 +18,9 @@ export class Simulator {
let cmds = []; let cmds = [];
let useProxy = this.blockchainConfig.proxy || false; let useProxy = this.blockchainConfig.proxy || false;
let host = (dockerHostSwap(options.host || this.blockchainConfig.rpcHost) || defaultHost); let {host, port} = deconstructUrl(this.blockchainConfig.endpoint);
// TODO change this host = (dockerHostSwap(options.host || host) || defaultHost);
const configPort = this.contractsConfig.deployment.type === 'rpc' ? this.blockchainConfig.rpcPort : this.blockchainConfig.wsPort; port = (options.port || port || 8545);
let port = (options.port || configPort || 8545);
port = parseInt(port, 10) + (useProxy ? constants.blockchain.servicePortOnProxy : 0); port = parseInt(port, 10) + (useProxy ? constants.blockchain.servicePortOnProxy : 0);
cmds.push("-p " + port); cmds.push("-p " + port);

View File

@ -271,10 +271,13 @@ class ENS {
const address = this.config.namesystemConfig.register.subdomains[subDomainName]; const address = this.config.namesystemConfig.register.subdomains[subDomainName];
const directivesRegExp = new RegExp(/\$(\w+\[?\d?\]?)/g); const directivesRegExp = new RegExp(/\$(\w+\[?\d?\]?)/g);
const directives = directivesRegExp.exec(address); const directives = directivesRegExp.exec(address);
if (directives && directives.length) { if (directives && directives.length) {
this.embark.registerActionForEvent("contracts:deploy:afterAll", async (deployActionCb) => { this.embark.registerActionForEvent("contracts:deploy:afterAll", async (deployActionCb) => {
if (!this.config.namesystemConfig.enabled) {
// ENS was disabled
return deployActionCb();
}
this.events.request("blockchain:defaultAccount:get", (currentDefaultAccount) => { this.events.request("blockchain:defaultAccount:get", (currentDefaultAccount) => {
if(defaultAccount !== currentDefaultAccount) { if(defaultAccount !== currentDefaultAccount) {
this.logger.trace(`Skipping registration of subdomain "${directives[1]}" as this action was registered for a previous configuration`); this.logger.trace(`Skipping registration of subdomain "${directives[1]}" as this action was registered for a previous configuration`);

View File

@ -208,7 +208,25 @@ class Test {
}; };
} }
if (!options.contracts) { if (!options.contracts) {
options.contracts = {}; options.contracts = {deploy: {}};
} else if (!options.contracts.deploy) {
// Check if it is the old syntax
let isOldSyntax = false;
Object.values(options.contracts).find(value => {
if (typeof value === 'string' || typeof value === 'number') {
isOldSyntax = true;
return true;
}
if (value.args) {
isOldSyntax = true;
return true;
}
});
if (isOldSyntax) {
this.logger.error(__('The contract configuration for tests has changed. Please use the following structure: `contracts: {deploy: MyContract: {}}}`\nFor more details: %s',
'https://embark.status.im/docs/contracts_testing.html#Configuring-Smart-Contracts-for-tests'.underline));
process.exit(1);
}
} }
self.ready = false; self.ready = false;
@ -293,8 +311,7 @@ class Test {
function setConfig(next) { function setConfig(next) {
contractConfig = prepareContractsConfig(contractConfig); contractConfig = prepareContractsConfig(contractConfig);
self.events.request('config:contractsConfig:set', self.events.request('config:contractsConfig:set',
// TODO find out what versions_default is where it went {contracts: contractConfig.contracts}, next);
{contracts: contractConfig.contracts, versions: self.versions_default}, next);
}, },
function getAccounts(next) { function getAccounts(next) {
self.events.request('blockchain:getAccounts', (err, accounts) => { self.events.request('blockchain:getAccounts', (err, accounts) => {

View File

@ -28,7 +28,7 @@ import {getBlockchainDefaults, getContractDefaults} from './configDefaults';
const DEFAULT_CONFIG_PATH = 'config/'; const DEFAULT_CONFIG_PATH = 'config/';
const PACKAGE = require('../../../package.json'); const PACKAGE = require('../../../package.json');
// TODO add URL here when post or page is publicated // TODO add URL here when post or page is published
const embark5ChangesUrl = 'https://...'; const embark5ChangesUrl = 'https://...';
var Config = function(options) { var Config = function(options) {

View File

@ -46,7 +46,7 @@ Option | Type: `default` | Value
`client` | string: `geth` | Client to use for the Ethereum node. Currently supported: `geth` and `parity` `client` | string: `geth` | Client to use for the Ethereum node. Currently supported: `geth` and `parity`
`miningMode` | string: `dev` | The mining mode to use for the node.<br/>`dev`: This is a special mode where the node uses a development account as defaultAccount. This account is already funded and transactions are faster.<br/>`auto`: Uses a mining script to mine only when needed.<br/>`always`: Miner is always on.<br/>`off`: Turns off the miner `miningMode` | string: `dev` | The mining mode to use for the node.<br/>`dev`: This is a special mode where the node uses a development account as defaultAccount. This account is already funded and transactions are faster.<br/>`auto`: Uses a mining script to mine only when needed.<br/>`always`: Miner is always on.<br/>`off`: Turns off the miner
`endpoint` | string | Endpoint to connect to. Works for external endpoints (like Infura) and local ones too (only for nodes started by `embark run`) `endpoint` | string | Endpoint to connect to. Works for external endpoints (like Infura) and local ones too (only for nodes started by `embark run`)
`accounts` | array | Accounts array for the node and to deploy. When no account is given, defaults to one node account. For more details, go [here](http://localhost:4000/docs/blockchain_accounts_configuration.html) `accounts` | array | Accounts array for the node and to deploy. When no account is given, defaults to one node account. For more details, go [here](/docs/blockchain_accounts_configuration.html)
## Advanced parameters ## Advanced parameters

View File

@ -8,7 +8,7 @@ As many decentralized applications are powered by Smart Contracts, configuring a
Unless specified differently in our application's `embark.json`, Smart Contracts are configured either in the `config/contracts.js` file, or, if we're dealing with a [Smart Contract only app](create_project.html#Creating-%E2%80%9Ccontracts-only%E2%80%9D-apps), the `./contracts.js` file in the root of our project. Unless specified differently in our application's `embark.json`, Smart Contracts are configured either in the `config/contracts.js` file, or, if we're dealing with a [Smart Contract only app](create_project.html#Creating-%E2%80%9Ccontracts-only%E2%80%9D-apps), the `./contracts.js` file in the root of our project.
A Smart Contract configuration is placed in an environment's `contracts` property, with the name of the Smart Contract being the identifier. The following code creates a configuration for the `SimpleStorage` contract in the `development` environment: A Smart Contract configuration is placed in an environment's `deploy` property, with the name of the Smart Contract being the identifier. The following code creates a configuration for the `SimpleStorage` contract in the `development` environment:
``` ```
module.exports = { module.exports = {
@ -133,8 +133,8 @@ In order to give users full control over which Smart Contracts should be deploye
There are two possible strategy options: There are two possible strategy options:
- **implicit** - This is the default. Using the `implicit` strategy, Embark tries to deploy all Smart Contracts configured in the `contracts` configuration, including its (3rd-party) dependencies. - **implicit** - This is the default. Using the `implicit` strategy, Embark tries to deploy all Smart Contracts configured in the `deploy` configuration, including its (3rd-party) dependencies.
- **explicit** - Setting this option to `explicit` tells Embark to deploy the Smart Contracts specified in the `contracts` configuration without their dependencies. This can be combined with [disabling deployment](#Disabling-deployment) of individual Smart Contracts for fine control. - **explicit** - Setting this option to `explicit` tells Embark to deploy the Smart Contracts specified in the `deploy` configuration without their dependencies. This can be combined with [disabling deployment](#Disabling-deployment) of individual Smart Contracts for fine control.
``` ```
strategy: 'explicit' // 'implicit' is the default strategy: 'explicit' // 'implicit' is the default

View File

@ -10,7 +10,7 @@ The following features are currently only supported for Smart Contracts written
## Importing files ## Importing files
If using Solidity it's possible to import other Smart Contract files inside a source file from the application's folders that are not explicitly defined in the `contracts` property of `embark.json`. If using Solidity it's possible to import other Smart Contract files inside a source file from the application's folders that are not explicitly defined in the `deploy` property of `embark.json`.
``` ```
import "another_folder/another_test.sol"; import "another_folder/another_test.sol";

View File

@ -23,7 +23,7 @@ This is a single test spec which will always pass. We're using a globally availa
`contract()` is just an alias for Mocha's `describe()` function and is globally available. In general, global functions and objects are: `contract()` is just an alias for Mocha's `describe()` function and is globally available. In general, global functions and objects are:
- `contract()`: Same as Mocha's `describe` - `contract()`: Same as Mocha's `describe`
- `config()`: Function to deploy Smart Contracts using custom configurations. - `config()`: Function to configure Embark and deploy contracts
- `web3`: Web3 object - `web3`: Web3 object
- `assert`: Node's assert - `assert`: Node's assert
- Mocha functions: `describe()`, `it()`, `before()`, etc. - Mocha functions: `describe()`, `it()`, `before()`, etc.
@ -72,16 +72,24 @@ When running tests, we can even get an idea of what the gas costs of our Smart C
$ embark test --gasDetails $ embark test --gasDetails
``` ```
## Test environment
When running tests, the default [environment}(/docs/environments.html) is `test`. You can obviously change this using the `--env` flag.
The special thing with the `test` environment is that if you do not have a `test` section in your module configuration, that module with be disabled (`enabled: false`). This is done to speed up the test as if you don't need a module, it is disabled.
## Configuring Smart Contracts for tests ## Configuring Smart Contracts for tests
Very similar to how we [configure our Smart Contracts](/docs/contracts_configuration.html) for deployment on test or production nets, we have to configure them for our tests as well. This is important, so that our Smart Contracts get deployed with the correct testing data. Very similar to how we [configure our Smart Contracts](/docs/contracts_configuration.html) for deployment, we have to configure them for our tests as well. This is important, so that our Smart Contracts get deployed with the correct testing data.
To do that, Embark adds a global `config()` function to the execution context, which uses the same API as the configuration object for our application's Smart Contracts. So if we had a `SomeContract` that should be picked up for deployment, this is what the configuration would look like: To do that, Embark adds a global `config()` function to the execution context, which uses the same API as the configuration object for our application's Smart Contracts. So if we had a `SomeContract` that should be picked up for deployment, this is what the configuration would look like:
``` ```
config({ config({
contracts: { contracts: {
SomeContract: {} // options as discussed in Smart Contract configuration guide deploy: {
SomeContract: {} // options as discussed in Smart Contract configuration guide
}
} }
}); });
@ -107,7 +115,9 @@ const SomeContract = require('EmbarkJS/contracts/SomeContract');
config({ config({
contracts: { contracts: {
SomeContract: {} deploy: {
SomeContract: {}
}
} }
}); });
@ -134,7 +144,7 @@ Accounts within the testing environment can be configured [just like we're used
``` ```
config({ config({
deployment: { blockchain: {
accounts: [ accounts: [
{ {
privateKeyFile: 'path/to/file', privateKeyFile: 'path/to/file',
@ -179,14 +189,37 @@ By default, Embark will use an internal VM to run the tests. However we can also
``` ```
config({ config({
deployment: { blockchain: {
"host": "localhost", "endpoint": "http://localhost:8545"
"port": 8545,
"type": "rpc"
} }
}); });
``` ```
## Configuring modules
You can configure the different Embark modules directly in your test file. The available modules are: [storage](/docs/storage_configuration.html), [namesystem](/docs/naming_configuration.html) and [communication](/docs/messages_configuration.html).
All configuration options for the respective modules are available. Also, the configurations you put inside the `config` function are merged inside the ones that are in the configuration file (meaning that you don't have to put all the provider options if they are already in the default configs).
```
config({
storage: {
enabled: true
},
communication: {
enabled: true
},
namesystem: {
enabled: true,
register: {
rootDomain: "test.eth"
}
}
});
```
If the module is not started (eg. IPFS), Embark will start it for you.
## Manually deploying Smart Contracts ## Manually deploying Smart Contracts
As mentioned earlier, Embark handles the deployment of our Smart Contracts using the function `config()` function. If we wish to deploy particular Smart Contracts manually, we can do so using an imported Smart Contract reference. We just need to make sure that we're doing this inside a `contract()` block as discussed earlier: As mentioned earlier, Embark handles the deployment of our Smart Contracts using the function `config()` function. If we wish to deploy particular Smart Contracts manually, we can do so using an imported Smart Contract reference. We just need to make sure that we're doing this inside a `contract()` block as discussed earlier: