Skip to content

Commit c326653

Browse files
committed
Merge pull request #3 from duizendnegen/feature/tests
write tests for contentFor, configure & willUpload
2 parents a4bf8cb + d9bf216 commit c326653

File tree

4 files changed

+255
-11
lines changed

4 files changed

+255
-11
lines changed

index.js

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -45,13 +45,6 @@ module.exports = {
4545
}
4646
},
4747
requiredConfig: ['publicUrl', 'sentryUrl', 'sentryOrganizationSlug', 'sentryProjectSlug', 'sentryApiKey', 'revisionKey'],
48-
configure: function(/* context */) {
49-
this.log('validating config');
50-
51-
['distDir', 'filePattern', 'revisionKey', 'enableRevisionTagging', 'didDeployMessage'].forEach(this.applyDefaultConfigProperty.bind(this));
52-
53-
this.log('config ok');
54-
},
5548

5649
willUpload: function(context) {
5750
var isEnabled = this.readConfig('enableRevisionTagging');
@@ -70,7 +63,6 @@ module.exports = {
7063
var index = fs.readFileSync(indexPath, 'utf8');
7164
var index = index.replace('<meta name="sentry:revision">',
7265
'<meta name="sentry:revision" content="'+revisionKey+'">');
73-
7466
fs.writeFileSync(indexPath, index);
7567
},
7668

package.json

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
"scripts": {
1010
"start": "ember server",
1111
"build": "ember build",
12-
"test": "ember try:testall"
12+
"test": "node tests/runner.js"
1313
},
1414
"repository": "dschmidt/ember-cli-deploy-sentry",
1515
"engines": {
@@ -35,7 +35,11 @@
3535
"ember-disable-proxy-controllers": "^1.0.0",
3636
"ember-export-application-global": "^1.0.2",
3737
"ember-try": "0.0.6",
38-
"rsvp": "^3.1.0"
38+
"rsvp": "^3.1.0",
39+
"chai": "^2.2.0",
40+
"chai-as-promised": "^5.0.0",
41+
"mocha": "^2.2.4",
42+
"mock-fs": "^3.3.0"
3943
},
4044
"keywords": [
4145
"ember-addon",
@@ -52,4 +56,4 @@
5256
"ember-addon": {
5357
"configPath": "tests/dummy/config"
5458
}
55-
}
59+
}

tests/runner.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/*jshint globalstrict: true*/
2+
'use strict';
3+
4+
var glob = require('glob');
5+
var Mocha = require('mocha');
6+
7+
var mocha = new Mocha({
8+
reporter: 'spec'
9+
});
10+
11+
var arg = process.argv[2];
12+
var root = 'tests/';
13+
14+
function addFiles(mocha, files) {
15+
glob.sync(root + files).forEach(mocha.addFile.bind(mocha));
16+
}
17+
18+
addFiles(mocha, '/**/*-nodetest.js');
19+
20+
if (arg === 'all') {
21+
addFiles(mocha, '/**/*-nodetest-slow.js');
22+
}
23+
24+
mocha.run(function(failures) {
25+
process.on('exit', function() {
26+
process.exit(failures);
27+
});
28+
});

tests/unit/index-nodetest.js

