Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 61d2143

Browse files
committedOct 15, 2014
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 61d2143

File tree

8 files changed

+146
-0
lines changed

8 files changed

+146
-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/fixture.js

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

‎test/e2e/tools/index.js

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

‎test/e2e/tools/middleware.js

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

‎test/e2e/tools/util.js

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

0 commit comments

Comments
 (0)
This repository has been archived.