Skip to content

Commit

Permalink
Merge pull request #13 from dana-yaish/test-bazel
Browse files Browse the repository at this point in the history
test bazel
  • Loading branch information
dana-yaish authored Jul 28, 2023
2 parents 9153e73 + 14c0ed8 commit f3c7704
Show file tree
Hide file tree
Showing 11 changed files with 158 additions and 38 deletions.
30 changes: 24 additions & 6 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,33 @@ jobs:
run:
runs-on: ubuntu-latest
steps:
- uses: bazelbuild/setup-bazelisk@v2
- name: Checkout
uses: actions/checkout@v2
uses: actions/checkout@v3
- name: Set up Python 3.10
uses: actions/setup-python@v2
uses: actions/setup-python@v4
with:
python-version: '3.10'
- name: Install dependencies
run: pip install -r requirements.txt
- name: Run tests and collect coverage
run: pytest --cov app
- name: Upload coverage to Codecov
uses: codecov/codecov-action@v3
- name: give wrapper permission
run: chmod +x app/projectA/codecov_wrapper.sh
- name: run tests by bazel and generate coverage
run: |
python -m pip install --upgrade pip
pip install virtualenv
virtualenv venv
source venv/bin/activate
pip install codecov-cli
echo "$GCP_CREDENTIALS" > service.json
echo "-----"
bazel build //app/...
bazel coverage --run_under //app/projectA:codecov_wrapper.sh \
--test_output=all \
--action_env=GIT_DIR=${PWD}/$(git rev-parse --git-dir) \
--action_env=CODECOV_TOKEN=${{ secrets.CODECOV_TOKEN }} \
--remote_cache=https://storage.googleapis.com/bazel-test-dana \
--google_credentials=service.json \
--combined_report=lcov //app/...
env:
GCP_CREDENTIALS: ${{ secrets.GCP_CREDENTIALS }}
18 changes: 18 additions & 0 deletions WORKSPACE.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#https://github.com/bazelbuild/rules_python - for using py rules
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
http_archive(
name = "rules_python",
sha256 = "84aec9e21cc56fbc7f1335035a71c850d1b9b5cc6ff497306f84cced9a769841",
strip_prefix = "rules_python-0.23.1",
url = "https://github.com/bazelbuild/rules_python/releases/download/0.23.1/rules_python-0.23.1.tar.gz",
)

load("@rules_python//python:repositories.bzl", "py_repositories", "python_register_toolchains")

py_repositories()

python_register_toolchains(
name = "python39",
python_version = "3.9",
register_coverage_tool = True,
)
15 changes: 15 additions & 0 deletions app/projectA/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
py_library(
name="calculator",
srcs=["calculator.py"],

)

py_test(
name="test_calculator",
srcs=["test_calculator.py"],
deps = [
"//app/projectA:calculator",
]
)

exports_files(["codecov_wrapper.sh"])
File renamed without changes.
16 changes: 16 additions & 0 deletions app/projectA/codecov_wrapper.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#/bin/bash

# `bazel coverage --run_under foo :bar` basically translates to:
# foo pytest --cov bar/src/a.py bar/src/b.py
# We want to run that `pytest` command unmodified, as below:
"$@"

# codecov doesn't recognise pylcov.dat as a coverage report, renaming it to lcov.dat so codecov can acknowledge it
mv $COVERAGE_DIR/pylcov.dat $COVERAGE_DIR/lcov.dat

# uploading coverage
codecovcli -v create-commit -t $CODECOV_TOKEN
codecovcli -v create-report -t $CODECOV_TOKEN
codecovcli -v do-upload -t $CODECOV_TOKEN -s $COVERAGE_DIR


40 changes: 40 additions & 0 deletions app/projectA/test_calculator.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@

from app.projectA.calculator import Calculator

import unittest



class TestMethods(unittest.TestCase):
def test_add(self):
self.assertEqual(True, True)

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

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

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

def test_divide(self):
self.assertEqual(Calculator.divide(1.0, 2.0) , 0.5)
self.assertEqual(Calculator.divide(0, 2.0) , 0)
self.assertEqual(Calculator.divide(-4, 2.0) , -2.0)

if __name__ == "__main__":
unittest.main()
15 changes: 15 additions & 0 deletions app/projectB/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
py_library(
name="adder",
srcs=["adder.py"],

)

py_test(
name="test_adder",
srcs=["test_adder.py"],
deps = [
"//app/projectB:adder",
]
)

exports_files(["codecov_wrapper.sh"])
3 changes: 3 additions & 0 deletions app/projectB/adder.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
class Adder:
def add(self, x, y):
return x + y
16 changes: 16 additions & 0 deletions app/projectB/codecov_wrapper.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#/bin/bash

# `bazel coverage --run_under foo :bar` basically translates to:
# foo pytest --cov bar/src/a.py bar/src/b.py
# We want to run that `pytest` command unmodified, as below:
"$@"

# codecov doesn't recognise pylcov.dat as a coverage report, renaming it to lcov.dat so codecov can acknowledge it
mv $COVERAGE_DIR/pylcov.dat $COVERAGE_DIR/lcov.dat

# uploading coverage
codecovcli -v create-commit -t $CODECOV_TOKEN
codecovcli -v create-report -t $CODECOV_TOKEN
codecovcli -v do-upload -t $CODECOV_TOKEN -s $COVERAGE_DIR


11 changes: 11 additions & 0 deletions app/projectB/test_adder.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import unittest

from app.projectB.adder import Adder


class TestAdder(unittest.TestCase):
def test_adder(self):
assert 2 + 3.0 == Adder().add(2, 3)

if __name__ == "__main__":
unittest.main()
32 changes: 0 additions & 32 deletions app/test_calculator.py

This file was deleted.

0 comments on commit f3c7704

Please sign in to comment.