/ test / utils.js
utils.js
 1  /*global describe, it*/
 2  const Utils = require('../lib/utils/utils');
 3  const assert = require('assert');
 4  const constants = require('../lib/constants');
 5  
 6  describe('embark.utils', function () {
 7    describe('#getExternalContractUrl', function () {
 8      it('should get the right url for a https://github file', function () {
 9        const fileObj = Utils.getExternalContractUrl(
10          'https://github.com/embark-framework/embark/blob/master/test_app/app/contracts/simple_storage.sol'
11        );
12        assert.deepEqual(fileObj,
13          {
14            filePath: constants.httpContractsDirectory + 'embark-framework/embark/master/test_app/app/contracts/simple_storage.sol',
15            url: 'https://raw.githubusercontent.com/embark-framework/embark/master/test_app/app/contracts/simple_storage.sol'
16          });
17      });
18  
19      it('should fail for a malformed https://github file', function () {
20        const fileObj = Utils.getExternalContractUrl(
21          'https://github/embark-framework/embark/blob/master/test_app/app/contracts/simple_storage.sol'
22        );
23        assert.strictEqual(fileObj, null);
24      });
25  
26      it('should get the right url for a git:// file with no branch #', function () {
27        const fileObj = Utils.getExternalContractUrl(
28          'git://github.com/status-im/contracts/contracts/identity/ERC725.sol'
29        );
30        assert.deepEqual(fileObj,
31          {
32            filePath: constants.httpContractsDirectory + 'status-im/contracts/master/contracts/identity/ERC725.sol',
33            url: 'https://raw.githubusercontent.com/status-im/contracts/master/contracts/identity/ERC725.sol'
34          });
35      });
36  
37      it('should get the right url for a git:// file with a branch #', function () {
38        const fileObj = Utils.getExternalContractUrl(
39          'git://github.com/status-im/contracts/contracts/identity/ERC725.sol#myBranch'
40        );
41        assert.deepEqual(fileObj,
42          {
43            filePath: constants.httpContractsDirectory + 'status-im/contracts/myBranch/contracts/identity/ERC725.sol',
44            url: 'https://raw.githubusercontent.com/status-im/contracts/myBranch/contracts/identity/ERC725.sol'
45          });
46      });
47  
48      it('should fail when the git:// file is malformed', function () {
49        const fileObj = Utils.getExternalContractUrl(
50          'git://github.com/identity/ERC725.sol#myBranch'
51        );
52        assert.strictEqual(fileObj, null);
53      });
54  
55      it('should get the right url with a github.com file without branch #', function () {
56        const fileObj = Utils.getExternalContractUrl(
57          'github.com/status-im/contracts/contracts/identity/ERC725.sol'
58        );
59        assert.deepEqual(fileObj,
60          {
61            filePath: constants.httpContractsDirectory + 'status-im/contracts/master/contracts/identity/ERC725.sol',
62            url: 'https://raw.githubusercontent.com/status-im/contracts/master/contracts/identity/ERC725.sol'
63          });
64      });
65  
66      it('should get the right url with a github.com file with branch #', function () {
67        const fileObj = Utils.getExternalContractUrl(
68          'github.com/status-im/contracts/contracts/identity/ERC725.sol#theBranch'
69        );
70        assert.deepEqual(fileObj,
71          {
72            filePath: constants.httpContractsDirectory + 'status-im/contracts/theBranch/contracts/identity/ERC725.sol',
73            url: 'https://raw.githubusercontent.com/status-im/contracts/theBranch/contracts/identity/ERC725.sol'
74          });
75      });
76  
77      it('should fail with a malformed github.com url', function () {
78        const fileObj = Utils.getExternalContractUrl(
79          'github/status-im/contracts/contracts/identity/ERC725.sol#theBranch'
80        );
81        assert.strictEqual(fileObj, null);
82      });
83  
84      it('should succeed with a generic http url', function () {
85        const fileObj = Utils.getExternalContractUrl(
86          'http://myurl.com/myFile.sol'
87        );
88        assert.deepEqual(fileObj, {
89          filePath: constants.httpContractsDirectory + 'myFile.sol',
90          url: 'http://myurl.com/myFile.sol'
91        });
92      });
93    });
94  });