write tests for ugit brew formula
Bhupesh-V opened this issue Β· 7 comments
I have a draft formula ready, but need help with writing test
class Ugit < Formula
desc "Undo git commands. Your damage control git buddy"
homepage "https://github.com/Bhupesh-V/ugit"
url "https://github.com/Bhupesh-V/ugit/archive/refs/tags/v4.1.tar.gz"
sha256 "5ce35179714263412f65cc78c247900c5349bfbf4bd9576e4075aa1a9055e0d6"
license "MIT"
depends_on "bash"
depends_on "fzf"
def install
bin.install "ugit"
bin.install "git-undo"
end
test do
# not good, will be rejected by maintainers probably
assert_match "ugit version", shell_output("#{bin}/ugit --version")
end
end
A basic set of tests would include setting up a directory (non-git) and running the command ugit
. The expected output should look like
Ummm, you are not inside a Git repo π
Apart from that I am out of ideas of what to test. Since both ugit --version
& ugit --help
are bad examples.
Any suggestions?
A test like the following should be acceptable:
test do
test_output = shell_output("#{bin}/ugit")
assert_match "Ummm, you are not inside a Git repo", test_output
end
I have tested it on my system and it works.
The bad news is that ugit
might not be "notable" enough for brew
; apparently the repo has to meet certain criteria:
GitHub repository not notable enough (<30 forks, <30 watchers and <75 stars)
- 30+ forks
- 30+ watchers
- 75+ stars
See: https://github.com/Homebrew/homebrew-core/runs/4978390108?check_suite_focus=true#step:6:31
- I actually tried creating a PR with a similar test, unfortunately, it isn't acceptable even if the documentation says so
Homebrew/homebrew-core#90513. Not sure if they will accept that as a test - Not sure about the "notable" concept was it introduced lately?
The only solution would be to set up a custom homebrew cask, wdyt?
Since user interaction is required, test like the one's created for gh
can be written and it will comply with the following condition:
We want tests that donβt require any user input and test the basic functionality of the application.
See: Add a test to the formula
This is the current test
block for gh
:
test do
assert_match "gh version #{version}", shell_output("#{bin}/gh --version")
assert_match "Work with GitHub issues", shell_output("#{bin}/gh issue 2>&1")
assert_match "Work with GitHub pull requests", shell_output("#{bin}/gh pr 2>&1")
end
So tests like those can be written and should be accepted.
test do
assert_match "ugit version #{version}", shell_output("#{bin}/ugit --version")
assert_match "Ummm, you are not inside a Git repo", shell_output("#{bin}/ugit")
end
Testing ugit
itself would require user input, and it breaks one of their basic rules.
So we might end up with something like this:
class Ugit < Formula
desc "Undo git commands. Your damage control git buddy"
homepage "https://bhupesh-v.github.io/undo-your-last-git-mistake-with-ugit/"
url "https://github.com/Bhupesh-V/ugit/archive/refs/tags/v5.1.tar.gz"
sha256 "52e72f9d44c3160987cdc0a393ccce3c0c76e76aa6b3e792e4b7ef2891a2b09d"
license "MIT"
depends_on "bash"
depends_on "fzf"
def install
bin.install "ugit"
bin.install "git-undo"
end
test do
assert_match "ugit version #{version}", shell_output("#{bin}/ugit --version")
assert_match "Ummm, you are not inside a Git repo", shell_output("#{bin}/ugit")
end
end
If that is not accepted by Homebrew maintainers, then probably creating your own Tap would be another way.
Since user interaction is required, test like the one's created for
gh
can be written and it will comply with the following condition:We want tests that donβt require any user input and test the basic functionality of the application.
See: Add a test to the formula
This is the current
test
block forgh
:test do assert_match "gh version #{version}", shell_output("#{bin}/gh --version") assert_match "Work with GitHub issues", shell_output("#{bin}/gh issue 2>&1") assert_match "Work with GitHub pull requests", shell_output("#{bin}/gh pr 2>&1") endSo tests like those can be written and should be accepted.
test do assert_match "ugit version #{version}", shell_output("#{bin}/ugit --version") assert_match "Ummm, you are not inside a Git repo", shell_output("#{bin}/ugit") endTesting
ugit
itself would require user input, and it breaks one of their basic rules.So we might end up with something like this:
class Ugit < Formula desc "Undo git commands. Your damage control git buddy" homepage "https://bhupesh-v.github.io/undo-your-last-git-mistake-with-ugit/" url "https://github.com/Bhupesh-V/ugit/archive/refs/tags/v5.1.tar.gz" sha256 "52e72f9d44c3160987cdc0a393ccce3c0c76e76aa6b3e792e4b7ef2891a2b09d" license "MIT" depends_on "bash" depends_on "fzf" def install bin.install "ugit" bin.install "git-undo" end test do assert_match "ugit version #{version}", shell_output("#{bin}/ugit --version") assert_match "Ummm, you are not inside a Git repo", shell_output("#{bin}/ugit") end end
Thanks a lot for referencing this π
Would you like to take a shot to get ugit
into brew one last time? (you can take a reference from the PR I created above) if that doesn't work out either, we will set up a ugit tap instead
A maintainer mentioned one could use PTY
for cli apps that require user interaction
E.g.
test do
require "pty"
# test goes here
You may find some examples at the core repo.
Hey, @thecesrom thanks a lot for your help. ugit is now available on brew π
Homebrew/homebrew-core#98457
Closing this finally!