aboutsummaryrefslogtreecommitdiffstats
path: root/packages/deployer/test/compiler_utils_test.ts
blob: 5377d33087e9e36f5fb5c64ff13d6e6862e251ff (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
import * as chai from 'chai';
import * as dirtyChai from 'dirty-chai';
import 'mocha';

import {
    createDirIfDoesNotExistAsync,
    getNormalizedErrMsg,
    parseDependencies,
    parseSolidityVersionRange,
} from '../src/utils/compiler';
import { fsWrapper } from '../src/utils/fs_wrapper';

chai.use(dirtyChai);
const expect = chai.expect;

describe('Compiler utils', () => {
    describe('#getNormalizedErrorMessage', () => {
        it('normalizes the error message', () => {
            const errMsg = 'base/Token.sol:6:46: Warning: Unused local variable';
            const normalizedErrMsg = getNormalizedErrMsg(errMsg);
            expect(normalizedErrMsg).to.be.equal('Token.sol:6:46: Warning: Unused local variable');
        });
    });
    describe('#createDirIfDoesNotExistAsync', () => {
        it('creates artifacts dir', async () => {
            const artifactsDir = `${__dirname}/artifacts`;
            expect(fsWrapper.doesPathExistSync(artifactsDir)).to.be.false();
            await createDirIfDoesNotExistAsync(artifactsDir);
            expect(fsWrapper.doesPathExistSync(artifactsDir)).to.be.true();
            fsWrapper.rmdirSync(artifactsDir);
            expect(fsWrapper.doesPathExistSync(artifactsDir)).to.be.false();
        });
    });
    describe('#parseSolidityVersionRange', () => {
        it('correctly parses the version range', () => {
            expect(parseSolidityVersionRange('pragma solidity ^0.0.1;')).to.be.equal('^0.0.1');
            expect(parseSolidityVersionRange('\npragma solidity 0.0.1;')).to.be.equal('0.0.1');
            expect(parseSolidityVersionRange('pragma solidity <=1.0.1;')).to.be.equal('<=1.0.1');
            expect(parseSolidityVersionRange('pragma solidity   ~1.0.1;')).to.be.equal('~1.0.1');
        });
        // TODO: For now that doesn't work. This will work after we switch to a grammar-based parser
        it.skip('correctly parses the version range with comments', () => {
            expect(parseSolidityVersionRange('// pragma solidity ~1.0.1;\npragma solidity ~1.0.2;')).to.be.equal(
                '~1.0.2',
            );
        });
    });
    describe('#parseDependencies', () => {
        it('correctly parses Exchange dependencies', async () => {
            const exchangeSource = await fsWrapper.readFileAsync(`${__dirname}/fixtures/contracts/main/Exchange.sol`, {
                encoding: 'utf8',
            });
            const sourceFileId = '/main/Exchange.sol';
            expect(parseDependencies(exchangeSource, sourceFileId)).to.be.deep.equal([
                '/main/TokenTransferProxy.sol',
                '/base/Token.sol',
                '/base/SafeMath.sol',
            ]);
        });
        it('correctly parses TokenTransferProxy dependencies', async () => {
            const exchangeSource = await fsWrapper.readFileAsync(
                `${__dirname}/fixtures/contracts/main/TokenTransferProxy.sol`,
                {
                    encoding: 'utf8',
                },
            );
            const sourceFileId = '/main/TokenTransferProxy.sol';
            expect(parseDependencies(exchangeSource, sourceFileId)).to.be.deep.equal([
                '/base/Token.sol',
                '/base/Ownable.sol',
            ]);
        });
        // TODO: For now that doesn't work. This will work after we switch to a grammar-based parser
        it.skip('correctly parses commented out dependencies', async () => {
            const contractWithCommentedOutDependencies = `// import "./TokenTransferProxy.sol";`;
            const sourceFileId = '/main/TokenTransferProxy.sol';
            expect(parseDependencies(contractWithCommentedOutDependencies, sourceFileId)).to.be.deep.equal([]);
        });
    });
});