|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +const querystring = require('querystring'); |
| 4 | +const expect = require('chai').expect; |
| 5 | + |
| 6 | +const gitProvider = require('../src/gitProvider'); |
| 7 | + |
| 8 | +const userName = 'testUser'; |
| 9 | +const repoName = 'testRepo'; |
| 10 | +const filePath = '/sampleDirectory/sampleTestFile.txt'; |
| 11 | +const branch = 'master'; |
| 12 | +const line = 123; |
| 13 | + |
| 14 | +suite('gitProvider', function () { |
| 15 | + suite('GitHub', function () { |
| 16 | + const remoteUrl = `https://github.com/${userName}/${repoName}.git`; |
| 17 | + const provider = gitProvider(remoteUrl); |
| 18 | + |
| 19 | + suite('#webUrl(branch, filePath)', function () { |
| 20 | + test('should returns file URL', function () { |
| 21 | + const expectedUrl = `https://github.com/${userName}/${repoName}/blob/${branch}${filePath}`; |
| 22 | + const webUrl = provider.webUrl(branch, filePath); |
| 23 | + expect(webUrl).to.equal(expectedUrl); |
| 24 | + }); |
| 25 | + }); |
| 26 | + |
| 27 | + suite('#webUrl(branch, filePath, line)', function () { |
| 28 | + test('should returns file URL with line hash', function () { |
| 29 | + const expectedUrl = `https://github.com/${userName}/${repoName}/blob/${branch}${filePath}#L${line}`; |
| 30 | + const webUrl = provider.webUrl(branch, filePath, line); |
| 31 | + expect(webUrl).to.equal(expectedUrl); |
| 32 | + }); |
| 33 | + }); |
| 34 | + |
| 35 | + suite('#webUrl(branch)', function () { |
| 36 | + test('should returns repository root URL', function () { |
| 37 | + const expectedUrl = `https://github.com/${userName}/${repoName}/tree/${branch}`; |
| 38 | + const webUrl = provider.webUrl(branch); |
| 39 | + expect(webUrl).to.equal(expectedUrl); |
| 40 | + }); |
| 41 | + }); |
| 42 | + |
| 43 | + suite('with ssh remote URL', function () { |
| 44 | + const remoteUrl = `[email protected]:${userName}/${repoName}.git`; |
| 45 | + const provider = gitProvider(remoteUrl); |
| 46 | + |
| 47 | + suite('#webUrl(branch, filePath)', function () { |
| 48 | + test('should returns HTTPS URL', function () { |
| 49 | + const expectedUrl = `https://github.com/${userName}/${repoName}/blob/${branch}${filePath}`; |
| 50 | + const webUrl = provider.webUrl(branch, filePath); |
| 51 | + expect(webUrl).to.equal(expectedUrl); |
| 52 | + }); |
| 53 | + }); |
| 54 | + }); |
| 55 | + }); |
| 56 | + |
| 57 | + suite('Bitbucket', function () { |
| 58 | + const remoteUrl = `https://bitbucket.org/${userName}/${repoName}.git`; |
| 59 | + const provider = gitProvider(remoteUrl); |
| 60 | + |
| 61 | + suite('#webUrl(branch, filePath)', function () { |
| 62 | + test('should returns file URL', function () { |
| 63 | + const expectedUrl = `https://bitbucket.org/${userName}/${repoName}/src/${branch}${filePath}`; |
| 64 | + const webUrl = provider.webUrl(branch, filePath); |
| 65 | + expect(webUrl).to.equal(expectedUrl); |
| 66 | + }); |
| 67 | + }); |
| 68 | + |
| 69 | + suite('#webUrl(branch, filePath, line)', function () { |
| 70 | + test('should returns file URL with line hash', function () { |
| 71 | + const expectedUrl = `https://bitbucket.org/${userName}/${repoName}/src/${branch}${filePath}#cl-${line}`; |
| 72 | + const webUrl = provider.webUrl(branch, filePath, line); |
| 73 | + expect(webUrl).to.equal(expectedUrl); |
| 74 | + }); |
| 75 | + }); |
| 76 | + |
| 77 | + suite('#webUrl(branch)', function () { |
| 78 | + test('should returns repository root URL', function () { |
| 79 | + const expectedUrl = `https://bitbucket.org/${userName}/${repoName}/src/${branch}`; |
| 80 | + const webUrl = provider.webUrl(branch); |
| 81 | + expect(webUrl).to.equal(expectedUrl); |
| 82 | + }); |
| 83 | + }); |
| 84 | + |
| 85 | + suite('with ssh remote URL', function () { |
| 86 | + const remoteUrl = `[email protected]:${userName}/${repoName}.git`; |
| 87 | + const provider = gitProvider(remoteUrl); |
| 88 | + |
| 89 | + suite('#webUrl(branch, filePath)', function () { |
| 90 | + test('should returns HTTPS URL', function () { |
| 91 | + const expectedUrl = `https://bitbucket.org/${userName}/${repoName}/src/${branch}${filePath}`; |
| 92 | + const webUrl = provider.webUrl(branch, filePath); |
| 93 | + expect(webUrl).to.equal(expectedUrl); |
| 94 | + }); |
| 95 | + }); |
| 96 | + }); |
| 97 | + }); |
| 98 | + |
| 99 | + suite('VisualStudio', function () { |
| 100 | + const projectName = 'testProject'; |
| 101 | + const remoteUrl = `https://test-account.visualstudio.com/${projectName}/_git/${repoName}.git`; |
| 102 | + const provider = gitProvider(remoteUrl); |
| 103 | + |
| 104 | + suite('#webUrl(branch, filePath)', function () { |
| 105 | + test('should returns file URL', function () { |
| 106 | + const expectedUrl = `https://test-account.visualstudio.com/${projectName}/_git/${repoName}`; |
| 107 | + const expectedQuery = { |
| 108 | + path: filePath, |
| 109 | + version: `GB${branch}`, |
| 110 | + }; |
| 111 | + const webUrl = provider.webUrl(branch, filePath); |
| 112 | + const [url, query] = webUrl.split('?'); |
| 113 | + expect(url).to.equal(expectedUrl); |
| 114 | + expect(querystring.parse(query)).to.deep.equal(expectedQuery); |
| 115 | + }); |
| 116 | + }); |
| 117 | + |
| 118 | + suite('#webUrl(branch, filePath, line)', function () { |
| 119 | + test('should returns file URL with line query', function () { |
| 120 | + const expectedUrl = `https://test-account.visualstudio.com/${projectName}/_git/${repoName}`; |
| 121 | + const expectedQuery = { |
| 122 | + path: filePath, |
| 123 | + version: `GB${branch}`, |
| 124 | + line: String(line), |
| 125 | + }; |
| 126 | + const webUrl = provider.webUrl(branch, filePath, line); |
| 127 | + const [url, query] = webUrl.split('?'); |
| 128 | + expect(url).to.equal(expectedUrl); |
| 129 | + expect(querystring.parse(query)).to.deep.equal(expectedQuery); |
| 130 | + }); |
| 131 | + }); |
| 132 | + |
| 133 | + suite('#webUrl(branch)', function () { |
| 134 | + test('should returns repository root URL', function () { |
| 135 | + const expectedUrl = `https://test-account.visualstudio.com/${projectName}/_git/${repoName}`; |
| 136 | + const expectedQuery = { |
| 137 | + version: `GB${branch}`, |
| 138 | + }; |
| 139 | + const webUrl = provider.webUrl(branch); |
| 140 | + const [url, query] = webUrl.split('?'); |
| 141 | + expect(url).to.equal(expectedUrl); |
| 142 | + expect(querystring.parse(query)).to.deep.equal(expectedQuery); |
| 143 | + }); |
| 144 | + }); |
| 145 | + |
| 146 | + suite('with ssh remote URL', function () { |
| 147 | + const remoteUrl = `ssh://[email protected]:22/${projectName}/_git/${repoName}.git`; |
| 148 | + const provider = gitProvider(remoteUrl); |
| 149 | + |
| 150 | + suite('#webUrl(branch, filePath)', function () { |
| 151 | + test('should returns HTTPS URL', function () { |
| 152 | + const expectedUrl = `https://test-account.visualstudio.com/${projectName}/_git/${repoName}`; |
| 153 | + const expectedQuery = { |
| 154 | + path: filePath, |
| 155 | + version: `GB${branch}`, |
| 156 | + }; |
| 157 | + const webUrl = provider.webUrl(branch, filePath); |
| 158 | + const [url, query] = webUrl.split('?'); |
| 159 | + expect(url).to.equal(expectedUrl); |
| 160 | + expect(querystring.parse(query)).to.deep.equal(expectedQuery); |
| 161 | + }); |
| 162 | + }); |
| 163 | + }); |
| 164 | + }); |
| 165 | +}); |
0 commit comments