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

Commit 1c02530

Browse files
Fail if no files, extend tests
1 parent 6c29370 commit 1c02530

8 files changed

Lines changed: 95 additions & 14 deletions

tasks/javascript_obfuscator.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,13 @@ module.exports = function(grunt) {
2727

2828
// Merge task-specific and/or target-specific options with these defaults.
2929
var options = this.options({});
30+
var files = this.files;
3031

31-
this.files.forEach(function(f) {
32+
if (files.length === 0) {
33+
throw new Error('Target files not found.');
34+
}
35+
36+
files.forEach(function(f) {
3237
var src;
3338

3439
try {
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_files1: {
4+
files: {
5+
'../../tmp/multiple_files1.js': ['first_sample.js', 'second_sample.js']
6+
}
7+
}
8+
});
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
module.exports = require('./gruntfile_generic').setup({
2+
options: {},
3+
multiple_files2: {
4+
src: ['first_sample.js', 'second_sample.js'],
5+
dest: '../../tmp/multiple_files2.js'
6+
}
7+
});

test/fixtures/gruntfile_no_file.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
module.exports = require('./gruntfile_generic').setup({
2+
options: {},
3+
no_file: {
4+
}
5+
});
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
module.exports = require('./gruntfile_generic').setup({
22
options: {},
33
overwrite: {
4-
src: ['../../tmp/overwrite.js'],
5-
dest: '../../tmp/overwrite.js'
4+
src: '../../tmp/overwrite1.js',
5+
dest: '../../tmp/overwrite1.js'
66
}
77
});
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
module.exports = require('./gruntfile_generic').setup({
22
options: {},
3-
multiple_files: {
3+
overwrite: {
44
files: {
5-
'../../tmp/multiple_files': ['first_sample.js', 'second_sample.js']
5+
'../../tmp/overwrite2.js': '../../tmp/overwrite2.js'
66
}
77
}
88
});

test/fixtures/gruntfile_single_file.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ module.exports = require('./gruntfile_generic').setup({
22
options: {},
33
single_file: {
44
files: {
5-
'../../tmp/single_file': ['first_sample.js']
5+
'../../tmp/single_file.js': 'first_sample.js'
66
}
77
}
88
});

test/javascript_obfuscator_test.js

Lines changed: 64 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,34 +19,67 @@ exports.javascript_obfuscator = {
1919
});
2020
},
2121

22+
no_file: function(test) {
23+
test.expect(2);
24+
25+
helper.callGruntfile('fixtures/gruntfile_no_file.js', function (error, stdout, stderr) {
26+
test.ok(error, "Command should fail.");
27+
test.ok(stdout.indexOf("Target files not found.") > -1, "Standard error stream should be contain error message.");
28+
29+
test.done();
30+
});
31+
},
32+
2233
single_file: function(test) {
2334
test.expect(5);
2435

2536
helper.callGruntfile('fixtures/gruntfile_single_file.js', function (error, stdout, stderr) {
2637
test.equal(error, null, "Command should not fail.");
2738
test.equal(stderr, '', "Standard error stream should be empty.");
2839

29-
var obfuscated = grunt.file.read('tmp/single_file');
40+
var obfuscated = grunt.file.read('tmp/single_file.js');
41+
42+
// should NOT be obfuscated
43+
test.ok(obfuscated.indexOf('console') > -1, '`console` shouldn\'t be obfuscated');
44+
test.ok(obfuscated.indexOf('FIRST_LOCAL_VARIABLE') > -1, '`FIRST_LOCAL_VARIABLE` shouldn\'t be obfuscated');
45+
46+
// should be obfuscated
47+
test.ok(obfuscated.indexOf('FIRST_STRING_LITERAL') === -1, '`FIRST_STRING_LITERAL` should be obfuscated');
48+
49+
test.done();
50+
});
51+
},
52+
53+
multiple_files1: function(test) {
54+
test.expect(7);
55+
56+
helper.callGruntfile('fixtures/gruntfile_multiple_files1.js', function (error, stdout, stderr) {
57+
test.equal(error, null, "Command should not fail.");
58+
test.equal(stderr, '', "Standard error stream should be empty.");
59+
60+
var obfuscated = grunt.file.read('tmp/multiple_files1.js');
3061

3162
// should NOT be obfuscated
3263
test.ok(obfuscated.indexOf('console') > -1, '`console` shouldn\'t be obfuscated');
3364
test.ok(obfuscated.indexOf('FIRST_LOCAL_VARIABLE') > -1, '`FIRST_LOCAL_VARIABLE` shouldn\'t be obfuscated');
65+
test.ok(obfuscated.indexOf('SECOND_LOCAL_VARIABLE') > -1, '`SECOND_LOCAL_VARIABLE` shouldn\'t be obfuscated');
3466

3567
// should be obfuscated
3668
test.ok(obfuscated.indexOf('FIRST_STRING_LITERAL') === -1, '`FIRST_STRING_LITERAL` should be obfuscated');
69+
test.ok(obfuscated.indexOf('SECOND_STRING_LITERAL') === -1, '`SECOND_STRING_LITERAL` should be obfuscated');
3770

3871
test.done();
3972
});
4073
},
4174

42-
multiple_files: function(test) {
75+
multiple_files2: function(test) {
4376
test.expect(7);
4477

45-
helper.callGruntfile('fixtures/gruntfile_multiple_files.js', function (error, stdout, stderr) {
78+
helper.callGruntfile('fixtures/gruntfile_multiple_files2.js', function (error, stdout, stderr) {
4679
test.equal(error, null, "Command should not fail.");
4780
test.equal(stderr, '', "Standard error stream should be empty.");
4881

49-
var obfuscated = grunt.file.read('tmp/multiple_files');
82+
var obfuscated = grunt.file.read('tmp/multiple_files2.js');
5083

5184
// should NOT be obfuscated
5285
test.ok(obfuscated.indexOf('console') > -1, '`console` shouldn\'t be obfuscated');
@@ -61,16 +94,39 @@ exports.javascript_obfuscator = {
6194
});
6295
},
6396

64-
overwrite: function(test) {
97+
98+
overwrite1: function(test) {
99+
test.expect(5);
100+
101+
grunt.file.copy('test/fixtures/first_sample.js', 'tmp/overwrite1.js');
102+
103+
helper.callGruntfile('fixtures/gruntfile_overwrite1.js', function (error, stdout, stderr) {
104+
test.equal(error, null, "Command should not fail.");
105+
test.equal(stderr, '', "Standard error stream should be empty.");
106+
107+
var obfuscated = grunt.file.read('tmp/overwrite1.js');
108+
109+
// should NOT be obfuscated
110+
test.ok(obfuscated.indexOf('console') > -1, '`console` shouldn\'t be obfuscated');
111+
test.ok(obfuscated.indexOf('FIRST_LOCAL_VARIABLE') > -1, '`FIRST_LOCAL_VARIABLE` shouldn\'t be obfuscated');
112+
113+
// should be obfuscated
114+
test.ok(obfuscated.indexOf('FIRST_STRING_LITERAL') === -1, '`FIRST_STRING_LITERAL` should be obfuscated');
115+
116+
test.done();
117+
});
118+
},
119+
120+
overwrite2: function(test) {
65121
test.expect(5);
66122

67-
grunt.file.copy('test/fixtures/first_sample.js', 'tmp/overwrite.js');
123+
grunt.file.copy('test/fixtures/first_sample.js', 'tmp/overwrite2.js');
68124

69-
helper.callGruntfile('fixtures/gruntfile_overwrite.js', function (error, stdout, stderr) {
125+
helper.callGruntfile('fixtures/gruntfile_overwrite2.js', function (error, stdout, stderr) {
70126
test.equal(error, null, "Command should not fail.");
71127
test.equal(stderr, '', "Standard error stream should be empty.");
72128

73-
var obfuscated = grunt.file.read('tmp/overwrite.js');
129+
var obfuscated = grunt.file.read('tmp/overwrite2.js');
74130

75131
// should NOT be obfuscated
76132
test.ok(obfuscated.indexOf('console') > -1, '`console` shouldn\'t be obfuscated');

0 commit comments

Comments
 (0)