Skip to content

Commit

Permalink
cli: add 'which'
Browse files Browse the repository at this point in the history
  • Loading branch information
doudou committed Jan 9, 2018
1 parent 3ae56cf commit 25bd4c9
Show file tree
Hide file tree
Showing 4 changed files with 118 additions and 0 deletions.
25 changes: 25 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "Minitest - all",
"type": "Ruby",
"request": "launch",
"useBundler": true,
"cwd": "${workspaceRoot}",
"program": "/usr/bin/rake",
"args": ["test"]
},
{
"name": "Minitest - active tasks only",
"type": "Ruby",
"request": "launch",
"useBundler": true,
"cwd": "${workspaceRoot}",
"program": "${file}"
}
]
}
6 changes: 6 additions & 0 deletions lib/autoproj/cli/main.rb
Original file line number Diff line number Diff line change
Expand Up @@ -454,6 +454,12 @@ def exec(*args)
require 'autoproj/cli/exec'
CLI::Exec.new.run(*args)
end

desc 'which', "resolves the full path to a command within the Autoproj workspace"
def which(cmd)
require 'autoproj/cli/which'
CLI::Which.new.run(cmd)
end
end
end
end
34 changes: 34 additions & 0 deletions lib/autoproj/cli/which.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
require 'autoproj/cli/inspection_tool'
module Autoproj
module CLI
class Which < InspectionTool
def run(cmd)
initialize_and_load
finalize_setup(Array.new)

# Resolve the command using the PATH if present
env = ws.full_env
absolute =
if Pathname.new(cmd).absolute?
File.expand_path(cmd)
else
env.find_in_path(cmd)
end

if absolute
if !File.file?(absolute)
Autoproj.error "given command `#{absolute}` does not exist"
exit 1
else
puts absolute
end
else
Autoproj.error "cannot resolve `#{cmd}` in the workspace"
exit 1
end
end
end
end
end


53 changes: 53 additions & 0 deletions test/cli/test_which.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
require 'autoproj/test'
require 'autoproj/cli/which'

module Autoproj
module CLI
describe Which do
include Autoproj::SelfTest;
before do
ws_create
@cli = Which.new(ws)
flexmock(@cli).should_receive(:initialize_and_load)
end

it "displays a given full path if it exists, regardless of PATH" do
ws.env.clear 'PATH'
path = File.join(ws.root_dir, 'test')
FileUtils.touch path
out, err = capture_io do
@cli.run path
end
assert_equal "#{path}\n", out
end

it "displays an error if a given full path does not exist, and exits with error" do
path = File.join(ws.root_dir, 'test')
flexmock(Autoproj).should_receive(:error).with("given command `#{path}` does not exist").
once
assert_raises(SystemExit) do
@cli.run path
end
end

it "displays the resolved full path if found" do
ws.env.set 'PATH', ws.root_dir
path = File.join(ws.root_dir, 'test')
FileUtils.touch path
out, err = capture_io do
@cli.run 'test'
end
assert_equal "#{path}\n", out
end

it "displays an error if the path cannot be resolved, and exits with error" do
flexmock(Autoproj).should_receive(:error).with("cannot resolve `test` in the workspace").
once
assert_raises(SystemExit) do
@cli.run 'test'
end
end
end
end
end

0 comments on commit 25bd4c9

Please sign in to comment.