Lines changed: 220 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,220 @@
1+
/*jshint globalstrict: true*/
2+
'use strict';
3+
4+
var RSVP = require('ember-cli/lib/ext/promise');
5+
6+
var assert = require('ember-cli/tests/helpers/assert');
7+
8+
var mockFs = require('mock-fs');
9+
var fs = require('fs');
10+
11+
describe('deploySentry plugin', function() {
12+
var subject, mockUi, context;
13+
14+
before(function() {
15+
subject = require('../../index');
16+
});
17+
18+
beforeEach(function() {
19+
mockUi = {
20+
messages: [],
21+
write: function() { },
22+
writeLine: function(message) {
23+
this.messages.push(message);
24+
}
25+
};
26+
27+
context = {
28+
distFiles: ['app.css', 'app.js'],
29+
ui: mockUi,
30+
config: {
31+
deploySentry: {
32+
publicUrl: 'http://example.org',
33+
sentryUrl: 'http://example.org',
34+
sentryOrganizationSlug: 'slug',
35+
sentryProjectSlug: 'slug',
36+
sentryApiKey: 'api-key',
37+
revisionKey: 'abcdef'
38+
}
39+
}
40+
};
41+
});
42+
43+
it('has a name', function() {
44+
var plugin = subject.createDeployPlugin({
45+
name: 'test-plugin'
46+
});
47+
48+
assert.equal(plugin.name, 'test-plugin');
49+
});
50+
51+
it('implements the correct deployment hooks', function() {
52+
var plugin = subject.createDeployPlugin({
53+
name: 'test-plugin'
54+
});
55+
56+
assert.equal(typeof plugin.configure, 'function');
57+
assert.equal(typeof plugin.willUpload, 'function');
58+
assert.equal(typeof plugin.upload, 'function');
59+
assert.equal(typeof plugin.didDeploy, 'function');
60+
});
61+
62+
describe('configure hook', function() {
63+
it('does not throw if config is ok', function() {
64+
var plugin = subject.createDeployPlugin({
65+
name: 'deploySentry'
66+
});
67+
plugin.beforeHook(context);
68+
plugin.configure(context);
69+
assert.ok(true); // it didn't throw
70+
});
71+
72+
it('throws if config is not valid', function() {
73+
var plugin = subject.createDeployPlugin({
74+
name: 'deploySentry'
75+
});
76+
77+
context.config = { deploySentry: {} };
78+
79+
plugin.beforeHook(context);
80+
assert.throws(function(){
81+
plugin.configure(context);
82+
});
83+
});
84+
85+
describe('without providing config', function () {
86+
var plugin;
87+
88+
beforeEach(function() {
89+
plugin = subject.createDeployPlugin({
90+
name: 'deploySentry'
91+
});
92+
});
93+
94+
it('warns about missing required config', function() {
95+
context.config = { deploySentry: {} };
96+
97+
plugin.beforeHook(context);
98+
assert.throws(function(error){
99+
plugin.configure(context);
100+
});
101+
var messages = mockUi.messages.reduce(function(previous, current) {
102+
if (/- Missing required config:\s.*/.test(current)) {
103+
previous.push(current);
104+
}
105+
106+
return previous;
107+
}, []);
108+
109+
assert.equal(messages.length, 1); // doesn't log all failures, just first one
110+
});
111+
112+
it('warns about missing optional config', function() {
113+
plugin.beforeHook(context);
114+
plugin.configure(context);
115+
var messages = mockUi.messages.reduce(function(previous, current) {
116+
if (/- Missing config:\s.*, using default:\s/.test(current)) {
117+
previous.push(current);
118+
}
119+
120+
return previous;
121+
}, []);
122+
123+
assert.equal(messages.length, 4);
124+
});
125+
126+
it('adds default config to the config object', function() {
127+
plugin.beforeHook(context);
128+
plugin.configure(context);
129+
assert.isDefined(context.config.deploySentry.distDir);
130+
assert.isDefined(context.config.deploySentry.filePattern);
131+
assert.isDefined(context.config.deploySentry.enableRevisionTagging);
132+
assert.isDefined(context.config.deploySentry.didDeployMessage);
133+
});
134+
});
135+
136+
describe('with optional config supplied', function () {
137+
var plugin;
138+
139+
beforeEach(function() {
140+
plugin = subject.createDeployPlugin({
141+
name: 'deploySentry'
142+
});
143+
context.config.deploySentry["distDir"] = "dist/dir";
144+
context.config.deploySentry["filePattern"] = "/**/*.{js,map}";
145+
context.config.deploySentry["enableRevisionTagging"] = false;
146+
context.config.deploySentry["didDeployMessage"] = "ok";
147+
});
148+
149+
it('does not warn about missing optional config', function() {
150+
plugin.beforeHook(context);
151+
plugin.configure(context);
152+
var messages = mockUi.messages.reduce(function(previous, current) {
153+
if (/- Missing config:\s.*, using default:\s/.test(current)) {
154+
previous.push(current);
155+
}
156+
157+
return previous;
158+
}, []);
159+
assert.equal(messages.length, 0);
160+
});
161+
});
162+
});
163+
164+
describe('contentFor hook', function() {
165+
it('is defined', function() {
166+
assert.equal(typeof subject.contentFor, 'function');
167+
});
168+
it('returns content for head-footer', function() {
169+
assert.equal(subject.contentFor('head-footer'), '<meta name="sentry:revision"></meta>');
170+
});
171+
it('does not return content for other types', function() {
172+
assert.notEqual(subject.contentFor('head-barter'), '<meta name="sentry:revision"></meta>');
173+
})
174+
});
175+
176+
describe('willUpload hook', function() {
177+
var plugin, fileSystem, indexFile;
178+
beforeEach(function() {
179+
plugin = subject.createDeployPlugin({
180+
name: 'deploySentry'
181+
});
182+
indexFile = mockFs.file({
183+
content: '<html><head><meta name="sentry:revision"></meta></head><body></body></html>'
184+
});
185+
fileSystem = {
186+
'/path/to/fake/dir': {
187+
'index.html': indexFile
188+
}
189+
};
190+
mockFs(fileSystem);
191+
context.distDir = '/path/to/fake/dir';
192+
context.revisionKey = 'abc123';
193+
});
194+
afterEach(function() {
195+
mockFs.restore();
196+
});
197+
198+
it('does not fill in revision data when disabled', function() {
199+
context.config.deploySentry.enableRevisionTagging = false;
200+
201+
plugin.beforeHook(context);
202+
plugin.configure(context);
203+
plugin.willUpload(context);
204+
var result = fs.readFileSync('/path/to/fake/dir/index.html', 'utf8');
205+
assert.notEqual(result.indexOf('<meta name="sentry:revision">'), -1);
206+
});
207+
208+
it('fills in revision data in the meta-tag', function() {
209+
plugin.beforeHook(context);
210+
plugin.configure(context);
211+
plugin.willUpload(context);
212+
var result = fs.readFileSync('/path/to/fake/dir/index.html', 'utf8');
213+
assert.notEqual(result.indexOf('<meta name="sentry:revision" content="'+context.config.deploySentry.revisionKey+'">'), -1);
214+
});
215+
});
216+
217+
describe('upload hook', function() {
218+
// possibly mock Sentry out here
219+
});
220+
});

0 commit comments

Comments
 (0)