UNPKG

@testim/testim-cli

Version:

Command line interface for running Testing on you CI

152 lines (129 loc) 5.07 kB
var Firebase = require('firebase'); var Promise = require('promise'); var firebaseLocation = "https://blinding-heat-7022.firebaseio.com/"; var root = new Firebase(firebaseLocation); function loginToFirebase(token){ return new Promise(function(resolve, reject){ root.authWithCustomToken(token, function(error){ if(error){ reject(error); } else { resolve(root); } }.bind(this)); }); } function getProject(project, root){ return Promise.resolve(root.child(project)); } var TestimFirebase = function(){ this._getRoot = new Promise(function(resolve){ this.startlogin = resolve; }.bind(this)); }; TestimFirebase.prototype.init = function(token, project){ this._project = project; this._token = token; this._getRoot = this._getRoot.then(loginToFirebase.bind(this, token)).then(getProject.bind(this, project)); this.startlogin(); } ; TestimFirebase.prototype.getToken = function(){ return this._token; }; TestimFirebase.prototype.getProject = function(){ return this._project; }; TestimFirebase.prototype.save = function(itemPath, data){ return this._getRoot.then(function(root){ return new Promise(function(resolve, reject){ root.child(itemPath).set(data, function (error) { return error ? reject(error) : resolve(); }.bind(this)); }.bind(this)); }); }; TestimFirebase.prototype.update = function(itemPath, data){ return this._getRoot.then(function(root){ return new Promise(function(resolve, reject){ return root.child(itemPath).update(data, function (error) { return error ? reject(error) : resolve(); }.bind(this)); }.bind(this)); }); }; TestimFirebase.prototype.saveExecution = function(executionId, data){ return this.save("executions/" + executionId, data); }; TestimFirebase.prototype.saveExecutionEnd = function(executionId){ return this.update("executions/" + executionId, { 'endTime': Date.now(), 'run-status': 'completed' }); }; TestimFirebase.prototype.waitFor = function(itemPath) { return this._getRoot.then(function(root){ var ref = root.child(itemPath); return new Promise(function(resolve, reject){ var listenToValueFunction = function(ref, dataSnapshot){ var value = dataSnapshot.val(); if(dataSnapshot.exists()){ ref.off('value', listenToValueFunction); resolve(value); } }; ref.on('value', listenToValueFunction.bind(this, ref), reject); }.bind(this)); }); }; TestimFirebase.prototype.clearTestResult = function(executionId, testId){ var itemPath = 'executions/' + executionId + '/testResults/' + testId + '/testResults'; return this.save(itemPath, {}); }; TestimFirebase.prototype.waitForTestStart = function(executionId, testId) { var itemPath = 'executions/' + executionId + '/testResults/' + testId + '/testResults/started'; return this.waitFor(itemPath); }; TestimFirebase.prototype.waitForTestResult = function(executionId, testId){ var itemPath = 'executions/' + executionId + '/testResults/' + testId; return this.waitFor(itemPath + '/testResults/success').then(function() { return this.getItem(itemPath); }.bind(this)); }; TestimFirebase.prototype.getItem = function(itemPath){ return this._getRoot.then(function(root){ return new Promise(function(resolve){ root.child(itemPath).once('value', function(valueSnapshot){ resolve(valueSnapshot.val()); }); }); }); }; TestimFirebase.prototype.getTestList = function(labels, testIds){ function isInTestLists(testIdToCheck){ return testIds.filter(function(testIdToRun){ return testIdToRun === testIdToCheck; }).length > 0; } function isInLabelList(labelListToCheck){ if(!labelListToCheck){ return false; } return labels.filter(function(labelToRun){ return labelListToCheck.filter(function(labelToCheck){ return labelToCheck === labelToRun; }).length > 0; }).length > 0; } return this.getItem('test').then(function(tests){ var testsToRun = Object.keys(tests || {}).reduce(function(testsToRun, currentTestId){ if(isInTestLists(currentTestId) || isInLabelList(tests[currentTestId].labels)){ testsToRun.push( {testId : currentTestId, name : tests[currentTestId].name }); } return testsToRun; }, []); return Promise.resolve(testsToRun); }); }; var testimFirebase = new TestimFirebase(); module.exports = testimFirebase;