From 194b219e2324709cf026c097f365dccc3f5aa7a7 Mon Sep 17 00:00:00 2001 From: Olivier Louvignes Date: Tue, 31 Mar 2015 20:23:52 +0100 Subject: [PATCH] chore(global): use two-space indenting and single quotes (closes #45) --- .editorconfig | 17 +- .jshintrc | 4 +- README.md | 32 +-- index.js | 83 +++---- test/.jshintrc | 4 +- test/path-parsing.spec.js | 184 +++++++-------- test/rename-sourcemap.spec.js | 50 ++-- test/rename.spec.js | 422 +++++++++++++++++----------------- test/spec-helper.js | 64 +++--- 9 files changed, 433 insertions(+), 427 deletions(-) diff --git a/.editorconfig b/.editorconfig index 6d4d6d9..10bf31b 100644 --- a/.editorconfig +++ b/.editorconfig @@ -1,9 +1,16 @@ -# http://editorconfig.org +# EditorConfig helps developers define and maintain consistent +# coding styles between different editors and IDEs +# editorconfig.org + root = true [*] -indent_style = tab -indent_size = 4 + +# Change these settings to your own preference +indent_style = space +indent_size = 2 + +# We recommend you to keep these unchanged end_of_line = lf charset = utf-8 trim_trailing_whitespace = true @@ -12,10 +19,6 @@ insert_final_newline = true [*.md] trim_trailing_whitespace = false -[*.{json,yml}] -indent_style = space -indent_size = 2 - [test/fixtures/*] insert_final_newline = false trim_trailing_whitespace = false diff --git a/.jshintrc b/.jshintrc index a9ccf70..da64b6e 100644 --- a/.jshintrc +++ b/.jshintrc @@ -6,11 +6,11 @@ "curly": true, "eqeqeq": true, "immed": true, - "indent": 4, + "indent": 2, "latedef": true, "newcap": true, "noarg": true, - "quotmark": "double", + "quotmark": "single", "regexp": true, "undef": true, "unused": true, diff --git a/README.md b/README.md index 379ed8c..b45c366 100644 --- a/README.md +++ b/README.md @@ -16,28 +16,28 @@ var rename = require("gulp-rename"); // rename via string gulp.src("./src/main/text/hello.txt") - .pipe(rename("main/text/ciao/goodbye.md")) - .pipe(gulp.dest("./dist")); // ./dist/main/text/ciao/goodbye.md + .pipe(rename("main/text/ciao/goodbye.md")) + .pipe(gulp.dest("./dist")); // ./dist/main/text/ciao/goodbye.md // rename via function gulp.src("./src/**/hello.txt") - .pipe(rename(function (path) { - path.dirname += "/ciao"; - path.basename += "-goodbye"; - path.extname = ".md" - })) - .pipe(gulp.dest("./dist")); // ./dist/main/text/ciao/hello-goodbye.md + .pipe(rename(function (path) { + path.dirname += "/ciao"; + path.basename += "-goodbye"; + path.extname = ".md" + })) + .pipe(gulp.dest("./dist")); // ./dist/main/text/ciao/hello-goodbye.md // rename via hash gulp.src("./src/main/text/hello.txt", { base: process.cwd() }) - .pipe(rename({ - dirname: "main/text/ciao", - basename: "aloha", - prefix: "bonjour-", - suffix: "-hola", - extname: ".md" - })) - .pipe(gulp.dest("./dist")); // ./dist/main/text/ciao/bonjour-aloha-hola.md + .pipe(rename({ + dirname: "main/text/ciao", + basename: "aloha", + prefix: "bonjour-", + suffix: "-hola", + extname: ".md" + })) + .pipe(gulp.dest("./dist")); // ./dist/main/text/ciao/bonjour-aloha-hola.md ``` **See test/rename.spec.js for more examples and test/path-parsing.spec.js for hairy details.** diff --git a/index.js b/index.js index 677d5d3..77f812d 100644 --- a/index.js +++ b/index.js @@ -1,62 +1,65 @@ -var Stream = require("stream"), - Path = require("path"); +'use strict'; + +var Stream = require('stream'); +var Path = require('path'); function gulpRename(obj) { - "use strict"; - var stream = new Stream.Transform({objectMode: true}); + var stream = new Stream.Transform({objectMode: true}); + + function parsePath(path) { + var extname = Path.extname(path); + return { + dirname: Path.dirname(path), + basename: Path.basename(path, extname), + extname: extname + }; + } + + stream._transform = function(file, unused, callback) { - function parsePath(path) { - var extname = Path.extname(path); - return { - dirname: Path.dirname(path), - basename: Path.basename(path, extname), - extname: extname - }; - } + var parsedPath = parsePath(file.relative); + var path; - stream._transform = function(file, unused, callback) { + var type = typeof obj; - var parsedPath = parsePath(file.relative); - var path; + if (type === 'string' && obj !== '') { - var type = typeof obj; + path = obj; - if (type === "string" && obj !== "") { + } else if (type === 'function') { - path = obj; + obj(parsedPath); + path = Path.join(parsedPath.dirname, parsedPath.basename + parsedPath.extname); - } else if (type === "function") { + } else if (type === 'object' && obj !== undefined && obj !== null) { - obj(parsedPath); - path = Path.join(parsedPath.dirname, parsedPath.basename + parsedPath.extname); + var dirname = 'dirname' in obj ? obj.dirname : parsedPath.dirname, + prefix = obj.prefix || '', + suffix = obj.suffix || '', + basename = 'basename' in obj ? obj.basename : parsedPath.basename, + extname = 'extname' in obj ? obj.extname : parsedPath.extname; - } else if (type === "object" && obj !== undefined && obj !== null) { + path = Path.join(dirname, prefix + basename + suffix + extname); - var dirname = "dirname" in obj ? obj.dirname : parsedPath.dirname, - prefix = obj.prefix || "", - suffix = obj.suffix || "", - basename = "basename" in obj ? obj.basename : parsedPath.basename, - extname = "extname" in obj ? obj.extname : parsedPath.extname; + } else { - path = Path.join(dirname, prefix + basename + suffix + extname); + callback(new Error('Unsupported renaming parameter type supplied'), undefined); + return; - } else { - callback(new Error("Unsupported renaming parameter type supplied"), undefined); - return; - } + } - file.path = Path.join(file.base, path); + file.path = Path.join(file.base, path); - // Rename sourcemap if present - if (file.sourceMap) { - file.sourceMap.file = file.relative; - } + // Rename sourcemap if present + if (file.sourceMap) { + file.sourceMap.file = file.relative; + } - callback(null, file); - }; + callback(null, file); + }; - return stream; + return stream; } module.exports = gulpRename; diff --git a/test/.jshintrc b/test/.jshintrc index 850da91..e26f0d4 100644 --- a/test/.jshintrc +++ b/test/.jshintrc @@ -1,4 +1,4 @@ { - "extends": "../.jshintrc", - "mocha": true + "extends": "../.jshintrc", + "mocha": true } diff --git a/test/path-parsing.spec.js b/test/path-parsing.spec.js index 387d92c..225e659 100644 --- a/test/path-parsing.spec.js +++ b/test/path-parsing.spec.js @@ -1,107 +1,107 @@ -/*global helper */ -"use strict"; +'use strict'; +/* global helper */ -require("./spec-helper"); -var Path = require("path"); +require('./spec-helper'); +var Path = require('path'); -describe("gulp-rename path parsing", function () { - describe("dirname", function () { - context("when src pattern contains no globs", function () { - it("dirname is '.'", function (done) { - var srcPattern = "test/fixtures/hello.txt"; - var obj = function (path) { - path.dirname.should.equal("."); - }; - helper(srcPattern, obj, null, done); - }); - }); +describe('gulp-rename path parsing', function () { + describe('dirname', function () { + context('when src pattern contains no globs', function () { + it('dirname is \'.\'', function (done) { + var srcPattern = 'test/fixtures/hello.txt'; + var obj = function (path) { + path.dirname.should.equal('.'); + }; + helper(srcPattern, obj, null, done); + }); + }); - context("when src pattern contains filename glob", function () { - it("dirname is '.'", function (done) { - var srcPattern = "test/fixtures/*.min.txt"; - var obj = function (path) { - path.dirname.should.equal("."); - }; - helper(srcPattern, obj, null, done); - }); - }); + context('when src pattern contains filename glob', function () { + it('dirname is \'.\'', function (done) { + var srcPattern = 'test/fixtures/*.min.txt'; + var obj = function (path) { + path.dirname.should.equal('.'); + }; + helper(srcPattern, obj, null, done); + }); + }); - var dirnameHelper = function (srcPattern) { - it("dirname is path from directory glob to file", function (done) { - var obj = function (path) { - path.dirname.should.match(/^fixtures[0-9]?$/); - }; - helper(srcPattern, obj, null, done); - }); - }; + var dirnameHelper = function (srcPattern) { + it('dirname is path from directory glob to file', function (done) { + var obj = function (path) { + path.dirname.should.match(/^fixtures[0-9]?$/); + }; + helper(srcPattern, obj, null, done); + }); + }; - context("when src pattern matches a directory with *", function () { - dirnameHelper("test/*/*.min.txt"); - }); + context('when src pattern matches a directory with *', function () { + dirnameHelper('test/*/*.min.txt'); + }); - context("when src pattern matches a directory with **", function () { - dirnameHelper("test/**/*.min.txt"); - }); + context('when src pattern matches a directory with **', function () { + dirnameHelper('test/**/*.min.txt'); + }); - context("when src pattern matches a directory with [...]", function () { - dirnameHelper("test/fixt[a-z]res/*.min.txt"); - }); + context('when src pattern matches a directory with [...]', function () { + dirnameHelper('test/fixt[a-z]res/*.min.txt'); + }); - /* SKIP: glob2base does not handle brace expansion as expected. See wearefractal/glob2base#1 */ - context.skip("when src pattern matches a directory with {...,...}", function () { - dirnameHelper("test/f{ri,ixtur}es/*.min.txt"); - }); + /* SKIP: glob2base does not handle brace expansion as expected. See wearefractal/glob2base#1 */ + context.skip('when src pattern matches a directory with {...,...}', function () { + dirnameHelper('test/f{ri,ixtur}es/*.min.txt'); + }); - /* SKIP: glob2base does not handle brace expansion as expected. See wearefractal/glob2base#1 */ - context.skip("when src pattern matches a directory with {#..#}", function () { - dirnameHelper("test/fixtures{0..9}/*.min.txt"); - }); + /* SKIP: glob2base does not handle brace expansion as expected. See wearefractal/glob2base#1 */ + context.skip('when src pattern matches a directory with {#..#}', function () { + dirnameHelper('test/fixtures{0..9}/*.min.txt'); + }); - context("when src pattern matches a directory with an extglob", function () { - dirnameHelper("test/f+(ri|ixtur)es/*.min.txt"); - }); + context('when src pattern matches a directory with an extglob', function () { + dirnameHelper('test/f+(ri|ixtur)es/*.min.txt'); + }); - /* requires glob-stream >= 3.1.0 */ - context.skip("when src pattern includes `base` option", function () { - it("dirname is path from given directory to file", function (done) { - var srcPattern = "test/**/*.min.txt"; - var srcOptions = {base: process.cwd()}; - var obj = function (path) { - path.dirname.should.equal("test/fixtures"); - }; - helper({pattern: srcPattern, options: srcOptions}, obj, null, done); - }); - }); - }); + /* requires glob-stream >= 3.1.0 */ + context.skip('when src pattern includes `base` option', function () { + it('dirname is path from given directory to file', function (done) { + var srcPattern = 'test/**/*.min.txt'; + var srcOptions = {base: process.cwd()}; + var obj = function (path) { + path.dirname.should.equal('test/fixtures'); + }; + helper({pattern: srcPattern, options: srcOptions}, obj, null, done); + }); + }); + }); - describe("basename", function () { - it("strips extension like Path.basename(path, ext)", function (done) { - var srcPattern = "test/fixtures/hello.min.txt"; - var obj = function (path) { - path.basename.should.equal("hello.min"); - path.basename.should.equal(Path.basename(srcPattern, Path.extname(srcPattern))); - }; - helper(srcPattern, obj, null, done); - }); - }); + describe('basename', function () { + it('strips extension like Path.basename(path, ext)', function (done) { + var srcPattern = 'test/fixtures/hello.min.txt'; + var obj = function (path) { + path.basename.should.equal('hello.min'); + path.basename.should.equal(Path.basename(srcPattern, Path.extname(srcPattern))); + }; + helper(srcPattern, obj, null, done); + }); + }); - describe("extname", function () { - it("includes '.' like Path.extname", function (done) { - var srcPattern = "test/fixtures/hello.txt"; - var obj = function (path) { - path.extname.should.equal(".txt"); - path.extname.should.equal(Path.extname(srcPattern)); - }; - helper(srcPattern, obj, null, done); - }); + describe('extname', function () { + it('includes \'.\' like Path.extname', function (done) { + var srcPattern = 'test/fixtures/hello.txt'; + var obj = function (path) { + path.extname.should.equal('.txt'); + path.extname.should.equal(Path.extname(srcPattern)); + }; + helper(srcPattern, obj, null, done); + }); - it("excludes multiple extensions like Path.extname", function (done) { - var srcPattern = "test/fixtures/hello.min.txt"; - var obj = function (path) { - path.extname.should.equal(".txt"); - path.extname.should.equal(Path.extname(srcPattern)); - }; - helper(srcPattern, obj, null, done); - }); - }); + it('excludes multiple extensions like Path.extname', function (done) { + var srcPattern = 'test/fixtures/hello.min.txt'; + var obj = function (path) { + path.extname.should.equal('.txt'); + path.extname.should.equal(Path.extname(srcPattern)); + }; + helper(srcPattern, obj, null, done); + }); + }); }); diff --git a/test/rename-sourcemap.spec.js b/test/rename-sourcemap.spec.js index ff14d6e..16f7ee1 100644 --- a/test/rename-sourcemap.spec.js +++ b/test/rename-sourcemap.spec.js @@ -1,37 +1,37 @@ -"use strict"; +'use strict'; -var rename = require("../index"); -var gutil = require("gulp-util"); -var sourceMaps = require("gulp-sourcemaps"); -require("should"); +var rename = require('../index'); +var gutil = require('gulp-util'); +var sourceMaps = require('gulp-sourcemaps'); +require('should'); -describe("gulp-rename", function () { +describe('gulp-rename', function () { - context("when file has source map", function () { + context('when file has source map', function () { - it ("updates source map file to match relative path of renamed file", function (done) { + it ('updates source map file to match relative path of renamed file', function (done) { - var init = sourceMaps.init(); - var stream = init - .pipe(rename({ prefix: "test-" })) - .pipe(rename({ prefix: "test-" })); + var init = sourceMaps.init(); + var stream = init + .pipe(rename({ prefix: 'test-' })) + .pipe(rename({ prefix: 'test-' })); - stream.on("data", function (file) { - file.sourceMap.file.should.equal("test-test-fixture.css"); - file.sourceMap.file.should.equal(file.relative); - done(); - }); + stream.on('data', function (file) { + file.sourceMap.file.should.equal('test-test-fixture.css'); + file.sourceMap.file.should.equal(file.relative); + done(); + }); - init.write(new gutil.File({ - base: "fixtures", - path: "fixtures/fixture.css", - contents: new Buffer("") - })); + init.write(new gutil.File({ + base: 'fixtures', + path: 'fixtures/fixture.css', + contents: new Buffer('') + })); - init.end(); + init.end(); - }); + }); - }); + }); }); diff --git a/test/rename.spec.js b/test/rename.spec.js index a8d59a4..b77637b 100644 --- a/test/rename.spec.js +++ b/test/rename.spec.js @@ -1,212 +1,212 @@ -/*global helper, helperError */ -"use strict"; - -require("./spec-helper"); - -describe("gulp-rename", function () { - context("with string parameter", function () { - context("when src pattern does not contain directory glob", function () { - it("sets filename to value", function (done) { - var srcPattern = "test/fixtures/hello.txt"; - var obj = "hola.md"; - var expectedPath = "test/fixtures/hola.md"; - helper(srcPattern, obj, expectedPath, done); - }); - }); - context("when src pattern contains directory glob", function () { - it("sets relative path to value", function (done) { - var srcPattern = "test/**/hello.txt"; - var obj = "fixtures/hola.md"; - var expectedPath = "test/fixtures/hola.md"; - helper(srcPattern, obj, expectedPath, done); - }); - }); - }); - - context("with object parameter", function () { - var srcPattern; - beforeEach(function () { - srcPattern = "test/**/hello.txt"; - }); - - context("with empty object", function () { - it("has no effect", function (done) { - var obj = {}; - var expectedPath = "test/fixtures/hello.txt"; - helper(srcPattern, obj, expectedPath, done); - }); - }); - - context("with dirname value", function () { - it("replaces dirname with value", function (done) { - var obj = { - dirname: "elsewhere", - }; - var expectedPath = "test/elsewhere/hello.txt"; - helper(srcPattern, obj, expectedPath, done); - }); - it("removes dirname with './'", function (done) { - var obj = { - dirname: "./", - }; - var expectedPath = "test/hello.txt"; - helper(srcPattern, obj, expectedPath, done); - }); - it("removes dirname with empty string", function (done) { - var obj = { - dirname: "", - }; - var expectedPath = "test/hello.txt"; - helper(srcPattern, obj, expectedPath, done); - }); - }); - - context("with prefix value", function () { - it("prepends value to basename", function (done) { - var obj = { - prefix: "bonjour-", - }; - var expectedPath = "test/fixtures/bonjour-hello.txt"; - helper(srcPattern, obj, expectedPath, done); - }); - }); - - context("with basename value", function () { - it("replaces basename with value", function (done) { - var obj = { - basename: "aloha", - }; - var expectedPath = "test/fixtures/aloha.txt"; - helper(srcPattern, obj, expectedPath, done); - }); - it("removes basename with empty string (for consistency)", function (done) { - var obj = { - prefix: "aloha", - basename: "", - }; - var expectedPath = "test/fixtures/aloha.txt"; - helper(srcPattern, obj, expectedPath, done); - }); - }); - - context("with suffix value", function () { - it("appends value to basename", function (done) { - var obj = { - suffix: "-hola", - }; - var expectedPath = "test/fixtures/hello-hola.txt"; - helper(srcPattern, obj, expectedPath, done); - }); - }); - - context("with extname value", function () { - it("replaces extname with value", function (done) { - var obj = { - extname: ".md", - }; - var expectedPath = "test/fixtures/hello.md"; - helper(srcPattern, obj, expectedPath, done); - }); - it("removes extname with empty string", function (done) { - var obj = { - extname: "", - }; - var expectedPath = "test/fixtures/hello"; - helper(srcPattern, obj, expectedPath, done); - }); - }); - }); - - context("with function parameter", function () { - var srcPattern; - beforeEach(function () { - srcPattern = "test/**/hello.txt"; - }); - - it("receives object with dirname", function (done) { - var obj = function (path) { - path.dirname.should.equal("fixtures"); - path.dirname = "elsewhere"; - }; - var expectedPath = "test/elsewhere/hello.txt"; - helper(srcPattern, obj, expectedPath, done); - }); - - it("receives object with basename", function (done) { - var obj = function (path) { - path.basename.should.equal("hello"); - path.basename = "aloha"; - }; - var expectedPath = "test/fixtures/aloha.txt"; - helper(srcPattern, obj, expectedPath, done); - }); - - it("receives object with extname", function (done) { - var obj = function (path) { - path.extname.should.equal(".txt"); - path.extname = ".md"; - }; - var expectedPath = "test/fixtures/hello.md"; - helper(srcPattern, obj, expectedPath, done); - }); - - it("ignores the return value", function (done) { - var obj = function (/*path*/) { - return { - dirname: "elsewhere", - basename: "aloha", - extname: ".md" - }; - }; - var expectedPath = "test/fixtures/hello.txt"; - helper(srcPattern, obj, expectedPath, done); - }); - - it("receives object with extname even if a different value is returned", function (done) { - var obj = function (path) { - path.extname.should.equal(".txt"); - path.extname = ".md"; - }; - var expectedPath = "test/fixtures/hello.md"; - helper(srcPattern, obj, expectedPath, done); - }); - }); - - context("throws unsupported parameter type", function () { - var srcPattern; - beforeEach(function () { - srcPattern = "test/**/hello.txt"; - }); - - var UNSUPPORTED_PARAMATER = "Unsupported renaming parameter type supplied"; - it("with undefined object", function (done) { - var obj; - var expectedError = UNSUPPORTED_PARAMATER; - helperError(srcPattern, obj, expectedError, done); - }); - - it("with null object", function (done) { - var obj = null; - var expectedError = UNSUPPORTED_PARAMATER; - helperError(srcPattern, obj, expectedError, done); - }); - - it("with empty string", function (done) { - var obj = ""; - var expectedError = UNSUPPORTED_PARAMATER; - helperError(srcPattern, obj, expectedError, done); - }); - - it("with boolean value", function (done) { - var obj = true; - var expectedError = UNSUPPORTED_PARAMATER; - helperError(srcPattern, obj, expectedError, done); - }); - - it("with numeric value", function (done) { - var obj = 1; - var expectedError = UNSUPPORTED_PARAMATER; - helperError(srcPattern, obj, expectedError, done); - }); - }); +'use strict'; +/* global helper, helperError */ + +require('./spec-helper'); + +describe('gulp-rename', function () { + context('with string parameter', function () { + context('when src pattern does not contain directory glob', function () { + it('sets filename to value', function (done) { + var srcPattern = 'test/fixtures/hello.txt'; + var obj = 'hola.md'; + var expectedPath = 'test/fixtures/hola.md'; + helper(srcPattern, obj, expectedPath, done); + }); + }); + context('when src pattern contains directory glob', function () { + it('sets relative path to value', function (done) { + var srcPattern = 'test/**/hello.txt'; + var obj = 'fixtures/hola.md'; + var expectedPath = 'test/fixtures/hola.md'; + helper(srcPattern, obj, expectedPath, done); + }); + }); + }); + + context('with object parameter', function () { + var srcPattern; + beforeEach(function () { + srcPattern = 'test/**/hello.txt'; + }); + + context('with empty object', function () { + it('has no effect', function (done) { + var obj = {}; + var expectedPath = 'test/fixtures/hello.txt'; + helper(srcPattern, obj, expectedPath, done); + }); + }); + + context('with dirname value', function () { + it('replaces dirname with value', function (done) { + var obj = { + dirname: 'elsewhere', + }; + var expectedPath = 'test/elsewhere/hello.txt'; + helper(srcPattern, obj, expectedPath, done); + }); + it('removes dirname with \'./\'', function (done) { + var obj = { + dirname: './', + }; + var expectedPath = 'test/hello.txt'; + helper(srcPattern, obj, expectedPath, done); + }); + it('removes dirname with empty string', function (done) { + var obj = { + dirname: '', + }; + var expectedPath = 'test/hello.txt'; + helper(srcPattern, obj, expectedPath, done); + }); + }); + + context('with prefix value', function () { + it('prepends value to basename', function (done) { + var obj = { + prefix: 'bonjour-', + }; + var expectedPath = 'test/fixtures/bonjour-hello.txt'; + helper(srcPattern, obj, expectedPath, done); + }); + }); + + context('with basename value', function () { + it('replaces basename with value', function (done) { + var obj = { + basename: 'aloha', + }; + var expectedPath = 'test/fixtures/aloha.txt'; + helper(srcPattern, obj, expectedPath, done); + }); + it('removes basename with empty string (for consistency)', function (done) { + var obj = { + prefix: 'aloha', + basename: '', + }; + var expectedPath = 'test/fixtures/aloha.txt'; + helper(srcPattern, obj, expectedPath, done); + }); + }); + + context('with suffix value', function () { + it('appends value to basename', function (done) { + var obj = { + suffix: '-hola', + }; + var expectedPath = 'test/fixtures/hello-hola.txt'; + helper(srcPattern, obj, expectedPath, done); + }); + }); + + context('with extname value', function () { + it('replaces extname with value', function (done) { + var obj = { + extname: '.md', + }; + var expectedPath = 'test/fixtures/hello.md'; + helper(srcPattern, obj, expectedPath, done); + }); + it('removes extname with empty string', function (done) { + var obj = { + extname: '', + }; + var expectedPath = 'test/fixtures/hello'; + helper(srcPattern, obj, expectedPath, done); + }); + }); + }); + + context('with function parameter', function () { + var srcPattern; + beforeEach(function () { + srcPattern = 'test/**/hello.txt'; + }); + + it('receives object with dirname', function (done) { + var obj = function (path) { + path.dirname.should.equal('fixtures'); + path.dirname = 'elsewhere'; + }; + var expectedPath = 'test/elsewhere/hello.txt'; + helper(srcPattern, obj, expectedPath, done); + }); + + it('receives object with basename', function (done) { + var obj = function (path) { + path.basename.should.equal('hello'); + path.basename = 'aloha'; + }; + var expectedPath = 'test/fixtures/aloha.txt'; + helper(srcPattern, obj, expectedPath, done); + }); + + it('receives object with extname', function (done) { + var obj = function (path) { + path.extname.should.equal('.txt'); + path.extname = '.md'; + }; + var expectedPath = 'test/fixtures/hello.md'; + helper(srcPattern, obj, expectedPath, done); + }); + + it('ignores the return value', function (done) { + var obj = function (/*path*/) { + return { + dirname: 'elsewhere', + basename: 'aloha', + extname: '.md' + }; + }; + var expectedPath = 'test/fixtures/hello.txt'; + helper(srcPattern, obj, expectedPath, done); + }); + + it('receives object with extname even if a different value is returned', function (done) { + var obj = function (path) { + path.extname.should.equal('.txt'); + path.extname = '.md'; + }; + var expectedPath = 'test/fixtures/hello.md'; + helper(srcPattern, obj, expectedPath, done); + }); + }); + + context('throws unsupported parameter type', function () { + var srcPattern; + beforeEach(function () { + srcPattern = 'test/**/hello.txt'; + }); + + var UNSUPPORTED_PARAMATER = 'Unsupported renaming parameter type supplied'; + it('with undefined object', function (done) { + var obj; + var expectedError = UNSUPPORTED_PARAMATER; + helperError(srcPattern, obj, expectedError, done); + }); + + it('with null object', function (done) { + var obj = null; + var expectedError = UNSUPPORTED_PARAMATER; + helperError(srcPattern, obj, expectedError, done); + }); + + it('with empty string', function (done) { + var obj = ''; + var expectedError = UNSUPPORTED_PARAMATER; + helperError(srcPattern, obj, expectedError, done); + }); + + it('with boolean value', function (done) { + var obj = true; + var expectedError = UNSUPPORTED_PARAMATER; + helperError(srcPattern, obj, expectedError, done); + }); + + it('with numeric value', function (done) { + var obj = 1; + var expectedError = UNSUPPORTED_PARAMATER; + helperError(srcPattern, obj, expectedError, done); + }); + }); }); diff --git a/test/spec-helper.js b/test/spec-helper.js index da669b0..18f1767 100644 --- a/test/spec-helper.js +++ b/test/spec-helper.js @@ -1,40 +1,40 @@ -"use strict"; +'use strict'; -require("should"); -require("mocha"); +require('should'); +require('mocha'); -var Path = require("path"), - gulp = require("gulp"), - rename = require("../"); +var Path = require('path'), + gulp = require('gulp'), + rename = require('../'); global.helper = function (srcArgs, obj, expectedPath, done) { - var srcPattern = srcArgs.pattern || srcArgs; - var srcOptions = srcArgs.options || {}; - var stream = gulp.src(srcPattern, srcOptions).pipe(rename(obj)); - var count = 0; - stream.on("error", done); - stream.on("data", function () { - count++; - }); - if (expectedPath) { - stream.on("data", function (file) { - var resolvedExpectedPath = Path.resolve(expectedPath); - var resolvedActualPath = Path.join(file.base, file.relative); - resolvedActualPath.should.equal(resolvedExpectedPath); - }); - } - stream.on("end", function () { - count.should.be.greaterThan(0); - done.apply(this, arguments); - }); + var srcPattern = srcArgs.pattern || srcArgs; + var srcOptions = srcArgs.options || {}; + var stream = gulp.src(srcPattern, srcOptions).pipe(rename(obj)); + var count = 0; + stream.on('error', done); + stream.on('data', function () { + count++; + }); + if (expectedPath) { + stream.on('data', function (file) { + var resolvedExpectedPath = Path.resolve(expectedPath); + var resolvedActualPath = Path.join(file.base, file.relative); + resolvedActualPath.should.equal(resolvedExpectedPath); + }); + } + stream.on('end', function () { + count.should.be.greaterThan(0); + done.apply(this, arguments); + }); }; global.helperError = function (srcPattern, obj, expectedError, done) { - var stream = gulp.src(srcPattern).pipe(rename(obj)); - stream.on("error", function (err) { - err.message.should.equal(expectedError); - done(); - }); - stream.on("data", function () {}); - stream.on("end", done); + var stream = gulp.src(srcPattern).pipe(rename(obj)); + stream.on('error', function (err) { + err.message.should.equal(expectedError); + done(); + }); + stream.on('data', function () {}); + stream.on('end', done); };