Skip to content

Commit

Permalink
Other: start writing tests
Browse files Browse the repository at this point in the history
  • Loading branch information
randy3k committed Nov 6, 2016
1 parent c12c943 commit 5e705e8
Show file tree
Hide file tree
Showing 8 changed files with 181 additions and 0 deletions.
6 changes: 6 additions & 0 deletions .coveragerc
@@ -0,0 +1,6 @@
[run]
omit =
*/common/vendor/*
*/tests/*
common/vendor/*
tests/*
43 changes: 43 additions & 0 deletions .travis.yml
@@ -0,0 +1,43 @@
env:
global:
- PACKAGE="GitSavvy"
- SUBLIME_TEXT_VERSION="3"

matrix:
include:
- os: linux
language: python
python: 3.3
- os: osx
language: generic

before_install:
- curl -OL https://raw.githubusercontent.com/randy3k/UnitTesting/master/sbin/travis.sh
- if [ "$TRAVIS_OS_NAME" == "linux" ]; then
export DISPLAY=:99.0;
sh -e /etc/init.d/xvfb start;
fi
- git config --global user.email gitsavvy@gitsavvy.com
- git config --global user.name GitSavvy

install:
- sh travis.sh bootstrap
- sh travis.sh install_package_control

script:
- sh travis.sh run_tests --coverage

after_success:
- if [[ "$TRAVIS_OS_NAME" == "osx" ]]; then
brew update;
brew install python3;
pip3 install python-coveralls;
fi
- if [[ "$TRAVIS_OS_NAME" == "linux" ]]; then
echo `python --version`;
pip install python-coveralls;
fi
- coveralls

notifications:
email: false
13 changes: 13 additions & 0 deletions appveyor.yml
@@ -0,0 +1,13 @@
environment:
PACKAGE: "GitSavvy"
SUBLIME_TEXT_VERSION : "3"

install:
- ps: appveyor DownloadFile "https://raw.githubusercontent.com/randy3k/UnitTesting/master/sbin/appveyor.ps1"
- ps: .\appveyor.ps1 "bootstrap" -verbose
- ps: .\appveyor.ps1 "install_package_control" -verbose

build: off

test_script:
- ps: .\appveyor.ps1 "run_tests" -verbose
Empty file added tests/test_git/__init__.py
Empty file.
73 changes: 73 additions & 0 deletions tests/test_git/common.py
@@ -0,0 +1,73 @@
import sublime
import subprocess
import os
import shutil
import tempfile
from unittesting import DeferrableTestCase


def subl(*args):
"""
Ideally, it would be moved into UnitTesting as a helper function.
"""
executable_path = sublime.executable_path()
if sublime.platform() == 'osx':
app_path = executable_path[:executable_path.rfind(".app/") + 5]
executable_path = app_path + "Contents/SharedSupport/bin/subl"
subprocess.Popen([executable_path] + list(args))
if sublime.platform() == "windows":
def fix_focus():
window = sublime.active_window()
view = window.active_view()
window.run_command('focus_neighboring_group')
window.focus_view(view)
sublime.set_timeout(fix_focus, 300)


class GSAssertionsMixin:

def assert_git_status(self, status):
"""
Assertion of the current git status. `status` should be a list of 4 intergers:
The lengths of the staged, unstaged, untracked and conflicted entries.
"""
self.assertEqual(
[len(x) for x in self.sort_status_entries(self.get_status())],
status)


class GSCreateRepoTestCase(DeferrableTestCase):

_temp_dir = None

@classmethod
def setUpClass(cls):
"""
Setup a repo for testing
"""
print(subprocess.check_output(["git", "--version"]).decode("ascii").strip())

cls._temp_dir = tempfile.mkdtemp()
subl("-n", cls._temp_dir)

def condition():
for d in sublime.active_window().folders():
# on Windows, `cls._temp_dir` is lowered cased,
# `os.path.normcase` is needed for comparison.
if cls._temp_dir == os.path.normcase(d):
return True

yield condition

cls.window = sublime.active_window()

@classmethod
def tearDownClass(cls):
# need at least one window in order to keep sublime running
if len(sublime.windows()) > 1:
cls.window.run_command("close")
yield 1000
try:
shutil.rmtree(cls._temp_dir)
except:
print("Cannot remove {}".format(cls._temp_dir))
42 changes: 42 additions & 0 deletions tests/test_git/test_git_init.py
@@ -0,0 +1,42 @@
import sublime
import os
import subprocess
from .common import GSCreateRepoTestCase, GSAssertionsMixin
from GitSavvy.core import git_command


def tidy_path(path):
return os.path.realpath(os.path.normcase(path))


class TestInit(GSCreateRepoTestCase, GSAssertionsMixin, git_command.GitCommand):

def test_01_init(self):
self.assertEqual(
tidy_path(self._temp_dir),
tidy_path(sublime.active_window().folders()[0]))
subprocess.check_call(["git", "init"], cwd=self._temp_dir)
self.assertEqual(
tidy_path(self._temp_dir),
tidy_path(self.window.folders()[0]))

def test_02_add_first_file(self):
readme = os.path.join(self.repo_path, "README.md")
f = open(readme, "w")
f.write("README")
f.close()
self.git("add", "README.md")
yield 100
self.git("commit", "-m", "Init")

def test_03_is_master(self):
branch = self.get_current_branch_name()
self.assertEqual(branch, "master")

def test_04_untrack_file(self):
foo = os.path.join(self.repo_path, "foo")
with open(foo, "w") as f:
f.write("foo")
self.assert_git_status([0, 0, 1, 0])
self.stage_file(foo)
self.assert_git_status([1, 0, 0, 0])
Empty file added tests/test_ui/__init__.py
Empty file.
4 changes: 4 additions & 0 deletions unittesting.json
@@ -0,0 +1,4 @@
{
"deferred": true,
"capture_console": true
}

0 comments on commit 5e705e8

Please sign in to comment.