Skip to content

Commit 60d3b8e

Browse files
committed
initial git hook pre push and notebook test
1 parent a933c4d commit 60d3b8e

File tree

4 files changed

+139
-0
lines changed

4 files changed

+139
-0
lines changed

.githooks/pre-push

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#!/bin/bash
2+
# .githooks/pre-push
3+
4+
# Navigate to the repository root (relative to the hook location)
5+
cd "$(dirname "$0")/.."
6+
7+
# Activate the virtual environment (if needed)
8+
VENV_PATH=$(find . -type d -name "ml_grid_env")
9+
if [ -n "$VENV_PATH" ]; then
10+
source "$VENV_PATH/bin/activate"
11+
else
12+
echo "Virtual environment not found. Exiting."
13+
exit 1
14+
fi
15+
16+
# Run the same test command as in the GitHub Actions workflow
17+
echo "Running tests before pushing..."
18+
pytest notebooks/test_notebook.py
19+
20+
# Check the exit status of the test command
21+
if [ $? -ne 0 ]; then
22+
echo "Tests failed. Push aborted."
23+
exit 1
24+
fi
25+
26+
echo "All checks passed. Proceeding with push."
27+
exit 0

.github/workflows/notebook-test.yml

+91
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
name: ML Project Test
2+
3+
on: [push, pull_request]
4+
5+
jobs:
6+
test:
7+
runs-on: ubuntu-latest
8+
env:
9+
DEBIAN_FRONTEND: noninteractive
10+
11+
steps:
12+
- name: Check out repository
13+
uses: actions/checkout@v3
14+
15+
- name: Install Act dependencies
16+
if: ${{ env.ACT }}
17+
run: |
18+
apt-get update && apt-get install sudo -y
19+
20+
- name: Install ping utility
21+
run: |
22+
sudo apt-get update
23+
sudo apt-get install -y iputils-ping
24+
25+
- name: Set timezone to UTC
26+
run: |
27+
export DEBIAN_FRONTEND=noninteractive
28+
if [ -n "$ACT" ]; then
29+
ln -fs /usr/share/zoneinfo/Etc/UTC /etc/localtime
30+
echo "Etc/UTC" > /etc/timezone
31+
apt-get update
32+
apt-get install -y tzdata
33+
dpkg-reconfigure -f noninteractive tzdata
34+
else
35+
sudo ln -fs /usr/share/zoneinfo/Etc/UTC /etc/localtime
36+
echo "Etc/UTC" | sudo tee /etc/timezone
37+
sudo apt-get update
38+
sudo apt-get install -y tzdata
39+
sudo dpkg-reconfigure -f noninteractive tzdata
40+
fi
41+
42+
- name: Install Python 3.10 and Git
43+
run: |
44+
export DEBIAN_FRONTEND=noninteractive
45+
sudo apt-get update
46+
sudo apt-get install -y gnupg lsb-release software-properties-common curl git
47+
48+
echo "deb [trusted=yes] http://ppa.launchpadcontent.net/deadsnakes/ppa/ubuntu $(lsb_release -cs) main" | sudo tee /etc/apt/sources.list.d/deadsnakes-ppa.list
49+
sudo apt-get update
50+
sudo apt-get install -y python3.10 python3.10-venv python3.10-distutils
51+
52+
# Set Python 3.10 as default
53+
ln -sf /usr/bin/python3.10 /usr/local/bin/python
54+
ln -sf /usr/bin/python3.10 /usr/local/bin/python3
55+
56+
curl -sS https://bootstrap.pypa.io/get-pip.py | python3.10
57+
python3.10 -m ensurepip --upgrade
58+
ln -sf /usr/local/bin/pip3.10 /usr/local/bin/pip
59+
ln -sf /usr/local/bin/pip3.10 /usr/local/bin/pip3
60+
61+
python --version
62+
pip --version
63+
64+
- name: Set environment variables
65+
run: |
66+
echo "PYTHONHTTPSVERIFY=0" >> $GITHUB_ENV
67+
echo "CURL_CA_BUNDLE=" >> $GITHUB_ENV
68+
echo "SSL_CERT_FILE=" >> $GITHUB_ENV
69+
echo "GIT_SSL_NO_VERIFY=true" >> $GITHUB_ENV
70+
71+
- name: Setup ML project
72+
run: |
73+
cd $GITHUB_WORKSPACE
74+
# Make install.sh executable if it isn't already
75+
chmod +x install.sh
76+
# Run the installation script
77+
./install.sh
78+
79+
- name: Debug virtual environment
80+
run: |
81+
VENV_PATH=$(find $GITHUB_WORKSPACE -type d -name "ml_grid_env")
82+
echo "VENV_PATH=$VENV_PATH" >> $GITHUB_ENV
83+
source "$VENV_PATH/bin/activate"
84+
which python
85+
python --version
86+
87+
- name: Run tests
88+
run: |
89+
cd $GITHUB_WORKSPACE
90+
source "$VENV_PATH/bin/activate"
91+
pytest notebooks/unit_test_synthetic.ipynb

notebooks/test_notebook.py

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import pytest
2+
import nbformat
3+
from nbconvert.preprocessors import ExecutePreprocessor
4+
5+
6+
def test_notebook():
7+
with open("notebooks/unit_test_synthetic.ipynb") as f:
8+
nb = nbformat.read(f, as_version=4)
9+
10+
ep = ExecutePreprocessor(timeout=600, kernel_name="ml_grid_env")
11+
ep.preprocess(nb) # Will raise an error if the notebook fails

setup-hooks.sh

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#!/bin/bash
2+
# setup-hooks.sh
3+
4+
# Set the core.hooksPath to .githooks
5+
git config core.hooksPath .githooks
6+
7+
# Make the pre-push hook executable
8+
chmod +x .githooks/pre-push
9+
10+
echo "Git hooks configured successfully."

0 commit comments

Comments
 (0)