Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
3 changes: 3 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,7 @@ trim_trailing_whitespace = true
indent_size = 4
max_line_length = 72

[*.py]
indent_size = 4

# EOF
2 changes: 2 additions & 0 deletions .perltidyrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
--perl-best-practices
--indent-columns=2
14 changes: 7 additions & 7 deletions .trunk/trunk.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,21 +18,21 @@ runtimes:
# This is the section where you manage your linters. (https://docs.trunk.io/check/configuration)
lint:
enabled:
- actionlint@1.7.0
- actionlint@1.7.1
- hadolint@2.12.0
- checkov@3.2.106
- checkov@3.2.125
- git-diff-check
- markdownlint@0.40.0
- prettier@3.2.5
- markdownlint@0.41.0
- prettier@3.3.1
- shellcheck@0.10.0
- shfmt@3.6.0
- trivy@0.51.2
- trufflehog@3.76.3
- trivy@0.52.0
- trufflehog@3.78.0
- yamllint@1.35.1
ignore:
- linters: [ALL]
paths:
- tests/test_helper/**
- vendor/**
actions:
enabled:
- trunk-announce
Expand Down
1 change: 1 addition & 0 deletions script/bats
123 changes: 50 additions & 73 deletions script/bootstrap
Original file line number Diff line number Diff line change
Expand Up @@ -6,26 +6,40 @@ set -euo pipefail
root="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
readonly root

readonly checkmark=$'\033[0;32m✔\033[0m'
readonly crossmark=$'\033[0;31m✘\033[0m'
red=$(tput setaf 1 2>/dev/null || :)
green=$(tput setaf 2 2>/dev/null || :)
yellow=$(tput setaf 3 2>/dev/null || :)
readonly red green yellow

bold=$(tput bold 2>/dev/null || :)
reset=$(tput sgr0 2>/dev/null || :)
readonly bold reset

readonly checkmark="${green}✔${reset}"
readonly crossmark="${red}✘${reset}"

failure=0
success=0

has() {
command -v "$1" >/dev/null 2>&1
}

success() {
echo -e "${checkmark} \033[32m${1}\033[0m"
success=$((success + 1))
printf "%s %s%s%s\n" "${checkmark}" "${green}${bold}" "$1" "${reset}"
}

failure() {
echo -e "${crossmark} \033[31m${1}\033[0m"
failure=$((failure + 1))
printf "%s %s%s%s\n" "${crossmark}" "${red}${bold}" "$1" "${reset}"
}

instructions() {
echo -e " \033[33m${1}\033[0m"
printf " %s%s%s\n" "${yellow}${bold}" "$1" "${reset}"
}

declare _os # memoize OS
declare _os='' # memoize OS
# os tries to detect the Operating System.
os() {
if [[ -z ${_os} ]]; then
Expand All @@ -44,88 +58,51 @@ os() {
printf "%s" "${_os}"
}

"${root}/script/trunk" install --no-progress >/dev/null
success "trunk"

## BATS-CORE
## CURL
############################################################
has_bats=0
if has bats; then
bats_version=$(bats --version 2>/dev/null)
if [[ ${bats_version} == *"Bats 1"* ]]; then
has_bats=1
fi
fi

if ((has_bats)); then
success "bats-core"
if has curl; then
success "curl is installed"
else
failure "missing bats-core"
instructions "Please install bats-core 1.x."
instructions " https://bats-core.readthedocs.io/en/stable/installation.html"

case "$(os)" in
macos)
instructions "For example, on macOS, you can use Homebrew:"
instructions " brew install bats-core"
;;
ubuntu | debian)
instructions "For example, on Ubuntu or Debian, you can use:"
instructions " sudo apt-get install bats"
;;
*) ;;
esac
fi

## SCRIPT
############################################################
if ! has script; then
failure "missing script"
instructions "Please install script(1)."
failure "missing curl"
instructions "Please install curl."
case "$(os)" in
ubuntu | debian)
instructions "On Ubuntu or Debian, you can use:"
instructions " sudo apt-get install bsdutils"
instructions " sudo apt-get install curl"
;;
fedora)
instructions "On Fedora, you can use:"
instructions " sudo dnf install util-linux"
instructions " sudo dnf install curl"
;;
alpine)
instructions "On Alpine, you can use:"
instructions " sudo apk add util-linux"
;;
*)
instructions "Check the documentation for your platform."
macos)
die "On macOS, you should already have curl installed."
;;
*) ;;
esac
fi

## shellcheck
## Trunk.IO
############################################################
if ! has shellcheck; then
failure "missing shellcheck"
case "$(os)" in
ubuntu | debian)
instructions "On Ubuntu or Debian, you can use:"
instructions " sudo apt-get install shellcheck"
;;
fedora)
instructions "On Fedora, you can use:"
instructions " sudo dnf install ShellCheck"
;;
alpine)
instructions "On Alpine, you can use:"
instructions " sudo apk add shellcheck"
;;
*)
instructions "Please install shellcheck."
instructions "Check the documentation for your platform."
;;
esac
printf '\n%sInstalling Trunk%s\n' "${bold}" "${reset}"
trunk_args=(install)
if [[ -n ${CI-} ]]; then
trunk_args+=(--no-progress)
fi
if "${root}/script/trunk" "${trunk_args[@]}"; then
success "trunk is installed"
else
failure "unable to install trunk"
fi

printf '\nsuccesss: %s%d%s failures: %s%d%s\n' "${green}" "${success}" "${reset}" "${red}" "${failure}" "${reset}"

echo
echo $'\033[0;32mDone! 🎉\033[0m'
if ((failure > 0)); then
printf '\n%sBootstrap failed!%s\n' "${red}" "${reset}"
printf '%sPlease resolve the above issues.%s\n' "${yellow}" "${reset}"
exit 1
else
printf '\n%sSuccess! 🎉%s\n' "${green}" "${reset}"
exit 0
fi

# EOF
24 changes: 12 additions & 12 deletions script/test
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,21 @@ set -euo pipefail

cd "$(dirname "${BASH_SOURCE[0]}")/.."

xargs_args=(-0 --no-run-if-empty)
bats_args=(--jobs 400)
# Ensure TERM is set to _something_.
export TERM="${TERM:-dumb}"

if [[ -n ${CI-} ]]; then
xargs_args+=(-t)
bats_args+=(--print-output-on-failure --pretty)
bats_command=(
vendor/bats-core/bin/bats
)

if command -v parallel &>/dev/null; then
bats_command=("${bats_command[@]:+"${bats_command[@]}"}" --jobs 4)
fi

find . -type f \
\( -name '*.bash' -o -name '*.bats' \) \
-not \
\( -path './tests/test_helper/*' -o -path './tmp/*' \) \
-print0 |
xargs "${xargs_args[@]}" shellcheck --check-sourced
if [[ -n ${CI-} ]]; then
bats_command=("${bats_command[@]:+"${bats_command[@]}"}" --print-output-on-failure --pretty)
fi

bats "${bats_args[@]}" tests
"${bats_command[@]}" tests

# EOF
43 changes: 4 additions & 39 deletions tests/test_helper.bash
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
# shellcheck disable=SC2034

load-helpers() {
load test_helper/bats-assert-2.1.0/load
load test_helper/bats-file-0.4.0/load
load test_helper/bats-support-0.3.0/load
load "${BASHABLE_GIT_ROOT}/vendor/bats-assert/load"
load "${BASHABLE_GIT_ROOT}/vendor/bats-file/load"
load "${BASHABLE_GIT_ROOT}/vendor/bats-support/load"
}

BASHABLE_GIT_ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
Expand All @@ -16,40 +16,5 @@ load-bashable() {
}

fake-tty() {
local tmp err cmd

# Create a temporary file for storing the status code
tmp=$(mktemp) || return 99

# Ensure it worked or fail with status 99
if [[ -z ${tmp} ]]; then
return 99
fi

# Produce a script that runs the command provided to faketty as
# arguments and stores the status code in the temporary file
cmd="$(printf '%q ' "$@"); echo \$? > $(printf '%q' "${tmp}")"

# Run the script through /bin/sh with fake tty
if [[ "$(uname -s || :)" == "Darwin" ]]; then
# MacOS
script -Fq /dev/null /bin/sh -c "${cmd}"
else
script -qfc "/bin/sh -c $(printf "%q " "${cmd}")" /dev/null
fi

# Ensure that the status code was written to the temporary file or
# fail with status 99
if ! [[ -s ${tmp} ]]; then
return 99
fi

# Collect the status code from the temporary file
err=$(cat "${tmp}")

# Remove the temporary file
rm -f "${tmp}"

# Return the status code
return "${err}"
"${BASHABLE_GIT_ROOT}/tests/test_helper/pseudo-tty" "$@"
}
39 changes: 39 additions & 0 deletions tests/test_helper/pseudo-tty
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#!/usr/bin/env bash

set -euo pipefail

dir=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)

# Why is this so complicated?
#
# In order to test the detection of TTYs, I need to some how force
# a TTY.
#
# There are a lot of hacky ways to do this and this is what
# I came up with.
#
# If you have a better technique, please share it with me.

progs=(
"${dir}/pseudo-tty.py"
"${dir}/pseudo-tty.pl"
)

for prog in "${progs[@]}"; do
if "${prog}" true >/dev/null 2>&1; then
exec "${prog}" "$@"
fi
done

for prog in "${progs[@]}"; do
printf -- '\n'
printf -- '---------------------->8------- CUT HERE -------8<----------------------\n'
printf -- 'Errors for %s\n' "${prog}"
"${prog}" true 2>&1 || true
printf -- '---------------------->8------- CUT HERE -------8<----------------------\n'
done >&2

printf '\nUnable to find a way to run pseudo-tty.\n' >&2
exit 1

# EOF
29 changes: 29 additions & 0 deletions tests/test_helper/pseudo-tty.pl
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#!/usr/bin/env perl

# Use strict mode.
$| = 1;

use IO::Pty;

my $pty = new IO::Pty;
defined( my $child = fork ) or exit 126;

if ($child) {
$pty->close_slave();
while (<$pty>) { }
wait();

exit 126 if ( $? == -1 );
exit( $? >> 8 );
}
else {
POSIX::setsid();
close($pty);

STDOUT->fdopen( $pty->slave, '>' ) || die $!;
STDERR->fdopen( \*STDOUT, '>' ) || die $!;

exec(@ARGV) || die "Can't exec @ARGV: $!";
}

# EOF
10 changes: 10 additions & 0 deletions tests/test_helper/pseudo-tty.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#!/usr/bin/env python3

import os, pty, sys


sys.exit(
os.waitstatus_to_exitcode(pty.spawn(sys.argv[1:], lambda fd: os.read(fd, 1024)))
)

# EOF
1 change: 1 addition & 0 deletions vendor/bats-assert
1 change: 1 addition & 0 deletions vendor/bats-core
3 changes: 3 additions & 0 deletions vendor/bats-core-1.11.0/.codespellrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[codespell]
skip = .git,*.pdf,*.svg
ignore-words-list = dne
Loading