2014-01-02 21:56:43 +00:00
|
|
|
#!/usr/bin/env python
|
2013-12-31 22:33:32 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
2014-01-01 00:35:57 +00:00
|
|
|
from random import randrange
|
2014-01-01 00:02:08 +00:00
|
|
|
from shutil import rmtree
|
2014-01-01 00:35:57 +00:00
|
|
|
from tempfile import gettempdir
|
2014-01-01 00:02:08 +00:00
|
|
|
from tempfile import mkdtemp
|
2013-12-31 23:15:25 +00:00
|
|
|
import os
|
|
|
|
|
2013-12-31 22:34:35 +00:00
|
|
|
import mock
|
|
|
|
from testify import TestCase
|
|
|
|
from testify import assert_equal
|
2013-12-31 23:15:25 +00:00
|
|
|
from testify import assert_false
|
2014-01-02 03:53:36 +00:00
|
|
|
from testify import assert_raises
|
2013-12-31 23:15:25 +00:00
|
|
|
from testify import assert_true
|
2014-01-01 00:35:57 +00:00
|
|
|
from testify import class_setup
|
|
|
|
from testify import class_teardown
|
2013-12-31 22:34:35 +00:00
|
|
|
from testify import run
|
2014-01-01 00:02:08 +00:00
|
|
|
from testify import setup
|
|
|
|
from testify import teardown
|
2013-12-31 22:33:32 +00:00
|
|
|
|
2013-12-31 22:34:35 +00:00
|
|
|
import autojump_utils
|
2014-01-01 00:35:57 +00:00
|
|
|
from autojump_utils import create_dir
|
2013-12-31 22:33:32 +00:00
|
|
|
from autojump_utils import decode
|
|
|
|
from autojump_utils import first
|
2014-01-01 00:02:08 +00:00
|
|
|
from autojump_utils import get_pwd
|
2014-01-01 00:39:45 +00:00
|
|
|
from autojump_utils import get_tab_entry_info
|
2013-12-31 23:41:12 +00:00
|
|
|
from autojump_utils import has_uppercase
|
2013-12-31 23:15:25 +00:00
|
|
|
from autojump_utils import in_bash
|
2013-12-31 22:33:32 +00:00
|
|
|
from autojump_utils import last
|
2014-01-01 00:35:57 +00:00
|
|
|
from autojump_utils import move_file
|
2013-12-31 22:34:35 +00:00
|
|
|
from autojump_utils import sanitize
|
|
|
|
from autojump_utils import second
|
|
|
|
from autojump_utils import surround_quotes
|
2013-12-31 22:33:32 +00:00
|
|
|
from autojump_utils import take
|
|
|
|
|
|
|
|
|
2014-01-01 00:02:08 +00:00
|
|
|
class StringUnitTests(TestCase):
|
2013-12-31 22:33:32 +00:00
|
|
|
def test_decode(self):
|
|
|
|
assert_equal(decode(r'blah'), u'blah')
|
|
|
|
assert_equal(decode(r'日本語'), u'日本語')
|
|
|
|
|
2013-12-31 23:41:12 +00:00
|
|
|
def test_has_uppercase(self):
|
|
|
|
assert_true(has_uppercase('Foo'))
|
|
|
|
assert_true(has_uppercase('foO'))
|
|
|
|
assert_false(has_uppercase('foo'))
|
|
|
|
assert_false(has_uppercase(''))
|
|
|
|
|
2013-12-31 22:34:35 +00:00
|
|
|
@mock.patch.object(autojump_utils, 'in_bash', return_value=True)
|
|
|
|
def test_surround_quotes_in_bash(self, _):
|
|
|
|
assert_equal(surround_quotes('foo'), '"foo"')
|
|
|
|
|
|
|
|
@mock.patch.object(autojump_utils, 'in_bash', return_value=False)
|
|
|
|
def test_dont_surround_quotes_not_in_bash(self, _):
|
|
|
|
assert_equal(surround_quotes('foo'), 'foo')
|
|
|
|
|
|
|
|
def test_sanitize(self):
|
|
|
|
assert_equal(sanitize([]), [])
|
|
|
|
assert_equal(sanitize([r'/foo/bar/', r'/']), [u'/foo/bar', u'/'])
|
|
|
|
|
2013-12-31 22:33:32 +00:00
|
|
|
|
2014-01-01 00:02:08 +00:00
|
|
|
class IterationUnitTests(TestCase):
|
2013-12-31 22:33:32 +00:00
|
|
|
def test_first(self):
|
2014-01-01 01:09:51 +00:00
|
|
|
assert_equal(first(xrange(5)), 0)
|
|
|
|
assert_equal(first([]), None)
|
2013-12-31 22:33:32 +00:00
|
|
|
|
|
|
|
def test_second(self):
|
2014-01-01 01:09:51 +00:00
|
|
|
assert_equal(second(xrange(5)), 1)
|
|
|
|
assert_equal(second([]), None)
|
2013-12-31 22:33:32 +00:00
|
|
|
|
|
|
|
def test_last(self):
|
2014-01-01 01:09:51 +00:00
|
|
|
assert_equal(last(xrange(4)), 3)
|
|
|
|
assert_equal(last([]), None)
|
2013-12-31 22:33:32 +00:00
|
|
|
|
|
|
|
def test_take(self):
|
2014-01-01 01:09:51 +00:00
|
|
|
assert_equal(list(take(1, xrange(3))), [0])
|
|
|
|
assert_equal(list(take(2, xrange(3))), [0, 1])
|
|
|
|
assert_equal(list(take(4, xrange(3))), [0, 1, 2])
|
2013-12-31 22:33:32 +00:00
|
|
|
assert_equal(list(take(10, [])), [])
|
|
|
|
|
|
|
|
|
2014-01-01 00:02:08 +00:00
|
|
|
class EnvironmentalVariableIntegrationTests(TestCase):
|
|
|
|
@setup
|
|
|
|
def create_tmp_dir(self):
|
|
|
|
self.tmp_dir = mkdtemp()
|
|
|
|
|
|
|
|
@teardown
|
|
|
|
def delete_tmp_dir(self):
|
|
|
|
try:
|
|
|
|
rmtree(self.tmp_dir)
|
|
|
|
except OSError:
|
|
|
|
pass
|
|
|
|
|
2013-12-31 23:15:25 +00:00
|
|
|
def test_in_bash(self):
|
|
|
|
os.environ['SHELL'] = '/bin/bash'
|
|
|
|
assert_true(in_bash())
|
|
|
|
os.environ['SHELL'] = '/usr/bin/zsh'
|
|
|
|
assert_false(in_bash())
|
2013-12-31 22:33:32 +00:00
|
|
|
|
2014-01-01 00:02:08 +00:00
|
|
|
def test_good_get_pwd(self):
|
|
|
|
os.chdir(self.tmp_dir)
|
|
|
|
assert_equal(get_pwd(), self.tmp_dir)
|
|
|
|
|
|
|
|
def test_bad_get_pwd(self):
|
|
|
|
os.chdir(self.tmp_dir)
|
|
|
|
rmtree(self.tmp_dir)
|
2014-01-02 03:53:36 +00:00
|
|
|
assert_raises(OSError, get_pwd)
|
2013-12-31 22:33:32 +00:00
|
|
|
|
2014-01-01 00:35:57 +00:00
|
|
|
|
|
|
|
class FileSystemIntegrationTests(TestCase):
|
|
|
|
@class_setup
|
|
|
|
def init(self):
|
|
|
|
self.tmp_dir = os.path.join(gettempdir(), 'autojump')
|
|
|
|
os.makedirs(self.tmp_dir)
|
|
|
|
|
|
|
|
@class_teardown
|
|
|
|
def cleanup(self):
|
|
|
|
try:
|
|
|
|
rmtree(self.tmp_dir)
|
|
|
|
except OSError:
|
|
|
|
pass
|
|
|
|
|
|
|
|
def get_random_path(self):
|
|
|
|
path = gettempdir()
|
|
|
|
|
|
|
|
while os.path.exists(path):
|
|
|
|
random_string = '%30x' % randrange(16 ** 30)
|
|
|
|
path = os.path.join(self.tmp_dir, random_string)
|
|
|
|
|
|
|
|
return path
|
|
|
|
|
|
|
|
def get_random_file(self):
|
|
|
|
path = self.get_random_path()
|
|
|
|
with open(path, 'w+') as f:
|
|
|
|
f.write('filler\n')
|
|
|
|
|
|
|
|
return path
|
|
|
|
|
|
|
|
def test_create_dir(self):
|
|
|
|
path = self.get_random_path()
|
|
|
|
create_dir(path)
|
|
|
|
assert_true(os.path.exists(path))
|
|
|
|
|
|
|
|
# should not raise OSError if directory already exists
|
|
|
|
create_dir(path)
|
|
|
|
assert_true(os.path.exists(path))
|
|
|
|
|
|
|
|
def test_move_file(self):
|
|
|
|
src = self.get_random_file()
|
|
|
|
dst = self.get_random_path()
|
|
|
|
assert_true(os.path.exists(src))
|
|
|
|
assert_false(os.path.exists(dst))
|
|
|
|
move_file(src, dst)
|
2014-01-02 03:53:36 +00:00
|
|
|
assert_false(os.path.exists(src))
|
2014-01-01 00:35:57 +00:00
|
|
|
assert_true(os.path.exists(dst))
|
|
|
|
|
|
|
|
|
2014-01-02 04:00:24 +00:00
|
|
|
class HelperFunctionsUnitTests(TestCase):
|
2014-01-01 00:39:45 +00:00
|
|
|
def test_get_needle(self):
|
2014-01-02 03:53:36 +00:00
|
|
|
assert_equal(
|
|
|
|
get_tab_entry_info('foo__', '__'),
|
|
|
|
('foo', None, None))
|
2014-01-01 00:39:45 +00:00
|
|
|
|
|
|
|
def test_get_index(self):
|
2014-01-02 03:53:36 +00:00
|
|
|
assert_equal(
|
|
|
|
get_tab_entry_info('foo__2', '__'),
|
|
|
|
('foo', 2, None))
|
2014-01-01 00:39:45 +00:00
|
|
|
|
|
|
|
def test_get_path(self):
|
|
|
|
assert_equal(
|
2014-01-02 04:00:24 +00:00
|
|
|
get_tab_entry_info('foo__3__/foo/bar', '__'),
|
|
|
|
('foo', 3, '/foo/bar'))
|
|
|
|
|
|
|
|
def test_get_none(self):
|
|
|
|
assert_equal(
|
|
|
|
get_tab_entry_info('gibberish content', '__'),
|
|
|
|
(None, None, None))
|
2014-01-01 00:39:45 +00:00
|
|
|
|
|
|
|
|
2013-12-31 22:33:32 +00:00
|
|
|
if __name__ == "__main__":
|
|
|
|
run()
|