Skip to content

Commit

Permalink
cli: fix and clarify the behavior of 'clean'
Browse files Browse the repository at this point in the history
  • Loading branch information
doudou committed Jul 11, 2017
1 parent a1ae52b commit e956ae5
Show file tree
Hide file tree
Showing 4 changed files with 108 additions and 4 deletions.
13 changes: 10 additions & 3 deletions lib/autoproj/cli/clean.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,17 @@ def validate_options(packages, options)
def run(selection, options = Hash.new)
initialize_and_load
packages, _ = normalize_command_line_package_selection(selection)

deps = if options.has_key?(:deps)
options[:deps]
else
selection.empty?
end

source_packages, * = resolve_selection(
selection,
recursive: false)
if packages.empty?
packages,
recursive: deps)
if source_packages.empty?
raise ArgumentError, "no packages or OS packages match #{selection.join(" ")}"
end

Expand Down
17 changes: 16 additions & 1 deletion lib/autoproj/cli/main.rb
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,22 @@ def cache(*cache_dir)
end

desc 'clean [PACKAGES]', 'remove build byproducts for the given packages'
option :all,
long_desc <<-EODESC
Remove build byproducts from disk
To avoid mistakes, 'clean' will ask for confirmation if no packages
are provided on the command line. Use --all to bypass this check (e.g.
in automated scripts)
When packages are explicitely provided on the command line, autoproj
will by default not clean the package dependencies. However, when
no packages are provided on the command line, all the workspace
packages will be cleaned. Use --deps=f or --deps=t to override
these defaults.
EODESC
option :deps, type: :boolean,
desc: "clean the given packages as well as their dependencies"
option :all, type: :boolean,
desc: 'bypass the safety question when you mean to clean all packages'
def clean(*packages)
run_autoproj_cli(:clean, :Clean, Hash[], *packages)
Expand Down
5 changes: 5 additions & 0 deletions lib/autoproj/package_definition.rb
Original file line number Diff line number Diff line change
Expand Up @@ -74,5 +74,10 @@ def add_setup_block(block)
def checked_out?
autobuild.checked_out?
end

# Add another package as a dependency of this one
def depends_on(pkg)
autobuild.depends_on(pkg.autobuild)
end
end
end
77 changes: 77 additions & 0 deletions test/cli/test_clean.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
require 'autoproj/test'
require 'autoproj/cli/clean'

module Autoproj
module CLI
describe Clean do
attr_reader :cli
before do
ws_create
@cli = Clean.new(ws)
flexmock(cli)
# bypass initialize_and_load since we do our own mock
# configuration
cli.should_receive(:initialize_and_load)
end

describe "#validate_options" do
describe "confirmation if no packages have been selected" do
it "passes if the user replied 'yes'" do
flexmock(TTY::Prompt).new_instances.
should_receive(:yes?).and_return(true)
assert_equal [[], Hash.new], cli.validate_options([], Hash.new)
end
it "raises if the user replied 'yes'" do
flexmock(TTY::Prompt).new_instances.
should_receive(:yes?).and_return(false)
assert_raises(Interrupt) do
cli.validate_options([], Hash.new)
end
end
it "does not ask confirmation if the 'all' options is true" do
flexmock(TTY::Prompt).new_instances.
should_receive(:yes?).never
assert_equal [[], Hash[all: true]], cli.validate_options([], all: true)
end
end
end

it "cleans recursively if no packages have been given on the command line" do
pkg0 = ws_add_package_to_layout :cmake, 'package'
pkg1 = ws_define_package :cmake, 'other'
pkg0.depends_on pkg1
flexmock(pkg0.autobuild).should_receive(:prepare_for_rebuild).once
flexmock(pkg1.autobuild).should_receive(:prepare_for_rebuild).once
cli.run([])
end

it "cleans only explicitely selected packages if no packages have been given on the command line and :deps is false" do
pkg0 = ws_add_package_to_layout :cmake, 'package'
pkg1 = ws_define_package :cmake, 'other'
pkg0.depends_on pkg1
flexmock(pkg0.autobuild).should_receive(:prepare_for_rebuild).once
flexmock(pkg1.autobuild).should_receive(:prepare_for_rebuild).never
cli.run([], deps: false)
end

it "cleans only explicitely given packages by default" do
pkg0 = ws_add_package_to_layout :cmake, 'package'
pkg1 = ws_define_package :cmake, 'other'
pkg0.depends_on pkg1
flexmock(pkg0.autobuild).should_receive(:prepare_for_rebuild).once
flexmock(pkg1.autobuild).should_receive(:prepare_for_rebuild).never
cli.run(['package'])
end

it "cleans explicitely given packages and their dependency if :deps is set" do
pkg0 = ws_add_package_to_layout :cmake, 'package'
pkg1 = ws_define_package :cmake, 'other'
pkg0.depends_on pkg1
flexmock(pkg0.autobuild).should_receive(:prepare_for_rebuild).once
flexmock(pkg1.autobuild).should_receive(:prepare_for_rebuild).once
cli.run(['package'], deps: true)
end
end
end
end

0 comments on commit e956ae5

Please sign in to comment.