|
| 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