【问题标题】:I want to remove one file after creating it --- node js integration testing我想在创建后删除一个文件---节点js集成测试
【发布时间】:2014-12-08 08:18:25
【问题描述】:

我正在使用 mocha 框架实现 logger(winston) 的集成测试。

假设我使用logger.error。并使用它创建一个文件。

我必须检查文件是否已创建

但是我必须在检查它的存在后删除这个文件。

// on file is created and logged the data
logger.error("module.testnamespace.info", "Info message", 1231, 12313); 

// Checking the existance of that file
fs.exists(filePath, function (exists) {
    should(exists).equal(true, 'Log File should exist');
});

// I want to delete that file
fs.unlinkSync(filePath);

但在这里我得到了这个错误

 1) 1. Integration Test Cases for logger wrapper : for `info` method A. Integration Test Cases for logger wrapper : for `info` method a. Check logger file is creating or not:
     Error: ETXTBSY, text file is busy '/web/log/rpc-test.log.2014-12-08'
      at Object.fs.unlinkSync (fs.js:760:18)
      at Context.<anonymous> (/web/gin/mochaTest/rpcTest/logger_rpc_test.js:49:10)
      at Test.Runnable.run (/web/gin/node_modules/mocha/lib/runnable.js:194:15)
      at Runner.runTest (/web/gin/node_modules/mocha/lib/runner.js:372:10)
      at /web/gin/node_modules/mocha/lib/runner.js:448:12
      at next (/web/gin/node_modules/mocha/lib/runner.js:297:14)
      at /web/gin/node_modules/mocha/lib/runner.js:307:7
      at next (/web/gin/node_modules/mocha/lib/runner.js:245:23)
      at Object._onImmediate (/web/gin/node_modules/mocha/lib/runner.js:274:5)
      at processImmediate [as _immediateCallback] (timers.js:336:15)

【问题讨论】:

    标签: node.js testing logging integration-testing mocha.js


    【解决方案1】:
    fs.exists(filePath, function (exists) {
        should(exists).equal(true, 'Log File should exist');
        fs.unlinkSync(filePath);
    });
    

    试试这个

    或者如果你只是想删除文件然后不要检查它只是删除并让 unlink 处理它。
    尽量避免竞争条件

    fs.unlink('message.txt',function(err,results){
     if(err)   console.log('File Doesnt exists');
      else console.log('deleted!');
    });
    

    【讨论】:

    • 其实我正在编写集成测试。为此,在每次测试之后,我都必须删除文件(我的要求)。但我无法删除它。
    • 我已经尝试过您的第一个示例。但得到同样的错误
    【解决方案2】:

    我无法在自己的系统上获得ETXTBUSY。我怀疑它与时间问题密切相关。但是,您尝试做的事情需要一些技巧来设置和拆除您的测试。值得注意的是:

    1. 您应该在删除之前告诉 Winston 停止使用日志文件。这是执行此操作的安全方法,因为它不假定记录器的任何特定行为,除了一旦您告诉它停止它就不会继续使用文件。请参阅下面示例中的afterEach

    2. 您的测试本身应该是异步的,因为 Winston 是异步写入的。我第一次尝试在此处复制您的问题时,测试失败,因为该文件尚不存在。请参阅下面示例中的test

    这是一个例子:

    var winston = require("winston");
    var should = require("chai").should;
    var fs = require("fs");
    
    var filePath = 'file.log';
    
    var logger = new (winston.Logger)();
    
    beforeEach(function () {
        // Because of the afterEach hook, we need add a transport before
        // each test.
        logger.add(winston.transports.File, { filename: filePath });
    });
    
    // An example of a test.
    function test(done) {
        logger.error(this.test.title);
    
        // Winston logs to its file asynchronously so using a synchronous
        // check will **NOT** work. This test fails if it does not find a
        // file before the timeout set by Mocha. (2s default).
        function check() {
            fs.exists(filePath, function (exists) {
                if (!exists)
                     // Not present, check again in 0.5s...
                    setTimeout(check, 500);
                else
                    done();
            });
        }
        check();
    }
    
    // Declare two tests to Mocha.
    it("test 1", test);
    it("test 2", test);
    
    afterEach(function () {
        // Stop using the file before deleting it.
        logger.remove(winston.transports.File, { filename: filePath });
        // If the test failed, we don't have a file to remove so handle it
        // with the try-catch.
        try {
            fs.unlinkSync(filePath);
        }
        catch (e) {
            if (code !== 'ENOENT')
                throw e;
        }
    });
    

    【讨论】:

      猜你喜欢
      • 2020-01-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-04-06
      • 2022-07-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多