From bb4558e0be3aa9c182217e56cd2ff2f042d5473a Mon Sep 17 00:00:00 2001 From: "F. Eugene Aumson" Date: Tue, 14 Aug 2018 17:40:31 -0700 Subject: test compiler --- packages/sol-compiler/test/compiler_test.ts | 86 +++++++++++++++++++--- .../test/fixtures/contracts/BadContractName.sol | 3 + .../test/fixtures/contracts/EmptyContract.sol | 3 + 3 files changed, 80 insertions(+), 12 deletions(-) create mode 100644 packages/sol-compiler/test/fixtures/contracts/BadContractName.sol create mode 100644 packages/sol-compiler/test/fixtures/contracts/EmptyContract.sol (limited to 'packages/sol-compiler/test') diff --git a/packages/sol-compiler/test/compiler_test.ts b/packages/sol-compiler/test/compiler_test.ts index c9e141ee9..4a3b6e4a7 100644 --- a/packages/sol-compiler/test/compiler_test.ts +++ b/packages/sol-compiler/test/compiler_test.ts @@ -1,5 +1,7 @@ -import { DoneCallback } from '@0xproject/types'; +import { join } from 'path'; + import * as chai from 'chai'; +import * as chaiAsPromised from 'chai-as-promised'; import 'mocha'; import { Compiler } from '../src/compiler'; @@ -9,29 +11,28 @@ import { CompilerOptions, ContractArtifact } from '../src/utils/types'; import { exchange_binary } from './fixtures/exchange_bin'; import { constants } from './util/constants'; +chai.use(chaiAsPromised); const expect = chai.expect; describe('#Compiler', function(): void { this.timeout(constants.timeoutMs); // tslint:disable-line:no-invalid-this const artifactsDir = `${__dirname}/fixtures/artifacts`; const contractsDir = `${__dirname}/fixtures/contracts`; - const exchangeArtifactPath = `${artifactsDir}/Exchange.json`; const compilerOpts: CompilerOptions = { artifactsDir, contractsDir, contracts: constants.contracts, }; - const compiler = new Compiler(compilerOpts); - beforeEach((done: DoneCallback) => { - (async () => { - if (fsWrapper.doesPathExistSync(exchangeArtifactPath)) { - await fsWrapper.removeFileAsync(exchangeArtifactPath); - } - await compiler.compileAsync(); - done(); - })().catch(done); - }); it('should create an Exchange artifact with the correct unlinked binary', async () => { + compilerOpts.contracts = ['Exchange']; + + const exchangeArtifactPath = `${artifactsDir}/Exchange.json`; + if (fsWrapper.doesPathExistSync(exchangeArtifactPath)) { + await fsWrapper.removeFileAsync(exchangeArtifactPath); + } + + await new Compiler(compilerOpts).compileAsync(); + const opts = { encoding: 'utf8', }; @@ -47,4 +48,65 @@ describe('#Compiler', function(): void { const exchangeBinaryWithoutMetadata = exchange_binary.slice(0, -metadataHexLength); expect(unlinkedBinaryWithoutMetadata).to.equal(exchangeBinaryWithoutMetadata); }); + it("should throw when Whatever.sol doesn't contain a Whatever contract", async () => { + const contract = 'BadContractName'; + + const exchangeArtifactPath = `${artifactsDir}/${contract}.json`; + if (fsWrapper.doesPathExistSync(exchangeArtifactPath)) { + await fsWrapper.removeFileAsync(exchangeArtifactPath); + } + + compilerOpts.contracts = [contract]; + const compiler = new Compiler(compilerOpts); + + expect(compiler.compileAsync()).to.be.rejectedWith(Error); + }); + describe('after a successful compilation', () => { + const contract = 'Exchange'; + let artifactPath: string; + let timeCompiled: number; + beforeEach(async () => { + compilerOpts.contracts = [contract]; + + artifactPath = `${artifactsDir}/${contract}.json`; + if (fsWrapper.doesPathExistSync(artifactPath)) { + await fsWrapper.removeFileAsync(artifactPath); + } + + await new Compiler(compilerOpts).compileAsync(); + + timeCompiled = (await fsWrapper.statAsync(artifactPath)).mtimeMs; + }); + it('recompilation should update artifact when source has changed', async () => { + fsWrapper.appendFileAsync(join(contractsDir, `${contract}.sol`), ' '); + + await new Compiler(compilerOpts).compileAsync(); + + const timeRecompiled = (await fsWrapper.statAsync(artifactPath)).mtimeMs; + + expect(timeRecompiled).to.not.equal(timeCompiled); + }); + it("recompilation should NOT update artifact when source hasn't changed", async () => { + await new Compiler(compilerOpts).compileAsync(); + + const timeRecompiled = (await fsWrapper.statAsync(artifactPath)).mtimeMs; + + expect(timeRecompiled).to.equal(timeCompiled); + }); + }); + it('should only compile what was requested', async () => { + // remove all artifacts + for (const artifact of await fsWrapper.readdirAsync(artifactsDir)) { + await fsWrapper.removeFileAsync(join(artifactsDir, artifact)); + } + + // compile EmptyContract + compilerOpts.contracts = ['EmptyContract']; + await new Compiler(compilerOpts).compileAsync(); + + // make sure the artifacts dir only contains EmptyContract.json + for (const artifact of await fsWrapper.readdirAsync(artifactsDir)) { + expect(artifact).to.equal('EmptyContract.json'); + } + }); }); diff --git a/packages/sol-compiler/test/fixtures/contracts/BadContractName.sol b/packages/sol-compiler/test/fixtures/contracts/BadContractName.sol new file mode 100644 index 000000000..3193cc0eb --- /dev/null +++ b/packages/sol-compiler/test/fixtures/contracts/BadContractName.sol @@ -0,0 +1,3 @@ +pragma solidity ^0.4.14; + +contract ContractNameThatDoesntMatchFilename { } diff --git a/packages/sol-compiler/test/fixtures/contracts/EmptyContract.sol b/packages/sol-compiler/test/fixtures/contracts/EmptyContract.sol new file mode 100644 index 000000000..971ca7826 --- /dev/null +++ b/packages/sol-compiler/test/fixtures/contracts/EmptyContract.sol @@ -0,0 +1,3 @@ +pragma solidity ^0.4.14; + +contract EmptyContract { } -- cgit v1.2.3 From 976d159e5275fa1fc0236fdd65c3acf40864657b Mon Sep 17 00:00:00 2001 From: "F. Eugene Aumson" Date: Wed, 15 Aug 2018 15:20:00 -0700 Subject: follow chai_setup pattern --- packages/sol-compiler/test/compiler_test.ts | 4 ++-- packages/sol-compiler/test/util/chai_setup.ts | 13 +++++++++++++ 2 files changed, 15 insertions(+), 2 deletions(-) create mode 100644 packages/sol-compiler/test/util/chai_setup.ts (limited to 'packages/sol-compiler/test') diff --git a/packages/sol-compiler/test/compiler_test.ts b/packages/sol-compiler/test/compiler_test.ts index 4a3b6e4a7..4be77c01b 100644 --- a/packages/sol-compiler/test/compiler_test.ts +++ b/packages/sol-compiler/test/compiler_test.ts @@ -1,7 +1,6 @@ import { join } from 'path'; import * as chai from 'chai'; -import * as chaiAsPromised from 'chai-as-promised'; import 'mocha'; import { Compiler } from '../src/compiler'; @@ -9,9 +8,10 @@ import { fsWrapper } from '../src/utils/fs_wrapper'; import { CompilerOptions, ContractArtifact } from '../src/utils/types'; import { exchange_binary } from './fixtures/exchange_bin'; +import { chaiSetup } from './util/chai_setup'; import { constants } from './util/constants'; -chai.use(chaiAsPromised); +chaiSetup.configure(); const expect = chai.expect; describe('#Compiler', function(): void { diff --git a/packages/sol-compiler/test/util/chai_setup.ts b/packages/sol-compiler/test/util/chai_setup.ts new file mode 100644 index 000000000..1a8733093 --- /dev/null +++ b/packages/sol-compiler/test/util/chai_setup.ts @@ -0,0 +1,13 @@ +import * as chai from 'chai'; +import chaiAsPromised = require('chai-as-promised'); +import ChaiBigNumber = require('chai-bignumber'); +import * as dirtyChai from 'dirty-chai'; + +export const chaiSetup = { + configure(): void { + chai.config.includeStack = true; + chai.use(ChaiBigNumber()); + chai.use(dirtyChai); + chai.use(chaiAsPromised); + }, +}; -- cgit v1.2.3 From 20ac6936ac19b141723cc88f03acfeeb79b89958 Mon Sep 17 00:00:00 2001 From: "F. Eugene Aumson" Date: Wed, 15 Aug 2018 16:01:04 -0700 Subject: change .rejectedWith(error) to .rejected() --- packages/sol-compiler/test/compiler_test.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'packages/sol-compiler/test') diff --git a/packages/sol-compiler/test/compiler_test.ts b/packages/sol-compiler/test/compiler_test.ts index 4be77c01b..08799c14b 100644 --- a/packages/sol-compiler/test/compiler_test.ts +++ b/packages/sol-compiler/test/compiler_test.ts @@ -59,7 +59,7 @@ describe('#Compiler', function(): void { compilerOpts.contracts = [contract]; const compiler = new Compiler(compilerOpts); - expect(compiler.compileAsync()).to.be.rejectedWith(Error); + expect(compiler.compileAsync()).to.be.rejected(); }); describe('after a successful compilation', () => { const contract = 'Exchange'; -- cgit v1.2.3 From 89202b7bdffe084511971e331f81dd5878f2a56c Mon Sep 17 00:00:00 2001 From: "F. Eugene Aumson" Date: Wed, 15 Aug 2018 16:01:57 -0700 Subject: clarify recompilation tests --- packages/sol-compiler/test/compiler_test.ts | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) (limited to 'packages/sol-compiler/test') diff --git a/packages/sol-compiler/test/compiler_test.ts b/packages/sol-compiler/test/compiler_test.ts index 08799c14b..7e22ff0ef 100644 --- a/packages/sol-compiler/test/compiler_test.ts +++ b/packages/sol-compiler/test/compiler_test.ts @@ -64,7 +64,7 @@ describe('#Compiler', function(): void { describe('after a successful compilation', () => { const contract = 'Exchange'; let artifactPath: string; - let timeCompiled: number; + let compilationTimestamp: number; beforeEach(async () => { compilerOpts.contracts = [contract]; @@ -75,23 +75,25 @@ describe('#Compiler', function(): void { await new Compiler(compilerOpts).compileAsync(); - timeCompiled = (await fsWrapper.statAsync(artifactPath)).mtimeMs; + compilationTimestamp = (await fsWrapper.statAsync(artifactPath)).mtimeMs; }); it('recompilation should update artifact when source has changed', async () => { + // append some meaningless data to the contract, so that its hash + // will change, so that the compiler will decide to recompile it. fsWrapper.appendFileAsync(join(contractsDir, `${contract}.sol`), ' '); await new Compiler(compilerOpts).compileAsync(); - const timeRecompiled = (await fsWrapper.statAsync(artifactPath)).mtimeMs; + const recompilationTimestamp = (await fsWrapper.statAsync(artifactPath)).mtimeMs; - expect(timeRecompiled).to.not.equal(timeCompiled); + expect(recompilationTimestamp).to.be.greaterThan(compilationTimestamp); }); it("recompilation should NOT update artifact when source hasn't changed", async () => { await new Compiler(compilerOpts).compileAsync(); - const timeRecompiled = (await fsWrapper.statAsync(artifactPath)).mtimeMs; + const recompilationTimestamp = (await fsWrapper.statAsync(artifactPath)).mtimeMs; - expect(timeRecompiled).to.equal(timeCompiled); + expect(recompilationTimestamp).to.equal(compilationTimestamp); }); }); it('should only compile what was requested', async () => { -- cgit v1.2.3 From 85427a84df9954c5849ea471f549549bc1486aec Mon Sep 17 00:00:00 2001 From: "F. Eugene Aumson" Date: Thu, 16 Aug 2018 11:17:57 -0700 Subject: clarify variable names for artifact mod times --- packages/sol-compiler/test/compiler_test.ts | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) (limited to 'packages/sol-compiler/test') diff --git a/packages/sol-compiler/test/compiler_test.ts b/packages/sol-compiler/test/compiler_test.ts index 7e22ff0ef..003d863e7 100644 --- a/packages/sol-compiler/test/compiler_test.ts +++ b/packages/sol-compiler/test/compiler_test.ts @@ -64,7 +64,7 @@ describe('#Compiler', function(): void { describe('after a successful compilation', () => { const contract = 'Exchange'; let artifactPath: string; - let compilationTimestamp: number; + let artifactCreatedAtMs: number; beforeEach(async () => { compilerOpts.contracts = [contract]; @@ -75,7 +75,7 @@ describe('#Compiler', function(): void { await new Compiler(compilerOpts).compileAsync(); - compilationTimestamp = (await fsWrapper.statAsync(artifactPath)).mtimeMs; + artifactCreatedAtMs = (await fsWrapper.statAsync(artifactPath)).mtimeMs; }); it('recompilation should update artifact when source has changed', async () => { // append some meaningless data to the contract, so that its hash @@ -84,16 +84,16 @@ describe('#Compiler', function(): void { await new Compiler(compilerOpts).compileAsync(); - const recompilationTimestamp = (await fsWrapper.statAsync(artifactPath)).mtimeMs; + const artifactModifiedAtMs = (await fsWrapper.statAsync(artifactPath)).mtimeMs; - expect(recompilationTimestamp).to.be.greaterThan(compilationTimestamp); + expect(artifactModifiedAtMs).to.be.greaterThan(artifactCreatedAtMs); }); it("recompilation should NOT update artifact when source hasn't changed", async () => { await new Compiler(compilerOpts).compileAsync(); - const recompilationTimestamp = (await fsWrapper.statAsync(artifactPath)).mtimeMs; + const artifactModifiedAtMs = (await fsWrapper.statAsync(artifactPath)).mtimeMs; - expect(recompilationTimestamp).to.equal(compilationTimestamp); + expect(artifactModifiedAtMs).to.equal(artifactCreatedAtMs); }); }); it('should only compile what was requested', async () => { -- cgit v1.2.3