-
Notifications
You must be signed in to change notification settings - Fork 58
/
Gruntfile.js
158 lines (153 loc) · 5.24 KB
/
Gruntfile.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
module.exports = function (grunt) {
var fs = require('fs');
var envPath = './.env.js';
if (fs.existsSync(envPath)) {
console.log("Adding environment variables");
require(envPath);
}
var browsers = require('./test/browsers');
var reporter = require('saucelabs-mocha-reporter');
var gruntConfig = {
pkg: grunt.file.readJSON('package.json'),
jshint: {
files: ['Gruntfile.js', 'src/*.js', 'test/**.js'],
options: {
laxbreak: true
}
},
watch: {
scripts: {
files: ['<%= jshint.files %>'],
tasks: ['jshint', 'mocha_phantomjs']
}
},
connect: {
server: {
options: {
port: 9000,
hostname: '*',
base: '.'
}
}
},
mocha_phantomjs: {
test: {
options: {
urls: [
'http://localhost:9000/test/mocha_test.html'
]
}
}
},
'saucelabs-mocha': {
// Settings programatically added below
}
};
var addTag = function(envVar) {
var ev = process.env[envVar];
if (ev && ev.length > 0) {
if (ev === 'false' || ev === 'true') {
if (ev === 'true') {
tags.push(envVar);
}
} else {
tags.push(ev);
}
}
};
for (var browser in browsers) {
var taskBrowsers = browsers[browser];
var tags = [];
['TRAVIS', 'TRAVIS_PULL_REQUEST', 'TRAVIS_BRANCH'].forEach(addTag);
gruntConfig['saucelabs-mocha'][browser] = {
options: {
urls: ["http://127.0.0.1:9000/test/mocha_test.html"],
tunnelTimeout: 5,
build: process.env.TRAVIS_JOB_ID,
tags: tags,
concurrency: 3,
browsers: taskBrowsers,
testname: browser + ' mocha tests',
'pollInterval': 1000,
'max-duration': 60
}
};
}
grunt.initConfig(gruntConfig);
grunt.loadNpmTasks('grunt-contrib-connect');
grunt.loadNpmTasks('grunt-contrib-jshint');
grunt.loadNpmTasks('grunt-contrib-watch');
grunt.loadNpmTasks('grunt-mocha-phantomjs');
grunt.loadNpmTasks('grunt-saucelabs');
grunt.registerTask('default', ['connect', 'watch']);
var SauceTunnel = require('sauce-tunnel');
var tunnel = new SauceTunnel(
process.env.SAUCE_USERNAME,
process.env.SAUCE_ACCESS_KEY);
grunt.registerTask('open-tunnel', function() {
var done = this.async();
tunnel.start(function(status) {
if (status) {
console.log("Tunnel started successfully");
} else {
console.log("Failed to start tunnel successfully");
process.exit(1);
}
done();
});
});
function closeTunnel(onExit) {
console.log("Closing tunnel");
tunnel.stop(function () {
console.log("Tunnel closed");
onExit();
});
}
grunt.registerTask('close-tunnel', function() {
var done = this.async();
closeTunnel(done);
});
grunt.registerTask('wait-tunnel', function() {
var done = this.async();
['SIGINT', 'SIGHUP', 'SIGQUIT', 'SIGABRT', 'SIGTERM'].forEach(function(signal) {
process.on(signal, function() {
done();
});
});
});
grunt.registerTask('tunnel', function() {
grunt.task.run('open-tunnel', 'connect', 'wait-tunnel', 'close-tunnel');
});
grunt.registerTask('test', 'Run tests', function (type, testType, multiple) {
if(!type) {
grunt.task.run(['connect', 'mocha_phantomjs']);
} else if (browsers[type]) {
var useTests = JSON.parse(testType || null);
gruntConfig['saucelabs-mocha'][type].options.onTestComplete = reporter.create(useTests);
if (type === 'ci') {
gruntConfig['saucelabs-mocha'][type].options.statusCheckAttempts = 600;
gruntConfig['saucelabs-mocha'][type].options['max-duration'] = 600;
grunt.task.run([
'connect',
'mocha_phantomjs',
'saucelabs-mocha:' + type
]);
} else {
multiple = parseInt(multiple);
if (multiple > 0) {
var browserList = [];
var configOptions = gruntConfig['saucelabs-mocha'][type].options;
for(var i = 0; i < multiple; i++) {
browserList.push.apply(browserList, configOptions.browsers);
}
configOptions.browsers = browserList;
}
grunt.task.run(['connect', 'saucelabs-mocha:' + type]);
}
} else {
// Error!
console.log("Couldn't find: " + type + " in browser config. Running phantomjs instead");
grunt.task.run(['connect', 'mocha_phantomjs']);
}
});
};