Skip to content

Commit

Permalink
Merge pull request #4 from codecov/th/make-run-script
Browse files Browse the repository at this point in the history
feat: make scripts runnable by default
  • Loading branch information
thomasrockhu-codecov authored Oct 1, 2024
2 parents c0d5281 + c90898f commit f492793
Show file tree
Hide file tree
Showing 11 changed files with 126 additions and 33 deletions.
21 changes: 21 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
name: Workflow for Codecov wrapper
on: [push, pull_request]
jobs:
run:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Set up Python 3.10
uses: actions/setup-python@v4
with:
python-version: '3.10'
- name: Install dependencies
run: pip install -r app/requirements.txt
- name: Run tests and collect coverage
run: pytest --cov app ${{ env.CODECOV_ATS_TESTS }}
- name: Upload coverage to Codecov
run: ./run.sh
env:
CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }}

2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
__pycache__/
.py[oc]
Empty file added app/__init__.py
Empty file.
15 changes: 15 additions & 0 deletions app/calculator.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
class Calculator:

def add(x, y):
return x + y

def subtract(x, y):
return x - y

def multiply(x, y):
return x * y

def divide(x, y):
if y == 0:
return 'Cannot divide by 0'
return x * 1.0 / y
1 change: 1 addition & 0 deletions app/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pytest-cov
31 changes: 31 additions & 0 deletions app/test_calculator.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
from .calculator import Calculator


def test_add():
assert Calculator.add(1, 2) == 3.0
assert Calculator.add(1.0, 2.0) == 3.0
assert Calculator.add(0, 2.0) == 2.0
assert Calculator.add(2.0, 0) == 2.0
assert Calculator.add(-4, 2.0) == -2.0

def test_subtract():
assert Calculator.subtract(1, 2) == -1.0
assert Calculator.subtract(2, 1) == 1.0
assert Calculator.subtract(1.0, 2.0) == -1.0
assert Calculator.subtract(0, 2.0) == -2.0
assert Calculator.subtract(2.0, 0.0) == 2.0
assert Calculator.subtract(-4, 2.0) == -6.0

def test_multiply():
assert Calculator.multiply(1, 2) == 2.0
assert Calculator.multiply(1.0, 2.0) == 2.0
assert Calculator.multiply(0, 2.0) == 0.0
assert Calculator.multiply(2.0, 0.0) == 0.0
assert Calculator.multiply(-4, 2.0) == -8.0

def test_divide():
# assert Calculator.divide(1, 2) == 0.5
assert Calculator.divide(1.0, 2.0) == 0.5
assert Calculator.divide(0, 2.0) == 0
assert Calculator.divide(-4, 2.0) == -2.0
# assert Calculator.divide(2.0, 0.0) == 'Cannot divide by 0'
14 changes: 8 additions & 6 deletions download.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
CODECOV_WRAPPER_VERSION="0.0.1"
echo "Codecov wrapper version: ${CODECOV_WRAPPER_VERSION}"

family=$(uname -s | tr '[:upper:]' '[:lower:]')
Expand All @@ -11,16 +10,19 @@ codecov_os="windows"
[[ $osID == "alpine" ]] && codecov_os="alpine"
[[ $(arch) == "aarch64" && $family == "linux" ]] && codecov_os+="-arm64"
echo "Detected ${codecov_os}"
echo "export codecov_os=${codecov_os}" >> $BASH_ENV
echo "export codecov_version=${PARAM_VERSION}" >> $BASH_ENV
export codecov_os=${codecov_os}
export codecov_version=${CODECOV_VERSION}

codecov_filename="codecov"
[[ $codecov_os == "windows" ]] && codecov_filename+=".exe"
echo "export codecov_filename=${codecov_filename}" >> $BASH_ENV
export codecov_filename=${codecov_filename}
[[ $codecov_os == "macos" ]] && \
HOMEBREW_NO_AUTO_UPDATE=1 brew install gpg
codecov_url="https://cli.codecov.io"
codecov_url="$codecov_url/${PARAM_VERSION}"
codecov_url="$codecov_url/${CODECOV_VERSION}"
codecov_url="$codecov_url/${codecov_os}/${codecov_filename}"
echo "Downloading ${codecov_url}"
curl -Os $codecov_url -v
curl -Os $codecov_url

echo "Finishing downloading CLI ${CODECOV_VERSION}"
echo ""
4 changes: 4 additions & 0 deletions run.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
. ./set_defaults.sh
. ./download.sh
. ./validate.sh
. ./upload.sh
21 changes: 21 additions & 0 deletions set_defaults.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
home_dir=$(bash -c "cd ~$(printf %q $USER) && pwd")
CODECOV_BASH_ENV="${CODECOV_BASH_ENV:-$home_dir/.bashrc}"

