Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

Commit a092380

Browse files
committed
WIP: implement e2e test harness
This is a work in progress, still trying to make the server do everything it should do.
1 parent 17f186d commit a092380

File tree

9 files changed

+157
-0
lines changed

9 files changed

+157
-0
lines changed

Gruntfile.js

+3
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ var files = require('./angularFiles').files;
44
var util = require('./lib/grunt/utils.js');
55
var versionInfo = require('./lib/versions/version-info');
66
var path = require('path');
7+
var e2e = require('./test/e2e/tools');
78

89
module.exports = function(grunt) {
910
//grunt plugins
@@ -50,6 +51,7 @@ module.exports = function(grunt) {
5051
return [
5152
util.conditionalCsp(),
5253
util.rewrite(),
54+
e2e.middleware(),
5355
connect.favicon('images/favicon.ico'),
5456
connect.static(base),
5557
connect.directory(base)
@@ -75,6 +77,7 @@ module.exports = function(grunt) {
7577

7678
next();
7779
},
80+
e2e.middleware(),
7881
util.conditionalCsp(),
7982
connect.favicon('images/favicon.ico'),
8083
connect.static(base)

test/e2e/templates/test.html

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<!DOCTYPE html>
2+
{% if eq(test.ngAppTag, "html") %}
3+
<html ng-app="{{ test.ngApp }}">
4+
{% else %}
5+
<html>
6+
{% endif %}
7+
<head>
8+
{% if scripts.jQuery %}
9+
<script src="{{ scripts.jQuery }}"></script>
10+
{% endif %}
11+
{% for script in test.scripts %}
12+
<script src="{{ test.scripts[script] }}"></script>
13+
{% endfor %}
14+
{{ test.head }}
15+
</head>
16+
{% if test.ngAppTag === "body" %}
17+
<body ng-app="{{ test.ngApp }}">
18+
{% else %}
19+
<body>
20+
{% endif %}
21+
{% test.body %}
22+
</body>
23+
</html>

test/e2e/tests/sample/index.html

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<!DOCTYPE html>
2+
<html ng-app="test">
3+
<p>Hello!</p>
4+
<script src="angular.js"></script>
5+
</html>

test/e2e/tests/sample/sample.json

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"good": true
3+
}

test/e2e/tools/.jshintrc

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"node": true
3+
}

test/e2e/tools/fixture.js

+68
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
'use strict';
2+
3+
var fs = require('fs');
4+
var path = require('path');
5+
var $ = require('cheerio');
6+
7+
var root = path.resolve(__dirname, '..');
8+
var tests = path.resolve(root, 'tests');
9+
10+
var projectRoot = path.resolve(__dirname, '../../..');
11+
var build = path.resolve(projectRoot, 'build');
12+
13+
function rewriteAngularSrc(src, query) {
14+
if (query) {
15+
if (query.build) {
16+
return query.build + '/' + src;
17+
} else if (query.cdn) {
18+
return '//ajax.googleapis.com/ajax/libs/angularjs/' + query.cdn + '/' + src;
19+
}
20+
}
21+
return '/build/' + src;
22+
}
23+
24+
function generateFixture(test, query) {
25+
var indexFile = path.resolve(tests, test, 'index.html');
26+
var text = fs.readFileSync(indexFile, 'utf8');
27+
28+
var $$ = $.load(text);
29+
30+
var firstScript = null;
31+
var jquery = null;
32+
var angular = null;
33+
$$('script').each(function(i, script) {
34+
var src = $(script).attr('src');
35+
if (src === 'jquery.js' && jquery === null) jquery = script;
36+
else if (src === 'angular.js' && angular === null) angular = script;
37+
if (firstScript === null) firstScript = script;
38+
if (src) {
39+
if (fs.statSync(path.resolve(build, src))) {
40+
$(script).attr('src', rewriteAngularSrc(src, query));
41+
}
42+
}
43+
});
44+
45+
if (jquery && (!('jquery' in query) || (/^(0|no|false|off|n)$/i).test(query.jquery))) {
46+
$(jquery).remove();
47+
} else if (query.jquery) {
48+
if (!jquery) {
49+
jquery = $.load('<script></script>')[0];
50+
if (firstScript) {
51+
$(firstScript).prepend(jquery);
52+
} else {
53+
$($$).first().before(jquery);
54+
}
55+
}
56+
if (!/^\d+\.\d+.*$/.test(query.jquery)) {
57+
$(jquery).attr('src', '/bower_components/jquery/dist/jquery.js');
58+
} else {
59+
$(jquery).attr('src', '//ajax.googleapis.com/ajax/libs/jquery/' + query.jquery + '/jquery.js');
60+
}
61+
}
62+
63+
return $$.html();
64+
}
65+
66+
module.exports = {
67+
generate: generateFixture
68+
};

test/e2e/tools/index.js

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
'use strict';
2+
3+
module.exports = {
4+
middleware: require('./middleware')
5+
};

test/e2e/tools/middleware.js

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
'use strict';
2+
3+
var url = require('url');
4+
var util = require('./util');
5+
var fixture = require('./fixture');
6+
7+
module.exports = middlewareFactory;
8+
9+
function middlewareFactory(base) {
10+
base = base || '/e2e';
11+
while (base.length && base[base.length-1] === '/') base = base.slice(0, base.length-1);
12+
var fixture_regexp = new RegExp('^' + base + '/tests/([a-zA-Z0-9_-]+)(/(index.html)?)?$');
13+
14+
return function(req, res, next) {
15+
var match;
16+
if ((match = fixture_regexp.exec(req.url))) {
17+
if (util.testExists(match[1])) {
18+
try {
19+
var query = url.parse(req.url, true).query;
20+
res.write(fixture.generate(match[1], query));
21+
res.end();
22+
} catch (e) {
23+
return next(e);
24+
}
25+
} else {
26+
return next(new Error('Test ' + match[1] + ' not found.'));
27+
}
28+
} else {
29+
return next();
30+
}
31+
};
32+
}

test/e2e/tools/util.js

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
'use strict';
2+
3+
var fs = require('fs');
4+
var path = require('path');
5+
var url = require('url');
6+
7+
var root = path.resolve(__dirname, '..');
8+
var tests = path.resolve(root, 'tests');
9+
10+
module.exports = {
11+
testExists: function(testname) {
12+
testname = path.resolve(tests, testname);
13+
return fs.statSync(testname).isDirectory();
14+
}
15+
};

0 commit comments

Comments
 (0)