Skip to content

Commit 6cb6645

Browse files
committed
(maint) Prefer apt-get to apt
For the couple of package manager operations in common.sh, apt had preference before apt-get when searching for a system tool. But, from what I've read, apt is not a replacement for apt-get, and the expectation is not that newer Debian systems will eventually have apt instead of apt-get. The apt tool is intended as a more user friendly interface, while apt-get remains a stabler UI and more complete toolset better suited for scripting. For this particular script, we aren't doing any parsing, so it's academic. But given that we're not relying on a behavior difference between the two, I've switched the order to favor apt-get to adhere to the spirit of their differences. I'm actually not aware of an installation where you would have apt-get without apt or vice-versa, so a fallback may be unnecessary as well, but there is undoubtadely some edge case somewhere...
1 parent b3f55d7 commit 6cb6645

File tree

2 files changed

+16
-16
lines changed

2 files changed

+16
-16
lines changed

files/common.sh

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -339,10 +339,10 @@ install_package() {
339339

340340
case ${_os_family} in
341341
debian|ubuntu)
342-
if exists 'apt'; then
343-
exec_and_capture apt install -y "${_package_and_version}"
344-
elif exists 'apt-get'; then
342+
if exists 'apt-get'; then
345343
exec_and_capture apt-get install -y "${_package_and_version}"
344+
elif exists 'apt'; then
345+
exec_and_capture apt install -y "${_package_and_version}"
346346
else
347347
fail "Unable to install ${_package}. Neither apt nor apt-get are installed."
348348
fi
@@ -363,10 +363,10 @@ install_package() {
363363

364364
# Update the package manager cache.
365365
refresh_package_cache() {
366-
if exists 'apt'; then
367-
exec_and_capture apt update
368-
elif exists 'apt-get'; then
366+
if exists 'apt-get'; then
369367
exec_and_capture apt-get update
368+
elif exists 'apt'; then
369+
exec_and_capture apt update
370370
elif exists 'dnf'; then
371371
exec_and_capture dnf clean all
372372
elif exists 'yum'; then

spec/unit/bash/common_sh_spec.rb

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -329,50 +329,50 @@
329329

330330
before do
331331
mock_facts_task_bash_sh(os)
332-
allow_script.to receive_command(:apt).and_exec('echo "apt given: $*"')
332+
allow_script.to receive_command('apt-get').and_exec('echo "apt-get given: $*"')
333333
end
334334

335335
it 'installs a package' do
336336
output, status = test('install_package foo')
337337

338-
expect(output).to include('apt given: install -y foo')
338+
expect(output).to include('apt-get given: install -y foo')
339339
expect(status.success?).to be(true)
340340
end
341341

342342
it 'installs a package with a version' do
343343
output, status = test('install_package foo 1.2.3')
344344

345345
expect(status.success?).to be(true)
346-
expect(output).to include('apt given: install -y foo=1.2.3-1+ubuntu24.04')
346+
expect(output).to include('apt-get given: install -y foo=1.2.3-1+ubuntu24.04')
347347
end
348348

349349
it 'installs a deb with full package version given' do
350350
output, status = test('install_package foo 1.2.3-1something')
351351

352352
expect(status.success?).to be(true)
353-
expect(output).to include('apt given: install -y foo=1.2.3-1something')
353+
expect(output).to include('apt-get given: install -y foo=1.2.3-1something')
354354
end
355355

356356
it 'fails if package manager fails' do
357-
allow_script.to receive_command(:apt).and_exec(<<~EOF)
358-
echo 'apt failed'
357+
allow_script.to receive_command('apt-get').and_exec(<<~EOF)
358+
echo 'apt-get failed'
359359
return 1
360360
EOF
361361

362362
output, status = test('install_package doesnotexist')
363363

364364
expect(status.success?).to be(false)
365-
expect(output).to include('apt failed')
365+
expect(output).to include('apt-get failed')
366366
end
367367

368368
it 'falls back to apt-get' do
369-
behave_as_if_command_does_not_exist(:apt)
370-
allow_script.to receive_command('apt-get').and_exec('echo "apt-get given: $*"')
369+
behave_as_if_command_does_not_exist('apt-get')
370+
allow_script.to receive_command(:apt).and_exec('echo "apt given: $*"')
371371

372372
output, status = test('install_package foo')
373373

374374
expect(status.success?).to be(true)
375-
expect(output).to include('apt-get given: install -y foo')
375+
expect(output).to include('apt given: install -y foo')
376376
end
377377

378378
it 'fails if neither apt nor apt-get are available' do

0 commit comments

Comments
 (0)