CODECOV_WRAPPER_VERSION="0.0.1"
CODECOV_VERSION="${CODECOV_VERSION:-latest}"
CODECOV_UPLOAD_NAME="${CODECOV_UPLOAD_NAME:-$CIRCLE_BUILD_NUM}"

codecov_vars=(
CODECOV_WRAPPER_VERSION
CODECOV_VERSION
)

echo "Running wrapper version $CODECOV_WRAPPER_VERSION"
for value in "${codecov_vars[@]}"
do
echo "==> \$$value=${!value}"
export $value=${!value}
done

echo "Finishing setting env variables"
echo ""
47 changes: 21 additions & 26 deletions upload.sh
Original file line number Diff line number Diff line change
@@ -1,20 +1,15 @@
unset NODE_OPTIONS
# See https://github.com/codecov/uploader/issues/475
source $BASH_ENV

chmod +x $codecov_filename
[ -n "${PARAM_FILE}" ] && \
set - "${@}" "-f" "${PARAM_FILE}"
[ -n "${PARAM_UPLOAD_ARGS}" ] && \
set - "${@}" "${PARAM_UPLOAD_ARGS}"

if [ -z "${PARAM_UPLOAD_NAME}" ]; then
PARAM_UPLOAD_NAME="${CIRCLE_BUILD_NUM}"
fi
[ -n "${CODECOV_FILE}" ] && \
set - "${@}" "-f" "${CODECOV_FILE}"
[ -n "${CODECOV_UPLOAD_ARGS}" ] && \
set - "${@}" "${CODECOV_UPLOAD_ARGS}"

FLAGS=""
OLDIFS=$IFS;IFS=,
for flag in $PARAM_FLAGS; do
for flag in $CODECOV_FLAGS; do
eval e="\$$flag"
for param in "${e}" "${flag}"; do
if [ -n "${param}" ]; then
Expand All @@ -34,33 +29,33 @@ if [ -n "$FLAGS" ]; then
fi

#create commit
echo "./\"$codecov_filename\" ${PARAM_CLI_ARGS} create-commit -t <redacted>"
echo "./\"$codecov_filename\" ${CODECOV_CLI_ARGS} create-commit -t $CODECOV_TOKEN"

./"$codecov_filename" \
${PARAM_CLI_ARGS} \
./$codecov_filename \
${CODECOV_CLI_ARGS} \
create-commit \
-t "$(eval echo \$$PARAM_TOKEN)" \
${PARAM_COMMIT_ARGS}
-t "$(eval echo $CODECOV_TOKEN)" \
${CODECOV_COMMIT_ARGS}

#create report
echo "./\"$codecov_filename\" ${PARAM_CLI_ARGS} create-report -t <redacted>"
echo "./\"$codecov_filename\" ${CODECOV_CLI_ARGS} create-report -t <redacted>"

./"$codecov_filename" \
${PARAM_CLI_ARGS} \
./$codecov_filename \
${CODECOV_CLI_ARGS} \
create-report \
-t "$(eval echo \$$PARAM_TOKEN)" \
${PARAM_REPORT_ARGS}
-t "$(eval echo $CODECOV_TOKEN)" \
${CODECOV_REPORT_ARGS}

#upload reports
# alpine doesn't allow for indirect expansion

echo "./${codecov_filename} ${PARAM_CLI_ARGS} do-upload -t <redacted> -n \"${PARAM_UPLOAD_NAME}\" ${FLAGS} ${PARAM_UPLOAD_ARGS} ${@}"
echo "./${codecov_filename} ${CODECOV_CLI_ARGS} do-upload -Z -t <redacted> ${CODECOV_UPLOAD_ARGS} ${@}"

./"$codecov_filename" \
${PARAM_CLI_ARGS} \
./$codecov_filename \
${CODECOV_CLI_ARGS} \
do-upload \
-t "$(eval echo \$$PARAM_TOKEN)" \
-n "${PARAM_UPLOAD_NAME}" \
-Z \
-t "$(eval echo $CODECOV_TOKEN)" \
${FLAGS} \
${PARAM_UPLOAD_ARGS} \
${CODECOV_UPLOAD_ARGS} \
${@}
3 changes: 2 additions & 1 deletion validate.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
source "$BASH_ENV"
echo "${CODECOV_PUBLIC_PGP_KEY}" | \
gpg --no-default-keyring --import
# One-time step
Expand All @@ -8,6 +7,8 @@ sha_url="${sha_url}/${codecov_filename}.SHA256SUM"
echo "Downloading ${sha_url}"
curl -Os "$sha_url"
curl -Os "${sha_url}.sig"

gpg --verify "${codecov_filename}.SHA256SUM.sig" "${codecov_filename}.SHA256SUM"
shasum -a 256 -c "${codecov_filename}.SHA256SUM" || \
sha256sum -c "${codecov_filename}.SHA256SUM"
echo ""

0 comments on commit f492793

Please sign in to comment.