Feature: unittestAppender #84

Closed
osher opened this Issue Jul 29, 2012 · 3 comments

Projects

None yet

2 participants

@osher
Contributor
osher commented Jul 29, 2012

Hi,

I thought to work on something that would look like this:

Synopsis

consider your unit-test to contain a test like this:

test("my operation emits logs properly", function(done) {

  var config = 
      { category: "my-operation"
      , maxEntries : 200   //keeps in memory maximum 200 entries per category 
      };

      testAppender = log4js.addAppender( log4js.unitestsAppender(config), "my-operation" ),
      myAutits = testAppender.getAudits();

  //this operation is assumed to send an entry to getLogger("my-operation")
  operationThatShouldEmitLogs(function(err, result){
      log4js.removeAppender( testAppender);

      assert.ok( 
        myAutits.contains(
          { level    : /INFO|WARN/
          , text     : /(Operation succeeded|Operation failed)/
          , category : "my-operation"
          }
        )
      );
});

or

var vows = require('vows')
   , claim = require('assert')
   ;

vows.describe(
   "my great utility"
).addBatch(
   { "my operation" :
     { topic: 
        function(){
           var cb = this.callback;
           var config = 
               { category: "my-operation"
               , maxEntries : 200   //keeps in memory maximum 200 entries per category 
               };

           testAppender = log4js.addAppender( log4js.unitestsAppender(config), "my-operation" ),
           myAutits = testAppender.getAudits();

           //this operation is assumed to send an entry to getLogger("my-operation")
           operationThatShouldEmitLogs( function(err,result) {
                  log4js.removeAppender( testAppender);
                  cb(err,result);
            });     
        }
     , "should emit loggs properly" :
        function(err, audit){
           assert.isUndefined(err);
           assert.isObject(audit);
           assert.ok( 
             myAutits.contains(
               { level    : /INFO|WARN/
               , text     : /(Operation succeeded|Operation failed)/
               , category : "my-operation"
               }
             )
           );
        }
     }
  }
);

API

I'm not final on the API design, and would love to talk about it with somebody
what I got now - and I'm not sure if its fully compatible with the underlying infra (log4js) is:

  • unitestsAppender
    -- implements Logger
    -- getAudits(0) - returns unitTestsAudits module
  • unitTestsAudits
    -- contains(query), where query can optionally specify level, text and category
    (why category? in case you audit few together)

Technicalities

I looked on ./lib/appenders/console - it looks quite easy to add an appender that will gather the entries in memory, so they can later be searched for assertions.
My real problems are -

  • technically how to do it with GIT and GITHUB...
  • I can't run the unit-tests - perhaps because I'm a windows user and the npm install fiddles with dependencies that are not cross-platform.

GITHUB

I'm still trying to take my first successful steps with github, with cloning a repo, working on a solution and sumbitting a pull request that is not editing a file on the github web editor...

Windows

I can offer help in making component cross-platform, the minuet we figure how to run it on windows, or how to ignore it if it's just wont...

I'd appreciate some help here... :)

@osher osher closed this Jul 29, 2012
@osher osher reopened this Jul 29, 2012
@nomiddlename
Owner

Sounds like it would be useful, but the simplest implementation would be just something like this:

var logs = [];
function logger(logEvent) {
  logs.push(logEvent);
}
log4js.addAppender(logger, 'some-category');

As far as contributing, in your fork of log4js-node create a branch for the new work (see http://gitref.org/branching/). Write your new appender, along with some tests, commit to that branch. Then submit a pull request to me (you can do this via github) to merge into the main log4js.

To get things working in Windows, I had to install the following:
http://www.microsoft.com/visualstudio/en-us/products/2010-editions/visual-cpp-express
http://www.python.org/download/releases/2.7.3/

I've also renamed the test files so that "npm test" will now pick them up. You'll need to update your fork with the latest from mine. I've tried it out on Windows 7, and npm test works now.

@osher
Contributor
osher commented Aug 1, 2012

Im doing my first steps on GIThub, so the goals are not supreme
arhitecture, but the flow -
so - Yes, I know the implementation is simple :) that's more or less what I
had in mind.
Now I'm "Injecting" a mock logger that does the same, and it's kind'a
tiering to have it exposed on every utility and inject it in every
suite-setup - so if we could provide something loadable programmatically to
unit-test frameworks, and then queriable for these tests through simple
usable API - we're winners :)

On Tue, Jul 31, 2012 at 7:58 AM, Gareth Jones <
reply@reply.github.com

wrote:

Sounds like it would be useful, but the simplest implementation would be
just something like this:

var logs = [];
function logger(logEvent) {
  logs.push(logEvent);
}
log4js.addAppender(logger, 'some-category');

As far as contributing, in your fork of log4js-node create a branch for
the new work (see http://gitref.org/branching/). Write your new appender,
along with some tests, commit to that branch. Then submit a pull request to
me (you can do this via github) to merge into the main log4js.

To get things working in Windows, I had to install the following:

http://www.microsoft.com/visualstudio/en-us/products/2010-editions/visual-cpp-express
http://www.python.org/download/releases/2.7.3/

I've also renamed the test files so that "npm test" will now pick them up.
You'll need to update your fork with the latest from mine. I've tried it
out on Windows 7, and npm test works now.


Reply to this email directly or view it on GitHub:
#84 (comment)

@nomiddlename
Owner

Cleaning up old issues.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment