From 927517adb9776527bdec7a9a284fa072b0236f7b Mon Sep 17 00:00:00 2001 From: Paul Irish Date: Fri, 16 Jun 2017 15:57:49 -0700 Subject: [PATCH] Add a test suite (#78) --- .gitmodules | 6 + .travis.yml | 5 +- README.md | 24 +++- git-open | 10 +- package.json | 3 +- test/git-open.bats | 201 ++++++++++++++++++++++++++++++++++ test/test_helper/bats-assert | 1 + test/test_helper/bats-support | 1 + 8 files changed, 245 insertions(+), 6 deletions(-) create mode 100644 .gitmodules create mode 100644 test/git-open.bats create mode 160000 test/test_helper/bats-assert create mode 160000 test/test_helper/bats-support diff --git a/.gitmodules b/.gitmodules new file mode 100644 index 0000000..76dfa52 --- /dev/null +++ b/.gitmodules @@ -0,0 +1,6 @@ +[submodule "test/test_helper/bats-assert"] + path = test/test_helper/bats-assert + url = https://github.com/ztombol/bats-assert +[submodule "test/test_helper/bats-support"] + path = test/test_helper/bats-support + url = https://github.com/ztombol/bats-support diff --git a/.travis.yml b/.travis.yml index 0a708fc..9b3e5d2 100644 --- a/.travis.yml +++ b/.travis.yml @@ -9,6 +9,8 @@ addons: cache: directories: - node_modules +before_install: + - git clone --depth 1 https://github.com/sstephenson/bats.git install: - npm install script: @@ -16,5 +18,4 @@ script: - npm run lint:readme - npm run lint:editorconfig - shellcheck git-open - - + - ./bats/bin/bats test/*.bats diff --git a/README.md b/README.md index e580f39..dc642da 100644 --- a/README.md +++ b/README.md @@ -119,7 +119,7 @@ If your Gitlab custom hosted is serving `http` you can also specify this: git config [--global] gitopen.gitlab.protocol http ``` -## Related projects / alternatives +## Alternative projects See [hub](https://github.com/github/hub) for complete GitHub opening support. It's the official GitHub project and provides `hub browse`. @@ -138,10 +138,30 @@ from which this plugin was forked. [jasonmccreary](https://github.com/jasonmccreary/) did [the initial hard work](https://github.com/jasonmccreary/gh). Since then, [many contributors](https://github.com/paulirish/git-open/graphs/contributors) have submitted great PRs. -## Contributing +## Contributing & Development Please provide examples of the URLs you are parsing with each PR. +You can run `git-open` in `echo` mode, which doesn't open your browser, but just prints the URL to stdout: +```sh +BATS_CWD="." ./git-open +``` + +#### Testing: + +You'll need to install [bats](https://github.com/sstephenson/bats#installing-bats-from-source), the Bash automated testing system. It's also available as `brew install bats` + +```sh +git submodule update --init # pull in the assertion libraries + +# Run the test suite once: +bats test + +# Run it on every change with `entr` +brew install entr +npm run watch +``` + ## Related projects - [`git recent`](https://github.com/paulirish/git-recent) - View your most recent git branches diff --git a/git-open b/git-open index db8ab52..e4e7806 100755 --- a/git-open +++ b/git-open @@ -158,6 +158,14 @@ case $( uname -s ) in *) open=${BROWSER:-xdg-open};; esac +# if testing, log results to stdout. otherwise, mute the output +if [ "$BATS_CWD" ]; then + open=echo +else + exec &>/dev/null +fi; + # open it in a browser -$open "$giturl" &> /dev/null +$open "$giturl" + exit $? diff --git a/package.json b/package.json index d179701..0df5c42 100644 --- a/package.json +++ b/package.json @@ -34,7 +34,8 @@ "lint:editorconfig": "eclint check git-open* readme* .travis.yml", "lint:package": "pjv --recommendations --warnings", "lint:readme": "node ./node_modules/markdownlint/lib/markdownlint.js --config markdownlint.json README.md", - "test": "npm run lint:package && npm run lint:readme && npm run lint:editorconfig" + "test": "npm run lint:package && npm run lint:readme && npm run lint:editorconfig", + "watch": "find . -maxdepth 2 -iname '*bats' -o -iname 'git-open' | entr bats test/" }, "dependencies": {}, "devDependencies": { diff --git a/test/git-open.bats b/test/git-open.bats new file mode 100644 index 0000000..1a6aa11 --- /dev/null +++ b/test/git-open.bats @@ -0,0 +1,201 @@ +#!/usr/bin/env bats + +load "test_helper/bats-support/load" +load "test_helper/bats-assert/load" + +foldername="sandboxrepo" + +setup() { + create_git_sandbox +} + + +## +## GitHub +## + +@test "gh: basic" { + git remote set-url origin "git@github.com:user/repo.git" + git checkout -B "master" + run ../git-open + assert_output "https://github.com/user/repo/" +} + +@test "gh: branch" { + git remote set-url origin "git@github.com:user/repo.git" + git checkout -B "mybranch" + run ../git-open + assert_output "https://github.com/user/repo/tree/mybranch" +} + +@test "gh: non-origin remote" { + git remote set-url origin "git@github.com:user/repo.git" + git remote add upstream "git@github.com:upstreamorg/repo.git" + run ../git-open "upstream" + assert_output "https://github.com/upstreamorg/repo/" + + git checkout -B "mybranch" + run ../git-open "upstream" "otherbranch" + assert_output "https://github.com/upstreamorg/repo/tree/otherbranch" +} + +@test "gh: without git user" { + # https://github.com/paulirish/git-open/pull/63 + git remote set-url origin "github.com:paulirish/git-open.git" + run ../git-open + assert_output "https://github.com/paulirish/git-open/" +} + +@test "gh: ssh origin" { + git remote set-url origin "ssh://git@github.com/user/repo" + run ../git-open + assert_output "https://github.com/user/repo/" + + # https://github.com/paulirish/git-open/pull/30 + git remote set-url origin "ssh://git@github.com/user/repo.git" + run ../git-open + assert_output "https://github.com/user/repo/" +} + +@test "gh: git protocol origin" { + # currently fails. derimagia rewrite fixes + skip + + git remote set-url origin "git://github.com/user/repo.git" + git checkout -B "master" + run ../git-open + assert_output "https://github.com/user/repo" +} + +@test "gh: git open issue" { + # https://github.com/paulirish/git-open/pull/46 + git remote set-url origin "github.com:paulirish/git-open.git" + git checkout -B "issues/#12" + run ../git-open "issue" + assert_output "https://github.com/paulirish/git-open/issues/12" +} + +@test "gh: gist" { + git remote set-url origin "git@gist.github.com:2d84a6db1b41b4020685.git" + run ../git-open + assert_output "https://gist.github.com/2d84a6db1b41b4020685/" +} + +@test "basic: # and % in branch names are URL encoded" { + # https://github.com/paulirish/git-open/pull/24 + git checkout -B "issue-#42" + run ../git-open + assert_output "https://github.com/paulirish/git-open/tree/issue-%2342" + + git checkout -B "just-50%" + run ../git-open + assert_output "https://github.com/paulirish/git-open/tree/just-50%25" +} + + + +## +## Bitbucket +## + +@test "bitbucket: basic" { + git remote set-url origin "git@bitbucket.org:paulirish/crbug-extension.git" + run ../git-open + assert_output --partial "https://bitbucket.org/paulirish/crbug-extension/" +} + +@test "bitbucket: non-origin remote" { + # https://github.com/paulirish/git-open/pull/4 + git remote add bbclone "git@bitbucket.org:rwhitbeck/git-open.git" + run ../git-open "bbclone" + assert_output --partial "https://bitbucket.org/rwhitbeck/git-open/" + assert_output --partial "//?at=master" +} + +@test "bitbucket: branch" { + git remote set-url origin "https://paulirish@bitbucket.org/malb/lwe-estimator.git" + git checkout -B "new-bkw" + run ../git-open + assert_output --partial "https://bitbucket.org/malb/lwe-estimator/src/" + assert_output --partial "?at=new-bkw" +} + +@test "bitbucket: ssh:// clone urls" { + # https://github.com/paulirish/git-open/pull/36 + git remote set-url origin "ssh://git@bitbucket.org/lbesson/bin.git" + run ../git-open + assert_output --partial "https://bitbucket.org/lbesson/bin/" + assert_output --partial "//?at=master" +} + +@test "bitbucket: no username@ in final url" { + # https://github.com/paulirish/git-open/pull/69 + git remote set-url origin "https://trend_rand@bitbucket.org/trend_rand/test-repo.git" + run ../git-open + refute_output --partial "@" +} + + +## +## GitLab +## + +@test "gitlab: separate domains" { + # https://github.com/paulirish/git-open/pull/56 + git remote set-url origin "git@git.example.com:namespace/project.git" + git config "gitopen.gitlab.domain" "gitlab.example.com" + git config "gitopen.gitlab.ssh.domain" "git.example.com" + run ../git-open + assert_output "https://gitlab.example.com/namespace/project/" +} + +@test "gitlab: default ssh origin style" { + # https://github.com/paulirish/git-open/pull/55 + git remote set-url origin "git@gitlab.example.com:user/repo" + git config "gitopen.gitlab.domain" "gitlab.example.com" + run ../git-open + assert_output "https://gitlab.example.com/user/repo/" +} + +@test "gitlab: ssh:// origin style" { + # https://github.com/paulirish/git-open/pull/51 + git remote set-url origin "ssh://git@gitlab.domain.com/user/repo" + git config "gitopen.gitlab.domain" "gitlab.domain.com" + run ../git-open + assert_output --partial "https://gitlab.domain.com/" + assert_output --partial "/user/repo/" + # assert_output "https://gitlab.domain.com//user/repo/" # TODO fix double slash +} + + +# Tests not yet written: +# * gitopen.gitlab.port +# * gitopen.gitlab.protocol +# * Atlassian Bitbucket Server (https://github.com/paulirish/git-open/pull/15) + + +teardown() { + cd .. + rm -rf "$foldername" +} + +# helper to create a test git sandbox that won't dirty the real repo +function create_git_sandbox() { + rm -rf "$foldername" + mkdir "$foldername" + cd "$foldername" + + git init -q + git config user.email "test@runner.com" && git config user.name "Test Runner" + # newer git auto-creates the origin remote + if [ $(git remote) ]; then + git remote set-url origin "github.com:paulirish/git-open.git" + else + git remote add origin "github.com:paulirish/git-open.git" + fi + git checkout -B "master" + + echo "ok" > readme.txt + git add readme.txt + git commit -m "add file" -q +} diff --git a/test/test_helper/bats-assert b/test/test_helper/bats-assert new file mode 160000 index 0000000..9f88b42 --- /dev/null +++ b/test/test_helper/bats-assert @@ -0,0 +1 @@ +Subproject commit 9f88b4207da750093baabc4e3f41bf68f0dd3630 diff --git a/test/test_helper/bats-support b/test/test_helper/bats-support new file mode 160000 index 0000000..004e707 --- /dev/null +++ b/test/test_helper/bats-support @@ -0,0 +1 @@ +Subproject commit 004e707638eedd62e0481e8cdc9223ad471f12ee