aboutsummaryrefslogtreecommitdiffstats
path: root/packages/sol-compiler/test/compiler_test.ts
blob: 7e22ff0ef3cd2f8baf4994e9b45279f48c762d98 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
import { join } from 'path';

import * as chai from 'chai';
import 'mocha';

import { Compiler } from '../src/compiler';
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';

chaiSetup.configure();
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 compilerOpts: CompilerOptions = {
        artifactsDir,
        contractsDir,
        contracts: constants.contracts,
    };
    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',
        };
        const exchangeArtifactString = await fsWrapper.readFileAsync(exchangeArtifactPath, opts);
        const exchangeArtifact: ContractArtifact = JSON.parse(exchangeArtifactString);
        // The last 43 bytes of the binaries are metadata which may not be equivalent
        const metadataByteLength = 43;
        const metadataHexLength = metadataByteLength * 2;
        const unlinkedBinaryWithoutMetadata = exchangeArtifact.compilerOutput.evm.bytecode.object.slice(
            2,
            -metadataHexLength,
        );
        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.rejected();
    });
    describe('after a successful compilation', () => {
        const contract = 'Exchange';
        let artifactPath: string;
        let compilationTimestamp: number;
        beforeEach(async () => {
            compilerOpts.contracts = [contract];

            artifactPath = `${artifactsDir}/${contract}.json`;
            if (fsWrapper.doesPathExistSync(artifactPath)) {
                await fsWrapper.removeFileAsync(artifactPath);
            }

            await new Compiler(compilerOpts).compileAsync();

            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 recompilationTimestamp = (await fsWrapper.statAsync(artifactPath)).mtimeMs;

            expect(recompilationTimestamp).to.be.greaterThan(compilationTimestamp);
        });
        it("recompilation should NOT update artifact when source hasn't changed", async () => {
            await new Compiler(compilerOpts).compileAsync();

            const recompilationTimestamp = (await fsWrapper.statAsync(artifactPath)).mtimeMs;

            expect(recompilationTimestamp).to.equal(compilationTimestamp);
        });
    });
    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');
        }
    });
});