Skip to content

Commit

Permalink
Unit tests (#31)
Browse files Browse the repository at this point in the history
Thanks @f18m!
  • Loading branch information
f18m authored and lovesegfault committed Jan 10, 2019
1 parent 50a17cc commit 258330f
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 15 deletions.
17 changes: 3 additions & 14 deletions beautysh/beautysh.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ def __init__(self):
self.tab_str = ' '
self.tab_size = 4
self.backup = False
self.check_only = False

def read_file(self, fp):
"""Read input file."""
Expand Down Expand Up @@ -181,15 +180,9 @@ def beautify_file(self, path):
data = self.read_file(path)
result, error = self.beautify_string(data, path)
if(data != result):
if(self.check_only):
if not error:
# we want to return 0 (success) only if the given file is already
# well formatted:
error = (result != data)
else:
if(self.backup):
self.write_file(path+'.bak', data)
self.write_file(path, result)
if(self.backup):
self.write_file(path+'.bak', data)
self.write_file(path, result)
return error

def main(self):
Expand All @@ -205,9 +198,6 @@ def main(self):
parser.add_argument('--backup', '-b', action='store_true',
help="Beautysh will create a backup file in the "
"same path as the original.")
parser.add_argument('--check', '-c', action='store_true',
help="Beautysh will just check the files without doing "
"any in-place beautify.")
parser.add_argument('--tab', '-t', action='store_true',
help="Sets indentation to tabs instead of spaces")
args = parser.parse_args()
Expand All @@ -218,7 +208,6 @@ def main(self):
args.indent_size = args.indent_size[0]
self.tab_size = args.indent_size
self.backup = args.backup
self.check_only = args.check
if (args.tab):
self.tab_size = 1
self.tab_str = '\t'
Expand Down
4 changes: 3 additions & 1 deletion setup.py
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -41,5 +41,7 @@ def get_version(file_name='beautysh/__init__.py'):
"Topic :: Utilities"
],
entry_points={'console_scripts': ['beautysh = beautysh.beautysh:main']},
py_modules=['beautysh']
py_modules=['beautysh'],
test_suite='nose.collector',
tests_require=['nose']
)
4 changes: 4 additions & 0 deletions tests/basictest.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
function dummyfunc()
{
local mytest=4
}
18 changes: 18 additions & 0 deletions tests/test_beautysh.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
from beautysh import Beautify
from unittest import TestCase
import os

TEST_BASIC_FILENAME = os.path.join(os.path.dirname(__file__), 'basictest.sh')

class TestBasic(TestCase):

def read_file(self, fp):
"""Read input file."""
with open(fp) as f:
return f.read()

def test_basic(self):
testdata = self.read_file(TEST_BASIC_FILENAME)
result, error = Beautify().beautify_string(testdata)
self.assertFalse(error); # we expect no parsing error
self.assertTrue(testdata == result) # we expect no change in formatting

0 comments on commit 258330f

Please sign in to comment.