Skip to content
This repository was archived by the owner on Oct 31, 2022. It is now read-only.

Commit 842236b

Browse files
#2 Add test cases
1 parent 179e382 commit 842236b

11 files changed

Lines changed: 114 additions & 70 deletions

Gruntfile.js

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -28,35 +28,21 @@ module.exports = function(grunt) {
2828
tests: ['tmp']
2929
},
3030

31-
// Configuration to be run (and then tested).
32-
javascript_obfuscator: {
33-
default_options: {
34-
options: {
35-
},
36-
files: {
37-
'tmp/default_options': ['test/fixtures/sample1.js']
38-
}
39-
}
40-
},
41-
4231
// Unit tests.
4332
nodeunit: {
4433
tests: ['test/*_test.js']
4534
}
4635

4736
});
4837

49-
// Actually load this plugin's task(s).
50-
grunt.loadTasks('tasks');
51-
5238
// These plugins provide necessary tasks.
5339
grunt.loadNpmTasks('grunt-contrib-jshint');
5440
grunt.loadNpmTasks('grunt-contrib-clean');
5541
grunt.loadNpmTasks('grunt-contrib-nodeunit');
5642

5743
// Whenever the "test" task is run, first clean the "tmp" dir, then run this
5844
// plugin's task(s), then test the result.
59-
grunt.registerTask('test', ['clean', 'javascript_obfuscator', 'nodeunit']);
45+
grunt.registerTask('test', ['clean', 'nodeunit']);
6046

6147
// By default, lint and run all tests.
6248
grunt.registerTask('default', ['jshint', 'test']);

tasks/javascript_obfuscator.js

Lines changed: 12 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -11,39 +11,33 @@
1111
var JavaScriptObfuscator = require('javascript-obfuscator');
1212

1313
function concat(grunt, paths) {
14+
console.log("paths", paths);
15+
1416
if (paths.length === 0) {
15-
grunt.log.warn('Source files don\'t exist');
16-
return '';
17+
throw new Error('Source files not found.');
1718
}
1819

19-
return paths.filter(function(path) {
20-
// Warn on and remove invalid source files (if nonull was set).
21-
if (!grunt.file.exists(path)) {
22-
grunt.log.warn('Source file "' + path + '" not found.');
23-
return false;
24-
} else {
25-
return true;
26-
}
27-
}).map(function(path) {
20+
return paths.map(function(path) {
21+
2822
// Read file source.
2923
return grunt.file.read(path);
3024
}).join(grunt.util.normalizelf(';'));
3125
}
3226

3327
module.exports = function(grunt) {
34-
35-
// Please see the Grunt documentation for more information regarding task
36-
// creation: http://gruntjs.com/creating-tasks
37-
3828
grunt.registerMultiTask('javascript_obfuscator', 'Obfuscates JavaScript files.', function() {
29+
3930
// Merge task-specific and/or target-specific options with these defaults.
4031
var options = this.options({});
4132

42-
// Iterate over all specified file groups.
4333
this.files.forEach(function(f) {
44-
// Concat specified files.
34+
var src;
4535

46-
var src = concat(grunt, f.src);
36+
try {
37+
src = concat(grunt, f.src);
38+
} catch (e) {
39+
return grunt.fail.warn(e);
40+
}
4741

4842
src = JavaScriptObfuscator.obfuscate(src, options).getObfuscatedCode();
4943

@@ -54,5 +48,4 @@ module.exports = function(grunt) {
5448
grunt.log.writeln('File "' + f.dest + '" created.');
5549
});
5650
});
57-
5851
};

test/fixtures/first_sample.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
var FIRST_LOCAL_VARIABLE = 1;
2+
console.log(FIRST_LOCAL_VARIABLE + 'FIRST_STRING_LITERAL');

test/fixtures/gruntfile_generic.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
exports.setup = function(configuration) {
2+
return function (grunt) {
3+
'use strict';
4+
5+
grunt.initConfig({
6+
javascript_obfuscator: configuration
7+
});
8+
9+
grunt.loadTasks('../../tasks');
10+
grunt.registerTask('default', 'javascript_obfuscator');
11+
};
12+
};
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
module.exports = require('./gruntfile_generic').setup({
2+
options: {},
3+
multiple_files: {
4+
files: {
5+
'../../tmp/multiple_files': ['first_sample.js', 'second_sample.js']
6+
}
7+
}
8+
});
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
module.exports = require('./gruntfile_generic').setup({
2+
options: {},
3+
single_file: {
4+
files: {
5+
'../../tmp/single_file': ['first_sample.js']
6+
}
7+
}
8+
});
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
module.exports = require('./gruntfile_generic').setup({
2+
options: {},
3+
unknown_file: {
4+
files: {
5+
'../../tmp/unknown_file': ['unknown.js']
6+
}
7+
}
8+
});

test/fixtures/sample1.js

Lines changed: 0 additions & 2 deletions
This file was deleted.

test/fixtures/second_sample.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
var SECOND_LOCAL_VARIABLE = 2;
2+
console.log(SECOND_LOCAL_VARIABLE + 'SECOND_STRING_LITERAL');

test/helper.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
var cp = require("child_process");
2+
3+
exports.callGruntfile = function (filename, whenDoneCallback) {
4+
var command, options;
5+
command = "grunt --gruntfile " + filename + " --no-color";
6+
options = {cwd: 'test/'};
7+
cp.exec(command, options, whenDoneCallback);
8+
};

0 commit comments

Comments
 (0)