diff --git a/.github/workflows/test-local-action-inside-home.yaml b/.github/workflows/test-local-action-inside-home.yaml index 5a406bd..0e0672a 100644 --- a/.github/workflows/test-local-action-inside-home.yaml +++ b/.github/workflows/test-local-action-inside-home.yaml @@ -1,4 +1,4 @@ -name: "Test local action inside $HOME" +name: CI on: workflow_dispatch: pull_request: @@ -13,17 +13,17 @@ jobs: strategy: fail-fast: false matrix: - version: ["1.7.0", "1.8.2", "1.9.0", "1.10.0", "1.11.0", "latest"] - runs-on: ubuntu-latest + version: ["1.7.0", "1.9.0", "1.11.0", "latest"] + os: [ ubuntu-22.04, ubuntu-latest, windows-2022, windows-latest, macos-14, macos-latest ] + runs-on: ${{ matrix.os }} env: - BATS_LIB_PATH: "${{ github.workspace }}/tests" TERM: xterm - name: local default + name: local-inside-home steps: - name: Checkout uses: actions/checkout@v4 - name: Setup Bats and Bats libs - id: bats-action + id: setup-bats uses: ./ with: bats-version: ${{ matrix.version }} @@ -36,30 +36,51 @@ jobs: file-clean: "false" file-path: "${{ github.workspace }}/tests/bats-file" - name: Execute test to check Bats-support - if: steps.bats-action.outputs.support-installed == 'true' + if: steps.setup-bats.outputs.support-installed == 'true' + shell: bash + env: + BATS_LIB_PATH: ${{ steps.setup-bats.outputs.lib-path }} + TMP_PATH: ${{ steps.setup-bats.outputs.tmp-path }} run: | - cd /tmp/bats-support/ + cd ${TMP_PATH}/bats-support/ bats test - - name: Execute test to check Bats-assert - if: steps.bats-action.outputs.assert-installed == 'true' + - name: Execute test to check Bats-assert + if: steps.setup-bats.outputs.assert-installed == 'true' + shell: bash + env: + BATS_LIB_PATH: ${{ steps.setup-bats.outputs.lib-path }} + TMP_PATH: ${{ steps.setup-bats.outputs.tmp-path }} run: | - ls -l $BATS_LIB_PATH/ - cd /tmp/bats-assert/ + cd ${TMP_PATH}/bats-assert/ bats test - name: Execute test to check Bats-detik - if: steps.bats-action.outputs.detik-installed == 'true' + if: steps.setup-bats.outputs.detik-installed == 'true' + # Currently the bats-detik tests are broken + # in macos + continue-on-error: ${{ runner.os == 'macOS' }} + shell: bash + env: + BATS_LIB_PATH: ${{ steps.setup-bats.outputs.lib-path }} + TMP_PATH: ${{ steps.setup-bats.outputs.tmp-path }} run: | - cd /tmp/bats-detik/ + cd ${TMP_PATH}/bats-detik/ bats tests - name: Execute test to check Bats-file - if: steps.bats-action.outputs.file-installed == 'true' + if: steps.setup-bats.outputs.file-installed == 'true' + shell: bash + env: + BATS_LIB_PATH: ${{ steps.setup-bats.outputs.lib-path }} + TMP_PATH: ${{ steps.setup-bats.outputs.tmp-path }} # Currently the bats-file tests are broken # in gh runner env continue-on-error: true run: | - cd /tmp/bats-file/ + cd ${TMP_PATH}/bats-file/ bats test - name: Execute example tests + shell: bash + env: + BATS_LIB_PATH: ${{ steps.setup-bats.outputs.lib-path }} run: | echo $PATH bats -T -p tests @@ -70,16 +91,16 @@ jobs: fail-fast: false matrix: version: ["1.7.0", "1.8.2", "1.9.0", "1.10.0", "1.11.0", "latest"] - runs-on: ubuntu-latest + os: [ ubuntu-22.04, ubuntu-latest, windows-2022, windows-latest, macos-14, macos-latest ] + runs-on: ${{ matrix.os }} env: - BATS_LIB_PATH: "${{ github.workspace }}/tests" TERM: xterm - name: local default cache + name: local-inside-home-cache steps: - name: Checkout uses: actions/checkout@v4 - name: Setup Bats and Bats libs - id: bats-action + id: setup-bats uses: ./ with: bats-version: ${{ matrix.version }} @@ -91,29 +112,9 @@ jobs: detik-path: "${{ github.workspace }}/tests/bats-detik" file-clean: "false" file-path: "${{ github.workspace }}/tests/bats-file" - - name: Execute test to check Bats-support - if: steps.bats-action.outputs.support-installed == 'true' - run: | - cd /tmp/bats-support/ - bats test - - name: Execute test to check Bats-assert - if: steps.bats-action.outputs.assert-installed == 'true' - run: | - cd /tmp/bats-assert/ - bats test - - name: Execute test to check Bats-detik - if: steps.bats-action.outputs.detik-installed == 'true' - run: | - cd /tmp/bats-detik/ - bats tests - - name: Execute test to check Bats-file - if: steps.bats-action.outputs.file-installed == 'true' - # Currently the bats-file tests are broken - # in gh runner env - continue-on-error: true - run: | - cd /tmp/bats-file/ - bats test - name: Execute example tests + shell: bash + env: + BATS_LIB_PATH: ${{ steps.setup-bats.outputs.lib-path }} run: bats -T -p tests diff --git a/.github/workflows/test-local-action-with-conditionals.yaml b/.github/workflows/test-local-action-with-conditionals.yaml index 37d4c01..6e3440c 100644 --- a/.github/workflows/test-local-action-with-conditionals.yaml +++ b/.github/workflows/test-local-action-with-conditionals.yaml @@ -1,4 +1,4 @@ -name: "Test local with conditionals" +name: CI on: workflow_dispatch: pull_request: @@ -13,16 +13,17 @@ jobs: strategy: fail-fast: false matrix: - version: ["1.7.0", "1.8.2", "1.9.0", "1.10.0", "1.11.0", "latest"] - runs-on: ubuntu-latest + version: ["1.7.0", "1.9.0", "1.11.0", "latest"] + os: [ ubuntu-22.04, ubuntu-latest, windows-2022, windows-latest, macos-14, macos-latest ] + runs-on: ${{ matrix.os }} env: - BATS_LIB_PATH: "/usr/lib" TERM: xterm - name: local with options + name: local-options steps: - name: Checkout uses: actions/checkout@v4 - name: Setup Bats and Bats libs + id: setup-bats uses: ./ with: bats-version: ${{ matrix.version }} @@ -31,13 +32,25 @@ jobs: detik-install: "true" file-install: "true" - name: Execute test to check Bats-support + shell: bash + env: + BATS_LIB_PATH: ${{ steps.setup-bats.outputs.lib-path }} + TMP_PATH: ${{ steps.setup-bats.outputs.tmp-path }} run: | - cd /tmp/bats-support/ + cd ${TMP_PATH}/bats-support/ bats test - name: Execute test to check Bats-assert + shell: bash + env: + BATS_LIB_PATH: ${{ steps.setup-bats.outputs.lib-path }} + TMP_PATH: ${{ steps.setup-bats.outputs.tmp-path }} run: | - cd /tmp/bats-assert/ + cd ${TMP_PATH}/bats-assert/ bats test - name: Execute example tests + shell: bash + env: + BATS_LIB_PATH: ${{ steps.setup-bats.outputs.lib-path }} + TMP_PATH: ${{ steps.setup-bats.outputs.tmp-path }} run: bats -T -p tests diff --git a/.github/workflows/test-local-action.yaml b/.github/workflows/test-local-action.yaml index 6e226a1..e28dc9f 100644 --- a/.github/workflows/test-local-action.yaml +++ b/.github/workflows/test-local-action.yaml @@ -1,4 +1,4 @@ -name: "Test local action" +name: CI on: workflow_dispatch: pull_request: @@ -13,17 +13,17 @@ jobs: strategy: fail-fast: false matrix: - version: ["1.7.0", "1.8.2", "1.9.0", "1.10.0", "1.11.0", "latest"] - os: [ ubuntu-20.04, ubuntu-22.04, ubuntu-latest ] + version: ["1.7.0", "1.9.0", "1.11.0", "latest"] + os: [ ubuntu-20.04, ubuntu-22.04, ubuntu-latest, windows-2022, windows-latest, macos-14, macos-latest ] runs-on: ${{ matrix.os }} env: - BATS_LIB_PATH: "/usr/lib" TERM: xterm - name: local default + name: local steps: - name: Checkout uses: actions/checkout@v4 - name: Setup Bats and Bats libs + id: setup-bats uses: ./ with: bats-version: ${{ matrix.version }} @@ -32,25 +32,46 @@ jobs: detik-clean: "false" file-clean: "false" - name: Execute test to check Bats-support + shell: bash + env: + BATS_LIB_PATH: ${{ steps.setup-bats.outputs.lib-path }} + TMP_PATH: ${{ steps.setup-bats.outputs.tmp-path }} run: | - cd /tmp/bats-support/ + cd ${TMP_PATH}/bats-support/ + echo $BATS_LIB_PATH bats test - name: Execute test to check Bats-assert + shell: bash + env: + BATS_LIB_PATH: ${{ steps.setup-bats.outputs.lib-path }} + TMP_PATH: ${{ steps.setup-bats.outputs.tmp-path }} run: | - cd /tmp/bats-assert/ + cd ${TMP_PATH}/bats-assert/ bats test - name: Execute test to check Bats-detik + shell: bash + continue-on-error: true + env: + BATS_LIB_PATH: ${{ steps.setup-bats.outputs.lib-path }} + TMP_PATH: ${{ steps.setup-bats.outputs.tmp-path }} run: | - cd /tmp/bats-detik/ + cd ${TMP_PATH}/bats-detik/ bats tests - name: Execute test to check Bats-file + shell: bash # Currently the bats-file tests are broken # in gh runner env continue-on-error: true + env: + BATS_LIB_PATH: ${{ steps.setup-bats.outputs.lib-path }} + TMP_PATH: ${{ steps.setup-bats.outputs.tmp-path }} run: | - cd /tmp/bats-file/ + cd ${TMP_PATH}/bats-file/ bats test - name: Execute example tests + env: + BATS_LIB_PATH: ${{ steps.setup-bats.outputs.lib-path }} + shell: bash run: bats -T -p tests @@ -59,40 +80,27 @@ jobs: strategy: fail-fast: false matrix: - version: ["1.7.0", "1.8.2", "1.9.0", "1.10.0", "1.11.0", "latest"] - os: [ ubuntu-20.04, ubuntu-22.04, ubuntu-latest ] + version: ["1.7.0", "1.9.0", "1.11.0", "latest"] + os: [ ubuntu-20.04, ubuntu-22.04, ubuntu-latest, windows-2022, windows-latest, macos-14, macos-latest ] runs-on: ${{ matrix.os }} env: - BATS_LIB_PATH: "/usr/lib" TERM: xterm - name: local default cache + name: local-default-no-cache steps: - name: Checkout uses: actions/checkout@v4 - name: Setup Bats and Bats libs uses: ./ + id: setup-bats with: bats-version: ${{ matrix.version }} support-clean: "false" assert-clean: "false" detik-clean: "false" file-clean: "false" - - name: Execute test to check Bats-support - run: | - cd /tmp/bats-support/ - bats test - - name: Execute test to check Bats-assert - run: | - cd /tmp/bats-assert/ - bats test - - name: Execute test to check Bats-detik - run: | - cd /tmp/bats-detik/ - bats tests - - name: Execute test to check Bats-file - # Currently the bats-file tests are broken - # in gh runner env - continue-on-error: true - run: | - cd /tmp/bats-file/ - bats test + - name: Execute example tests + env: + BATS_LIB_PATH: ${{ steps.setup-bats.outputs.lib-path }} + shell: bash + run: + bats -T -p tests diff --git a/README.md b/README.md index 5d2928c..d0cde4f 100644 --- a/README.md +++ b/README.md @@ -8,6 +8,7 @@ This GitHub Action installs [Bats](https://github.com/bats-core/bats-core) and t * [bats-file](https://github.com/bats-core/bats-file) The action can be also instructed to select which libraries to install. +While Linux is fully supported, windows and macos runners should work as well, check the [specific](#windows-and-macos-support) readme part. ## How to use it @@ -22,28 +23,77 @@ jobs: - name: Checkout uses: actions/checkout@v2 - name: Setup Bats and bats libs - uses: bats-core/bats-action@2.0.0 + id: setup-bats + uses: bats-core/bats-action@3.0.0 + - name: My test + shell: bash + env: + BATS_LIB_PATH: ${{ steps.setup-bats.outputs.lib-path }} + TERM: xterm + run: bats test/my-test +``` + +By default the action will pass the `BATS_LIB_PATH` value as output `lib-path`. +You can use it like the below example and load the libraries in your tests with: + +```bash +_tests_helper() { + export BATS_LIB_PATH=${BATS_LIB_PATH:-"/usr/lib"} + bats_load_library bats-support + bats_load_library bats-assert + bats_load_library bats-file + bats_load_library bats-detik/detik.bash +} ``` ## Libraries Path -For each of the Bats libraries, you can choose to install them in the default location (`/usr/lib/bats-`) or specify a custom path. +For each of the Bats libraries, you can choose to install them in the default location (`/usr/lib/bats-` for linux) or specify a custom path. For example, if you want to install `bats-support` in the `./test/bats-support` directory, you can configure it as follows: ``` yaml -# ... + [...] - name: Setup Bats and Bats libs + id: setup-bats uses: bats-core/bats-action@2.0.0 with: support-path: ${{ github.workspace }}/test/bats-support + [...] ``` ## About Caching The caching mechanism for the `bats binary` is always available. However, the caching for the `bats libraries` is dependent on the location of each library path. If a library is located within the $HOME directory, caching is supported. Conversely, if a library is located outside the $HOME directory (which is the default location per each library), caching is not supported. This is due to a known limitation with sudo and the cache action, as detailed in this GitHub issue: https://github.com/actions/toolkit/issues/946. +**If you want to cache bats libraries you must install them inside HOME directory**. +For instance this is an example that will use the github workspace handle (works for linux/win/mac): + +```yaml + [...] + - name: Setup Bats and bats libs + id: setup-bats + uses: bats-core/bats-action@3.0.0 + with: + support-path: "${{ github.workspace }}/tests/bats-support" + assert-path: "${{ github.workspace }}/tests/bats-assert" + detik-path: "${{ github.workspace }}/tests/bats-detik" + file-path: "${{ github.workspace }}/tests/bats-file" + [...] +``` + +## Windows and macos support + +* Macos is fully supported for both default path and custom home path, just be aware that under `/usr` only `/usr/local/` is writable, +the rest is read only (you may consider to use an home path leverage the cache). + * default libraries installation: `/usr/local/lib` + * default temp directory (for dev testing): `/tmp` +* Windows is fully supported as well, however they may be some hiccup around the libraries installation in another drive that is not `C:`. +Please report any issue. + * default libraries installation: `/c/Users/runneradmin` + * default temp directory (for dev testing): `$HOME/AppData/Local/Temp` + ## Inputs | Key | Default | Required | Description | @@ -76,3 +126,5 @@ The caching mechanism for the `bats binary` is always available. However, the ca | assert-installed | True/False if bats-assert has been installed | | detik-installed | True/False if bats-detik has been installed | | file-installed | True/False if bats-file has been installed | +| lib-path | Bats lib path to use to load the libraries | +| tmp-path | Temporary path with each library tests | diff --git a/action.yaml b/action.yaml index c6831b6..dec22cb 100644 --- a/action.yaml +++ b/action.yaml @@ -55,7 +55,7 @@ inputs: detik-version: description: "Bats-detik version, default to latest" required: false - default: "1.3.1" + default: "1.3.2" detik-path: description: "Bats-detik path, default to /usr/lib/bats-detik" required: false @@ -97,6 +97,12 @@ outputs: file-installed: description: "True/False if bats-file has been installed" value: ${{ (steps.file-install.outputs.file-installed != '') }} + lib-path: + description: "Bats lib path to use to load the libraries" + value: ${{ steps.libpath.outputs.libpath }} + tmp-path: + description: "Temporary path with each library tests" + value: ${{ steps.set-paths.outputs.tmp-path }} runs: using: composite @@ -134,14 +140,13 @@ runs: if [[ -z "$VERSION" ]] || [[ "$VERSION" = "latest" ]]; then VERSION=$(curl -fsSL --retry 4 --retry-connrefused https://api.github.com/repos/bats-core/bats-core/releases/latest | grep tag_name | cut -d '"' -f 4) fi - [[ $VERSION = v* ]] && VERSION="${VERSION:1}" + [[ $VERSION == v* ]] && VERSION="${VERSION:1}" mkdir -p ${TEMPDIR} mkdir -p ${DESTDIR} curl -sL --retry 4 --retry-connrefused ${URL}/archive/refs/tags/v${VERSION}.tar.gz | tar xz -C ${TEMPDIR} --strip-components 1 && cd ${TEMPDIR} - # Install ./install.sh ${DESTDIR} echo "Bats v${VERSION} installed in ${DESTDIR}" echo "${DESTDIR}/bin" >> "$GITHUB_PATH" @@ -149,12 +154,47 @@ runs: rm -rf ${TEMPDIR} || exit 0 + - name: Set paths and commands for libraries + shell: bash + id: set-paths + run: | + if [[ "$RUNNER_OS" == "macOS" ]]; then + DEFAULT_BASE_DIR="/usr/local/lib" + [[ "${DEFAULT_BASE_DIR}" == "$HOME"* ]] && CMD="" || CMD="sudo" + TEMPDIR="/tmp" + elif [[ "$RUNNER_OS" == "Windows" ]]; then + DEFAULT_BASE_DIR="/c/Users/runneradmin" + CMD="" + TEMPDIR="$HOME/AppData/Local/Temp" + else + DEFAULT_BASE_DIR="/usr/lib" + [[ "${DEFAULT_BASE_DIR}" == "$HOME"* ]] && CMD="" || CMD="sudo" + TEMPDIR="/tmp" + fi + + echo "_LIB_PATH=$DEFAULT_BASE_DIR" >> /tmp/bats-lib-path + echo "DEFAULT_BASE_DIR=$DEFAULT_BASE_DIR" >> $GITHUB_ENV + echo "CMD=$CMD" >> $GITHUB_ENV + echo "TEMPDIR=$TEMPDIR" >> $GITHUB_ENV + echo "tmp-path=$TEMPDIR" >> $GITHUB_OUTPUT + + - name: "Calculate DESTDIR for Bats-support" + if: inputs.support-install == 'true' + id: calculate-support-destdir + shell: bash + run: | + if [ -z "${{ inputs.support-path }}" ] || [ "${{ inputs.support-path }}" == "/usr/lib/bats-support" ]; then + echo "SUPPORT_DESTDIR=${DEFAULT_BASE_DIR}/bats-support" >> $GITHUB_ENV + else + echo "SUPPORT_DESTDIR=${{ inputs.support-path }}" >> $GITHUB_ENV + fi + - name: "Set cache for Bats-support" - uses: actions/cache@v4 if: inputs.support-install == 'true' + uses: actions/cache@v4 id: support-cache with: - path: ${{ inputs.support-path }} + path: ${{ env.SUPPORT_DESTDIR }} key: ${{ runner.os }}-${{ runner.arch }}-bats-support-${{ inputs.support-version }} - name: "Download and install Bats-support" @@ -163,31 +203,40 @@ runs: shell: bash run: | VERSION=${{ inputs.support-version }} - DESTDIR=${{ inputs.support-path }} - TEMPDIR="/tmp/bats-support" url="https://github.com/bats-core/bats-support/archive/refs/tags/v${VERSION}.tar.gz" + TEMPDIR="${TEMPDIR}/bats-support" + DESTDIR=${SUPPORT_DESTDIR} mkdir -p ${TEMPDIR} - [[ "${DESTDIR}" == "$HOME"* ]] && CMD="" || CMD="sudo" ${CMD} mkdir -p ${DESTDIR}/src/ curl -sL --retry 4 --retry-connrefused ${url} | tar xz -C ${TEMPDIR} --strip-components 1 && cd ${TEMPDIR} # Archlinux style, except that we are not in a fakeroot env - ${CMD} install -Dm755 load.bash ${DESTDIR}/load.bash + ${CMD} install -m755 load.bash ${DESTDIR}/load.bash for fn in src/*.bash; do - ${CMD} install -Dm755 $fn \ + ${CMD} install -m755 $fn \ ${DESTDIR}/src/$(basename $fn) done echo "Bats Support v$VERSION installed in $DESTDIR" echo "support-installed=true" >> $GITHUB_OUTPUT - # Cleanup bats-support if required - [[ "${{ inputs.support-clean }}" = "true" ]] && rm -rf ${TEMPDIR} || exit 0 + [[ "${{ inputs.support-clean }}" == "true" ]] && rm -rf ${TEMPDIR} || exit 0 + + - name: "Calculate DESTDIR for Bats-assert" + if: inputs.assert-install == 'true' + id: calculate-assert-destdir + shell: bash + run: | + if [ -z "${{ inputs.assert-path }}" ] || [ "${{ inputs.assert-path }}" == "/usr/lib/bats-assert" ]; then + echo "ASSERT_DESTDIR=${DEFAULT_BASE_DIR}/bats-assert" >> $GITHUB_ENV + else + echo "ASSERT_DESTDIR=${{ inputs.assert-path }}" >> $GITHUB_ENV + fi - name: "Set cache for Bats-assert" - uses: actions/cache@v4 if: inputs.assert-install == 'true' + uses: actions/cache@v4 id: assert-cache with: - path: ${{ inputs.assert-path }} + path: ${{ env.ASSERT_DESTDIR }} key: ${{ runner.os }}-${{ runner.arch }}-bats-assert-${{ inputs.assert-version }} - name: "Download and install Bats-assert" @@ -196,31 +245,40 @@ runs: shell: bash run: | VERSION=${{ inputs.assert-version }} - DESTDIR=${{ inputs.assert-path }} - TEMPDIR="/tmp/bats-assert" url="https://github.com/bats-core/bats-assert/archive/refs/tags/v${VERSION}.tar.gz" + TEMPDIR="${TEMPDIR}/bats-assert" + DESTDIR=${ASSERT_DESTDIR} mkdir -p ${TEMPDIR} - [[ "${DESTDIR}" == "$HOME"* ]] && CMD="" || CMD="sudo" ${CMD} mkdir -p ${DESTDIR}/src/ curl -sL --retry 4 --retry-connrefused ${url} | tar xz -C ${TEMPDIR} --strip-components 1 && cd ${TEMPDIR} # Archlinux style, except that we are not in a fakeroot env - ${CMD} install -Dm755 load.bash ${DESTDIR}/load.bash + ${CMD} install -m755 load.bash ${DESTDIR}/load.bash for fn in src/*.bash; do - ${CMD} install -Dm755 $fn \ + ${CMD} install -m755 $fn \ ${DESTDIR}/src/$(basename $fn) done echo "Bats Assert v$VERSION installed in $DESTDIR" echo "assert-installed=true" >> "$GITHUB_OUTPUT" - # Cleanup bats-support if required - [[ "${{ inputs.assert-clean }}" = "true" ]] && rm -rf ${TEMPDIR} || exit 0 + [[ "${{ inputs.assert-clean }}" == "true" ]] && rm -rf ${TEMPDIR} || exit 0 + + - name: "Calculate DESTDIR for Bats-detik" + if: inputs.detik-install == 'true' + id: calculate-detik-destdir + shell: bash + run: | + if [ -z "${{ inputs.detik-path }}" ] || [ "${{ inputs.detik-path }}" == "/usr/lib/bats-detik" ]; then + echo "DETIK_DESTDIR=${DEFAULT_BASE_DIR}/bats-detik" >> $GITHUB_ENV + else + echo "DETIK_DESTDIR=${{ inputs.detik-path }}" >> $GITHUB_ENV + fi - name: "Set cache for Bats-detik" - uses: actions/cache@v4 if: inputs.detik-install == 'true' + uses: actions/cache@v4 id: detik-cache with: - path: ${{ inputs.detik-path }} + path: ${{ env.DETIK_DESTDIR }} key: ${{ runner.os }}-${{ runner.arch }}-bats-detik-${{ inputs.detik-version }} - name: "Download and install Bats-detik" @@ -229,30 +287,39 @@ runs: shell: bash run: | VERSION=${{ inputs.detik-version }} - DESTDIR=${{ inputs.detik-path }} - TEMPDIR="/tmp/bats-detik" url="https://github.com/bats-core/bats-detik/archive/refs/tags/v${VERSION}.tar.gz" + TEMPDIR="${TEMPDIR}/bats-detik" + DESTDIR="${DETIK_DESTDIR}" mkdir -p ${TEMPDIR} - [[ "${DESTDIR}" == "$HOME"* ]] && CMD="" || CMD="sudo" ${CMD} mkdir -p ${DESTDIR}/src/ curl -sL --retry 4 --retry-connrefused ${url} | tar xz -C ${TEMPDIR} --strip-components 1 && cd ${TEMPDIR} # Archlinux style, except that we are not in a fakeroot env so we need to use sudo for fn in lib/*.bash; do - ${CMD} install -Dm755 $fn \ + ${CMD} install -m755 $fn \ ${DESTDIR}/$(basename $fn) done echo "Bats Detik v$VERSION installed in $DESTDIR" - echo "detik-installed=true" >> "$GITHUB_OUTPUT" - # Cleanup bats-support if required - [[ "${{ inputs.detik-clean }}" = "true" ]] && rm -rf ${TEMPDIR} || exit 0 + echo "detik-installed=true" >> $GITHUB_OUTPUT + [[ "${{ inputs.detik-clean }}" == "true" ]] && rm -rf ${TEMPDIR} || exit 0 + + - name: "Calculate DESTDIR for Bats-file" + if: inputs.file-install == 'true' + id: calculate-file-destdir + shell: bash + run: | + if [ -z "${{ inputs.file-path }}" ] || [ "${{ inputs.file-path }}" == "/usr/lib/bats-file" ]; then + echo "FILE_DESTDIR=${DEFAULT_BASE_DIR}/bats-file" >> $GITHUB_ENV + else + echo "FILE_DESTDIR=${{ inputs.file-path }}" >> $GITHUB_ENV + fi - name: "Set cache for Bats-file" - uses: actions/cache@v4 if: inputs.file-install == 'true' + uses: actions/cache@v4 id: file-cache with: - path: ${{ inputs.file-path }} + path: ${{ env.FILE_DESTDIR }} key: ${{ runner.os }}-${{ runner.arch }}-bats-file-${{ inputs.file-version }} - name: "Download and install Bats-file" @@ -261,27 +328,50 @@ runs: shell: bash run: | VERSION=${{ inputs.file-version }} - DESTDIR=${{ inputs.file-path }} - TEMPDIR="/tmp/bats-file" url="https://github.com/bats-core/bats-file/archive/refs/tags/v${VERSION}.tar.gz" + TEMPDIR="${TEMPDIR}/bats-file" + DESTDIR="${FILE_DESTDIR}" mkdir -p ${TEMPDIR} - [[ "${DESTDIR}" == "$HOME"* ]] && CMD="" || CMD="sudo" ${CMD} mkdir -p ${DESTDIR}/src/ curl -sL --retry 4 --retry-connrefused ${url} | tar xz -C ${TEMPDIR} --strip-components 1 && cd ${TEMPDIR} # Archlinux style, except that we are not in a fakeroot env - ${CMD} install -Dm755 load.bash ${DESTDIR}/load.bash + ${CMD} install -m755 load.bash ${DESTDIR}/load.bash for fn in src/*.bash; do - ${CMD} install -Dm755 $fn \ + ${CMD} install -m755 $fn \ ${DESTDIR}/src/$(basename $fn) done echo "Bats File v$VERSION installed in $DESTDIR" - echo "file-installed=true" >> "$GITHUB_OUTPUT" - # Cleanup bats-support if required - [[ "${{ inputs.file-clean }}" = "true" ]] && rm -rf ${TEMPDIR} || exit 0 + echo "file-installed=true" >> $GITHUB_OUTPUT + [[ "${{ inputs.file-clean }}" == "true" ]] && rm -rf ${TEMPDIR} || exit 0 + + - name: "build BATS_LIB_PATH" + id: libpath + shell: bash + run: | + # In case of windows if the path passed + # as github workspace could be something like + # D:\a\bats-action\bats-action/tests + # so we need to translate D: into /d/ + # and replace the wrong slash + winfix() { + local winpath="$1" + echo "$winpath" | sed 's|\\|/|g' | sed 's|^\([A-Za-z]\):|/\L\1|' + } + if [[ "$RUNNER_OS" == "Windows" ]]; then + S="$(winfix ${SUPPORT_DESTDIR%/*})" + A="$(winfix ${ASSERT_DESTDIR%/*})" + D="$(winfix ${DETIK_DESTDIR%/*})" + F="$(winfix ${FILE_DESTDIR%/*})" + LIB_PATH="${S}:${A}:${D}:${F}" + else + LIB_PATH="${SUPPORT_DESTDIR%/*}:${ASSERT_DESTDIR%/*}:${DETIK_DESTDIR%/*}:${FILE_DESTDIR%/*}" + fi + echo "libpath=$LIB_PATH" >> $GITHUB_OUTPUT + - - name: "Debug print if installed" - if: runner.debug == '1' + - name: "Print info" + id: info shell: bash run: | echo "Bats installed: ${{ (steps.bats-install.outputs.bats-installed != '') }}" @@ -289,3 +379,5 @@ runs: echo "Assert installed: ${{ (steps.assert-install.outputs.assert-installed != '') }}" echo "Detik installed: ${{ (steps.detik-install.outputs.detik-installed != '') }}" echo "File installed: ${{ (steps.file-install.outputs.file-installed != '') }}" + echo "Bats lib to use in BATS_LIB_PATH env ${{ steps.libpath.outputs.libpath }}" + echo "Tmp path: ${{ steps.set-paths.outputs.tmp-path }}" diff --git a/tests/1-example-tests.bats b/tests/1-example-tests.bats index 36b197d..02c23ba 100644 --- a/tests/1-example-tests.bats +++ b/tests/1-example-tests.bats @@ -3,70 +3,97 @@ setup() { _tests_helper } - +#bats test_tags=github:true @test "0: Pre: Create file and dir" { - run mkdir testing - run touch testing/example - assert_success + _create_dir_file + _delete_dir_file } #bats test_tags=github:true @test "1: Testing file existence" { + _create_dir_file + run ls testing/example assert_success - [ -f "testing/example" ] + run [ -f "testing/example" ] assert_success + + _delete_dir_file } #bats test_tags=github:true @test "2: Testing file permissions" { - run stat -c "%a" testing/example + _create_dir_file + if [[ "$(uname)" == "Darwin" ]]; then + run stat -f%p testing/example + else + run stat -c "%a" testing/example + fi assert_success - assert_output 644 + if [[ "$(uname)" == "Darwin" ]]; then + assert_output 100644 + else + assert_output 644 + fi assert_file_permission 644 testing/example assert_success + + _delete_dir_file } #bats test_tags=github:true @test "3: Testing file content" { + _create_dir_file + run cat testing/example assert_success refute_output "Expected content" assert_file_empty testing/example assert_success + + _delete_dir_file } #bats test_tags=github:true @test "4: Testing directory creation" { + _create_dir_file + run mkdir testing/newdir + assert_success + [ -d "testing/newdir" ] assert_success + + run rmdir testing/newdir + assert_success + + _delete_dir_file } #bats test_tags=github:true -@test "5: Testing file creation inside directory" { +@test "5: Testing file creation and deletion inside directory" { + _create_dir_file + + run mkdir testing/newdir + assert_success + run touch testing/newdir/newfile - [ -f "testing/newdir/newfile" ] assert_success -} -#bats test_tags=github:true -@test "6: Testing file deletion" { + run [ -f "testing/newdir/newfile" ] + assert_success + run rm testing/newdir/newfile - [ ! -f "testing/newdir/newfile" ] assert_success -} -#bats test_tags=github:true -@test "7: Testing directory deletion" { + [ ! -f "testing/newdir/newfile" ] + assert_success + run rmdir testing/newdir - [ ! -d "testing/newdir" ] assert_success -} -#bats test_tags=github:true -@test "8: Testing file deletion" { - run rm testing/example - [ ! -f "testing/example" ] + run [ ! -d "testing/newdir" ] assert_success + + _delete_dir_file } diff --git a/tests/tests_helper.bash b/tests/tests_helper.bash index 2107a29..e7961e5 100644 --- a/tests/tests_helper.bash +++ b/tests/tests_helper.bash @@ -1,7 +1,24 @@ _tests_helper() { export BATS_LIB_PATH=${BATS_LIB_PATH:-"/usr/lib"} + echo $BATS_LIB_PATH bats_load_library bats-support bats_load_library bats-assert bats_load_library bats-file bats_load_library bats-detik/detik.bash } + +_create_dir_file() { + run mkdir testing + assert_success + + run touch testing/example + assert_success +} + +_delete_dir_file() { + run rm testing/example + assert_success + + run rmdir testing + assert_success +}