Skip to content

Commit

Permalink
prettify: factor out selftest to separate _TEST.py file
Browse files Browse the repository at this point in the history
  • Loading branch information
dev-zero committed May 16, 2019
1 parent 6ef98e5 commit 9192da5
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 39 deletions.
4 changes: 2 additions & 2 deletions tools/docker/scripts/test_python.sh
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ ERRORS=0
cd /workspace/cp2k

# find executable python scripts
ALL_SCRIPTS=$(find ./src/ ./tools/ -name "*.py" -executable)
ESSENTIAL_SCRIPTS=$(find ./tools/build_utils -name "*.py" -executable)
ALL_TEST_SCRIPTS=$(find ./src/ ./tools/ -name "*_test.py" -executable)
ESSENTIAL_TEST_SCRIPTS=$(find ./tools/build_utils -name "*_test.py" -executable)

# python 2.6
run_selftests python2.6 "${ESSENTIAL_SCRIPTS}"
Expand Down
38 changes: 1 addition & 37 deletions tools/prettify/prettify.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
from prettify_cp2k import normalizeFortranFile
from prettify_cp2k import replacer
from fprettify import reformat_ffile, fparse_utils, log_exception
from prettify_cp2k import selftest


OPERATORS_STR = r"\.(?:and|eqv?|false|g[et]|l[et]|n(?:e(?:|qv)|ot)|or|true)\."
Expand Down Expand Up @@ -380,41 +379,6 @@ def main(argv=None):
"Processing file '" + fileName + "'\n")
return(failure > 0)

#=========================================================================


def run_selftest():
# create temporary file with example code
fn = os.path.join(tempfile.gettempdir(), "prettify_selftest.F")
ref = selftest.content
f = open(fn, "w")
f.write(ref)
f.close()

# call prettify
rtn = main([sys.argv[0], fn])
assert(rtn == 0)

# check if file was altered
result = open(fn).read()
for i, (l1, l2) in enumerate(zip(result.split("\n"), ref.split("\n"))):
if(l1 != l2):
print("Error: Line %d is not invariant." % i)
print("before: " + l1)
print("after : " + l2)
os.remove(fn)
return(1)

os.remove(fn)
print("Prettify selftest passed.")
return(0)

#=========================================================================
if(__name__ == '__main__'):
if(len(sys.argv) == 2 and sys.argv[-1] == "--selftest"):
rtn = run_selftest()
else:
rtn = main()

sys.exit(rtn)
# EOF
sys.exit(main())
38 changes: 38 additions & 0 deletions tools/prettify/prettify_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#!/usr/bin/env python

from __future__ import absolute_import

import unittest
import os
import tempfile
import shutil
import sys

from prettify import main
from prettify_cp2k import selftest

class TestSingleFileFolder(unittest.TestCase):
def setUp(self):
self.tempdir = tempfile.mkdtemp()
self.fname = os.path.join(self.tempdir, "prettify_selftest.F")

# create temporary file with example code
with open(self.fname, "w") as fhandle:
fhandle.write(selftest.content)

def tearDown(self):
shutil.rmtree(self.tempdir)

def test_prettify(self):

# call prettify, the return value should be 0 (OK)
self.assertEqual(main([sys.argv[0], self.fname]), 0)

# check if file was altered (it shouldn't)
with open(self.fname) as fhandle:
result = fhandle.read()

self.assertEqual(result.splitlines(), selftest.content.splitlines())

if __name__ == '__main__':
unittest.main()

0 comments on commit 9192da5

Please sign in to comment.