Skip to content
This repository has been archived by the owner on Jul 29, 2022. It is now read-only.

Commit

Permalink
Fully support single symbolic chmods (e.g. u+rwx)
Browse files Browse the repository at this point in the history
Closes #4.
  • Loading branch information
rezonant authored and JamesMGreene committed Aug 27, 2015
1 parent 54fac76 commit 30ffc86
Show file tree
Hide file tree
Showing 7 changed files with 148 additions and 14 deletions.
58 changes: 50 additions & 8 deletions Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

'use strict';

var fs = require('fs');

module.exports = function(grunt) {

// Project configuration.
Expand Down Expand Up @@ -74,7 +76,33 @@ module.exports = function(grunt) {
mode: '444'
},
src: ['tmp/custom_options_dir/']
}
},
custom_options_file_symbolic_1: {
options: {
mode: 'u+r' // 400
},
src: ['tmp/custom_options_file_symbolic_1.js']
},
custom_options_file_symbolic_2: {
options: {
mode: 'u+rw' // 600
},
src: ['tmp/custom_options_file_symbolic_2.js']
},
custom_options_file_symbolic_3: {
options: {
mode: 'u+rwx' // 700
},
src: ['tmp/custom_options_file_symbolic_3.js']
},

custom_options_file_symbolic_4: {
options: {
mode: 'uo+r' // 404
},
src: ['tmp/custom_options_file_symbolic_4.js']
},

},

