Permalink
Browse files

- refactoring

  • Loading branch information...
1 parent a286045 commit 8488ad5aeb537727c7420fcf4ee1eca594d30223 @scf2k scf2k committed Jun 5, 2012
Showing with 54 additions and 91 deletions.
  1. +8 −36 test/bem-make.mocha.js
  2. +10 −55 test/bem-server.mocha.js
  3. +36 −0 test/common.js
@@ -8,6 +8,8 @@ var UTIL = require('util'),
BEMUTIL = require('../lib/util'),
BEM = require('../lib/coa').api,
+ C = require('./common'),
+
projectPath = PATH.resolve('./test/data/make/project'),
referencePath = PATH.resolve('./test/data/make/reference-result'),
buildPath = PATH.resolve('./test-make-temp');
@@ -16,7 +18,7 @@ describe('bem', function() {
describe('make', function() {
before(function(done){
- prepareProject()
+ C.prepareProject(projectPath, buildPath)
.then(done)
.end();
});
@@ -31,7 +33,7 @@ describe('bem', function() {
});
it('creates proper artifacts', function(done) {
- return command(
+ return C.command(
UTIL.format(
'find %s -type f -exec diff -q {} %s/{} \\; 2>&1',
'.',
@@ -82,7 +84,7 @@ describe('bem', function() {
return BEM.make({root: buildPath, verbosity: 'error'});
})
.then(function() {
- return command(
+ return C.command(
UTIL.format(
'find %s -type f -exec diff -q {} %s/{} \\; 2>&1',
'.',
@@ -119,7 +121,7 @@ describe('bem', function() {
it('builds two targets', function(done) {
this.timeout(0);
- prepareProject()
+ C.prepareProject(projectPath, buildPath)
.then(function(){
return BEM.make({
root: buildPath,
@@ -145,7 +147,7 @@ describe('bem', function() {
})
})
.then(function() {
- return command(
+ return C.command(
UTIL.format(
'diff -rq %s %s 2>&1 | grep -v ^O; true',
'.',
@@ -163,36 +165,6 @@ describe('bem', function() {
});
-function prepareProject() {
- return QFS.exists(buildPath)
- .then(function(exists) {
- return exists && command(UTIL.format('rm -rf %s', buildPath));
- })
- .then(function() {
- return command(UTIL.format('cp -r %s %s', projectPath, buildPath));
- });
-}
-
-function command(cmd, options, resolveWithOutput) {
- var d = Q.defer(),
- output = '',
- cp = require('child_process').exec(cmd, options);
-
- cp.on('exit', function (code) {
- code === 0? d.resolve(resolveWithOutput && output?output:null): d.reject(new Error(UTIL.format('%s failed: %s', cmd, output)));
- });
-
- cp.stderr.on('data', function (data) {
- output += data;
- });
-
- cp.stdout.on('data', function (data) {
- output += data;
- });
-
- return d.promise;
-}
-
function collectTimestamps(root) {
var list = {};
@@ -221,4 +193,4 @@ function dirHasOnly(dir, files) {
return dirFiles.length === files.length &&
_.union(files, dirFiles).length === files.length;
});
-}
+}
@@ -1,23 +1,23 @@
var assert = require('assert'),
UTIL = require('util'),
PATH = require('path'),
- FS = require('fs'),
Q = require('q'),
_ = require('underscore'),
- QFS = require('q-fs'),
- SA = require('superagent'),
+ QHTTP = require('q-http'),
BEM = require('../lib/coa').api,
- projectPath = PATH.resolve('./test/data/make/project'),
- referencePath = PATH.resolve('./test/data/make/reference-result'),
- buildPath = PATH.resolve('./test-make-temp');
+ C = require('./common'),
+
+ projectPath = PATH.resolve(__dirname, '../test/data/make/project'),
+ referencePath = PATH.resolve(__dirname, '../test/data/make/reference-result'),
+ buildPath = PATH.resolve(__dirname, '../test-make-temp');
describe('bem', function() {
describe('server', function() {
before(function(done){
- prepareProject()
+ C.prepareProject(projectPath, buildPath)
.then(function() {
return BEM.server({root: buildPath, verbosity: 'error'});
})
@@ -76,61 +76,16 @@ describe('bem', function() {
});
});
-function prepareProject() {
- return QFS.exists(buildPath)
- .then(function(exists) {
- return exists && command(UTIL.format('rm -rf %s', buildPath));
- })
- .then(function() {
- return command(UTIL.format('cp -r %s %s', projectPath, buildPath));
- });
-}
-
-function command(cmd, options, resolveWithOutput) {
- var d = Q.defer(),
- output = '',
- cp = require('child_process').exec(cmd, options);
-
- cp.on('exit', function (code) {
- code === 0? d.resolve(resolveWithOutput && output?output:null): d.reject(new Error(UTIL.format('%s failed: %s', cmd, output)));
- });
-
- cp.stderr.on('data', function (data) {
- output += data;
- });
-
- cp.stdout.on('data', function (data) {
- output += data;
- });
-
- if (options && options.stdin) cp.stdin.end(options.stdin);
-
- return d.promise;
-}
-
function req(path) {
- var d = Q.defer();
-
- SA
- .get('http://localhost:8080/' + path)
- .end(function(res) {
- d.resolve(res);
- });
-
- return d.promise;
+ return QHTTP.read('http://localhost:8080/' + path);
}
function reqAndValidate(path) {
return req(path)
.then(function(res){
- assert.equal(res.status, 200, 'response status is 200');
-
- return res;
- })
- .then(function(res){
- return command(UTIL.format('diff -q %s/%s -', referencePath, path), {stdin: res.text}, true);
+ return C.command(UTIL.format('diff -q %s/%s -', referencePath, path), {stdin: res}, true);
})
.then(function(result) {
return result;
});
-}
+}
View
@@ -0,0 +1,36 @@
+var UTIL = require('util'),
+ Q = require('q'),
+ QFS = require('q-fs');
+
+exports.prepareProject = function(projectPath, buildPath) {
+ return QFS.exists(buildPath)
+ .then(function(exists) {
+ return exists && command(UTIL.format('rm -rf %s', buildPath));
+ })
+ .then(function() {
+ return command(UTIL.format('cp -r %s %s', projectPath, buildPath));
+ });
+};
+
+var command = exports.command = function (cmd, options, resolveWithOutput) {
+ var d = Q.defer(),
+ output = '',
+ cp = require('child_process').exec(cmd, options);
+
+ cp.on('exit', function (code) {
+ code === 0? d.resolve(resolveWithOutput && output?output:null): d.reject(new Error(UTIL.format('%s failed: %s', cmd, output)));
+ });
+
+ cp.stderr.on('data', function (data) {
+ output += data;
+ });
+
+ cp.stdout.on('data', function (data) {
+ output += data;
+ });
+
+ if (options && options.stdin) cp.stdin.end(options.stdin);
+
+ return d.promise;
+}
+

0 comments on commit 8488ad5

Please sign in to comment.