Automated testing on push in Git - Time & Space Complexity
When code is pushed to a repository, automated tests run to check for errors. We want to understand how the time taken to run these tests changes as the number of tests grows.
How does adding more tests affect the total time before feedback?
Analyze the time complexity of this git hook running automated tests on push.
#!/bin/sh
# pre-push hook
echo "Running automated tests..."
for test in tests/*.sh; do
sh "$test" || exit 1
done
echo "All tests passed!"
This script runs each test script in the tests folder one by one before allowing the push.
- Primary operation: Running each test script once.
- How many times: Once per test script in the tests directory.
As the number of test scripts increases, the total time grows proportionally because each test runs separately.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | Runs 10 tests |
| 100 | Runs 100 tests |
| 1000 | Runs 1000 tests |
Pattern observation: Doubling the number of tests roughly doubles the total time.
Time Complexity: O(n)
This means the total time grows linearly with the number of tests run on each push.
[X] Wrong: "Running more tests won't affect push time much because they run fast."
[OK] Correct: Even fast tests add up when many run one after another, so total time grows with test count.
Understanding how test count affects push time helps you design better workflows and explain trade-offs clearly in real projects.
"What if tests ran in parallel instead of one by one? How would the time complexity change?"
Practice
Solution
Step 1: Understand automated testing on push
Automated testing on push means tests run automatically when code is pushed to a repository.Step 2: Identify the main benefit
This helps catch errors early and keeps the project stable by preventing broken code from merging.Final Answer:
To run tests automatically when code is pushed to catch errors early -> Option AQuick Check:
Automated testing on push = run tests automatically [OK]
- Thinking it speeds up git push command
- Confusing with manual code review
- Assuming it deletes branches automatically
Solution
Step 1: Identify configuration file for GitHub Actions
GitHub Actions uses YAML files inside the .github/workflows directory to define workflows.Step 2: Match file to automated testing on push
The file .github/workflows/ci.yml typically contains instructions to run tests on push events.Final Answer:
.github/workflows/ci.yml -> Option DQuick Check:
GitHub Actions config = .github/workflows/ci.yml [OK]
- Choosing .gitignore which ignores files
- Selecting README.md which is documentation
- Picking package.json which is for Node.js dependencies
name: CI
on:
push:
branches: [main]
jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Run tests
run: echo "Tests passed!"Solution
Step 1: Analyze the trigger event
The workflow triggers on push events to the 'main' branch as specified under 'on: push: branches: [main]'.Step 2: Check the job steps
The job runs on ubuntu-latest, checks out the code, then runs the command echo "Tests passed!" which prints that message.Final Answer:
The workflow runs and prints 'Tests passed!' on every push to main -> Option CQuick Check:
Push to main triggers workflow = prints message [OK]
- Thinking it runs only on pull requests
- Assuming 'run' command is invalid syntax
- Believing branches list is empty
name: Test
on:
push:
branch: main
jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- run: echo "Running tests"
What is the error?Solution
Step 1: Check the event trigger syntax
The workflow uses 'branch' instead of 'branches' under 'push'. The correct key is 'branches' which expects a list.Step 2: Understand impact of wrong key
Because 'branch' is invalid, the workflow never triggers on any push event.Final Answer:
The key 'branch' should be 'branches' under 'push' to specify branches list -> Option BQuick Check:
Correct key is 'branches' not 'branch' [OK]
- Confusing 'branch' with 'branches'
- Thinking 'runs-on' value is wrong
- Assuming checkout action is deprecated
Solution
Step 1: Understand correct YAML syntax for branches
The 'branches' key expects a list of branch names, each on its own line or in a list format.Step 2: Compare options
on: push: branch: ['main', 'develop'] uses incorrect singular 'branch' key instead of plural 'branches'. on: push: branches: - main - develop uses the correct 'branches' key with standard YAML list format using dashes, which is widely used.Step 3: Identify invalid options
on: push: branch: 'main, develop' uses 'branch' (wrong key) and a string with comma, which is invalid. on: push: refs: ['main', 'develop'] uses 'refs' which is not the correct key for branches under push.Final Answer:
on: push: branches: - main - develop -> Option AQuick Check:
Use 'branches' with list syntax for multiple branches [OK]
- Using 'branch' instead of 'branches'
- Passing branches as a comma string
- Using 'refs' instead of 'branches'
Ready to prove it?
Pass the test and this pattern moves unknown
