helpers.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. var Q = require('q');
  2. var path = require('path');
  3. var mkdirp = require('mkdirp');
  4. var rimraf = require('rimraf');
  5. var uuid = require('node-uuid');
  6. var object = require('mout/object');
  7. var fs = require('fs');
  8. var glob = require('glob');
  9. var os = require('os');
  10. var cmd = require('../lib/util/cmd');
  11. var config = require('../lib/config');
  12. // Those are needed for Travis or not configured git environment
  13. var env = {
  14. 'GIT_AUTHOR_DATE': 'Sun Apr 7 22:13:13 2013 +0000',
  15. 'GIT_AUTHOR_NAME': 'André Cruz',
  16. 'GIT_AUTHOR_EMAIL': 'amdfcruz@gmail.com',
  17. 'GIT_COMMITTER_DATE': 'Sun Apr 7 22:13:13 2013 +0000',
  18. 'GIT_COMMITTER_NAME': 'André Cruz',
  19. 'GIT_COMMITTER_EMAIL': 'amdfcruz@gmail.com'
  20. };
  21. // Preserve the original environment
  22. object.mixIn(env, process.env);
  23. var tmpLocation = path.join(
  24. os.tmpdir ? os.tmpdir() : os.tmpDir(),
  25. 'bower-tests',
  26. uuid.v4().slice(0, 8)
  27. );
  28. exports.require = function (name) {
  29. return require(path.join(__dirname, '../', name));
  30. };
  31. // We need to reset cache because tests are reusing temp directories
  32. beforeEach(function () {
  33. config.reset();
  34. });
  35. after(function () {
  36. rimraf.sync(tmpLocation);
  37. });
  38. exports.TempDir = (function() {
  39. function TempDir (defaults) {
  40. this.path = path.join(tmpLocation, uuid.v4());
  41. this.defaults = defaults;
  42. }
  43. TempDir.prototype.create = function (files) {
  44. var that = this;
  45. files = object.merge(files || {}, this.defaults);
  46. if (files) {
  47. object.forOwn(files, function (contents, filepath) {
  48. if (typeof contents === 'object') {
  49. contents = JSON.stringify(contents, null, ' ') + '\n';
  50. }
  51. var fullPath = path.join(that.path, filepath);
  52. mkdirp.sync(path.dirname(fullPath));
  53. fs.writeFileSync(fullPath, contents);
  54. });
  55. }
  56. return this;
  57. };
  58. TempDir.prototype.prepare = function (files) {
  59. rimraf.sync(this.path);
  60. mkdirp.sync(this.path);
  61. this.create(files);
  62. return this;
  63. };
  64. // TODO: Rewrite to synchronous form
  65. TempDir.prototype.prepareGit = function (revisions) {
  66. var that = this;
  67. revisions = object.merge(revisions || {}, this.defaults);
  68. rimraf.sync(that.path);
  69. mkdirp.sync(that.path);
  70. var promise = new Q();
  71. object.forOwn(revisions, function (files, tag) {
  72. promise = promise.then(function () {
  73. return that.git('init');
  74. }).then(function () {
  75. that.glob('./!(.git)').map(function (removePath) {
  76. var fullPath = path.join(that.path, removePath);
  77. rimraf.sync(fullPath);
  78. });
  79. that.create(files);
  80. }).then(function () {
  81. return that.git('add', '-A');
  82. }).then(function () {
  83. return that.git('commit', '-m"commit"');
  84. }).then(function () {
  85. return that.git('tag', tag);
  86. });
  87. });
  88. return promise;
  89. };
  90. TempDir.prototype.glob = function (pattern) {
  91. return glob.sync(pattern, {
  92. cwd: this.path,
  93. dot: true
  94. });
  95. };
  96. TempDir.prototype.read = function (name) {
  97. return fs.readFileSync(path.join(this.path, name), 'utf8');
  98. };
  99. TempDir.prototype.git = function () {
  100. var args = Array.prototype.slice.call(arguments);
  101. return cmd('git', args, { cwd: this.path, env: env });
  102. };
  103. TempDir.prototype.exists = function (name) {
  104. return fs.existsSync(path.join(this.path, name));
  105. };
  106. return TempDir;
  107. })();
  108. exports.expectEvent = function (emitter, eventName) {
  109. var deferred = Q.defer();
  110. emitter.once(eventName, function () {
  111. deferred.resolve(arguments);
  112. });
  113. return deferred.promise;
  114. };