Browse files

vows gzip test

  • Loading branch information...
1 parent a404856 commit e7fae1dd2fcbfe74d570eb9c360aaeaba95ba67e @sdolard committed Mar 21, 2012
Showing with 91 additions and 138 deletions.
  1. +1 −1 git_hooks/pre-commit
  2. +0 −137 test/logtofile-gz.js
  3. +90 −0 test/vows-logtofile-gz.js
View
2 git_hooks/pre-commit
@@ -1,5 +1,5 @@
#!/bin/sh
-RESULT_TEXT_OUTPUT=$(node test/run_test.js)
+RESULT_TEXT_OUTPUT=$(vows test/vows*.js --spec)
RESULT_VALUE_OUTPUT=$?
echo "$RESULT_TEXT_OUTPUT"
exit $RESULT_VALUE_OUTPUT
View
137 test/logtofile-gz.js
@@ -1,137 +0,0 @@
-/*
-Copyright © 2011 by Sebastien Dolard (sdolard@gmail.com)
-
-
-Permission is hereby granted, free of charge, to any person obtaining a copy
-of this software and associated documentation files (the "Software"), to deal
-in the Software without restriction, including without limitation the rights
-to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
-copies of the Software, and to permit persons to whom the Software is
-furnished to do so, subject to the following conditions:
-
-The above copyright notice and this permission notice shall be included in
-all copies or substantial portions of the Software.
-
-THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
-IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
-FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
-AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
-LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
-OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
-*/
-
-var
-assert = require('assert'),
-path = require('path'),
-util = require('util'),
-fs = require('fs'),
-zlib = require('zlib'),
-logToFile = require('../lib/logtofile'),
-dataTest = [
- '1234567890AZERTYUIOPQSDFGHJKLMWXCVBN',
- '&é"(§è!çà)-azertyuiop^qsdfghjklmù`xcvbn,;:='
-].join(''),
-endData = '',
-rs,
-log,
-endEvent = 0,
-closeEventUnlink = 0,
-errorEvent = 0,
-writtingEvent = 0,
-writeEvent = 0,
-writtenEvent = 0,
-backupedEvent = 0,
-gzippingEvent = 0,
-gzippedEvent = 0;
-
-
-log = logToFile.create({
- directory: __dirname,
- fileName: path.basename(__filename) + '.test.txt',
- gzipBackupFile: true,
- fileMaxSize: dataTest.length
-});
-
-log.on('error', function(err){
- errorEvent++;
- console.log("Error: ", err);
-});
-
-log.on('writting', function(){
- //console.log('writting');
- writtingEvent++;
-});
-
-log.on('write', function(){
- //console.log('write');
- writeEvent++;
-});
-
-log.on('written', function(fileName){
- //console.log('written');
- writtenEvent++;
-});
-
-log.on('backuped', function (filePath, newFilePath) {
- backupedEvent++;
- //console.log('backuped: %s', newFilePath);
-});
-
-log.on('gzipping', function (filePath, newFilePath) {
- gzippingEvent++;
- //console.log('gzipping %s...', filePath);
-});
-
-log.on('gzipped', function (filePath, newFilePath) {
- gzippedEvent++;
- //console.log('gzipped: %s', newFilePath);
- rs = fs.createReadStream(newFilePath).pipe(zlib.createGunzip());
- rs.on('data', function (data) {
- endData += data.toString();
- });
- rs.on('end', function () {
- endEvent++;
- // Clean up
- fs.unlink(log.filePath, function (err) {
- if (err) {
- throw err;
- }
- closeEventUnlink++;
- fs.unlink(newFilePath, function (err) {
- if (err) {
- throw err;
- }
- closeEventUnlink++;
-
- });
-
- });
- assert.equal(endData, dataTest);
- });
-
-});
-
-log.write(dataTest);
-
-process.on('uncaughtException', function (err) {
- console.log('Caught exception: ' + err);
-});
-
-process.on('exit', function () {
- // Event
- assert.strictEqual(errorEvent, 0, 'errorEvent');
- assert.strictEqual(writtingEvent, 1, 'writtingEvent');
- assert.strictEqual(writeEvent, 1, 'writeEvent');
- assert.strictEqual(writtenEvent, 1, 'writtenEvent');
- assert.strictEqual(backupedEvent, 1, 'backupedEvent');
- assert.strictEqual(gzippingEvent, 1, 'gzippingEvent');
- assert.strictEqual(gzippedEvent, 1, 'gzippedEvent');
-
- // Cleanup
- assert.strictEqual(endEvent, 1, 'endEvent');
- assert.strictEqual(closeEventUnlink, 2, 'closeEventUnlink ');
-});
-
-
-
-
View
90 test/vows-logtofile-gz.js
@@ -0,0 +1,90 @@
+var
+vows = require('vows'),
+assert = require('assert'),
+util = require('util'),
+events = require("events"),
+path = require("path"),
+fs = require("fs"),
+zlib = require("zlib"),
+
+logToFile = require('../lib/logtofile'),
+dataTest = [
+ '1234567890AZERTYUIOPQSDFGHJKLMWXCVBN',
+ '&é"(§è!çà)-azertyuiop^qsdfghjklmù`xcvbn,;:='
+].join(''),
+testFileName = 'tmpTest-gz.txt',
+testFilePath = path.normalize(__dirname + '/' + testFileName);
+
+exports.suite1 = vows.describe('logtofile gz').addBatch({
+ 'When calling write method with a fileMaxSize set to data size': {
+ topic: function() {
+ var
+ promise = new events.EventEmitter(),
+ log = logToFile.create({
+ directory: __dirname,
+ fileName: testFileName,
+ gzipBackupFile: true,
+ fileMaxSize: dataTest.length
+ });
+ log.on('gzipped', function (oldFilePath, newFilePath) {
+ promise.emit('success', oldFilePath, newFilePath);
+ });
+ log.write(dataTest);
+ return promise;
+
+ },
+ "It creates a gz file": function (log, oldFilePath, newFilePath) {
+ assert.isTrue(path.existsSync(newFilePath));
+ },
+ "It removes old file": function (log, oldFilePath, newFilePath) {
+ assert.isFalse(path.existsSync(testFilePath));
+ }
+ }
+}).
+addBatch({
+ "When reading gz content": {
+ topic: function () {
+ var
+ promise = new events.EventEmitter(),
+ rs = fs.createReadStream(testFilePath + '.00.gz').pipe(zlib.createGunzip());
+
+ rs.on('data', function (data) {
+ promise.emit('success', data.toString());
+ });
+ return promise;
+ },
+ "content is correct": function(rs, data){
+ assert.strictEqual(data, dataTest);
+ }
+ }
+}).
+addBatch({
+ 'Then': {
+ topic: function() {
+ setTimeout(this.callback, 100); // must be async
+ },
+ "it creates a new empty file": function () {
+ assert.isTrue(path.existsSync(testFilePath));
+ }
+ }
+}).
+addBatch({
+ 'Finnaly, we delete tmpTest-gz.txt.00.gz': {
+ topic: function() {
+ fs.unlink(testFilePath + '.00.gz', this.callback);
+ },
+ "Ok": function (err) {
+ assert.isUndefined(err);
+ }
+ }
+}).
+addBatch({
+ 'and we delete tmpTest-gz.txt': {
+ topic: function() {
+ fs.unlink(testFilePath, this.callback);
+ },
+ "Ok": function (err) {
+ assert.isUndefined(err);
+ }
+ }
+});

0 comments on commit e7fae1d

Please sign in to comment.