Skip to content

Latest commit

 

History

History
210 lines (142 loc) · 5.15 KB

File metadata and controls

210 lines (142 loc) · 5.15 KB

Lab 3: Git Bisect

In this lab, you're going to use git bisect to locate a commit that introduced a bug in your game's unit tests.

Scenario

It looks like your unit tests aren't passing! You're not sure when the bug was introduced, but you know it wasn't there when you first created the game.

Task 1: Setup git bisect

  1. Start the git bisect process

    git bisect start
  2. Indicate a commit where you know the code contains the change you're looking for

    Omitting the commit hash will tell Git that the current commit is bad.

    git bisect bad
  3. Locate a commit where the code change is not present

    git log --oneline

    From the output of the above command, find a commit where the code change is not present and copy the commit hash. You can simply select the earliest commit in the logs (the one labeled Initial commit).

  4. Indicate a commit where you know the code does not contain the change you're looking for

    Replace <sha> with the commit hash you found in the previous step.

    git bisect good <sha>

Task 2: Locate the Commit

The git bisect command will check out the middle commit between the good and bad ones.

  1. Check if the code change is present in the file

    grep -w "expect(true).toBe(false)" __tests__/keyboard_input_manager.test.ts

    If the code change is present, the grep command will output the matching line(s).

    # Found
    $ grep -w "expect(true).toBe(false)" __tests__/keyboard_input_manager.test.ts
     expect(true).toBe(false);
    
    # Not found
    $ grep -w "expect(true).toBe(false)" __tests__/keyboard_input_manager.test.ts
    
  2. If the change is present, tell git bisect that the current commit is bad

    git bisect bad
  3. If the change is not present, tell git bisect that the current commit is good

    git bisect good

    Git will locate and checkout the new mid point.

  4. Repeat the previous steps until Git locates the first commit with the specified change.

    When the process is complete, Git will output the commit that introduced the change.

    53336f05d7e3d124ed872bbecd38c5cdbcca89be is the first bad commit
    commit 53336f05d7e3d124ed872bbecd38c5cdbcca89be (HEAD)
    Author: Nick Alteen <ncalteen@github.com>
    Date:   Fri Sep 27 13:35:45 2024 -0400
    
        Disable broken test
    
    __tests__/keyboard_input_manager.test.ts          |  10 ++-------
    solutions/3-bisect/keyboard_input_manager.test.ts | 124 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    2 files changed, 126 insertions(+), 8 deletions(-)

Task 3: Finish git bisect

  1. Finish the git bisect process

    git bisect reset

    This will return you to the HEAD commit.

Task 4: Fix the Broken Test

  1. Create a new feature branch

    git checkout -b fix/unit-test
  2. Open __tests__/keyboard_input_manager.test.ts

  3. Locate the comment // Lab 3: Git Bisect

  4. Add the following code below the comment

    Make sure to replace expect(true).toBe(false) with the correct test code.

    // Lab 3: Git Bisect
    const listen = jest
      .spyOn(KeyboardInputManager, 'listen')
      .mockImplementation(() => {})
    
    new KeyboardInputManager()
    
    expect(KeyboardInputManager.events).toMatchObject({})
    expect(listen).toHaveBeenCalledTimes(1)
  5. Save the file

  6. (Optional) Run the tests

    If you have Node.js v22+ installed, you can run the tests with the following commands:

    npm i && npm test

Task 5: Commit your Changes

Now that you've fixed the broken test, you should commit your changes to your feature branch.

  1. Open the terminal or command prompt

  2. Add your changes to the staging area

    git add __tests__/keyboard_input_manager.test.ts
  3. Commit your changes

    git commit -m "Fix broken test"

Task 6: Switch to the main Branch

Now that you've added the feature, you should switch back to the main branch so you can start working on something new!

  1. Checkout the main branch

    git checkout main

Task 7: Merge the fix/unit-test Branch

Now that you're on the main branch, you should merge the fix/unit-test branch into the main branch. That way, when your changes are pushed, the unit tests will pass the next time the run as part of the continuous integration process.

  1. Merge the fix/unit-test branch into the main branch

    git merge fix/unit-test
  2. Push your changes to GitHub

    git push

Need Help?

If you're having trouble with any of the steps, you can ask for help in the meeting chat.

The code changes for this lab can be found in the solutions directory.