testcjdroute.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. /* vim: set expandtab ts=4 sw=4: */
  2. /*
  3. * You may redistribute this program and/or modify it under the terms of
  4. * the GNU General Public License as published by the Free Software Foundation,
  5. * either version 3 of the License, or (at your option) any later version.
  6. *
  7. * This program is distributed in the hope that it will be useful,
  8. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. * GNU General Public License for more details.
  11. *
  12. * You should have received a copy of the GNU General Public License
  13. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  14. */
  15. var Fs = require("fs");
  16. var nThen = require("nthen");
  17. var Fs_stat = function (file, callback) {
  18. Fs.stat(file, function (err, ret) {
  19. if (err === 'ENOENT') {
  20. console.log("Magical ENOENT on a file which definitely does exist, good job Linus");
  21. setTimeout(function () {
  22. Fs_stat(file, callback);
  23. }, 1000);
  24. } else {
  25. callback(err, ret);
  26. }
  27. });
  28. };
  29. var getTests = function (file, tests, callback) {
  30. if (/\/(.git|build_.*|node_build|contrib)\//.test(file)) { callback(); return; }
  31. Fs_stat(file, function (err, stat) {
  32. if (err) { throw err; }
  33. if (stat.isDirectory()) {
  34. nThen(function (waitFor) {
  35. Fs.readdir(file, waitFor(function (err, list) {
  36. if (err) { throw err; }
  37. list.forEach(function (subFile) {
  38. getTests(file + '/' + subFile, tests, waitFor());
  39. });
  40. }));
  41. }).nThen(function (waitFor) {
  42. callback();
  43. });
  44. return;
  45. } else if (/_test\.c$/.test(file) && tests.indexOf(file) === -1) {
  46. tests.push(file);
  47. }
  48. callback();
  49. });
  50. };
  51. var generate = module.exports.generate = function (file, builder, callback)
  52. {
  53. var tests = [];
  54. getTests('.', tests, function () {
  55. var prototypes = [];
  56. var listContent = [];
  57. tests.forEach(function (test) {
  58. var main = /^.*\/([^\/]+)\.c$/.exec(test)[1] + '_main';
  59. (builder.config['cflags'+test] =
  60. builder.config['cflags'+test] || []).push('-D', 'main='+main);
  61. file.links.push(test);
  62. listContent.push('{ .func = '+main+', .name = "'+test.replace(/^.*\/|.c$/g, '')+'" },');
  63. prototypes.push('int '+main+'(int argc, char** argv);');
  64. });
  65. file.testcjdroute_tests = listContent.join('\n');
  66. file.testcjdroute_prototypes = prototypes.join('\n');
  67. callback();
  68. });
  69. };