/ test / file.js
file.js
 1  /*globals describe, it*/
 2  const File = require('../lib/core/file');
 3  const fs = require('fs-extra');
 4  const path = require('path');
 5  const assert = require('assert');
 6  const sinon = require('sinon');
 7  
 8  describe('embark.File', function () {
 9    describe('parseFileForImport', () => {
10      it('should find all the imports', function (done) {
11        const contract = fs.readFileSync('./test/contracts/contract_with_import.sol').toString();
12        const file = new File({filename: '.embark/contracts/embark-framework/embark/master/test_app/app/contracts/simple_storage.sol',
13          path: 'https://raw.githubusercontent.com/embark-framework/embark/develop/test_apps/test_app/app/contracts/simple_storage.sol'});
14        const downloadFileStub = sinon.stub(file, 'downloadFile')
15          .callsFake((path, url, cb) => {
16            cb();
17          });
18  
19        file.parseFileForImport(contract, true, () => {
20          assert.strictEqual(downloadFileStub.callCount, 1);
21          assert.strictEqual(downloadFileStub.firstCall.args[0],
22            path.normalize('.embark/contracts/embark-framework/embark/master/test_app/app/contracts/ownable.sol'));
23          assert.strictEqual(downloadFileStub.firstCall.args[1],
24            'https://raw.githubusercontent.com/embark-framework/embark/develop/test_apps/test_app/app/contracts/./ownable.sol');
25          done();
26        });
27      });
28  
29      it('should find all the imports but not call download because not a http contract', function (done) {
30        const contract = fs.readFileSync('./test/contracts/contract_with_import.sol').toString();
31        const file = new File({filename: '.embark/contracts/embark-framework/embark/master/test_app/app/contracts/simple_storage.sol',
32          path: 'https://raw.githubusercontent.com/embark-framework/embark/develop/test_apps/test_app/app/contracts/simple_storage.sol'});
33        const downloadFileStub = sinon.stub(file, 'downloadFile')
34          .callsFake((path, url, cb) => {
35            cb();
36          });
37  
38        file.parseFileForImport(contract, () => {
39          assert.strictEqual(downloadFileStub.callCount, 0);
40          done();
41        });
42      });
43  
44      it('should find all the imports and call downlaod because it is an http import', function (done) {
45        const contract = fs.readFileSync('./test/contracts/contract_with_http_import.sol').toString();
46        const file = new File({filename: '.embark/contracts/embark-framework/embark/master/test_app/app/contracts/simple_storage.sol',
47          path: 'https://raw.githubusercontent.com/embark-framework/embark/develop/test_apps/test_app/app/contracts/simple_storage.sol'});
48        const downloadFileStub = sinon.stub(file, 'downloadFile')
49          .callsFake((path, url, cb) => {
50            cb();
51          });
52  
53        file.parseFileForImport(contract, () => {
54          assert.strictEqual(downloadFileStub.callCount, 1);
55          assert.strictEqual(downloadFileStub.firstCall.args[0],
56            '.embark/contracts/embark-framework/embark/develop/test_apps/contracts_app/contracts/contract_args.sol');
57          assert.strictEqual(downloadFileStub.firstCall.args[1],
58            'https://raw.githubusercontent.com/embark-framework/embark/develop/test_apps/contracts_app/contracts/contract_args.sol');
59          done();
60        });
61      });
62  
63      it('should find all the imports but only once if called twice', function (done) {
64        const contract = fs.readFileSync('./test/contracts/contract_with_http_import.sol').toString();
65        const file = new File({filename: '.embark/contracts/embark-framework/embark/master/test_app/app/contracts/simple_storage.sol',
66          path: 'https://raw.githubusercontent.com/embark-framework/embark/develop/test_apps/test_app/app/contracts/simple_storage.sol'});
67        const downloadFileStub = sinon.stub(file, 'downloadFile')
68          .callsFake((path, url, cb) => {
69            cb();
70          });
71  
72        file.parseFileForImport(contract, () => {
73          // Parse again
74          file.parseFileForImport(contract, () => {
75            assert.strictEqual(downloadFileStub.callCount, 1);
76            assert.strictEqual(downloadFileStub.firstCall.args[0],
77              '.embark/contracts/embark-framework/embark/develop/test_apps/contracts_app/contracts/contract_args.sol');
78            assert.strictEqual(downloadFileStub.firstCall.args[1],
79              'https://raw.githubusercontent.com/embark-framework/embark/develop/test_apps/contracts_app/contracts/contract_args.sol');
80            done();
81          });
82        });
83      });
84    });
85  });