Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions lessons/03-set-up-your-machine/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -114,14 +114,19 @@ gh adds `upstream` for you because it knows your repo is a fork.

## Step 6: Get the latest changes, then run the doctor

Other learners' commits have landed since you forked. Bring your copy up to
date, then run the course's checker:
Other learners' commits have landed since you forked. Download them, then
run the course's checker:

```bash norun
git pull upstream main
git fetch upstream
python3 tools/doctor.py
```

`git fetch` only downloads: it changes none of your files. (Why not
`git pull`? Your `main` holds your first commit, which may not be merged
yet, and git refuses to pull when both sides have new commits. In Lesson 05
you start a branch from what you just fetched.)

Each line should be a green ✔:

```text
Expand Down
2 changes: 1 addition & 1 deletion lessons/03-set-up-your-machine/exercise.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
- [ ] Set `user.name`, `user.email` and `init.defaultBranch` with `git config --global`
- [ ] `gh auth login`
- [ ] `gh repo clone YOUR-USERNAME/first-python-contribution` and `cd` into it
- [ ] `git pull upstream main`
- [ ] `git fetch upstream`
- [ ] `python3 tools/doctor.py` shows only green ✔

**Stuck on a red ✘?** The message says what to fix. If it still fails,
Expand Down
6 changes: 3 additions & 3 deletions lessons/03-set-up-your-machine/script.md
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ visual: terminal
focus: 1 | 2 | 3-10 | 12
code:
```
$ git pull upstream main
$ git fetch upstream
$ python3 tools/doctor.py
✔ Python 3.13.1
✔ venv module available
Expand All @@ -191,8 +191,8 @@ $ python3 tools/doctor.py
All set. You're ready for Lesson 04.
```

> Last step. Other learners' commits have landed since you forked. So pull
> from upstream, to bring your copy up to date.
> Last step. Other learners' commits have landed since you forked. So fetch
> from upstream. That downloads them, and changes none of your files.

> Then run the course's checker. Python three, tools, slash, doctor
> {.py|dot p y}.
Expand Down
23 changes: 16 additions & 7 deletions lessons/05-second-pr-from-terminal/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,12 @@ if you get it wrong.)

```bash norun
cd first-python-contribution
git switch main
git pull upstream main
git fetch upstream
```

(Commands in this lesson are marked as not run in CI because they need your
fork.) Always start new work from an up-to-date `main`.
fork.) `git fetch` downloads the original repo's newest commits without
changing your files. You start your branch from them in Step 3.

## Step 2: Make a virtual environment

Expand All @@ -57,10 +57,14 @@ never commits it.
## Step 3: Make a branch

```bash norun
git switch -c exercise-2
git switch -c exercise-2 upstream/main
```

Never work on `main` directly. One branch per pull request.
`upstream/main` is the original repo's newest commit, as you just fetched
it. **Always start new work from there**, not from your own `main`: your
`main` still holds your first commit from Lesson 02, which may not be merged
yet, and it would come along into this pull request. One branch per pull
request, one change per branch. Never work on `main` directly.

## Step 4: Write the test first

Expand Down Expand Up @@ -155,8 +159,8 @@ git commit -m "Add exercise 2 greeting for ada-lovelace"
```

`git status` should show only your two new files. Add exactly those. (If
you see `.venv/` listed, your `.gitignore` is missing: run
`git pull upstream main` again.)
you see `.venv/` listed, your `.gitignore` is missing: check that you made
the branch from `upstream/main` in Step 3.)

## Step 8: Push your branch to your fork

Expand Down Expand Up @@ -240,4 +244,9 @@ git branch -d exercise-2

`main` now includes your merged work, so the `exercise-2` branch can go.

