Fixed the test, which required a bit of dependency mapping. Fixed '/' redirect. query string's don't need =true to work.

This commit is contained in:
Spencer Alger 2014-03-05 22:08:22 -07:00
parent 2d1fd66b0b
commit a619c0b52b
13 changed files with 67 additions and 81 deletions

View file

@ -4,10 +4,16 @@ define(function (require) {
require('css!./styles/main.css');
require('directives/config');
require('services/courier');
require('services/config');
require('apps/dashboard/directives/grid');
require('apps/dashboard/directives/panel');
var app = require('modules').get('app/dashboard');
var app = require('modules').get('app/dashboard', [
'elasticsearch',
'ngRoute',
'kibana/services'
]);
app.config(function ($routeProvider) {
$routeProvider

View file

@ -9,7 +9,5 @@ define(function (require) {
.constant('kbnVersion', '@REV@')
// Use this for cache busting partials
.constant('cacheBust', 'cache-bust=' + Date.now())
;
.constant('cacheBust', 'cache-bust=' + Date.now());
});

View file

@ -23,8 +23,6 @@ define(function (require) {
// proceed once setup is complete
setup(function (err) {
kibana
// config.js in the root
.value('configFile', configFile)
// setup default routes
.config(function ($routeProvider) {
$routeProvider

View file

@ -1,6 +1,7 @@
define(function (require) {
var _ = require('lodash');
var nextTick = require('utils/next_tick');
var configFile = require('../../config');
require('services/courier');
@ -10,6 +11,7 @@ define(function (require) {
require('modules')
.get('kibana/services')
.constant('configFile', configFile)
.service('config', function ($q, $rootScope, courier, kbnVersion, configFile) {
var watchers = {};
var unwatchers = [];

View file

@ -1,4 +1,6 @@
define(function (require) {
require('elasticsearch');
var es; // share the client amoungst all apps
require('modules')
.get('kibana/services')

View file

@ -1,6 +1,6 @@
module.exports = function (grunt) {
var instrumentationMiddleware = require('../utils/instrumentation');
var amdWrapMiddleware = require('../utils/amd-wrapper');
var amdRapperMiddleware = require('../utils/amd-rapper');
return {
dev: {
@ -33,17 +33,14 @@ module.exports = function (grunt) {
}));
// minimize code duplication (especially in the istanbul reporter)
// by allowing node_modules to be requested in an AMD wrapper
stack.push(amdWrapMiddleware({
// by allowing node_modules to be requested in an AMD rapper
stack.push(amdRapperMiddleware({
root: root
}));
// standard static middleware reading from the root
stack.push(connect.static(root));
// allow browsing directories
stack.push(connect.directory(root));
// redirect requests for '/' to '/src/'
stack.push(function (req, res, next) {
if (req.url !== '/') return next();
@ -52,6 +49,16 @@ module.exports = function (grunt) {
res.end();
});
// allow browsing directories
stack.push(
function (req, res, next) {
// prevent chrome's stupid "this page is in spanish"
res.setHeader('Content-Language', 'en');
next();
},
connect.directory(root)
);
return stack;
}
}

View file

@ -1,11 +1,19 @@
module.exports = function amdWrapMiddleware(opts) {
module.exports = function amdRapMiddleware(opts) {
opts = opts || {};
var root = opts.root || '/';
var path = require('path');
var fs = require('fs');
var random = require('lodash').sample;
var pathPrefix = opts.pathPrefix || '/amd-wrap/';
var rap = [
'yo yo yo',
'check it',
'yeeaahh!',
'grocery bag...'
];
return function (req, res, next) {
// only allow prefixed requests
if (req.url.substring(0, pathPrefix.length) !== pathPrefix) return next();
@ -20,7 +28,11 @@ module.exports = function amdWrapMiddleware(opts) {
// respond with the wrapped code
res.statusCode = 200;
res.setHeader('Content-Type', 'application/javascript');
res.end('define(function (require, exports, module) {\n' + contents + '\n});');
res.end([
'define(function (require, exports, module) { console.log("' + random(rap) + '");',
contents,
'\n});'
].join('\n'));
});
};
};

View file

@ -26,7 +26,7 @@ module.exports = function instrumentationMiddleware(opts) {
var fileMap = {};
function filenameForReq(req) {
if (!~req.url.indexOf('instrument=true')) return false;
if (req._parsedUrl.query && !~req._parsedUrl.query.indexOf('instrument')) return false;
// expected absolute path to the file
var filename = path.join(root, req._parsedUrl.pathname);
@ -51,25 +51,34 @@ module.exports = function instrumentationMiddleware(opts) {
// the file either doesn't exist of it was filtered out by opts.filter
if (!filename) return next();
fs.readFile(filename, 'utf8', function (err, content) {
if (err) {
if (err.code !== 'ENOENT') {
// other issue, report!
return next(err);
}
fs.stat(filename, function (err, stat) {
if (err && err.code !== 'ENOENT') return next(err);
if (err || !stat.isFile()) {
// file was deleted, clear cache and move on
delete fileMap[filename];
return next();
}
res.statusCode = 200;
res.setHeader('Content-Type', 'application/javascript');
res.end(i.instrumentSync(
content,
// make file names easier to read
displayRoot ? path.relative(displayRoot, filename) : filename
));
var etag = '"' + stat.size + '-' + Number(stat.mtime) + '"';
if (req.headers['if-none-match'] === etag) {
res.statusCode = 304;
res.end();
return;
}
fs.readFile(filename, 'utf8', function (err, content) {
if (err) return next(err);
res.statusCode = 200;
res.setHeader('Content-Type', 'application/javascript');
res.setHeader('ETag', etag);
res.end(i.instrumentSync(
content,
// make file names easier to read
displayRoot ? path.relative(displayRoot, filename) : filename
));
});
});
};

View file

@ -1,4 +1,4 @@
<!DOCTYPE html><html><head><title>Kibana4 Tests</title><link rel="stylesheet" href="/node_modules/mocha/mocha.css"></head><body><div id="mocha"></div><script src="/node_modules/expect.js/expect.js"></script><script src="/node_modules/mocha/mocha.js"></script><script src="/src/bower_components/requirejs/require.js"></script><script src="/src/kibana/require.config.js"></script><script type="text/javascript">window.COVERAGE = !!(/coverage=true/i.test(location.search));
<!DOCTYPE html><html><head><title>Kibana4 Tests</title><link rel="stylesheet" href="/node_modules/mocha/mocha.css"></head><body><div id="mocha"></div><script src="/node_modules/expect.js/expect.js"></script><script src="/node_modules/mocha/mocha.js"></script><script src="/src/bower_components/requirejs/require.js"></script><script src="/src/kibana/require.config.js"></script><script type="text/javascript">window.COVERAGE = !!(/coverage/i.test(window.location.search));
mocha.setup('bdd');
require.config({
@ -28,7 +28,7 @@ function setupCoverage(done) {
}
function runTests() {
require(["../../test/unit/specs/apps/dashboard/index","../../test/unit/specs/apps/dashboard/mocks/modules","../../test/unit/specs/calculate_indices","../../test/unit/specs/courier","../../test/unit/specs/data_source","../../test/unit/specs/mapper"], function () {
require(["../../test/unit/specs/apps/dashboard/index","../../test/unit/specs/calculate_indices","../../test/unit/specs/courier","../../test/unit/specs/data_source","../../test/unit/specs/mapper"], function () {
window.mochaRunner = mocha.run().on('end', function () {
window.mochaResults = this.stats;
});

View file

@ -10,7 +10,7 @@ html
script(src='/src/bower_components/requirejs/require.js')
script(src='/src/kibana/require.config.js')
script(type="text/javascript").
window.COVERAGE = !!(/coverage=true/i.test(location.search));
window.COVERAGE = !!(/coverage/i.test(window.location.search));
mocha.setup('bdd');
require.config({

View file

@ -1,5 +1,4 @@
define(function (require) {
var angular = require('angular');
var mocks = require('angular-mocks');
var _ = require('lodash');
var $ = require('jquery');
@ -11,17 +10,11 @@ define(function (require) {
// Load the code for the modules
require('apps/dashboard/index');
// Create the kibana module
require('./mocks/modules.js');
describe('Mapper', function () {
var $scope;
beforeEach(function () {
// Start the kibana module
module('kibana');
module('app/dashboard');
// Create the scope
inject(function ($rootScope, $controller) {

View file

@ -1,24 +0,0 @@
define(function (require) {
var angular = require('angular');
angular.module('app/dashboard', []);
// Need this because the controller requires it
angular.module('kibana/directives', []);
// create mock service for courier
var mock = {
getvalue: function () {}
};
angular.module('kibana/services', [])
.service('courier', function () {
return mock;
});
// Could probably get rid of ngRoute if you want to stub it
angular.module('kibana', ['ngRoute', 'kibana/services', 'app/dashboard']);
});

View file

@ -74,9 +74,9 @@ define(function (require) {
toSec(stats, 'create report');
toSec(stats, 'duration');
console.log(JSON.stringify(stats, null, ' '));
linkNav();
show(stats);
if (gotoFile) {
var header = document.getElementById(gotoFile.substring(1));
if (header) {
@ -218,22 +218,5 @@ define(function (require) {
return stats[prop] = (stats[prop] / 1000).toFixed(2) + ' sec';
}
function show(info) {
var width = _(info).keys().sortBy('length').pop().length;
$('<pre>')
.addClass('coverage-stats')
.appendTo('#menu')
.text(
_.map(info, function (val, name) {
var row = val + ' - ' + name;
if (width - name.length) {
row += (new Array(width - name.length + 1)).join(' ');
}
return row;
}).join('\n')
);
}
return IstanbulReporter;
});