// Unit tests.
Expand All @@ -95,21 +123,31 @@ module.exports = function(grunt) {
var forceValue = (function(val) {
return (typeof val === 'boolean' ? val : !!val);
})(grunt.option('force'));

// Enable force for tests to avoid the build halting on warnings
grunt.registerTask('force-enable', function() {
grunt.option('force', true);
});
grunt.registerTask('force-revert', function() {
grunt.option('force', forceValue);
});

// Test setup
grunt.registerTask('test-setup', function() {
grunt.file.mkdir('tmp/custom_options_dir');
grunt.file.write('tmp/custom_options_file.js', '');

grunt.file.write('tmp/custom_options_file_symbolic_1.js', '');
grunt.file.write('tmp/custom_options_file_symbolic_2.js', '');
grunt.file.write('tmp/custom_options_file_symbolic_3.js', '');
grunt.file.write('tmp/custom_options_file_symbolic_4.js', '');

fs.chmodSync('tmp/custom_options_file_symbolic_1.js', '000');
fs.chmodSync('tmp/custom_options_file_symbolic_2.js', '000');
fs.chmodSync('tmp/custom_options_file_symbolic_3.js', '000');
fs.chmodSync('tmp/custom_options_file_symbolic_4.js', '000');
});

// Test emission listeners
var args = (function() {
var slicer = Array.prototype.slice;
Expand All @@ -134,14 +172,14 @@ module.exports = function(grunt) {
});
grunt.registerTask('listeners-off', function() {
grunt.file.write('tmp/' + taskTargetName + '.json', JSON.stringify(emissions, null, 2));

emissions.length = 0;
grunt.event.removeAllListeners('chmod.*');
});

var badConfigOptions = [
'default_options',
'custom_options_without_string_mode',
'custom_options_without_string_mode',
'custom_options_with_empty_mode',
'custom_options_with_invalid_mode',
'custom_options_nonexistent_file'
Expand All @@ -160,7 +198,11 @@ module.exports = function(grunt) {
});
var goodConfigOptions = [
'custom_options_file',
'custom_options_dir'
'custom_options_dir',
'custom_options_file_symbolic_1',
'custom_options_file_symbolic_2',
'custom_options_file_symbolic_3',
'custom_options_file_symbolic_4'
];
goodConfigOptions.forEach(function(e, i) {
grunt.registerTask(
Expand Down
9 changes: 5 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,12 @@ grunt.initConfig({
Type: `String`
Default value: _none_ (required)

A string value to specify the permissions' [numeric mode](http://ss64.com/bash/chmod.html) to set on the files and/or directories, e.g.:
A string value to specify the permissions' [`chmod`-style numeric or symbolic mode](http://ss64.com/bash/chmod.html) to set on the files and/or directories, e.g.:
- `'755'`
- `'644'`
- `'400'`

- `'a+X'`
- `'ug+rw'`

### Usage Examples

Expand Down Expand Up @@ -100,7 +101,7 @@ In lieu of a formal styleguide, take care to maintain the existing coding style.
## Release History
- 1.0.3: Published to NPM on 2013-02-20.
- Initial release, plus fixed tests and README.

## License
Copyright (c) 2013 James M. Greene
Copyright (c) 2013 James M. Greene
Licensed under the MIT license.
71 changes: 69 additions & 2 deletions test/chmod_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,73 @@ exports.chmod = {
test.strictEqual(actualPerms, expectedPerms, 'Permissions should match.');

test.done();
}
},

custom_options_file_symbolic_1: function(test) {
test.expect(2);

var actual = grunt.file.readJSON('tmp/custom_options_file_symbolic_1.json');
var expected = grunt.file.readJSON('test/expected/custom_options_file_symbolic_1.json');
test.deepEqual(actual, expected, 'Task should pass and modify the file permissions of all files that match.');

var rawMode = require('fs').statSync('tmp/custom_options_file_symbolic_1.js').mode;
var numericMode = helpers.permsFromMode(rawMode);

var actualPerms = '' + numericMode;
var expectedPerms = '400';
test.strictEqual(actualPerms, expectedPerms, 'Permissions should match.');

test.done();
},

custom_options_file_symbolic_2: function(test) {
test.expect(2);

var actual = grunt.file.readJSON('tmp/custom_options_file_symbolic_2.json');
var expected = grunt.file.readJSON('test/expected/custom_options_file_symbolic_2.json');
test.deepEqual(actual, expected, 'Task should pass and modify the file permissions of all files that match.');

var rawMode = require('fs').statSync('tmp/custom_options_file_symbolic_2.js').mode;
var numericMode = helpers.permsFromMode(rawMode);

var actualPerms = '' + numericMode;
var expectedPerms = '600';
test.strictEqual(actualPerms, expectedPerms, 'Permissions should match.');

};
test.done();
},

custom_options_file_symbolic_3: function(test) {
test.expect(2);

var actual = grunt.file.readJSON('tmp/custom_options_file_symbolic_3.json');
var expected = grunt.file.readJSON('test/expected/custom_options_file_symbolic_3.json');
test.deepEqual(actual, expected, 'Task should pass and modify the file permissions of all files that match.');

var rawMode = require('fs').statSync('tmp/custom_options_file_symbolic_3.js').mode;
var numericMode = helpers.permsFromMode(rawMode);

var actualPerms = '' + numericMode;
var expectedPerms = '700';
test.strictEqual(actualPerms, expectedPerms, 'Permissions should match.');

test.done();
},

custom_options_file_symbolic_4: function(test) {
test.expect(2);

var actual = grunt.file.readJSON('tmp/custom_options_file_symbolic_4.json');
var expected = grunt.file.readJSON('test/expected/custom_options_file_symbolic_4.json');
test.deepEqual(actual, expected, 'Task should pass and modify the file permissions of all files that match.');

var rawMode = require('fs').statSync('tmp/custom_options_file_symbolic_4.js').mode;
var numericMode = helpers.permsFromMode(rawMode);

var actualPerms = '' + numericMode;
var expectedPerms = '404';
test.strictEqual(actualPerms, expectedPerms, 'Permissions should match.');

test.done();
}
};
6 changes: 6 additions & 0 deletions test/expected/custom_options_file_symbolic_1.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[
{
"name": "chmod.success",
"args": []
}
]
6 changes: 6 additions & 0 deletions test/expected/custom_options_file_symbolic_2.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[
{
"name": "chmod.success",
"args": []
}
]
6 changes: 6 additions & 0 deletions test/expected/custom_options_file_symbolic_3.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[
{
"name": "chmod.success",
"args": []
}
]
6 changes: 6 additions & 0 deletions test/expected/custom_options_file_symbolic_4.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[
{
"name": "chmod.success",
"args": []
}
]

0 comments on commit 30ffc86

Please sign in to comment.