From 67fd02cdc217326cab2aa3d1d7beca9ea12cfde6 Mon Sep 17 00:00:00 2001 From: Blaine Bublitz Date: Wed, 8 Jun 2016 10:54:44 -0700 Subject: [PATCH] Breaking: Return wrapped task instead of original function, add unwrap method to wrapper, update tests --- README.md | 3 ++- lib/get-task.js | 16 +--------------- lib/last-run.js | 8 ++++++++ lib/set-task.js | 5 +++++ test/task.js | 29 ++++++++++++++++++++++------- 5 files changed, 38 insertions(+), 23 deletions(-) diff --git a/README.md b/README.md index 27d3e5c..f4485d3 100644 --- a/README.md +++ b/README.md @@ -58,7 +58,8 @@ to create the registry for this instance. Both a `getter` and `setter` for tasks. If a string (`taskName`) is given as the only argument, it behaves as a `getter` -and returns the registered function. +and returns the wrapped task (not the original function). The wrapped task has a `unwrap` +method that will return the original function. If a function (`fn`) and optionally a string (`taskName`) is given, it behaves as a `setter` and will register the task by the `taskName`. If `taskName` is not diff --git a/lib/get-task.js b/lib/get-task.js index ec1fd72..14ced1e 100644 --- a/lib/get-task.js +++ b/lib/get-task.js @@ -1,21 +1,7 @@ 'use strict'; -var metadata = require('./helpers/metadata'); - function get(name) { - var wrapper = this._registry.get(name); - - if (!wrapper) { - return; - } - - var meta = metadata.get(wrapper); - - if (meta) { - return meta.orig; - } - - return wrapper; + return this._registry.get(name); } module.exports = get; diff --git a/lib/last-run.js b/lib/last-run.js index 187be9d..2d30960 100644 --- a/lib/last-run.js +++ b/lib/last-run.js @@ -2,6 +2,8 @@ var retrieveLastRun = require('last-run'); +var metadata = require('./helpers/metadata'); + function lastRun(task, timeResolution) { if (timeResolution == null) { timeResolution = process.env.UNDERTAKER_TIME_RESOLUTION; @@ -12,6 +14,12 @@ function lastRun(task, timeResolution) { fn = this._getTask(task); } + var meta = metadata.get(fn); + + if (meta) { + fn = meta.orig || fn; + } + return retrieveLastRun(fn, timeResolution); } diff --git a/lib/set-task.js b/lib/set-task.js index 8083b62..3fd6c00 100644 --- a/lib/set-task.js +++ b/lib/set-task.js @@ -13,6 +13,11 @@ function set(name, fn) { return fn.apply(this, arguments); } + function unwrap() { + return fn; + } + + taskWrapper.unwrap = unwrap; taskWrapper.displayName = name; var meta = metadata.get(fn) || {}; diff --git a/test/task.js b/test/task.js index 17d39d2..7c590f9 100644 --- a/test/task.js +++ b/test/task.js @@ -25,20 +25,20 @@ describe('task', function() { it('should register a named function', function(done) { taker.task(noop); - expect(taker.task('noop')).to.equal(noop); + expect(taker.task('noop').unwrap()).to.equal(noop); done(); }); it('should register an anonymous function by string name', function(done) { taker.task('test1', anon); - expect(taker.task('test1')).to.equal(anon); + expect(taker.task('test1').unwrap()).to.equal(anon); done(); }); it('should register an anonymous function by displayName property', function(done) { anon.displayName = ''; taker.task(anon); - expect(taker.task('')).to.equal(anon); + expect(taker.task('').unwrap()).to.equal(anon); delete anon.displayName; done(); }); @@ -54,7 +54,7 @@ describe('task', function() { it('should register a named function by string name', function(done) { taker.task('test1', noop); - expect(taker.task('test1')).to.equal(noop); + expect(taker.task('test1').unwrap()).to.equal(noop); done(); }); @@ -65,7 +65,22 @@ describe('task', function() { it('should get a task that was registered', function(done) { taker.task('test1', noop); - expect(taker.task('test1')).to.equal(noop); + expect(taker.task('test1').unwrap()).to.equal(noop); + done(); + }); + + it('should get the wrapped task, not original function', function(done) { + var registry = taker.registry(); + taker.task('test1', noop); + expect(taker.task('test1').unwrap).to.be.a.function(); + expect(taker.task('test1')).to.equal(registry.get('test1')); + done(); + }); + + it('provides an `unwrap` method to get the original function', function(done) { + taker.task('test1', noop); + expect(taker.task('test1').unwrap).to.be.a.function(); + expect(taker.task('test1').unwrap()).to.equal(noop); done(); }); @@ -79,7 +94,7 @@ describe('task', function() { function fn() {} fn.displayName = 'test1'; taker.task(fn); - expect(taker.task('test1')).to.equal(fn); + expect(taker.task('test1').unwrap()).to.equal(fn); done(); }); @@ -87,7 +102,7 @@ describe('task', function() { function fn() {} taker.task('foo', fn); taker.task('bar', fn); - expect(taker.task('foo' )).to.equal(taker.task('bar')); + expect(taker.task('foo').unwrap()).to.equal(taker.task('bar').unwrap()); done(); });