代码之家  ›  专栏  ›  技术社区  ›  yegor256

为什么在Minitest测试中fork以非零退出?

  •  2
  • yegor256  · 技术社区  · 7 年前

    代码如下:

    require 'minitest/autorun'
    class Foo < Minitest::Test
      def test_foo
        Process.fork do
          exit(0)
        end
        p Process.waitall
      end
    end
    

    这是输出:

    $ ruby a.rb
    Run options: --seed 40445
    
    # Running:
    
    [[41827, #<Process::Status: pid 41827 exit 1>]]
    .
    
    Finished in 0.016218s, 61.6599 runs/s, 0.0000 assertions/s.
    1 runs, 0 assertions, 0 failures, 0 errors, 0 skips
    

    为什么退出代码不是零?

    没有Minitest的相同代码可以正常工作:

    Process.fork do
      exit(0)
    end
    p Process.waitall
    

    发生了什么?

    1 回复  |  直到 7 年前
        1
  •  2
  •   Sergio Tulentsev    7 年前

    https://github.com/seattlerb/minitest/issues/467

    问题是 fork 复制 at_exit 出口处 Kernel.exit! 避免运行任何 出口处

    推荐文章