Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix windows #4

Merged
merged 10 commits into from
Apr 12, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 2 additions & 7 deletions lib/print-script.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,8 @@ module.exports = {
this.subject(script)

assert.includes(log.read(), 'Executing "' + script + '":\n')
assert.includes(log.read(),
'> #!/usr/bin/env sh\n' +
'> \n' +
'> npm test -- --debug-brk\n' +
'> \n' +
'\n'
)
assert.includes(log.read(), '> #!/usr/bin/env sh')
assert.includes(log.read(), '> npm test -- --debug-brk')
},
sadPath: function () {
var script = '/silly/nonsense'
Expand Down
4 changes: 2 additions & 2 deletions lib/resolve-script/find-executables.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
var glob = require('glob')
var path = require('path')
var async = require('async')
var isExecutable = require('./is-executable')
var _ = require('lodash')
Expand All @@ -9,7 +10,7 @@ module.exports = function (pattern, cb) {
async.map(results, function (result, cb) {
isExecutable(result, function (er, itIsExecutable) {
if (itIsExecutable) {
cb(er, result)
cb(er, path.resolve(result))
} else {
console.warn(
'Warning: scripty - ignoring script "' + result + '" because it' +
Expand All @@ -24,4 +25,3 @@ module.exports = function (pattern, cb) {
})
})
}

3 changes: 1 addition & 2 deletions lib/resolve-script/find-executables.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@ module.exports = {
})
},
noFilesFound: function (done) {
fs.writeFileSync(this.foo1, 'hi')

this.subject(path.resolve(__dirname, 'foo*'), function (er, result) {
assert.deepEqual(result, [])
done(er)
Expand All @@ -34,6 +32,7 @@ module.exports = {
}.bind(this))
},
oneFileFoundWithOneNonExecutable: function (done) {
if (process.platform === 'win32') return done()
fs.writeFileSync(this.foo1, 'hi')
fs.writeFileSync(this.foo2, 'hi')
exec('chmod +x "' + this.foo1 + '"', function () {
Expand Down
6 changes: 3 additions & 3 deletions lib/resolve-script/generate-glob.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ var path = require('path')
var fs = require('fs')

module.exports = function (dir1, dir2) {
var expanded = path.resolve(dir1, dir2.replace(/\:/g, '/'))
var expanded = path.resolve(dir1, path.join.apply(this, dir2.split(':')))
if (isDir(expanded)) {
if (containsIndexFile(expanded)) {
return expanded + '/index*'
return path.join(expanded, 'index') + '*'
} else {
return expanded + '/*'
return path.join(expanded, '/') + '*'
}
} else {
return expanded + '*'
Expand Down
16 changes: 10 additions & 6 deletions lib/resolve-script/generate-glob.test.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,27 @@
var path = require('path')

var base = path.resolve('test/fixtures/user-scripts')
module.exports = {
beforeEach: function () {
this.subject = require('./generate-glob')
this.root = path.resolve('test/fixtures/user-scripts')
},
fileInADir: function () {
assert.equal(this.root + '/foo/bar*', this.subject(this.root, 'foo:bar'))
assert.equal(this.subject(base, 'foo:bar'), globFor('foo/bar'))
},
nonExistentFile: function () {
assert.equal(this.root + '/fake/stuff*', this.subject(this.root, 'fake:stuff'))
assert.equal(this.subject(base, 'fake:stuff'), globFor('fake/stuff'))
},
dirWithNoIndex: function () {
assert.equal(this.root + '/baz/*', this.subject(this.root, 'baz'))
assert.equal(this.subject(base, 'baz'), globFor('baz/'))
},
dirWithAnIndexFile: function () {
assert.equal(this.root + '/car/index*', this.subject(this.root, 'car'))
assert.equal(this.subject(base, 'car'), globFor('car/index'))
},
dirWithADirNamedIndex: function () {
assert.equal(this.root + '/dog/*', this.subject(this.root, 'dog'))
assert.equal(this.subject(base, 'dog'), globFor('dog/'))
}
}

function globFor (partialPath) {
return path.join(base, partialPath) + '*'
}
10 changes: 4 additions & 6 deletions lib/resolve-script/index.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
var path = require('path')

var generateGlob = require('./generate-glob')
var findExecutables = require('./find-executables')
var scriptDirs = require('./script-dirs')

module.exports = function (name, options, cb) {
var userDir = options.scripts || path.resolve(process.cwd(), 'scripts')
var userGlob = generateGlob(userDir, name)
var dirs = scriptDirs(options)
var userGlob = generateGlob(dirs.userDir, name)
findExecutables(userGlob, function (er, userPaths) {
if (userPaths.length > 0) {
cb(er, userPaths)
} else {
var ourDir = options.builtIn || path.resolve(__dirname, '../../scripts')
var ourGlob = generateGlob(ourDir, name)
var ourGlob = generateGlob(dirs.ourDir, name)
findExecutables(ourGlob, function (er, ourPaths) {
if (ourPaths.length > 0) {
cb(er, ourPaths)
Expand Down
9 changes: 5 additions & 4 deletions lib/resolve-script/index.test.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
var path = require('path')
var isA = td.matchers.isA

module.exports = {
beforeEach: function () {
td.when(td.replace(process, 'cwd')()).thenReturn('/user-dir')
this.generateGlob = td.replace('./generate-glob')
this.findExecutables = td.replace('./find-executables')
this.scriptDirs = td.replace('./script-dirs')
this.subject = require('./index')

this.scriptsDir = path.resolve(__dirname, '../../scripts')
td.when(this.generateGlob('/user-dir/scripts', 'fake')).thenReturn('glob1')
td.when(this.generateGlob(this.scriptsDir, 'fake')).thenReturn('glob2')
td.when(this.scriptDirs(isA(Object))).thenReturn({userDir: 'A', ourDir: 'B'})
td.when(this.generateGlob('A', 'fake')).thenReturn('glob1')
td.when(this.generateGlob('B', 'fake')).thenReturn('glob2')
},
bothUserAndBuiltInScriptsExist: function (done) {
td.when(this.findExecutables('glob1')).thenCallback(null, ['user-path'])
Expand Down
3 changes: 2 additions & 1 deletion lib/resolve-script/is-executable.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
var fs = require('fs')

module.exports = function (path, cb) {
if (process.platform === 'win32') return cb(null, true)
fs.stat(path, function (er, stats) {
if (er) return cb(er)
var mode = stats.mode

var owner = mode >> 6
var group = (mode << 3) >> 6
var others = (mode << 6) >> 6

if (er) return cb(er)
cb(er, !!(owner & 1) || !!(group & 1) || !!(others & 1))
})
}
8 changes: 8 additions & 0 deletions lib/resolve-script/is-executable.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,18 @@ module.exports = {
})
},
noXIsNotX: function (done) {
if (process.platform === 'win32') return done()
fs.chmodSync(root + '/no-x', '644')
subject(root + '/no-x', function (er, answer) {
assert.equal(answer, false)
done(er)
})
},
anyWindowsIsAllX: function (done) {
if (process.platform !== 'win32') return done()
subject(root + '/silly-lololo', function (er, answer) {
assert.equal(answer, true)
done(er)
})
}
}
19 changes: 19 additions & 0 deletions lib/resolve-script/script-dirs.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
var path = require('path')
var fs = require('fs')

module.exports = function (options) {
return {
userDir: findDir(process.cwd(), options.scripts),
ourDir: findDir(path.resolve(__dirname, '../..'), options.builtIn)
}
}

function findDir (base, custom) {
if (custom) {
return custom
} else if (process.platform === 'win32' && fs.existsSync(path.resolve(base, 'scripts-win'))) {
return path.resolve(base, 'scripts-win')
} else {
return path.resolve(base, 'scripts')
}
}
26 changes: 26 additions & 0 deletions lib/resolve-script/script-dirs.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
var path = require('path')

var subject = require('./script-dirs')

module.exports = {
optionSet: function () {
assert.equal(subject({scripts: 'A'}).userDir, 'A')
assert.equal(subject({builtIn: 'B'}).ourDir, 'B')
},
noOptionSet: function () {
var dirName = process.platform === 'win32' ? 'scripts-win' : 'scripts'

var result = subject({})

assert.equal(result.userDir, path.resolve(process.cwd(), dirName))
assert.equal(result.ourDir, path.resolve(__dirname, '../..', dirName))
},
onWindowsAndNoScriptsWinAssumesScripts: function () {
if (process.platform !== 'win32') return
td.when(td.replace(process, 'cwd')()).thenReturn(__dirname)

var result = subject({})

assert.equal(result.userDir, path.resolve(process.cwd(), 'scripts'))
}
}
1 change: 1 addition & 0 deletions scripts-win/noop.cmd
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

5 changes: 5 additions & 0 deletions test/fixtures/built-in-scripts-win/hello/world.cmd
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
@ECHO OFF

SET WORLD=World

ECHO Hello, %WORLD%!
Empty file.
Empty file.
Empty file.
3 changes: 3 additions & 0 deletions test/fixtures/user-scripts-win/fail.cmd
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
@ECHO OFF

type C:\silly\nonsense
Empty file.
3 changes: 3 additions & 0 deletions test/fixtures/user-scripts-win/parent/a.cmd
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
@ECHO OFF

ECHO AAA
3 changes: 3 additions & 0 deletions test/fixtures/user-scripts-win/parent/b.cmd
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
@ECHO OFF

ECHO BBB
3 changes: 3 additions & 0 deletions test/fixtures/user-scripts-win/parent/c.cmd
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
@ECHO OFF

ECHO CCC
3 changes: 3 additions & 0 deletions test/fixtures/user-scripts-win/top/index.cmd
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
@ECHO OFF

ECHO rubby
13 changes: 11 additions & 2 deletions test/run-scripty.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,22 @@ var path = require('path')
var scripty = require('../index')
var grabStdio = require('./grab-stdio')

var builtInPath, userPath
if (process.platform === 'win32') {
builtInPath = path.resolve('test/fixtures/built-in-scripts-win')
userPath = path.resolve('test/fixtures/user-scripts-win')
} else {
builtInPath = path.resolve('test/fixtures/built-in-scripts')
userPath = path.resolve('test/fixtures/user-scripts')
}

module.exports = function (name, opts, cb) {
var stdio = {}

scripty(name, _.extend({}, {
resolve: {
builtIn: path.resolve('test/fixtures/built-in-scripts'),
scripts: path.resolve('test/fixtures/user-scripts')
builtIn: builtInPath,
scripts: userPath
},
spawn: {
tap: grabStdio(stdio)
Expand Down
14 changes: 10 additions & 4 deletions test/safe/basic-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@ module.exports = {
outputAndRunScript: function (done) {
runScripty('hello:world', {}, function (er, code, stdio) {
assert.equal(0, code)
assert.includes(log.read(), '> echo "Hello, $WORLD!"')
if (process.platform === 'win32') {
assert.includes(log.read(), '> ECHO Hello, %WORLD%!')
} else {
assert.includes(log.read(), '> echo "Hello, $WORLD!')
}
assert.includes(stdio.stdout, 'Hello, World!')

done(er)
Expand All @@ -28,9 +32,11 @@ module.exports = {
'Error: scripty - script "fail" failed by exiting ' +
'with a non-zero code (' + code + ').'
)
assert.includes(stdio.stderr,
'cat: /silly/nonsense: No such file or directory'
)
if (process.platform === 'win32') {
assert.includes(stdio.stderr, 'The system cannot find the path specified.')
} else {
assert.includes(stdio.stderr, 'cat: /silly/nonsense: No such file or directory')
}
done(null)
})
}
Expand Down