weixin_39533052
weixin_39533052
2021-01-12 05:29

bundle open does not quote gem path which may contain spaces

If path to project directory happens to contain spaces, and a gem was installed into that project directory using the --path option, bundle open does not work as expected because it does not quote the path when calling the system method. Here is a little test script I wrote to understand the issue:

 ruby
require 'minitest/autorun'

describe 'using `system` on a file path with spaces' do
  before do
     = "a file with spaces in its name.rb"
    File.write , ""
  end
  after do
    File.delete 
  end
  it 'does not work if path is not quoted' do
    system("ruby #{  }").must_equal false
  end
  it 'works if path is quoted' do
    system("ruby \"#{  }\"").must_equal true
  end
end

begin
  # Only run the following test if this OS supports double quotes in file names.
  # E.g. OS X does, MS Windows does not. Not sure about others - the easiest way
  # to find out is just try it:
  filepath = "\"double quotes\".txt"
  File.write filepath, ""
  File.delete filepath
rescue
else
  describe 'using `system` on a file path with double quotes' do
    before do
       = "a file with \"double quotes\" in its name.rb"
      File.binwrite , ""
    end
    after do
      File.delete 
    end
    it 'does not work if double quotes are not escaped' do
      system("ruby \"#{  }\"").must_equal false
    end
    it 'works if double quotes are escaped' do
      system("ruby \"#{ .gsub('"', '\\"') }\"").must_equal true
    end
  end
end

该提问来源于开源项目:rubygems/bundler

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

5条回答

  • weixin_39598941 weixin_39598941 4月前

    Thanks! Can you add a test to open_spec.rb please?

    点赞 评论 复制链接分享
  • weixin_39533052 weixin_39533052 4月前

    Hmm... Looks like this would not be a new test, but a modification to the existing tests. What I mean is:

     ruby
    expect(out).to eq("editor #{default_bundle_path('gems', 'rails-2.3.2')}")
    # would be re-written as
    expect(out).to eq("editor \"#{default_bundle_path('gems', 'rails-2.3.2')}\"")
    

    Unfortunately the echo command that is used to fake the editor, ..."EDITOR" => "echo editor"..., happens to strip out double quotes. E.g: echo hello "world" outputs hello world, without the quotes, not hello "world" as I would expect. Not sure how to work around that... This peculiarity of echo happens to be the reason why all the existing tests in open_spec.rb continue to run just fine even after the change I made in cli/open.rb. Any suggestions?

    点赞 评论 复制链接分享
  • weixin_39533052 weixin_39533052 4月前

    Ok, here is an idea:

     ruby
      it "quotes the path" do
        bundle "open rails", :env => {"EDITOR" => "ruby -e \"puts ARGV.length\"", "VISUAL" => "", "BUNDLER_EDITOR" => ""}
        expect(out).to eq("1")
      end
    

    In the test above, if there are spaces in full_gem_path and it is not quoted, ARGV.length will be greater than one and the test will fail. But I do not know how to make full_gem_path to have spaces in for this test. I am new to rspec - there must be a way to mock this method and return a predefined string?

    点赞 评论 复制链接分享
  • weixin_39856208 weixin_39856208 4月前

    You can set the path the gem gets created at, there should be some examples in places where build_gem(:path => blah) is called.

    点赞 评论 复制链接分享
  • weixin_39598941 weixin_39598941 4月前

    Closing due to inactivity.

    点赞 评论 复制链接分享

相关推荐