Skip to content

Commit

Permalink
Add bisect exercise
Browse files Browse the repository at this point in the history
In this example we will be doing a bisect
  • Loading branch information
oyvindeide committed Sep 10, 2023
1 parent 4fa93d1 commit 24d8100
Show file tree
Hide file tree
Showing 6 changed files with 122 additions and 4 deletions.
3 changes: 3 additions & 0 deletions .devcontainer/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# [Choice] Python version (use -bullseye variants on local arm64/Apple Silicon): 3, 3.10, 3.9, 3.8, 3.7, 3.6, 3-bullseye, 3.10-bullseye, 3.9-bullseye, 3.8-bullseye, 3.7-bullseye, 3.6-bullseye, 3-buster, 3.10-buster, 3.9-buster, 3.8-buster, 3.7-buster, 3.6-buster
ARG VARIANT=3-bullseye
FROM mcr.microsoft.com/vscode/devcontainers/python:${VARIANT}
46 changes: 42 additions & 4 deletions .devcontainer/devcontainer.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,43 @@
// For format details, see https://aka.ms/devcontainer.json. For config options, see the README at:
// https://github.com/microsoft/vscode-dev-containers/tree/v0.238.1/containers/python-3
{
"image": "mcr.microsoft.com/devcontainers/base:ubuntu",
"postCreateCommand": "bash scripts/install.sh"
}

"name": "Python 3",
"build": {
"dockerfile": "Dockerfile",
"context": "..",
"args": {
// Update 'VARIANT' to pick a Python version: 3, 3.10, 3.9, 3.8, 3.7, 3.6
// Append -bullseye or -buster to pin to an OS version.
// Use -bullseye variants on local on arm64/Apple Silicon.
"VARIANT": "3.10-bullseye",
// Options
"NODE_VERSION": "none"
}
},
"customizations": {
"vscode": {
// Set *default* container specific settings.json values on container create.
"settings": {
"python.defaultInterpreterPath": "/usr/local/bin/python",
"python.linting.enabled": true,
"python.linting.pylintEnabled": true,
"python.formatting.autopep8Path": "/usr/local/py-utils/bin/autopep8",
"python.formatting.blackPath": "/usr/local/py-utils/bin/black",
"python.formatting.yapfPath": "/usr/local/py-utils/bin/yapf",
"python.linting.banditPath": "/usr/local/py-utils/bin/bandit",
"python.linting.flake8Path": "/usr/local/py-utils/bin/flake8",
"python.linting.mypyPath": "/usr/local/py-utils/bin/mypy",
"python.linting.pycodestylePath": "/usr/local/py-utils/bin/pycodestyle",
"python.linting.pydocstylePath": "/usr/local/py-utils/bin/pydocstyle",
"python.linting.pylintPath": "/usr/local/py-utils/bin/pylint"
},
// Add the IDs of extensions you want installed when the container is created.
"extensions": [
"ms-python.python",
"ms-python.vscode-pylance"
]
}
},
"postCreateCommand": "bash scripts/install.sh", // Comment out to connect as root instead. More info: https://aka.ms/vscode-remote/containers/non-root.
"remoteUser": "vscode"
}
59 changes: 59 additions & 0 deletions scripts/create_example_7.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
#!/bin/bash

pushd examples
git init example_7
cp ../template/add.py example_7/add.py
cp ../template/check_function.sh example_7/check_function.sh

pushd example_7

# Initial commit
git add add.py
commit_msg="Add bisect exercise
In this example we will be doing a bisect, there will
be a very long history, where you will not be able to
tell from the commit message where a bug has been
introduced, however at one point the python function has
changed from returning a + b -> a * b, your task is to
use bisect to figure out in which commit that happened
You have a helper script, check_function.sh which you
can use to determine if the commit is good or bad."

git commit -m "$commit_msg"

last_var="<random>"
random_commits=$(( $RANDOM % 2000 + 500 ))
# Add random text
for ((i=1;i<=$random_commits;i++))
do
var=$RANDOM
sed -i "s/$last_var/$var/" add.py
git add add.py
git commit -m "$var"
last_var=$var
done

sed -i "s/[+]/*/" add.py
git add add.py

commit_msg="$RANDOM
Congratulations, you found me"

git commit -m "$commit_msg"


random_commits=$(( $RANDOM % 2000 + 500 ))
for ((i=1;i<=$random_commits;i++))
do
var=$RANDOM
sed -i "s/$last_var/$var/" add.py
git add add.py
git commit -m "$var"
last_var=$var
done

popd
popd
1 change: 1 addition & 0 deletions scripts/install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ mkdir examples
./scripts/create_example_3.sh
./scripts/create_example_4.sh
./scripts/create_example_5.sh
./scripts/create_example_7.sh
14 changes: 14 additions & 0 deletions template/add.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#!/usr/bin/env python

import sys


def add(a, b):
return a + b # <random>


if __name__ == "__main__":
result = add(2, 3)
if result == 5:
sys.exit(0)
sys.exit(f"Wrong result, should be 5, is {result}")
3 changes: 3 additions & 0 deletions template/check_function.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/bin/bash

python add.py

0 comments on commit 24d8100

Please sign in to comment.