簡體   English   中英

Rspec安裝錯誤-參數無效

[英]Rspec installation error - Invalid argument

我剛移動到在Win 10 / ruby​​ 2.2.2 / Rails 4.2.0上運行的新筆記本電腦(在以前的Win 8 ruby​​ 2.0.0 / Rails 4.2 / rspec 3.2上沒有問題)。 現在,在安裝rspec ,下面有一個奇怪的錯誤:

$ rails g rspec:install
   identical  .rspec
       exist  spec
      create  spec/C:/Users/Jun C/AppData/Local/Temp/d20160219-10996-1x6hu8w/spec/spec_helper.rb
C:/Ruby22-x64/lib/ruby/2.2.0/fileutils.rb:252:in `mkdir': Invalid argument @ dir_s_mkdir - C:/D/code/rails_proj/engines/simple_orderx/spec/C: (Errno::EINVAL)
        from C:/Ruby22-x64/lib/ruby/2.2.0/fileutils.rb:252:in `fu_mkdir'
        from C:/Ruby22-x64/lib/ruby/2.2.0/fileutils.rb:226:in `block (2 levels) in mkdir_p'
        from C:/Ruby22-x64/lib/ruby/2.2.0/fileutils.rb:224:in `reverse_each'
        from C:/Ruby22-x64/lib/ruby/2.2.0/fileutils.rb:224:in `block in mkdir_p'
        from C:/Ruby22-x64/lib/ruby/2.2.0/fileutils.rb:210:in `each'
        from C:/Ruby22-x64/lib/ruby/2.2.0/fileutils.rb:210:in `mkdir_p'
        from C:/Ruby22-x64/lib/ruby/gems/2.2.0/gems/thor-0.19.1/lib/thor/actions/create_file.rb:61:in `block in invoke!'
        from C:/Ruby22-x64/lib/ruby/gems/2.2.0/gems/thor-0.19.1/lib/thor/actions/empty_directory.rb:116:in `call'
        from C:/Ruby22-x64/lib/ruby/gems/2.2.0/gems/thor-0.19.1/lib/thor/actions/empty_directory.rb:116:in `invoke_with_conflict_check'
        from C:/Ruby22-x64/lib/ruby/gems/2.2.0/gems/thor-0.19.1/lib/thor/actions/create_file.rb:60:in `invoke!'

Rails應用程序根目錄下有/spec spec-helper文件應在/spec /spec/spec-helper.rb成為/spec/spec-helper.rb 但是,安裝嘗試在spec/C:/Users/Jun C/AppData/Local/Temp/d20160219-10996-1x6hu8w/spec/上生成spec-helper 似乎路徑是錯誤的。

這是Gemfile的行:

s.add_development_dependency "rspec-rails", ">= 3.2.0"

這是engine.rb的配置

   config.generators do |g|
      g.template_engine :erb
      g.integration_tool :rspec
      g.test_framework :rspec
      g.fixture_replacement :factory_girl, :dir => "spec/factories"  
    end  

rspec版本是3.2.2 如何在rspec安裝中解決此錯誤路徑?

我的環境

  • Windows 10
  • 紅寶石2.4.4p296(2018-03-28修訂版63013)[x64-mingw32]
  • Rails 5.2.0
  • rspec-rails(3.7.2)
  • 雷神(0.20.0)

TL; DR

由於Dir.mktmpdir (RSpec-rails使用)和Dir.glob (Thor使用)在Windows 10上返回不匹配的用戶目錄路徑,因此Thor無法正確生成相對路徑,從而導致無效路徑,從而導致文件操作失敗。 此問題是Windows 10特有的。請確保您的RSpec-rails和Thor寶石是最新的。 如果這樣不能解決您的問題,請參見下面的解決方法。

引擎蓋下

rails generate rspec:install命令:

  1. 創建一個臨時目錄
  2. 用它需要的文件填充它
  3. 使用Thor將這些文件復制到Rails應用中

RSpec-rails在第一步中使用Dir.mktmpdir 在Windows 10上,結果路徑如下所示:

Dir.mktmpdir() => C:/Users/BEN~1.AMO/AppData/Local/Temp/...

此路徑派生自Windows 10 TMP環境變量:

irb(main):005:0> ENV['TMP']
=> "C:\\Users\\BEN~1.AMO\\AppData\\Local\\Temp"

在第三步中查找要復制的文件時,Thor使用Dir.glob 在Windows 10上,找到的路徑如下所示:

Dir.glob(lookup, File::FNM_DOTMATCH) => C:/Users/ben.amos/AppData/Local/Temp/...

這些路徑是從HOME環境變量派生的:

irb(main):006:0> ENV['HOME']
=> "C:/Users/ben.amos"

當Thor找到要用Dir.glob復制的文件時,它首先嘗試通過刪除路徑的temp目錄部分(由Dir.mktmpdir創建的目錄)來導出相對路徑。 但由於Dir.globDir.mktmpdir路徑不匹配, String.gsub什么也不做,絕對路徑被保留,后續的文件路徑操作創建一個無效的路徑字符串,... BOOM。 如OP所述,復制失敗。

如果好奇,您可以通過查看以下兩個文件來自己驗證此行為:

RSpec-rails gem: lib\generators\rspec\install\install_generator.rb
Thor gem: lib\thor\actions\directory.rb

解決方法

我能夠產生的最簡單,侵入性最小的解決方法是將ENV['TMP']臨時設置為其他值。 您可以在Rails設置過程的早期進行此操作。 只需使用一些非用戶特定的路徑,如下所示:

  1. 打開config/application.rb
  2. 將以下內容添加到Application類的底部。

    ENV['TMP'] = 'C:/Windows/Temp' # or any other valid temp directory

  3. 運行rails generate rspec:install

  4. 刪除在步驟2中添加的行。

所需文件放置到位后,您不必再為此擔心。

正式修復

RSpec可能需要Windows伙計使用不同的臨時目錄,或者Thor需要為其路徑使用更智能的邏輯。 雷神(Thor)的Github上一張公開票

認為最快的解決方案是使用正確的版本,您可以使用:

gem 'rails', '4.2.3'
gem 'sqlite3'
gem 'sass-rails', '~> 5.0'
gem 'uglifier', '>= 1.3.0'
gem 'coffee-rails', '~> 4.1.0'
gem 'therubyracer', platforms: :ruby
gem 'jquery-rails'
gem 'turbolinks'
gem 'jbuilder', '~> 2.0'
gem 'sdoc', '~> 0.4.0', group: :doc

gem 'web-console', '~> 2.0', group: :development

group :development, :test do
  gem 'byebug'
  gem 'spring'
end

group :test do
 gem 'rspec-rails', '~> 3.0'
end
# Windows does not include zoneinfo files, so bundle the tzinfo-data gem
gem 'tzinfo-data', platforms: [:mingw, :mswin, :x64_mingw, :jruby]

gem 'capybara', '~> 2.4.4'
gem 'poltergeist', '~> 1.6.0'
gem 'phantomjs', '~> 1.9.8.0'
gem 'bootstrap-sass'
gem 'minitest'

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM