externalize.js 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. var test = require('tap').test;
  2. var spawn = require('child_process').spawn;
  3. var concat = require('concat-stream');
  4. var path = require('path');
  5. var fs = require('fs');
  6. var vm = require('vm');
  7. var temp = require('temp');
  8. temp.track();
  9. var tmpdir = temp.mkdirSync({prefix: 'browserify-test'});
  10. var pubdir = path.join(tmpdir, 'public');
  11. fs.mkdirSync(pubdir);
  12. fs.writeFileSync(
  13. path.join(tmpdir, 'robot.js'),
  14. fs.readFileSync(path.join(__dirname, 'externalize/robot.js'))
  15. );
  16. fs.writeFileSync(
  17. path.join(tmpdir, 'beep.js'),
  18. fs.readFileSync(path.join(__dirname, 'externalize/beep.js'))
  19. );
  20. fs.writeFileSync(
  21. path.join(tmpdir, 'boop.js'),
  22. fs.readFileSync(path.join(__dirname, 'externalize/boop.js'))
  23. );
  24. test('externalize bin', function (t) {
  25. t.plan(5);
  26. var commands = [
  27. [ '-r', './robot.js', '-o', path.join(pubdir, 'common.js') ],
  28. [ '-x', './robot.js', 'beep.js', '-o', path.join(pubdir, 'beep.js') ],
  29. [ '-x', './robot.js', 'boop.js', '-o', path.join(pubdir, 'boop/bop.js') ]
  30. ];
  31. (function next () {
  32. if (commands.length === 0) {
  33. var common = fs.readFileSync(path.join(pubdir, 'common.js'));
  34. var beep = fs.readFileSync(path.join(pubdir, 'beep.js'));
  35. var boop = fs.readFileSync(path.join(pubdir, 'boop/bop.js'));
  36. vm.runInNewContext(common + beep, {
  37. console: { log: function (msg) { t.equal(msg, 'BEEP!') } }
  38. });
  39. vm.runInNewContext(common + boop, {
  40. console: { log: function (msg) { t.equal(msg, 'BOOP!') } }
  41. });
  42. }
  43. else {
  44. var args = commands.shift();
  45. args.unshift(path.join(__dirname, '../bin/cmd.js'));
  46. var ps = spawn(process.execPath, args, { cwd: tmpdir });
  47. ps.stderr.pipe(process.stderr);
  48. ps.on('exit', function (code) {
  49. t.equal(code, 0, 'exit code');
  50. next()
  51. });
  52. }
  53. })();
  54. });