diff --git a/.github/workflows/ci_cd_wf.yml b/.github/workflows/ci_cd_wf.yml index 9e95f402..fe5a2b82 100644 --- a/.github/workflows/ci_cd_wf.yml +++ b/.github/workflows/ci_cd_wf.yml @@ -1,29 +1,31 @@ -name : test - -on: - push: - branches: - - main - pull_request_target: - branches: - - main - -jobs: - test: - runs-on : ubuntu-latest - - steps: - - name: Check out repo code - uses: actions/checkout@v3 - - - name: Setup Python - uses: actions/setup-python@v3 - with: - python-version: "3.x" - - - name: Install Dependencies - run: | - python -m pip install -r requirements.txt - - name: Run tests - run: | - python -m pytest calculator.py \ No newline at end of file +name : test + +on: + push: + branches: + - main + +jobs: + test: + runs-on : ubuntu-latest + + steps: + - name: Check out repo code + uses: actions/checkout@v3 + + - name: Setup Python + uses: actions/setup-python@v3 + with: + python-version: "3.x" + + - name: Install Dependencies + run: | + python -m pip install -r requirements.txt + - name: Run tests + run: | + python -m pytest calculator.py + - name: Pytest coverage comment + uses: MishaKav/pytest-coverage-comment@main + with: + pytest-coverage-path: ./pytest-coverage.txt + junitxml-path: ./pytest.xml \ No newline at end of file diff --git a/.gitignore b/.gitignore new file mode 100644 index 00000000..b1cb1601 --- /dev/null +++ b/.gitignore @@ -0,0 +1,161 @@ +# Byte-compiled / optimized / DLL files +__pycache__/ +*.py[cod] +*$py.class + +# C extensions +*.so + +# Distribution / packaging +.Python +build/ +develop-eggs/ +dist/ +downloads/ +eggs/ +.eggs/ +lib/ +lib64/ +parts/ +sdist/ +var/ +wheels/ +share/python-wheels/ +*.egg-info/ +.installed.cfg +*.egg +MANIFEST + +# PyInstaller +# Usually these files are written by a python script from a template +# before PyInstaller builds the exe, so as to inject date/other infos into it. +*.manifest +*.spec + +# Installer logs +pip-log.txt +pip-delete-this-directory.txt + +# Unit test / coverage reports +htmlcov/ +.tox/ +.nox/ +.coverage +.coverage.* +.cache +nosetests.xml +coverage.xml +*.cover +*.py,cover +.hypothesis/ +.pytest_cache/ +cover/ + +# Translations +*.mo +*.pot + +# Django stuff: +*.log +local_settings.py +db.sqlite3 +db.sqlite3-journal + +# Flask stuff: +instance/ +.webassets-cache + +# Scrapy stuff: +.scrapy + +# Sphinx documentation +docs/_build/ + +# PyBuilder +.pybuilder/ +target/ + +# Jupyter Notebook +.ipynb_checkpoints + +# IPython +profile_default/ +ipython_config.py + +# pyenv +# For a library or package, you might want to ignore these files since the code is +# intended to run in multiple environments; otherwise, check them in: +# .python-version + +# pipenv +# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control. +# However, in case of collaboration, if having platform-specific dependencies or dependencies +# having no cross-platform support, pipenv may install dependencies that don't work, or not +# install all needed dependencies. +#Pipfile.lock + +# poetry +# Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control. +# This is especially recommended for binary packages to ensure reproducibility, and is more +# commonly ignored for libraries. +# https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control +#poetry.lock + +# pdm +# Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control. +#pdm.lock +# pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it +# in version control. +# https://pdm.fming.dev/#use-with-ide +.pdm.toml + +# PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm +__pypackages__/ + +# Celery stuff +celerybeat-schedule +celerybeat.pid + +# SageMath parsed files +*.sage.py + +# Environments +.env +.venv +env/ +venv/ +ENV/ +env.bak/ +venv.bak/ + +# Spyder project settings +.spyderproject +.spyproject + +# Rope project settings +.ropeproject + +# mkdocs documentation +/site + +# mypy +.mypy_cache/ +.dmypy.json +dmypy.json + +# Pyre type checker +.pyre/ + +# pytype static type analyzer +.pytype/ + +# Cython debug symbols +cython_debug/ + +# PyCharm +# JetBrains specific template is maintained in a separate JetBrains.gitignore that can +# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore +# and can be added to the global gitignore or merged into this file. For a more nuclear +# option (not recommended) you can uncomment the following to ignore the entire idea folder. +#.idea/ + diff --git a/README.md b/README.md index 174488da..b6f5799d 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,98 @@ -# week1-devops -All Code and Files for Intro to DevOps Week 1 Project +# Week 1 - Project + +# Goal +To Establish a Robust and Reliable Development process by settting version control using Git, implementing comprehensive Unit Tests, and setting up effective workflows to ensure that only properly tested code is merged from other branches into the Master branch. + +## Requirements + +
Link to setup Python 3.9 - https://www.python.org/downloads/
+
Link to setup Git the first time - https://git-scm.com/book/en/v2/Getting-Started-First-Time-Git-Setup
+
Use a Editor of your choice
+
Link to get pip3 - https://pip.pypa.io/en/stable/installation/
+ +## Step 1 - Get the inital start code onto your system + +Git clone initial code from this Github Repo - https://github.com/vj-m/week1-devops/tree/initial_starter_code +``` +git clone git@github.com:vj-m/week1-devops.git +``` +## Step 2 - Add Unit Tests + +``` +def test_add(): + assert add(1,1) == 2 + +def test_sub(): + assert sub(1,1) == 0 + +def test_mul(): + assert mul(1,1) == 1 + +def test_div(): + assert div(2,1) == 2 +``` + +## Step 3 - Create a workflow + +1.) Create a directory called .github/workflows within the directory containing .git +2.) And create a workflow ‘YAML’ file, let's call it ci_cd_wf.yml +3.) Git add and push changes (This automatically invokes the Git actions and sets up the CI/CD Pipeline which on push to the master branch) + +``` +name : test + +on: + push: + branches: + - main + +jobs: + test: + runs-on : ubuntu-latest + + steps: + - name: Check out repo code + uses: actions/checkout@v3 + + - name: Setup Python + uses: actions/setup-python@v3 + with: + python-version: "3.x" + + - name: Install Dependencies + run: | + python -m pip install -r requirements.txt + - name: Run tests + run: | + python -m pytest calculator.py +``` + +## Step 4 - Check our Gitflow with a new branch +
1.) Create a new feature branch to check a case where the push to master or PR fails by making a purposeful mistake in the code to fail a test
+
2.) Create a new feature branch called test
+ +``` +def test_add(): + assert add(1,1) == 0 +``` +
We have changed the test_add method to assert a False
+
3.) Git add, push and commit
+ +## Step 5 - Fix all the errors to make a successful build +
1.) Change back the test_add function
+
2.) Add, commit, and push the new changes to the master branch again
+ +### As the first week of the project comes to a close, it's important to reflect on the progress made so far. + +## Future Scope Recommendations - +
Now that the initial code is in place, it's time to focus on improving its quality. One way to do this is to add more checks such as linters, code coverage, and code quality checks.
+
Another recommended step is to add 'runs on' actions like a pull request. This allows for a more streamlined workflow and can help ensure that the code changes are thoroughly tested before being merged into the main branch.
+ +### As you move forward into Week 2 of the project, keep these next steps in mind. By continually improving the quality of your code, you'll be setting yourself up for success in the long run. + +## Appendix +
This repository has three branches - +### 'main' branch contains the file code for the project +### 'initial_starter_code' contains the inital code to start with +### 'test' - The state of the project with a wrong test added for reference - this is supposed to fail the merge to master process in our workflow +
diff --git a/calculator.py b/calculator.py index fbcead9c..969f08a4 100644 --- a/calculator.py +++ b/calculator.py @@ -1,37 +1,30 @@ # Demo Calculator App For Week 1 Project # Addition Method + def add(a, b): - return (a + b) + return a + b + # Subtract Method def sub(a, b): - return (a - b) + return a - b + # Multiplication def mul(a, b): - return (a * b) + return a * b -# Division -def div(a,b): - return (a/b) -def test_add(): - assert add(1,1) == 2 - -def test_sub(): - assert sub(1,1) == 0 - -def test_mul(): - assert mul(1,1) == 1 +# Division +def div(a, b): + return a / b -def test_div(): - assert div(2,1) == 2 if __name__ == "__main__": # Declare variable and set default values a = 4 b = 2 - print('Sum of ' + str(a) + ' and ' + str(b) + ' is ', add(a, b)) - print('Difference of ' + str(a) + ' and ' + str(b) + ' is ', sub(a, b)) - print('Product of ' + str(a) + ' and ' + str(b) + ' is ', mul(a, b)) - print('Division of ' + str(a) + ' and ' + str(b) + ' is ', div(a, b)) \ No newline at end of file + print("Sum of " + str(a) + " and " + str(b) + " is ", add(a, b)) + print("Difference of " + str(a) + " and " + str(b) + " is ", sub(a, b)) + print("Product of " + str(a) + " and " + str(b) + " is ", mul(a, b)) + print("Division of " + str(a) + " and " + str(b) + " is ", div(a, b)) diff --git a/tests/test_calculator.py b/tests/test_calculator.py new file mode 100644 index 00000000..e104a8af --- /dev/null +++ b/tests/test_calculator.py @@ -0,0 +1,14 @@ +from calculator import add, div, mul, sub + + +def test_add(): + assert add(1, 1) == 2 + +def test_sub(): + assert sub(1, 1) == 0 + +def test_mul(): + assert mul(1, 1) == 1 + +def test_div(): + assert div(2, 1) == 2