diff --git a/CHANGELOG.md b/CHANGELOG.md index 2bb8b57..1cda33f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,8 @@ ## Unreleased +- Warn when Aptfile contains no packages ([#126](https://github.com/heroku/heroku-buildpack-apt/pull/126)) + ## 2024-03-14 - Shell hardening ([#115](https://github.com/heroku/heroku-buildpack-apt/pull/115)) diff --git a/bin/compile b/bin/compile index 8e6b7de..c10cdf1 100755 --- a/bin/compile +++ b/bin/compile @@ -29,6 +29,16 @@ function indent() { esac } +if ! grep --invert-match -e "^\s*#" -e "^\s*$" -e "^:repo:" -q "${BUILD_DIR}/Aptfile"; then + echo " +! You have no packages listed in your Aptfile. If you don't need custom Apt packages, +! delete your Aptfile and remove the buildpack with: +! +! $ heroku buildpacks:remove heroku-community/apt +" + exit 0 +fi + # Store which STACK we are running on in the cache to bust the cache if it changes if [[ -f "$CACHE_DIR/.apt/STACK" ]]; then CACHED_STACK=$(cat "$CACHE_DIR/.apt/STACK") diff --git a/test/fixtures/custom-repository-no-packages/Aptfile b/test/fixtures/custom-repository-no-packages/Aptfile new file mode 100644 index 0000000..003c0c6 --- /dev/null +++ b/test/fixtures/custom-repository-no-packages/Aptfile @@ -0,0 +1 @@ +:repo:deb http://us.archive.ubuntu.com/ubuntu/ jammy multiverse diff --git a/test/fixtures/empty/Aptfile b/test/fixtures/empty/Aptfile new file mode 100644 index 0000000..e69de29 diff --git a/test/fixtures/only-comments/Aptfile b/test/fixtures/only-comments/Aptfile new file mode 100644 index 0000000..349c538 --- /dev/null +++ b/test/fixtures/only-comments/Aptfile @@ -0,0 +1,4 @@ +# no packages + # only comments + +# and whitespace diff --git a/test/run b/test/run index 2039756..6718ba7 100755 --- a/test/run +++ b/test/run @@ -78,6 +78,51 @@ testReportCustomRepository() { assertCapturedSuccess } +testCompileEmpty() { + compile "empty" + assertCaptured "You have no packages listed in your Aptfile" + assertNotCaptured "Updating apt caches" + assertCapturedSuccess +} + +testReportEmpty() { + report "empty" + assertNotCaptured "^packages" + assertNotCaptured "custom_packages" + assertNotCaptured "custom_repositories" + assertCapturedSuccess +} + +testCompileOnlyComments() { + compile "only-comments" + assertCaptured "You have no packages listed in your Aptfile" + assertNotCaptured "Updating apt caches" + assertCapturedSuccess +} + +testReportOnlyComments() { + report "only-comments" + assertNotCaptured "^packages" + assertNotCaptured "custom_packages" + assertNotCaptured "custom_repositories" + assertCapturedSuccess +} + +testCompileCustomRepositoryNoPackages() { + compile "custom-repository-no-packages" + assertCaptured "You have no packages listed in your Aptfile" + assertNotCaptured "Updating apt caches" + assertCapturedSuccess +} + +testReportCustomRepositoryNoPackages() { + report "custom-repository-no-packages" + assertNotCaptured "^packages" + assertNotCaptured "custom_packages" + assertCaptured "custom_repositories: \"deb http://us.archive.ubuntu.com/ubuntu/ jammy multiverse\"" + assertCapturedSuccess +} + pushd "$(dirname 0)" >/dev/null || exit 1 popd >/dev/null || exit 1