【问题标题】:Wercker test fails to open tempfilesWercker 测试无法打开临时文件
【发布时间】:2014-06-15 20:45:17
【问题描述】:

在我的代码中,我有以下块

Tempfile.open([model.id.to_s, '.txt'], Rails.root.join('tmp')) do |file|
  begin
    file << somedata_i_have_before
    model.file = file # using paperclip gem attached file
  ensure
    # close and delete file
    file.close
    file.unlink
  end
end

此代码在本地和生产环境中都可以正常工作...问题是我已经设置了Wercker 应用程序来自动测试和部署,但是上面提到的块在 wercker 上失败并返回以下错误

Errno::ENOENT:
No such file or directory @ rb_sysopen - /pipeline/build/tmp/539e01d4776572049647010020140615-1174-ajp5tf.txt
# ./lib/some_lib.rb:63:in `some_method'

任何想法如何解决这个问题,以便 wercker 上的构建通过?

【问题讨论】:

  • 我猜 Wercker 不允许你创建文件。通常,您希望测试行为而不是实际的磁盘 I/O。有一个很好的宝石可以帮助解决这个问题:github.com/defunkt/fakefs

标签: ruby-on-rails tdd paperclip temporary-files wercker


【解决方案1】:

我猜 tmp 文件夹在您的存储库中被忽略 (.gitignore),因此在您执行干净的存储库克隆时不会创建它。

我可能错了,但Tempfile.open([model.id.to_s, '.txt'], Rails.root.join('tmp')) 没有创建 tmp 文件夹,它希望它已经存在。

我在忽略文件夹时遇到了类似的问题 - 您可以使用干净的 git clone 对其进行测试,然后像在 CI/CD 服务器上运行一样执行此测试。

【讨论】:

    【解决方案2】:

    问题是 wercker 没有创建 tmp,要解决这个问题,只需将以下步骤添加到您的 wercker.yml(在运行规范之前)

        - script:
            name: create and grant writing permission to /tmp directory
            code: |
                mkdir $WERCKER_ROOT/tmp
                chmod -R 755 $WERCKER_ROOT/tmp
                echo "$(ls -l $WERCKER_ROOT)"
    
        # A step that executes `rspec` command
        - script:
            name: rspec
            code: bundle exec rspec
    

    并确保ls -l $WERCKER_ROOT 包含以下内容

    drwxr-xr-x  2 ubuntu ubuntu 4096 Jun 15 22:39 tmp
    

    解决此问题的另一种方法是创建 tmp/.gitkeep 并将其提交到您的存储库...这也将解决问题(这是一个更清洁的解决方案)

    【讨论】:

    • .gitkeep 也解决了这个问题——除非你的一个脚本删除了 tmp 文件夹,但这不太可能:)
    猜你喜欢
    • 2017-12-03
    • 2020-07-08
    • 2018-11-07
    • 1970-01-01
    • 1970-01-01
    • 2020-06-10
    • 1970-01-01
    • 1970-01-01
    • 2020-12-04
    相关资源
    最近更新 更多