You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

303 lines
9.4 KiB

  1. // Generated by CoffeeScript 1.6.3
  2. var CSON, DocPad, PluginTester, RendererTester, ServerTester, balUtil, expect, extendr, joe, pathUtil, pluginPort, safefs, test, testers, _, _ref, _ref1,
  3. __indexOf = [].indexOf || function(item) { for (var i = 0, l = this.length; i < l; i++) { if (i in this && this[i] === item) return i; } return -1; },
  4. __hasProp = {}.hasOwnProperty,
  5. __extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; };
  6. pathUtil = require('path');
  7. safefs = require('safefs');
  8. balUtil = require('bal-util');
  9. extendr = require('extendr');
  10. joe = require('joe');
  11. expect = require('chai').expect;
  12. CSON = require('cson');
  13. _ = require('lodash');
  14. DocPad = require('./docpad');
  15. pluginPort = 2000 + String((new Date()).getTime()).substr(-6, 4);
  16. testers = {
  17. CSON: CSON,
  18. DocPad: DocPad
  19. };
  20. testers.PluginTester = PluginTester = (function() {
  21. PluginTester.prototype.config = {
  22. testerName: null,
  23. pluginName: null,
  24. pluginPath: null,
  25. autoExit: true,
  26. testPath: null,
  27. outExpectedPath: null,
  28. removeEmptyLines: false
  29. };
  30. PluginTester.prototype.docpadConfig = {
  31. port: null,
  32. growl: false,
  33. logLevel: ((__indexOf.call(process.argv, '-d') >= 0) ? 7 : 5),
  34. rootPath: null,
  35. outPath: null,
  36. srcPath: null,
  37. pluginPaths: null,
  38. enableUnlistedPlugins: true,
  39. enabledPlugins: null,
  40. skipUnsupportedPlugins: false,
  41. catchExceptions: false,
  42. environment: null
  43. };
  44. PluginTester.prototype.docpad = null;
  45. function PluginTester(config, docpadConfig, next) {
  46. var defaultEnabledPlugins, tester, _base, _base1, _base2, _base3, _base4, _base5, _base6, _base7, _base8;
  47. if (config == null) {
  48. config = {};
  49. }
  50. if (docpadConfig == null) {
  51. docpadConfig = {};
  52. }
  53. tester = this;
  54. this.config = extendr.deepExtendPlainObjects({}, PluginTester.prototype.config, this.config, config);
  55. this.docpadConfig = extendr.deepExtendPlainObjects({}, PluginTester.prototype.docpadConfig, this.docpadConfig, docpadConfig);
  56. if ((_base = this.docpadConfig).port == null) {
  57. _base.port = ++pluginPort;
  58. }
  59. if ((_base1 = this.config).testerName == null) {
  60. _base1.testerName = this.config.pluginName;
  61. }
  62. (_base2 = this.config).testPath || (_base2.testPath = pathUtil.join(this.config.pluginPath, 'test'));
  63. (_base3 = this.config).outExpectedPath || (_base3.outExpectedPath = pathUtil.join(this.config.testPath, 'out-expected'));
  64. (_base4 = this.docpadConfig).rootPath || (_base4.rootPath = this.config.testPath);
  65. (_base5 = this.docpadConfig).outPath || (_base5.outPath = pathUtil.join(this.docpadConfig.rootPath, 'out'));
  66. (_base6 = this.docpadConfig).srcPath || (_base6.srcPath = pathUtil.join(this.docpadConfig.rootPath, 'src'));
  67. if ((_base7 = this.docpadConfig).pluginPaths == null) {
  68. _base7.pluginPaths = [this.config.pluginPath];
  69. }
  70. defaultEnabledPlugins = {};
  71. defaultEnabledPlugins[this.config.pluginName] = true;
  72. (_base8 = this.docpadConfig).enabledPlugins || (_base8.enabledPlugins = defaultEnabledPlugins);
  73. joe.describe(this.config.testerName, function(suite, task) {
  74. tester.describe = tester.suite = suite;
  75. tester.it = tester.test = task;
  76. tester.done = tester.exit = function() {};
  77. return typeof next === "function" ? next(null, tester) : void 0;
  78. });
  79. this;
  80. }
  81. PluginTester.prototype.testCreate = function() {
  82. var docpadConfig, tester;
  83. tester = this;
  84. docpadConfig = this.docpadConfig;
  85. this.test("create", function(done) {
  86. return DocPad.createInstance(docpadConfig, function(err, docpad) {
  87. if (err) {
  88. return done(err);
  89. }
  90. tester.docpad = docpad;
  91. return tester.docpad.action('clean', function(err) {
  92. if (err) {
  93. return done(err);
  94. }
  95. return tester.docpad.action('install', function(err) {
  96. return done(err);
  97. });
  98. });
  99. });
  100. });
  101. return this;
  102. };
  103. PluginTester.prototype.testLoad = function() {
  104. var tester;
  105. tester = this;
  106. this.test("load plugin " + tester.config.pluginName, function(done) {
  107. return tester.docpad.loadedPlugin(tester.config.pluginName, function(err, loaded) {
  108. if (err) {
  109. return done(err);
  110. }
  111. expect(loaded).to.be.ok;
  112. return done();
  113. });
  114. });
  115. return this;
  116. };
  117. PluginTester.prototype.testServer = function(next) {
  118. var tester;
  119. tester = this;
  120. this.test("server", function(done) {
  121. return tester.docpad.action('server', function(err) {
  122. return done(err);
  123. });
  124. });
  125. return this;
  126. };
  127. PluginTester.prototype.testGenerate = function() {
  128. var tester;
  129. tester = this;
  130. this.test("generate", function(done) {
  131. return tester.docpad.action('generate', function(err) {
  132. return done(err);
  133. });
  134. });
  135. return this;
  136. };
  137. PluginTester.prototype.testEverything = function() {
  138. var tester;
  139. tester = this;
  140. this.testCreate();
  141. this.testLoad();
  142. this.testGenerate();
  143. this.testServer();
  144. if (typeof this.testCustom === "function") {
  145. this.testCustom();
  146. }
  147. this.finish();
  148. return this;
  149. };
  150. PluginTester.prototype.finish = function() {
  151. var tester;
  152. tester = this;
  153. if (tester.config.autoExit) {
  154. this.test('finish up', function(done) {
  155. done();
  156. tester.exit();
  157. if (tester.config.autoExit !== 'safe') {
  158. return process.exit();
  159. }
  160. });
  161. }
  162. return this;
  163. };
  164. return PluginTester;
  165. })();
  166. testers.ServerTester = ServerTester = (function(_super) {
  167. __extends(ServerTester, _super);
  168. function ServerTester() {
  169. _ref = ServerTester.__super__.constructor.apply(this, arguments);
  170. return _ref;
  171. }
  172. return ServerTester;
  173. })(PluginTester);
  174. testers.RendererTester = RendererTester = (function(_super) {
  175. __extends(RendererTester, _super);
  176. function RendererTester() {
  177. _ref1 = RendererTester.__super__.constructor.apply(this, arguments);
  178. return _ref1;
  179. }
  180. RendererTester.prototype.testGenerate = function() {
  181. var tester;
  182. tester = this;
  183. this.suite("generate", function(suite, test) {
  184. test('action', function(done) {
  185. return tester.docpad.action('generate', function(err) {
  186. return done(err);
  187. });
  188. });
  189. return suite('results', function(suite, test, done) {
  190. return balUtil.scantree(tester.docpadConfig.outPath, function(err, outResults) {
  191. if (err) {
  192. return done(err);
  193. }
  194. return balUtil.scantree(tester.config.outExpectedPath, function(err, outExpectedResults) {
  195. var outExpectedResultsKeys, outResultsKeys, replaceLinesRegex;
  196. if (err) {
  197. return done(err);
  198. }
  199. if (tester.config.removeWhitespace) {
  200. replaceLinesRegex = /(\\r|\\n|\\t|\s)+/g;
  201. outResults = JSON.parse(JSON.stringify(outResults).replace(replaceLinesRegex, ''));
  202. outExpectedResults = JSON.parse(JSON.stringify(outExpectedResults).replace(replaceLinesRegex, ''));
  203. }
  204. outResultsKeys = Object.keys(outResults);
  205. outExpectedResultsKeys = Object.keys(outExpectedResults);
  206. test('same files', function() {
  207. var outDifferenceKeys;
  208. outDifferenceKeys = _.difference(outResultsKeys, outExpectedResultsKeys);
  209. try {
  210. return expect(outDifferenceKeys).to.be.empty;
  211. } catch (_error) {
  212. err = _error;
  213. console.log(outDifferenceKeys);
  214. throw err;
  215. }
  216. });
  217. outResultsKeys.forEach(function(key) {
  218. return test("same file content for: " + key, function() {
  219. return expect(outResults[key]).to.eql(outExpectedResults[key]);
  220. });
  221. });
  222. return done();
  223. });
  224. });
  225. });
  226. });
  227. return this;
  228. };
  229. return RendererTester;
  230. })(PluginTester);
  231. testers.test = test = function(testerConfig, docpadConfig) {
  232. var complete;
  233. if (testerConfig.testerClass == null) {
  234. testerConfig.testerClass = PluginTester;
  235. }
  236. testerConfig.pluginPath = pathUtil.resolve(testerConfig.pluginPath);
  237. if (testerConfig.pluginName == null) {
  238. testerConfig.pluginName = pathUtil.basename(testerConfig.pluginPath).replace('docpad-plugin-', '');
  239. }
  240. if (testerConfig.testerPath == null) {
  241. testerConfig.testerPath = pathUtil.join('out', "" + testerConfig.pluginName + ".tester.js");
  242. }
  243. if (testerConfig.testerPath) {
  244. testerConfig.testerPath = pathUtil.resolve(testerConfig.pluginPath, testerConfig.testerPath);
  245. }
  246. complete = function() {
  247. if (typeof testerConfig.testerClass === 'string') {
  248. testerConfig.testerClass = testers[testerConfig.testerClass];
  249. }
  250. return new testerConfig.testerClass(testerConfig, docpadConfig, function(err, testerInstance) {
  251. if (err) {
  252. throw err;
  253. }
  254. return testerInstance.testEverything();
  255. });
  256. };
  257. if (testerConfig.testerPath) {
  258. safefs.exists(testerConfig.testerPath, function(exists) {
  259. if (exists) {
  260. testerConfig.testerClass = require(testerConfig.testerPath)(testers);
  261. }
  262. return complete();
  263. });
  264. } else {
  265. complete();
  266. }
  267. return testers;
  268. };
  269. module.exports = testers;