Skip to content

Commit

Permalink
Merge pull request #58 from mrhooray/path
Browse files Browse the repository at this point in the history
better path handling
  • Loading branch information
Rui Hu committed Feb 1, 2016
2 parents 9ba960a + dbafb70 commit 6bfebe9
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 6 deletions.
24 changes: 18 additions & 6 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
'use strict';

var fs = require('fs');
var path = require('path');
var url = require('url');
var spawn = require('child_process').spawn;
var through = require('through2');
var gutil = require('gulp-util');

var pluginName = require('./package.json').name;
var extend = require('./extend');

Expand All @@ -20,7 +22,7 @@ function mochaPhantomJS(options) {
return through.obj(function (file, enc, cb) {
var args = [
scriptPath,
toURI(file.path, options.mocha),
toURL(file.path, options.mocha),
options.reporter || 'spec',
JSON.stringify(options.phantomjs || {})
];
Expand All @@ -37,18 +39,28 @@ function mochaPhantomJS(options) {
});
}

function toURI(path, query) {
function toURL(path, query) {
var parsed = url.parse(path, true);

parsed.query = extend(parsed.query, query);
parsed.search = null;

if (!parsed.protocol) {
parsed.protocol = 'file:';
parsed.slashes = true;
if (parsed.protocol === 'http:' ) {
return url.format(parsed);
} else {
return fileURL(url.format(parsed));
}
}

function fileURL(str) {
var pathName = path.resolve(str).replace(/\\/g, '/');

// for windows
if (pathName[0] !== '/') {
pathName = '/' + pathName;
}

return url.format(parsed);
return encodeURI('file://' + pathName);
}

function spawnPhantomJS(args, options, stream, cb) {
Expand Down
2 changes: 2 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ var mochaPhantomJS = require('../index');
var out = process.stdout.write.bind(process.stdout);

describe('gulp-mocha-phantomjs', function () {
this.timeout(0);

it('should pass when test passed', function (cb) {
var file = new gutil.File({path: path.join(__dirname, 'fixture-pass.html')});
var stream = mochaPhantomJS();
Expand Down

0 comments on commit 6bfebe9

Please sign in to comment.