Javascript 如何检查两个文件的内容是否相同?
我正在使用mocha/supertest/should.js测试REST服务Javascript 如何检查两个文件的内容是否相同?,javascript,node.js,compare,mocha.js,should.js,Javascript,Node.js,Compare,Mocha.js,Should.js,我正在使用mocha/supertest/should.js测试REST服务 GET/files/将文件作为流返回 如何在should.js中断言文件内容相同 it('should return file as stream', function (done) { var writeStream = fs.createWriteStream('test/fixtures/tmp.json'); var req = api.get('/files/676dfg1430af
GET/files/
将文件作为流返回
如何在should.js
中断言文件内容相同
it('should return file as stream', function (done) {
var writeStream = fs.createWriteStream('test/fixtures/tmp.json');
var req = api.get('/files/676dfg1430af3595');
req.on('end', function(){
var tmpBuf = fs.readFileSync('test/fixtures/tmp.json');
var testBuf = fs.readFileSync('test/fixtures/test.json');
// How to assert with should.js file contents are the same (tmpBuf == testBuf )
// ...
done();
});
});
您有3种解决方案:
首先:
比较结果字符串
tmpBuf.toString() === testBuf.toString();
秒:
使用循环逐字节读取缓冲区
var index = 0,
length = tmpBuf.length,
match = true;
while (index < length) {
if (tmpBuf[index] === testBuf[index]) {
index++;
} else {
match = false;
break;
}
}
match; // true -> contents are the same, false -> otherwise
var指数=0,
长度=tmpBuf.length,
匹配=真;
while(索引<长度){
if(tmpBuf[index]==testBuf[index]){
索引++;
}否则{
匹配=假;
打破
}
}
匹配;//true->内容相同,false->否则
第三:
使用第三方模块,如和buffertools.compare(buffer,buffer | string)方法。在
should.js
中,您可以使用.eql
来比较缓冲区的实例:
> var buf1 = new Buffer('abc');
undefined
> var buf2 = new Buffer('abc');
undefined
> var buf3 = new Buffer('dsfg');
undefined
> buf1.should.be.eql(buf1)
...
> buf1.should.be.eql(buf2)
...
> buf1.should.be.eql(buf3)
AssertionError: expected <Buffer 61 62 63> to equal <Buffer 64 73 66 67>
...
>
>var buf1=新缓冲区(“abc”);
未定义
>var buf2=新缓冲区(“abc”);
未定义
>var buf3=新缓冲区('dsfg');
未定义
>buf1.should.be.eql(buf1)
...
>buf1.should.be.eql(buf2)
...
>buf1.should.be.eql(buf3)
AssertionError:应等于
...
>
使用文件比较和节点温度的解决方案:
it('should return test2.json as a stream', function (done) {
var writeStream = temp.createWriteStream();
temp.track();
var req = api.get('/files/7386afde8992');
req.on('end', function() {
comparator.compare(writeStream.path, TEST2_JSON_FILE, function(result, err) {
if (err) {
return done(err);
}
result.should.true;
done();
});
});
req.pipe(writeStream);
});
用于比较大型文件,例如,在断言文件上载时比较缓冲区或字符串与should.eql
需要时间。我建议使用crypto模块断言缓冲区哈希:
const buf1Hash=crypto.createHash('sha256').update(buf1.digest();
const buf2Hash=crypto.createHash('sha256').update(buf2.digest();
buf1Hash.should.eql(buf2Hash);
一种更简单的方法是断言缓冲区长度,如下所示:
buf1.length.should.eql(buf2.length)
不用shouldjs作为断言模块,您当然可以使用不同的工具,但令人惊讶的是,没有人建议这样做。这似乎是最快、最简单的方法,自v0.11以来一直存在
因此,您的代码将变成tmpBuf.equals(testBuf)
我认为您应该在JavaScript中使用,以获得更好的性能,至少防止阻塞其他操作:
阻塞是指Node.js进程中附加JavaScript的执行必须等待非JavaScript操作完成。发生这种情况是因为在发生阻塞操作时,事件循环无法继续运行JavaScript
在Node.js中,由于CPU密集而不是等待非JavaScript操作(如I/O)而表现出较差性能的JavaScript通常不被称为阻塞。Node.js标准库中使用libuv的同步方法是最常用的阻塞操作。本机模块也可能有阻塞方法
因此,我将使用如下代码更改Sync
调用。此外,我将使用equals
方法,该方法是Max
建议的,用于比较两个文件:
const fs = require('fs')
fs.readFile('file1', (err, data1) => {
if (err) throw err;
fs.readFile('file2', (err, data2) => {
if (err) throw err;
if (data1.equals(data2)) {
console.log('EQUAL')
} else {
console.log('NON EQUAL')
}
});
});
虽然对于小脚本和单个脚本,结果几乎相同但最好的解决方案是简单的请不要使用哈希。哈希通常是唯一的,但由于其原理,哈希并不总是唯一的。此外,生成散列所需的计算比简单地比较原始内容要昂贵得多。有两个不同的文件和散列的可能性是如此之小,以至于提到它是荒谬的。