install.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. var expect = require('expect.js');
  2. var object = require('mout').object;
  3. var helpers = require('../helpers');
  4. var commands = helpers.require('lib/index').commands;
  5. describe('bower install', function () {
  6. var tempDir = new helpers.TempDir();
  7. var package = new helpers.TempDir({
  8. 'bower.json': {
  9. name: 'package'
  10. }
  11. }).prepare();
  12. var gitPackage = new helpers.TempDir();
  13. var installLogger = function(packages, options, config) {
  14. config = object.merge(config || {}, {
  15. cwd: tempDir.path
  16. });
  17. return commands.install(packages, options, config);
  18. };
  19. var install = function(packages, options, config) {
  20. var logger = installLogger(packages, options, config);
  21. return helpers.expectEvent(logger, 'end');
  22. };
  23. it('writes to bower.json if --save flag is used', function () {
  24. package.prepare();
  25. tempDir.prepare({
  26. 'bower.json': {
  27. name: 'test'
  28. }
  29. });
  30. return install([package.path], { save: true }).then(function() {
  31. expect(tempDir.read('bower.json')).to.contain('dependencies');
  32. });
  33. });
  34. it('reads .bowerrc from cwd', function () {
  35. package.prepare({ foo: 'bar' });
  36. tempDir.prepare({
  37. '.bowerrc': { directory: 'assets' },
  38. 'bower.json': {
  39. name: 'test',
  40. dependencies: {
  41. package: package.path
  42. }
  43. }
  44. });
  45. return install().then(function() {
  46. expect(tempDir.read('assets/package/foo')).to.be('bar');
  47. });
  48. });
  49. it('runs preinstall hook', function () {
  50. package.prepare();
  51. tempDir.prepare({
  52. 'bower.json': {
  53. name: 'test',
  54. dependencies: {
  55. package: package.path
  56. }
  57. },
  58. '.bowerrc': {
  59. scripts: {
  60. preinstall: 'bash -c "echo -n % > preinstall.txt"'
  61. }
  62. }
  63. });
  64. return install().then(function() {
  65. expect(tempDir.read('preinstall.txt')).to.be('package');
  66. });
  67. });
  68. it('runs preinstall hook', function () {
  69. package.prepare();
  70. tempDir.prepare({
  71. 'bower.json': {
  72. name: 'test',
  73. dependencies: {
  74. package: package.path
  75. }
  76. },
  77. '.bowerrc': {
  78. scripts: {
  79. postinstall: 'bash -c "echo -n % > postinstall.txt"'
  80. }
  81. }
  82. });
  83. return install().then(function() {
  84. expect(tempDir.read('postinstall.txt')).to.be('package');
  85. });
  86. });
  87. // To be discussed, but that's the implementation now
  88. it('does not run hooks if nothing is installed', function () {
  89. tempDir.prepare({
  90. 'bower.json': {
  91. name: 'test'
  92. },
  93. '.bowerrc': {
  94. scripts: {
  95. postinstall: 'bash -c "echo -n % > hooks.txt"',
  96. preinstall: 'bash -c "echo -n % > hooks.txt"'
  97. }
  98. }
  99. });
  100. return install().then(function() {
  101. expect(tempDir.exists('hooks.txt')).to.be(false);
  102. });
  103. });
  104. it('runs postinstall after bower.json is written', function () {
  105. package.prepare();
  106. tempDir.prepare({
  107. 'bower.json': {
  108. name: 'test'
  109. },
  110. '.bowerrc': {
  111. scripts: {
  112. postinstall: 'bash -c "cat bower.json > hook.txt"',
  113. }
  114. }
  115. });
  116. return install([package.path], { save: true }).then(function() {
  117. expect(tempDir.read('hook.txt')).to.contain('dependencies');
  118. });
  119. });
  120. it('display the output of hook scripts', function (next) {
  121. package.prepare();
  122. tempDir.prepare({
  123. 'bower.json': {
  124. name: 'test',
  125. dependencies: {
  126. package: package.path
  127. }
  128. },
  129. '.bowerrc': {
  130. scripts: {
  131. postinstall: 'bash -c "echo foobar"'
  132. }
  133. }
  134. });
  135. var lastAction = null;
  136. installLogger().intercept(function (log) {
  137. if (log.level === 'action') {
  138. lastAction = log;
  139. }
  140. }).on('end', function () {
  141. expect(lastAction.message).to.be('foobar');
  142. next();
  143. });
  144. });
  145. it('works for git repositories', function () {
  146. return gitPackage.prepareGit({
  147. '1.0.0': {
  148. 'bower.json': {
  149. name: 'package'
  150. },
  151. 'version.txt': '1.0.0'
  152. },
  153. '1.0.1': {
  154. 'bower.json': {
  155. name: 'package'
  156. },
  157. 'version.txt': '1.0.1'
  158. }
  159. }).then(function() {
  160. tempDir.prepare({
  161. 'bower.json': {
  162. name: 'test',
  163. dependencies: {
  164. package: gitPackage.path + '#1.0.0'
  165. }
  166. }
  167. });
  168. return install().then(function() {
  169. expect(tempDir.read('bower_components/package/version.txt')).to.contain('1.0.0');
  170. });
  171. });
  172. });
  173. });