Skip to content

Commit

Permalink
Fixes #37993 - Update cvpurge count to a better description
Browse files Browse the repository at this point in the history
  • Loading branch information
chris1984 committed Nov 20, 2024
1 parent ff599e1 commit 416b9a7
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 12 deletions.
22 changes: 15 additions & 7 deletions lib/hammer_cli_katello/content_view_purge.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@ class ContentViewPurgeCommand < HammerCLIKatello::Command
command_name "purge"
desc "Delete old versions of a content view"

option "--id", "ID", _("Content View numeric identifier")
option "--name", "NAME", _("Content View name")
option "--count", "COUNT", _("count of unused versions to keep"),
option "--id", "Id", _("Content View numeric identifier")
option "--name", "Name", _("Content View name")
option "--count", "Count", _("(deprecated) Number of versions to keep"),
:deprecated => true, format: HammerCLI::Options::Normalizers::Number.new
option "--versions-to-keep", "Versions to keep", _("Number of unused versions to keep"),
default: 3, format: HammerCLI::Options::Normalizers::Number.new

validate_options :before, 'IdResolution' do
Expand Down Expand Up @@ -49,17 +51,23 @@ def option_sources
end

def execute
if option_count.negative?
output.print_error _("Invalid value for --count option: value must be 0 or greater.")
options['option_versions_to_keep'] = option_versions_to_keep
if option_versions_to_keep.negative?
output.print_error _("Invalid value for --versions-to-keep: value must be 0 or greater.")
return HammerCLI::EX_USAGE
end

# Check if there is something to do
if option_count >= old_unused_versions.size
if option_count
warn _("The --count option is deprecated and will be removed in the next release.")
options['option_versions_to_keep'] = option_count
end
if options['option_versions_to_keep'] >= old_unused_versions.size
output.print_error _("No versions to delete.")
HammerCLI::EX_NOT_FOUND
else
versions_to_purge = old_unused_versions.slice(0, old_unused_versions.size - option_count)
versions_to_purge = old_unused_versions.slice(0, old_unused_versions.size -
options['option_versions_to_keep'])

versions_to_purge.each do |v|
purge_version(v)
Expand Down
21 changes: 16 additions & 5 deletions test/functional/content_view/purge_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,16 +52,27 @@ module HammerCLIKatello
it 'fails gracefully if count <= 0' do
api_expects_no_call

r = run_cmd(%w(content-view purge --id 2 --count -1))
assert(r.err.include?('Invalid value for --count option'), 'Incorrect error message')
r = run_cmd(%w(content-view purge --id 2 --versions-to-keep -1))
assert(r.err.include?('Invalid value for --versions-to-keep'), 'Incorrect error message')
end

it 'fails gracefully when there are no versions to delete' do
it 'gives a deprecation warning with the --count option' do
ex = api_expects(:content_view_versions, :index)
ex = ex.with_params("content_view_id" => '2')
ex.returns(versions)

r = run_cmd(%w(content-view purge --id 2 --count 3))
dep_warning = 'The --count option is deprecated and will be removed in the next release.'
_(r.err.lines.first.strip).must_equal(dep_warning)
assert(r.err.include?('No versions to delete.'), 'Incorrect error message')
end

it 'fails gracefully when there are no versions to delete' do
ex = api_expects(:content_view_versions, :index)
ex = ex.with_params("content_view_id" => '2')
ex.returns(versions)

r = run_cmd(%w(content-view purge --id 2 --versions-to-keep 3))
assert(r.err.include?('No versions to delete.'), 'Incorrect error message')
end

Expand All @@ -77,7 +88,7 @@ module HammerCLIKatello
expect_foreman_task('3')
end

run_cmd(%w(content-view purge --id 2 --count 0))
run_cmd(%w(content-view purge --id 2 --versions-to-keep 0))
end

it 'allows for async purge of versions' do
Expand All @@ -91,7 +102,7 @@ module HammerCLIKatello
ex.returns('id' => '3')
end

run_cmd(%w(content-view purge --id 2 --count 0 --async))
run_cmd(%w(content-view purge --id 2 --versions-to-keep 0 --async))
end
end
end

0 comments on commit 416b9a7

Please sign in to comment.