If `git pull` says you have **divergent branches**, your Lesson 02 pull
request isn't merged yet. Leave `main` as it is and try again after the
merge. [Troubleshooting, section 8](../A-troubleshooting/#8-your-fork-is-out-of-date)
explains why.

Next: [Lesson 06: house rules](../06-house-rules/), the rules every project expects you to read first.
4 changes: 2 additions & 2 deletions lessons/05-second-pr-from-terminal/exercise.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# Exercise 05: A function, a test, a green CI

- [ ] `git switch main` and `git pull upstream main`
- [ ] `git fetch upstream`
- [ ] Create and activate `.venv`, then `python -m pip install pytest ruff`
- [ ] `git switch -c exercise-2`
- [ ] `git switch -c exercise-2 upstream/main`
- [ ] Write `practice/tests/test_YOUR_NAME.py` and watch it fail
- [ ] Write `practice/greetings/YOUR_NAME.py` and watch it pass
- [ ] `ruff format practice` and `ruff check practice` are clean
Expand Down
31 changes: 17 additions & 14 deletions lessons/05-second-pr-from-terminal/script.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,24 +61,20 @@ bullets:

## Scene 4: Start from the latest upstream
visual: terminal
focus: 1 | 2 | 3
focus: 1 | 2
notes:
- Go into your copy of the course repo
- Always start new work from `main`
- `upstream` is the original repo. This gets its newest commits.
- `upstream` is the original repo. This downloads its newest commits.
code:
```
$ cd first-python-contribution
$ git switch main
$ git pull upstream main
$ git fetch upstream
```

> Step one. Open your terminal, and go into the course folder.

> Switch to main.

> Then pull from upstream. Now your main is up to date. Always start new work
> from an up to date main.
> Then fetch from upstream. That downloads the original repo's newest
> commits, and changes none of your files.

## Scene 5: Make a virtual environment
visual: terminal
Expand Down Expand Up @@ -108,19 +104,26 @@ $ source .venv/bin/activate

## Scene 6: Make a branch
visual: terminal
focus: 1 | 1-2
focus: 1 | 1 | 1-3
notes:
- `-c` means create
- `-c` means create. `upstream/main` is where the branch starts.
- Your own `main` may still hold your first commit. This leaves it out.
- One branch per pull request. Never work on `main`.
code:
```
(.venv) $ git switch -c exercise-2
(.venv) $ git switch -c exercise-2 upstream/main
Switched to a new branch 'exercise-2'
branch 'exercise-2' set up to track 'upstream/main'.
```

> Step three. Make a branch. Git switch, dash c, {exercise-2|exercise two}.
> Step three. Make a branch. Git switch, dash c, {exercise-2|exercise two},
> upstream slash main.

> That starts the branch from the original repo's newest commit. Your own
> main branch may still hold your first commit, waiting to be merged. This
> way, it stays out of your new pull request.

> Never work on main directly. One branch per pull request.
> Always start new work like this. One branch per pull request.

## Scene 7: Write the test first
visual: code
Expand Down
7 changes: 5 additions & 2 deletions lessons/09-opening-an-issue/exercise.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
`bug-hunt/splitbill.py` has **five** bugs on purpose. Find one and report it
the way a maintainer would want.

- [ ] Get the latest course files: `git switch main` and `git pull upstream main`
- [ ] In the course folder, check the bug hunt is there: `ls bug-hunt`. (Missing? Your fork is older than this
lesson: `git fetch upstream`, then `git switch -c bug-hunt upstream/main`.)
- [ ] Read [`bug-hunt/README.md`](../../bug-hunt/README.md): it's the spec, what splitbill *should* do
- [ ] Try splitbill with different bills, people, tips and tax, and compare with the README
- [ ] Found something different? Make it **minimal**: the smallest numbers and fewest options that still show it
Expand All @@ -16,7 +17,9 @@ the way a maintainer would want.

- [ ] Search the [issues](https://github.com/thejesh23/first-python-contribution/issues) for your bug. Here a
duplicate is fine, since other learners hunt the same bugs; in a real project, add to the existing issue instead
- [ ] Open **Issues → New issue → Bug hunt: report a splitbill bug**, fill in every field, and submit
- [ ] Open **Issues → New issue → Bug hunt: report a splitbill bug**, fill in every field, and submit.
Use the form in the browser, not `gh issue create`: the form adds the `bug-hunt` label the bot looks for,
and only maintainers can add labels from the command line
- [ ] Read the bot's comment. Fix any ✘ by editing your issue (**···** → **Edit**); the bot checks again when you save

Please **don't** open a pull request that fixes the bug: the next learner
Expand Down
5 changes: 2 additions & 3 deletions lessons/10-making-the-change/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,8 @@ request for it, because the next learner needs the bug.
## Step 1: Branch, then reproduce

```bash norun
git switch main
git pull upstream main
git switch -c fix-slugify
git fetch upstream
git switch -c fix-slugify upstream/main
cd practice
python -c "from textkit import slugify; print(repr(slugify('Hello World')))"
cd ..
Expand Down
2 changes: 1 addition & 1 deletion lessons/10-making-the-change/exercise.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
Exercise 3 happens on your machine. **Don't open a pull request**: the bug
has to stay for the next learner.

- [ ] `git switch main`, `git pull upstream main`, `git switch -c fix-slugify`
- [ ] `git fetch upstream`, then `git switch -c fix-slugify upstream/main`
- [ ] Reproduce the bug from the lesson's bug report
- [ ] Add a parametrized test to `practice/tests/test_textkit.py` and watch it fail
- [ ] Fix `slugify` in `practice/textkit.py`, and update its docstring
Expand Down
4 changes: 2 additions & 2 deletions lessons/10-making-the-change/script.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,12 @@ bullets:
visual: terminal
focus: 1 | 2-3 | 4
notes:
- A new branch for the fix
- A new branch for the fix, from the newest upstream (`git fetch upstream` first)
- Call the function yourself
- Two hyphens. The bug is real.
code:
```
$ git switch -c fix-slugify
$ git switch -c fix-slugify upstream/main
$ cd practice
$ python -c "from textkit import slugify; print(repr(slugify('Hello World')))"
'hello--world'
Expand Down
17 changes: 14 additions & 3 deletions lessons/A-troubleshooting/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ read the fix.
| Wrong files or message in your last commit | [7. Fix your last commit](#7-fix-your-last-commit) |
| `This branch has conflicts that must be resolved` | [Lesson 12](../12-ci-and-review/) |
| Your fork says "X commits behind" | [8. Fork out of date](#8-your-fork-is-out-of-date) |
| `fatal: Need to specify how to reconcile divergent branches` | [8. Fork out of date](#8-your-fork-is-out-of-date) |
| `nothing to commit, working tree clean` but you changed something | [9. Nothing to commit](#9-nothing-to-commit) |
| `error: externally-managed-environment` | [10. Python environment](#10-python-and-pip-problems) |
| `No module named pytest` / `command not found: pytest` | [10. Python environment](#10-python-and-pip-problems) |
Expand Down Expand Up @@ -240,9 +241,19 @@ git push origin main
Or without touching your computer: `gh repo sync YOUR-USERNAME/REPO`, or the
**Sync fork** button on your fork's page.

If `git pull upstream main` says your branches have **diverged** (common
after a project squash-merged your PR), and your fork's `main` has nothing you
need, reset it to match upstream:
If `git pull upstream main` stops with `Need to specify how to reconcile
divergent branches`, your `main` and the original's `main` each have commits
the other lacks.

**In this course**, that means your Lesson 02 pull request isn't merged yet:
its commit is on your `main`. Don't reset anything, because that pull
request is made from your fork's `main`. You don't need the pull: start new
work with `git fetch upstream` and `git switch -c my-branch upstream/main`,
as Lesson 05 does. After the merge, `git pull upstream main` works again.

**In a real project**, it's common after your PR was squash-merged. If your
fork's `main` has nothing you need and no open pull request is made from it,
reset it to match upstream:

```bash norun
git switch main
Expand Down
52 changes: 52 additions & 0 deletions tests/test_check_exercise_2.py
Original file line number Diff line number Diff line change
Expand Up @@ -171,3 +171,55 @@ def git(*args):
code = main(["--login", "ada", "--base", base, "--head", "HEAD", "--tests", "success"])
assert code == NOT_A_LEARNER_PR
assert capsys.readouterr().out == ""


def _repo(tmp_path):
def git(*args):
return subprocess.run(["git", *args], cwd=tmp_path, check=True, capture_output=True, text=True).stdout.strip()

git("init", "-q", "-b", "main")
git("config", "user.email", "t@example.com")
git("config", "user.name", "t")
for folder in ("practice/greetings", "practice/tests", "first-commits"):
(tmp_path / folder).mkdir(parents=True)
(tmp_path / "practice/README.md").write_text("hi\n")
git("add", ".")
git("commit", "-qm", "base")
return git


FIRST_COMMIT = 'name = "Ada"\ngithub = "ada"\nmessage = "Hello"\n'


def test_first_commit_carried_along_is_accepted_and_explained(tmp_path, monkeypatch):
"""PR #47: the first PR isn't merged yet, and the Exercise 2 branch started from the fork's main."""
git = _repo(tmp_path)
base = git("rev-parse", "HEAD")
(tmp_path / "first-commits/ada.py").write_text(FIRST_COMMIT)
git("add", ".")
git("commit", "-qm", "first commit, on the fork's main")
(tmp_path / "practice/greetings/ada.py").write_text(GREETING)
(tmp_path / "practice/tests/test_ada.py").write_text(TEST.replace("thejesh23", "ada"))
git("add", ".")
git("commit", "-qm", "exercise 2")
monkeypatch.chdir(tmp_path)

result = validate("ada", base, git("rev-parse", "HEAD"))
assert result.ok, result.errors
comment = format_comment(result, "ada", "success", "https://run")
assert "maintainer will review" in comment
assert "first-commits/ada.py" in comment and "upstream/main" in comment

# The carried file gets the first-commit checks too.
(tmp_path / "first-commits/ada.py").write_text('name = "Ada"\ngithub = "ada"\nmessage = "Hello\n')
git("commit", "-qam", "break the quote")
errors = validate("ada", base, git("rev-parse", "HEAD")).errors
assert any("first-commit file" in e and "valid Python" in e for e in errors), errors


def test_someone_elses_first_commit_file_is_still_rejected():
changes = [("A", "practice/greetings/ada.py"), ("A", "practice/tests/test_ada.py"), ("A", "first-commits/bob.py")]
result = check_changes(changes, "ada")
assert any("first-commits/bob.py" in e for e in result.errors)
deleted = [("A", "practice/greetings/ada.py"), ("A", "practice/tests/test_ada.py"), ("D", "first-commits/ada.py")]
assert any("first-commits/ada.py" in e for e in check_changes(deleted, "ada").errors)
41 changes: 41 additions & 0 deletions tests/test_check_first_commit.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,3 +112,44 @@ def test_is_learner_pr(paths, expected):
from check_first_commit import is_learner_pr

assert is_learner_pr(paths) is expected


def test_main_is_silent_on_an_exercise_2_pr_that_carries_the_first_commit(tmp_path, monkeypatch, capsys):
"""PR #47: the Exercise 2 bot reviews it, so this bot must not ask to undo the exercise files."""
from check_first_commit import NOT_A_LEARNER_PR, main

def git(*args):
return subprocess.run(["git", *args], cwd=tmp_path, check=True, capture_output=True, text=True).stdout.strip()

git("init", "-q", "-b", "main")
git("config", "user.email", "t@example.com")
git("config", "user.name", "t")
(tmp_path / "README.md").write_text("hi\n")
git("add", ".")
git("commit", "-qm", "base")
base = git("rev-parse", "HEAD")
for folder in ("first-commits", "practice/greetings", "practice/tests"):
(tmp_path / folder).mkdir(parents=True)
(tmp_path / "first-commits/ada.py").write_text(GOOD)
git("add", ".")
git("commit", "-qm", "first commit")
first = git("rev-parse", "HEAD")
(tmp_path / "practice/greetings/ada.py").write_text("def greet(name: str) -> str:\n return name\n")
(tmp_path / "practice/tests/test_ada.py").write_text("from greetings.ada import greet\n")
git("add", ".")
git("commit", "-qm", "exercise 2")
monkeypatch.chdir(tmp_path)

assert main(["--login", "ada", "--base", base, "--head", first]) == 0 # the first PR itself is still checked
capsys.readouterr()
assert main(["--login", "ada", "--base", base, "--head", git("rev-parse", "HEAD")]) == NOT_A_LEARNER_PR
assert capsys.readouterr().out == ""


def test_a_syntax_error_says_its_line_once():
"""Python adds "(detected at line 3)" to some messages; the bot used to add "(line 3)" after it."""
errors = check_source('name = "Ada"\ngithub = "ada"\nmessage = "Hello\n', "ada").errors
assert errors == [
"Your file isn't valid Python: unterminated string literal (line 3). "
'A missing quote `"` is the most common cause.'
]
Loading
Loading