From 6a692487c872c23d39efa7938210b2326d8ef866 Mon Sep 17 00:00:00 2001 From: Joe Previte Date: Thu, 16 Sep 2021 16:19:10 -0700 Subject: [PATCH] feat: add test for get_nfpm_arch --- ci/build/arch-override.json | 8 -------- ci/build/build-lib.sh | 28 ++++++++++++++++++++++++++++ ci/build/build-packages.sh | 17 +++-------------- test/scripts/build-lib.bats | 21 +++++++++++++++++++++ 4 files changed, 52 insertions(+), 22 deletions(-) delete mode 100644 ci/build/arch-override.json create mode 100755 ci/build/build-lib.sh create mode 100644 test/scripts/build-lib.bats diff --git a/ci/build/arch-override.json b/ci/build/arch-override.json deleted file mode 100644 index 44804ddee..000000000 --- a/ci/build/arch-override.json +++ /dev/null @@ -1,8 +0,0 @@ -{ - "rpm": { - "armv7l": "armhfp" - }, - "deb": { - "armv7l": "armhf" - } -} diff --git a/ci/build/build-lib.sh b/ci/build/build-lib.sh new file mode 100755 index 000000000..520276c1b --- /dev/null +++ b/ci/build/build-lib.sh @@ -0,0 +1,28 @@ +#!/usr/bin/env bash + +# This is a library which contains functions used inside ci/build +# +# We separated it into it's own file so that we could easily unit test +# these functions and helpers. + +# On some CPU architectures (notably node/uname "armv7l", default on Raspberry Pis), +# different package managers have different labels for the same CPU (deb=armhf, rpm=armhfp). +# This function returns the overriden arch on platforms +# with alternate labels, or the same arch otherwise. +get_nfpm_arch() { + local PKG_FORMAT="${1:-}" + local ARCH="${2:-}" + + case "$ARCH" in + armv7l) + if [ "$PKG_FORMAT" = "deb" ]; then + echo armhf + elif [ "$PKG_FORMAT" = "rpm" ]; then + echo armhfp + fi + ;; + *) + echo "$ARCH" + ;; + esac +} diff --git a/ci/build/build-packages.sh b/ci/build/build-packages.sh index cd335ae1f..8da6aec38 100755 --- a/ci/build/build-packages.sh +++ b/ci/build/build-packages.sh @@ -7,6 +7,7 @@ set -euo pipefail main() { cd "$(dirname "${0}")/../.." source ./ci/lib.sh + source ./ci/build/build-lib.sh # Allow us to override architecture # we use this for our Linux ARM64 cross compile builds @@ -43,18 +44,6 @@ release_gcp() { cp "./release-packages/$release_name.tar.gz" "./release-gcp/latest/$OS-$ARCH.tar.gz" } -# On some CPU architectures (notably node/uname "armv7l", default on Raspberry Pis), -# different package managers have different labels for the same CPU (deb=armhf, rpm=armhfp). -# This function parses arch-override.json and returns the overriden arch on platforms -# with alternate labels, or the same arch otherwise. -get_nfpm_arch() { - if jq -re ".${PKG_FORMAT}.${ARCH}" ./ci/build/arch-override.json > /dev/null; then - jq -re ".${PKG_FORMAT}.${ARCH}" ./ci/build/arch-override.json - else - echo "$ARCH" - fi -} - # Generates deb and rpm packages. release_nfpm() { local nfpm_config @@ -62,14 +51,14 @@ release_nfpm() { export NFPM_ARCH PKG_FORMAT="deb" - NFPM_ARCH="$(get_nfpm_arch)" + NFPM_ARCH="$(get_nfpm_arch $PKG_FORMAT "$ARCH")" nfpm_config="$(envsubst < ./ci/build/nfpm.yaml)" echo "Building deb" echo "$nfpm_config" | head --lines=4 nfpm pkg -f <(echo "$nfpm_config") --target "release-packages/code-server_${VERSION}_${NFPM_ARCH}.deb" PKG_FORMAT="rpm" - NFPM_ARCH="$(get_nfpm_arch)" + NFPM_ARCH="$(get_nfpm_arch $PKG_FORMAT "$ARCH")" nfpm_config="$(envsubst < ./ci/build/nfpm.yaml)" echo "Building rpm" echo "$nfpm_config" | head --lines=4 diff --git a/test/scripts/build-lib.bats b/test/scripts/build-lib.bats new file mode 100644 index 000000000..e855f270a --- /dev/null +++ b/test/scripts/build-lib.bats @@ -0,0 +1,21 @@ +#!/usr/bin/env bats + +SCRIPT_NAME="build-lib.sh" +SCRIPT="$BATS_TEST_DIRNAME/../../ci/build/$SCRIPT_NAME" + +source "$SCRIPT" + +@test "get_nfpm_arch should return armhfp for rpm on armv7l" { + run get_nfpm_arch rpm armv7l + [ "$output" = "armhfp" ] +} + +@test "get_nfpm_arch should return armhf for deb on armv7l" { + run get_nfpm_arch deb armv7l + [ "$output" = "armhf" ] +} + +@test "get_nfpm_arch should return arch if no arch override exists " { + run get_nfpm_arch deb i386 + [ "$output" = "i386" ] +} \ No newline at end of file