/test/gitProvider.test.js

https://github.com/ziyasal/vscode-open-in-github · JavaScript · 274 lines · 241 code · 33 blank · 0 comment · 6 complexity · 1ca88d4b8e09297515064019334b4b43 MD5 · raw file

  1. 'use strict';
  2. const path = require('path');
  3. const querystring = require('querystring');
  4. const expect = require('chai').expect;
  5. const proxyquire = require('proxyquire');
  6. const gitProvider = require('../src/gitProvider');
  7. const userName = 'testUser';
  8. const repoName = 'testRepo';
  9. const filePath = '/sampleDirectory/sampleTestFile.txt';
  10. const fileName = path.basename(filePath);
  11. const branch = 'master';
  12. const line = 123;
  13. suite('gitProvider', function () {
  14. suite('GitHub', function () {
  15. const remoteUrl = `https://github.com/${userName}/${repoName}.git`;
  16. const provider = gitProvider(remoteUrl);
  17. suite('#webUrl(branch, filePath)', function () {
  18. test('should returns file URL', function () {
  19. const expectedUrl = `https://github.com/${userName}/${repoName}/blob/${branch}${filePath}`;
  20. const webUrl = provider.webUrl(branch, filePath);
  21. expect(webUrl).to.equal(expectedUrl);
  22. });
  23. });
  24. suite('#webUrl(branch, filePath, line)', function () {
  25. test('should returns file URL with line hash', function () {
  26. const expectedUrl = `https://github.com/${userName}/${repoName}/blob/${branch}${filePath}#L${line}`;
  27. const webUrl = provider.webUrl(branch, filePath, line);
  28. expect(webUrl).to.equal(expectedUrl);
  29. });
  30. });
  31. suite('#webUrl(branch)', function () {
  32. test('should returns repository root URL', function () {
  33. const expectedUrl = `https://github.com/${userName}/${repoName}/tree/${branch}`;
  34. const webUrl = provider.webUrl(branch);
  35. expect(webUrl).to.equal(expectedUrl);
  36. });
  37. });
  38. suite('with ssh remote URL', function () {
  39. const remoteUrl = `git@github.com:${userName}/${repoName}.git`;
  40. const provider = gitProvider(remoteUrl);
  41. suite('#webUrl(branch, filePath)', function () {
  42. test('should returns HTTPS URL', function () {
  43. const expectedUrl = `https://github.com/${userName}/${repoName}/blob/${branch}${filePath}`;
  44. const webUrl = provider.webUrl(branch, filePath);
  45. expect(webUrl).to.equal(expectedUrl);
  46. });
  47. });
  48. });
  49. suite('with custom domain', function () {
  50. const testDomain = 'github.testdomain.com';
  51. const remoteUrl = `https://${testDomain}/${userName}/${repoName}.git`;
  52. const fakeVscode = {
  53. workspace: {
  54. getConfiguration: function () {
  55. return {
  56. get: function (configKey) {
  57. if (configKey === 'gitHubDomain') {
  58. return testDomain;
  59. } else if (configKey === 'useCommitSHAInURL') {
  60. return false;
  61. }
  62. },
  63. };
  64. },
  65. },
  66. };
  67. const gitProvider = proxyquire('../src/gitProvider.js', { vscode: fakeVscode });
  68. const provider = gitProvider(remoteUrl);
  69. suite('#webUrl(branch, filePath)', function () {
  70. test('should return custom domain URL', function () {
  71. const expectedUrl = `https://${testDomain}/${userName}/${repoName}/blob/${branch}${filePath}`;
  72. const webUrl = provider.webUrl(branch, filePath);
  73. expect(webUrl).to.equal(expectedUrl);
  74. });
  75. });
  76. });
  77. suite('with custom domain and protocol', function () {
  78. const testDomain = 'github.testdomain.com';
  79. const testProtocol = 'http';
  80. const remoteUrl = `https://${testDomain}/${userName}/${repoName}.git`;
  81. const fakeVscode = {
  82. workspace: {
  83. getConfiguration: function () {
  84. return {
  85. get: function (configKey) {
  86. if (configKey === 'gitHubDomain') {
  87. return testDomain;
  88. } else if (configKey === 'providerProtocol') {
  89. return testProtocol;
  90. }
  91. },
  92. };
  93. },
  94. },
  95. };
  96. const gitProvider = proxyquire('../src/gitProvider.js', { vscode: fakeVscode });
  97. const provider = gitProvider(remoteUrl);
  98. suite('#webUrl(branch, filePath)', function () {
  99. test('should return custom domain URL', function () {
  100. const expectedUrl = `http://${testDomain}/${userName}/${repoName}/blob/${branch}${filePath}`;
  101. const webUrl = provider.webUrl(branch, filePath);
  102. expect(webUrl).to.equal(expectedUrl);
  103. });
  104. });
  105. });
  106. });
  107. suite('Bitbucket', function () {
  108. const remoteUrl = `https://bitbucket.org/${userName}/${repoName}.git`;
  109. const sha = 'f9f2dcbf56e88ee3612c9890a6df1cfd4dde8c5e';
  110. const provider = gitProvider(remoteUrl, sha);
  111. suite('#webUrl(branch, filePath)', function () {
  112. test('should returns file URL', function () {
  113. const expectedUrl = `https://bitbucket.org/${userName}/${repoName}/src/${sha}${filePath}`;
  114. const webUrl = provider.webUrl(branch, filePath);
  115. expect(webUrl).to.equal(expectedUrl);
  116. });
  117. });
  118. suite('#webUrl(branch, filePath, line)', function () {
  119. test('should returns file URL with line hash', function () {
  120. const expectedUrl = `https://bitbucket.org/${userName}/${repoName}/src/${sha}${filePath}#${fileName}-${line}`;
  121. const webUrl = provider.webUrl(branch, filePath, line);
  122. expect(webUrl).to.equal(expectedUrl);
  123. });
  124. });
  125. suite('#webUrl(branch)', function () {
  126. test('should returns repository root URL', function () {
  127. const expectedUrl = `https://bitbucket.org/${userName}/${repoName}/src/${sha}`;
  128. const webUrl = provider.webUrl(branch, '');
  129. expect(webUrl).to.equal(expectedUrl);
  130. });
  131. });
  132. suite('with ssh remote URL', function () {
  133. const remoteUrl = `git@bitbucket.org:${userName}/${repoName}.git`;
  134. const provider = gitProvider(remoteUrl, sha);
  135. suite('#webUrl(branch, filePath)', function () {
  136. test('should returns HTTPS URL', function () {
  137. const expectedUrl = `https://bitbucket.org/${userName}/${repoName}/src/${sha}${filePath}`;
  138. const webUrl = provider.webUrl(branch, filePath);
  139. expect(webUrl).to.equal(expectedUrl);
  140. });
  141. });
  142. });
  143. });
  144. suite('GitLab', function () {
  145. const remoteUrl = `https://gitlab.com/${userName}/${repoName}.git`;
  146. const provider = gitProvider(remoteUrl);
  147. suite('#webUrl(branch, filePath)', function () {
  148. test('should returns file URL', function () {
  149. const expectedUrl = `https://gitlab.com/${userName}/${repoName}/blob/${branch}${filePath}`;
  150. const webUrl = provider.webUrl(branch, filePath);
  151. expect(webUrl).to.equal(expectedUrl);
  152. });
  153. });
  154. suite('#webUrl(branch, filePath, line)', function () {
  155. test('should returns file URL with line hash', function () {
  156. const expectedUrl = `https://gitlab.com/${userName}/${repoName}/blob/${branch}${filePath}#L${line}`;
  157. const webUrl = provider.webUrl(branch, filePath, line);
  158. expect(webUrl).to.equal(expectedUrl);
  159. });
  160. });
  161. suite('#webUrl(branch)', function () {
  162. test('should returns repository root URL', function () {
  163. const expectedUrl = `https://gitlab.com/${userName}/${repoName}/tree/${branch}`;
  164. const webUrl = provider.webUrl(branch);
  165. expect(webUrl).to.equal(expectedUrl);
  166. });
  167. });
  168. suite('with ssh remote URL', function () {
  169. const remoteUrl = `git@gitlab.com:${userName}/${repoName}.git`;
  170. const provider = gitProvider(remoteUrl);
  171. suite('#webUrl(branch, filePath)', function () {
  172. test('should returns HTTPS URL', function () {
  173. const expectedUrl = `https://gitlab.com/${userName}/${repoName}/blob/${branch}${filePath}`;
  174. const webUrl = provider.webUrl(branch, filePath);
  175. expect(webUrl).to.equal(expectedUrl);
  176. });
  177. });
  178. });
  179. });
  180. suite('VisualStudio', function () {
  181. const projectName = 'testProject';
  182. const remoteUrl = `https://test-account.visualstudio.com/${projectName}/_git/${repoName}.git`;
  183. const provider = gitProvider(remoteUrl);
  184. suite('#webUrl(branch, filePath)', function () {
  185. test('should returns file URL', function () {
  186. const expectedUrl = `https://test-account.visualstudio.com/${projectName}/_git/${repoName}`;
  187. const expectedQuery = {
  188. path: filePath,
  189. version: `GB${branch}`,
  190. };
  191. const webUrl = provider.webUrl(branch, filePath);
  192. const [url, query] = webUrl.split('?');
  193. expect(url).to.equal(expectedUrl);
  194. expect(querystring.parse(query)).to.deep.equal(expectedQuery);
  195. });
  196. });
  197. suite('#webUrl(branch, filePath, line)', function () {
  198. test('should returns file URL with line query', function () {
  199. const expectedUrl = `https://test-account.visualstudio.com/${projectName}/_git/${repoName}`;
  200. const expectedQuery = {
  201. path: filePath,
  202. version: `GB${branch}`,
  203. line: String(line),
  204. };
  205. const webUrl = provider.webUrl(branch, filePath, line);
  206. const [url, query] = webUrl.split('?');
  207. expect(url).to.equal(expectedUrl);
  208. expect(querystring.parse(query)).to.deep.equal(expectedQuery);
  209. });
  210. });
  211. suite('#webUrl(branch)', function () {
  212. test('should returns repository root URL', function () {
  213. const expectedUrl = `https://test-account.visualstudio.com/${projectName}/_git/${repoName}`;
  214. const expectedQuery = {
  215. version: `GB${branch}`,
  216. };
  217. const webUrl = provider.webUrl(branch);
  218. const [url, query] = webUrl.split('?');
  219. expect(url).to.equal(expectedUrl);
  220. expect(querystring.parse(query)).to.deep.equal(expectedQuery);
  221. });
  222. });
  223. suite('with ssh remote URL', function () {
  224. const remoteUrl = `ssh://test-account@test-account.visualstudio.com:22/${projectName}/_git/${repoName}.git`;
  225. const provider = gitProvider(remoteUrl);
  226. suite('#webUrl(branch, filePath)', function () {
  227. test('should returns HTTPS URL', function () {
  228. const expectedUrl = `https://test-account.visualstudio.com/${projectName}/_git/${repoName}`;
  229. const expectedQuery = {
  230. path: filePath,
  231. version: `GB${branch}`,
  232. };
  233. const webUrl = provider.webUrl(branch, filePath);
  234. const [url, query] = webUrl.split('?');
  235. expect(url).to.equal(expectedUrl);
  236. expect(querystring.parse(query)).to.deep.equal(expectedQuery);
  237. });
  238. });
  239. });
  240. });
  241. });