Add tests and samples for legacy bash8

This change introduces some basic tests for bash8
checks, including a sample file that is used to verify
checked errors before/in the 0.1.0 release.

Change-Id: Icc8f9889e5539949a6475769d1083cd619c2c79e
This commit is contained in:
Mathew Odden
2014-05-13 22:29:46 -05:00
parent 7266653449
commit 2e40f65b2e
6 changed files with 128 additions and 0 deletions

View File

@@ -66,6 +66,10 @@ def print_error(error, line,
filelineno = fileinput.filelineno()
global ERRORS
ERRORS = ERRORS + 1
log_error(error, line, filename, filelineno)
def log_error(error, line, filename, filelineno):
print("%s: '%s'" % (error, line.rstrip('\n')))
print(" - %s: L%s" % (filename, filelineno))

View File

@@ -0,0 +1,4 @@
#!/bin/bash
# E001
somefunction args

View File

@@ -0,0 +1,3 @@
#!/bin/bash
echo "E002: Has a tab"

View File

@@ -0,0 +1,40 @@
#!/bin/bash
# E020
function somefunction () {
echo "E002: Has a tab"
echo "E003: Not an indent with multiple of 4"
}
# E001
somefunction args
# E010
for thing in things
do
run_things thing
done
while 0
do
run_thing
done
until 1
do
run_thing
done
# E011
if [ 0 ]
then
run_morethings
else
run_otherthings
fi
# E012
cat <<EOH
this heredoc is bad
# E004

View File

@@ -19,7 +19,10 @@ test_bash8
Tests for `bash8` module.
"""
import mock
from bash8 import bash8
from bash8.tests import base
@@ -43,3 +46,76 @@ class TestBash8(base.TestCase):
bash8.register_ignores('E001')
bash8.check_no_trailing_whitespace("if ")
self.assertEqual(bash8.ERRORS, 0)
@mock.patch('bash8.bash8.print_error')
def test_while_check_for_do(self, m_print_error):
test_line = 'while `do something args`'
bash8.check_for_do(test_line)
m_print_error.assert_called_once_with(
'E010: Do not on same line as while', test_line)
class TestBash8Samples(base.TestCase):
"""End to end regression testing of bash8 against script samples."""
def setUp(self):
super(TestBash8Samples, self).setUp()
log_error_patcher = mock.patch('bash8.bash8.log_error')
self.m_log_error = log_error_patcher.start()
self.addCleanup(log_error_patcher.stop)
def assert_error_found(self, error, lineno):
error_found = False
for call in self.m_log_error.call_args_list:
# unwrap args
args = call[0]
if (args[0].startswith(error) and
lineno == args[3]):
error_found = True
if not error_found:
self.fail('Error %s expected at line %d not found!' %
(error, lineno))
def test_sample_E001(self):
test_file = 'bash8/tests/samples/E001_bad.sh'
bash8.check_files(test_file, False)
self.assert_error_found('E001', 4)
def test_sample_E002(self):
test_file = 'bash8/tests/samples/E002_bad.sh'
bash8.check_files(test_file, False)
self.assert_error_found('E002', 3)
def test_pre_zero_dot_one_sample_file(self):
"""Test the sample file with all pre 0.1.0 release checks.
This is a legacy compatibility check to make sure we still
catch the same errors as we did before the first 0.1.0
release of the bash8 pypi package. There were no tests
before this, so it is our baseline regression check.
New checks shouldn't need to be added here, and should
have their own separate unit test and/or sample file checks.
"""
test_file = 'bash8/tests/samples/legacy_sample.sh'
bash8.check_files(test_file, False)
# NOTE(mrodden): E012 actually requires iterating more than one
# file to detect at the moment; this is bug
expected_errors = [
('E002', 4),
('E003', 6),
('E001', 10),
('E010', 13),
('E010', 18),
('E010', 23),
('E011', 29),
('E020', 3)
]
for error in expected_errors:
self.assert_error_found(*error)

View File

@@ -1,4 +1,5 @@
hacking>=0.5.6,<0.8
mock>=1.0
coverage>=3.6
discover