How do I use paths with spaces in a Rake task?

I am trying to create a rake file for my ASP.Net application. This is the path to the mstest.exe file:

mstest = "C:/Program Files (x86)/Microsoft Visual Studio 12.0/Common7/IDE/mstest.exe"

      

This is part of the rakefile I have:

task :default do
      mstest = "C:/Program\ Files\ (x86)/Microsoft\ Visual\ Studio\ 12.0/Common7/IDE/mstest.exe"
      sh "#{mstest_file} \"#{testdll_file} "
end

      

I am getting this error:

rake interrupted! Albacore :: CommandNotFoundError: the command failed with the status (127) - in particular, the number 127 means that the operating system could not find the executable file:

C: / Program Files (x86) / Microsoft Visual Studio 12.0 / Common7 / IDE / mstest.exe

I think the spaces in the path are causing the error: "Program Files (86x) / Visual Studio ..." because if there is no space in the path, the rakefile works correctly.

I've tried these things so far, but no successful results so far:

  • mstest = "C:/Program Files (x86)/Microsoft Visual Studio 12.0/Common7/IDE/mstest.exe"

  • mstest = "C:/Program\ Files\ (x86)/Microsoft\ Visual\ Studio\ 12.0/Common7/IDE/mstest.exe"

  • mstest = "C:/Program\s Files\s (x86)/Microsoft\s Visual\s Studio 12.0/Common7/IDE/mstest.exe"

  • mstest = "C:\\Program Files (x86)\\Microsoft Visual Studio 12.0\\Common7\\IDE\\mstest.exe"

  • Insert mstest and # {mstest} in single quotes, double quotes, and combinations of the two.

How can I fix this?

+3


source to share


1 answer


The problem is that you are using double quotes with escaped spaces instead of single quotes. Consider the following:

"foo\ bar" # => "foo bar"
'foo\ bar' # => "foo\\ bar"

      

or

"C:/Program Files\ (x86)/Microsoft\ Visual\ Studio\ 12.0/Common7/IDE/mstest.exe" 
# => "C:/Program Files (x86)/Microsoft Visual Studio 12.0/Common7/IDE/mstest.exe"
'C:/Program Files\ (x86)/Microsoft\ Visual\ Studio\ 12.0/Common7/IDE/mstest.exe' 
# => "C:/Program Files\\ (x86)/Microsoft\\ Visual\\ Studio\\ 12.0/Common7/IDE/mstest.exe"

      

Notice how the first, using double quotes, says "use a literal character" when the interpreter encounters it "\ "

. By using single quotes, it says "use a backslash followed by a space" which you want to use, since you want the backslash to propagate across the OS when passing a string.

Indeed, you should probably take a look at using Ruby's built-in Shellwords class:



Manipulates strings like UNIX Bourne shell

This module manipulates strings according to the UNIX Bourne shell parsing rules.

In particular, take a look shellescape

:

Calls a string so that it can be used safely on the Bourne shell command line.

Consider this:

require 'shellwords'

'C:/Program Files\ (x86)/Microsoft\ Visual\ Studio\ 12.0/Common7/IDE/mstest.exe' 
# => "C:/Program Files\\ (x86)/Microsoft\\ Visual\\ Studio\\ 12.0/Common7/IDE/mstest.exe"
Shellwords.escape("C:/Program Files (x86)/Microsoft Visual Studio 12.0/Common7/IDE/mstest.exe")
# => "C:/Program\\ Files\\ \\(x86\\)/Microsoft\\ Visual\\ Studio\\ 12.0/Common7/IDE/mstest.exe"

      

Note that shellescape

parentheses have also been avoided.

+3


source







All Articles