Manager.js 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. var expect = require('expect.js');
  2. var path = require('path');
  3. var rimraf = require('rimraf');
  4. var Logger = require('bower-logger');
  5. var Manager = require('../../lib/core/Manager');
  6. var defaultConfig = require('../../lib/config');
  7. describe('Manager', function () {
  8. var manager;
  9. var packagesCacheDir =
  10. path.join(__dirname, '../assets/temp-resolve-cache');
  11. var registryCacheDir =
  12. path.join(__dirname, '../assets/temp-registry-cache');
  13. after(function () {
  14. rimraf.sync(registryCacheDir);
  15. rimraf.sync(packagesCacheDir);
  16. });
  17. beforeEach(function (next) {
  18. var logger = new Logger();
  19. var config = defaultConfig({
  20. storage: {
  21. packages: packagesCacheDir,
  22. registry: registryCacheDir
  23. }
  24. });
  25. manager = new Manager(config, logger);
  26. next();
  27. });
  28. describe('_areCompatible', function () {
  29. describe('resolved is being fetched', function() {
  30. it('accepts endpoints with same targets', function () {
  31. expect(manager._areCompatible(
  32. { name: 'foo', target: 'xxx' },
  33. { name: 'bar', target: 'xxx' }
  34. )).to.be(true);
  35. });
  36. it('rejects endpoints with different targets', function () {
  37. expect(manager._areCompatible(
  38. { name: 'foo', target: 'xxx' },
  39. { name: 'bar', target: 'yyy' }
  40. )).to.be(false);
  41. });
  42. it('accepts with version and matching range', function () {
  43. expect(manager._areCompatible(
  44. { name: 'foo', target: '0.1.2' },
  45. { name: 'bar', target: '~0.1.0' }
  46. )).to.be(true);
  47. });
  48. it('rejects with version and non-matching range', function () {
  49. expect(manager._areCompatible(
  50. { name: 'foo', target: '0.1.2' },
  51. { name: 'bar', target: '~0.1.3' }
  52. )).to.be(false);
  53. });
  54. it('accepts with matching range and version', function () {
  55. expect(manager._areCompatible(
  56. { name: 'foo', target: '~0.1.0' },
  57. { name: 'bar', target: '0.1.2' }
  58. )).to.be(true);
  59. });
  60. it('accepts with non-matching range and version', function () {
  61. expect(manager._areCompatible(
  62. { name: 'foo', target: '~0.1.3' },
  63. { name: 'bar', target: '0.1.2' }
  64. )).to.be(false);
  65. });
  66. it('accepts with matching ranges', function () {
  67. expect(manager._areCompatible(
  68. { name: 'foo', target: '~0.1.0' },
  69. { name: 'bar', target: '~0.1.3' }
  70. )).to.be(true);
  71. });
  72. it('rejects with non-matching ranges', function () {
  73. expect(manager._areCompatible(
  74. { name: 'foo', target: '~0.1.0' },
  75. { name: 'bar', target: '~0.2.3' }
  76. )).to.be(false);
  77. });
  78. it('rejects with non-matching ranges', function () {
  79. expect(manager._areCompatible(
  80. { name: 'foo', target: '~0.1.0' },
  81. { name: 'bar', target: 'xxx' }
  82. )).to.be(false);
  83. });
  84. });
  85. describe('resolved is already fetched', function () {
  86. var resolved = {
  87. name: 'foo',
  88. target: '~1.2.1',
  89. pkgMeta: {
  90. version: '1.2.3'
  91. }
  92. };
  93. it('accepts if the same version as resolved', function () {
  94. expect(manager._areCompatible(
  95. { name: 'foo', target: '1.2.3' },
  96. resolved
  97. )).to.be(true);
  98. });
  99. it('rejects if different version than resolved', function () {
  100. expect(manager._areCompatible(
  101. { name: 'foo', target: '1.2.4' },
  102. resolved
  103. )).to.be(false);
  104. });
  105. it('accepts if range matches resolved version', function () {
  106. expect(manager._areCompatible(
  107. { name: 'foo', target: '~1.2.1' },
  108. resolved
  109. )).to.be(true);
  110. });
  111. it('rejects if range does not match', function () {
  112. expect(manager._areCompatible(
  113. { name: 'foo', target: '~1.2.4' },
  114. resolved
  115. )).to.be(false);
  116. });
  117. });
  118. });
  119. describe('_getCap', function () {
  120. it('finds highest bound', function () {
  121. var highest = manager._getCap(
  122. [['2.1.1-0', '<2.2.0-0'], '<3.2.0'],
  123. 'highest'
  124. );
  125. expect(highest).to.eql({
  126. version: '3.2.0',
  127. comparator: '<'
  128. });
  129. });
  130. it('finds lowest bound', function () {
  131. var highest = manager._getCap(
  132. [['2.1.1-0', '<2.2.0-0'], '<3.2.0'],
  133. 'lowest'
  134. );
  135. expect(highest).to.eql({
  136. version: '2.1.1-0',
  137. comparator: ''
  138. });
  139. });
  140. it('defaults to highest bound', function () {
  141. var highest = manager._getCap(
  142. ['1.0.0', '2.0.0']
  143. );
  144. expect(highest).to.eql({
  145. version: '2.0.0',
  146. comparator: ''
  147. });
  148. });
  149. it('ignores non-semver elements', function () {
  150. var highest = manager._getCap(
  151. ['0.9', '>1.0.1', ['<1.0.0', 'lol']]
  152. );
  153. expect(highest).to.eql({
  154. version: '1.0.1',
  155. comparator: '>'
  156. });
  157. });
  158. it('returns empty object if cap is not found', function () {
  159. var highest = manager._getCap(
  160. ['0.9'] // Not a semver
  161. );
  162. expect(highest).to.eql({});
  163. });
  164. });
  165. describe('_uniquify', function () {
  166. it('leaves last unique element', function () {
  167. var unique = manager._uniquify([
  168. { name: 'foo', id: 1 },
  169. { name: 'foo', id: 2 }
  170. ]);
  171. expect(unique).to.eql([
  172. { name: 'foo', id: 2 }
  173. ]);
  174. });
  175. it('compares by name first', function () {
  176. var unique = manager._uniquify([
  177. { name: 'foo', source: 'google.com' },
  178. { name: 'foo', source: 'facebook.com' }
  179. ]);
  180. expect(unique).to.eql([
  181. { name: 'foo', source: 'facebook.com' }
  182. ]);
  183. });
  184. it('compares by source if name is not available', function () {
  185. var unique = manager._uniquify([
  186. { source: 'facebook.com' },
  187. { source: 'facebook.com' }
  188. ]);
  189. expect(unique).to.eql([
  190. { source: 'facebook.com' }
  191. ]);
  192. });
  193. it('leaves different targets intact', function() {
  194. var unique = manager._uniquify([
  195. { source: 'facebook.com', target: 'a1b2c3' },
  196. { source: 'facebook.com', target: 'ffffff' }
  197. ]);
  198. expect(unique).to.eql([
  199. { source: 'facebook.com', target: 'a1b2c3' },
  200. { source: 'facebook.com', target: 'ffffff' }
  201. ]);
  202. });
  203. it('removes if same targets', function() {
  204. var unique = manager._uniquify([
  205. { source: 'facebook.com', target: 'ffffff' },
  206. { source: 'facebook.com', target: 'ffffff' }
  207. ]);
  208. expect(unique).to.eql([
  209. { source: 'facebook.com', target: 'ffffff' }
  210. ]);
  211. });
  212. it('ignores other fields', function() {
  213. var unique = manager._uniquify([
  214. { source: 'facebook.com', foo: 12 },
  215. { source: 'facebook.com', bar: 13 }
  216. ]);
  217. expect(unique).to.eql([
  218. { source: 'facebook.com', bar: 13 }
  219. ]);
  220. });
  221. });
  222. });