Skip to content
This repository
Browse code

Add new reporter for machinally analysis

This **machineout** reporter will report any Fail or Error as format as
below::

    <Type>:<Method>:<File>:<Row>:<Column>:<Message>

So you can analyse the message with simply splitting the line with ":"
In python, the analyse method will be like below::

    def analyse_nodeunit(s):
        type, method, file, row, column, message = s.split(":", 5)
        return type, method, file, row, column, message
  • Loading branch information...
commit ca3ecf8caf778edaf8977b8345966bc24ee063ed 1 parent eb210ae
Λlisue lambdalisue authored
BIN  img/example_machineout.png
3  lib/reporters/index.js
@@ -3,7 +3,8 @@ module.exports = {
3 3 'default': require('./default'),
4 4 'skip_passed': require('./skip_passed'),
5 5 'minimal': require('./minimal'),
6   - 'html': require('./html')
  6 + 'html': require('./html'),
  7 + 'machineout': require('./machineout')
7 8 // browser test reporter is not listed because it cannot be used
8 9 // with the command line tool, only inside a browser.
9 10 };
109 lib/reporters/machineout.js
... ... @@ -0,0 +1,109 @@
  1 +/*!
  2 + * Nodeunit
  3 + *
  4 + * @author Alisue (lambdalisue@hashnote.net)
  5 + * @url http://hashnote.net/
  6 + *
  7 + * Copyright (c) 2011 Alisue
  8 + * MIT Licensed
  9 + */
  10 +
  11 +/**
  12 + * Module dependencies
  13 + */
  14 +
  15 +var nodeunit = require('../nodeunit'),
  16 + utils = require('../utils'),
  17 + fs = require('fs'),
  18 + track = require('../track'),
  19 + path = require('path');
  20 + AssertionError = require('../assert').AssertionError;
  21 +
  22 +/**
  23 + * Reporter info string
  24 + */
  25 +
  26 +exports.info = "Tests reporter for machinally analysis";
  27 +
  28 +
  29 +/**
  30 + * Run all tests within each module, reporting the results to the command-line.
  31 + *
  32 + * @param {Array} files
  33 + * @api public
  34 + */
  35 +
  36 +exports.run = function (files, options) {
  37 + // options doesn't effect
  38 +
  39 + var parseStack = function (stack, delimiter) {
  40 + var parseTrace = function (trace) {
  41 + var filename, row, column;
  42 + pattern1 = /\s{4}at\s\S+\s\(([^:]+):(\d+):(\d+)\)/;
  43 + pattern2 = /\s{4}at\s([^:]+):(\d+):(\d+)/;
  44 +
  45 + if (trace.match(pattern1) !== null) {
  46 + filename = RegExp.$1;
  47 + row = RegExp.$2;
  48 + column = RegExp.$3;
  49 + } else if (trace.match(pattern2) !== null) {
  50 + filename = RegExp.$1;
  51 + row = RegExp.$2;
  52 + column = RegExp.$3;
  53 + } else {
  54 + throw new Error("Could not parse a line of stack trace: " + trace);
  55 + }
  56 + return {filename: filename, row: row, column: column};
  57 + };
  58 + if (delimiter === undefined) {
  59 + delimiter = ':';
  60 + }
  61 + traceback = stack.split('\n');
  62 + firstline = traceback.shift();
  63 + trace = parseTrace(traceback[0]);
  64 + return {filename: trace.filename, row: trace.row, column: trace.column, message: firstline};
  65 + };
  66 + var createErrorMessage = function(type, name, filename, row, column, message){
  67 + return [type, name, filename, row, column, message].join(":");
  68 + };
  69 + var paths = files.map(function (p) {
  70 + return path.join(process.cwd(), p);
  71 + });
  72 + var tracker = track.createTracker(function (tracker) {
  73 + if (tracker.unfinished()) {
  74 + var names = tracker.names();
  75 + for (var i = 0; i < names.length; i += 1) {
  76 + console.log(createErrorMessage(
  77 + 'Error', names[i],
  78 + '', '', '',
  79 + 'Undone tests - To fix this, make sure all tests call test.done()'
  80 + ));
  81 + }
  82 + process.reallyExit(tracker.unfinished());
  83 + }
  84 + });
  85 +
  86 + nodeunit.runFiles(paths, {
  87 + testspec: options.testspec,
  88 + moduleStart: function (name) {},
  89 + testDone: function (name, assertions) {
  90 + tracker.remove(name);
  91 + if (assertions.failures()) {
  92 + assertions.forEach(function (a) {
  93 + var stacks, message, filename, row, column;
  94 + if (a.failed()) {
  95 + stackinfo = parseStack(a.error.stack, ':');
  96 + console.log(createErrorMessage(
  97 + 'Fail', name, stackinfo.filename,
  98 + stackinfo.row, stackinfo.column, stackinfo.message));
  99 + }
  100 + });
  101 + }
  102 + },
  103 + done: function (assertions, end) {},
  104 + testStart: function(name) {
  105 + tracker.put(name);
  106 + }
  107 + });
  108 +};
  109 +
27 share/nodeunit.vim
... ... @@ -0,0 +1,27 @@
  1 +" Vim compiler file
  2 +" Compiler: Unit testing for javascript using nodeunit
  3 +" Maintainer: lambdalisue <lambdalisue@hashnote.net>
  4 +" Last Change: 2011 Sep 06
  5 +"
  6 +" How to install:
  7 +" copy this vim script into $VIM/compiler/ directory
  8 +" and add the line below to $VIM/ftplugin/javascript.vim (or coffee.vim)
  9 +"
  10 +" compiler nodeunit
  11 +"
  12 +" How to use:
  13 +" Test with ':make' command of vim. See vim plugin called 'vim-makegreen'
  14 +"
  15 +if exists("current_compiler")
  16 + finish
  17 +endif
  18 +let current_compiler = "nodeunit"
  19 +
  20 +if exists(":CompilerSet") != 2 " older Vim always used :setlocal
  21 + command -nargs=* CompilerSet setlocal <args>
  22 +endif
  23 +
  24 +" Using folked version nodeunit found at
  25 +" http://github.com/lambdalisue/nodeunit.git
  26 +CompilerSet makeprg=echo\ $*\ >/dev/null;\ nodeunit\ --reporter\ machineout\ \"%\"
  27 +CompilerSet efm=%s:%f:%l:%c:%m

0 comments on commit ca3ecf8

Please sign in to comment.
Something went wrong with that request. Please try again.