Skip to content

Commit

Permalink
add pre-commit configuration
Browse files Browse the repository at this point in the history
same as zkg

initial run
  • Loading branch information
ottobackwards committed Jan 25, 2023
1 parent 4a3512d commit 1318eb0
Show file tree
Hide file tree
Showing 17 changed files with 848 additions and 649 deletions.
21 changes: 21 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# See https://pre-commit.com for more information
# See https://pre-commit.com/hooks.html for more hooks
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.4.0
hooks:
- id: trailing-whitespace
exclude: testing/baselines
- id: end-of-file-fixer
exclude: testing/baselines
- id: check-yaml
- id: check-added-large-files
- repo: https://github.com/psf/black
rev: 23.1a1
hooks:
- id: black
- repo: https://github.com/asottile/pyupgrade
rev: v3.3.1
hooks:
- id: pyupgrade
args: ["--py37-plus"]
59 changes: 30 additions & 29 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,66 +7,67 @@

class BinaryDistribution(Distribution):
"""Distribution which always forces a binary package with platform name"""

# https://stackoverflow.com/a/62668026
def has_ext_modules(self):
return True


class InstallPlatlib(install):
"""Additional tweak to treat the package as platform-specific."""

# https://github.com/google/or-tools/issues/616#issuecomment-371480314
def finalize_options(self):
install.finalize_options(self)
self.install_lib = self.install_platlib


class BuildCommand(setuptools.command.build_py.build_py):
"""A customized build command that also rebuilds the parser bindings as needed."""

def run(self):
self.refresh_bindings()
super().run() # Run regular build procedure
super().run() # Run regular build procedure

def refresh_bindings(self):
try:
import tree_sitter
except ImportError:
print('Warning: tree_sitter module not found, not refreshing bindings')
print("Warning: tree_sitter module not found, not refreshing bindings")
return

# Recompile the tree-sitter bindings. This is a no-op when the parser
# needs no rebuild. We specify the library output path and the
# tree-sitter repo path to include.
tree_sitter.Language.build_library('./zeekscript/zeek-language.so',
['tree-sitter-zeek'])
tree_sitter.Language.build_library(
"./zeekscript/zeek-language.so", ["tree-sitter-zeek"]
)


setup(
name='zeekscript',
version=open('VERSION').read().replace('-', '.dev', 1).strip(),
description='A Zeek script formatter and analyzer',
long_description=open('README.md').read(),
long_description_content_type='text/markdown',
keywords='zeek scripts language formatter formatting indenter indenting parsing',
maintainer='The Zeek Project',
maintainer_email='[email protected]',
url='https://github.com/zeek/zeekscript',

scripts=['zeek-format', 'zeek-script'],
packages=['zeekscript'],
package_data={'zeekscript': ['zeek-language.so']},

name="zeekscript",
version=open("VERSION").read().replace("-", ".dev", 1).strip(),
description="A Zeek script formatter and analyzer",
long_description=open("README.md").read(),
long_description_content_type="text/markdown",
keywords="zeek scripts language formatter formatting indenter indenting parsing",
maintainer="The Zeek Project",
maintainer_email="[email protected]",
url="https://github.com/zeek/zeekscript",
scripts=["zeek-format", "zeek-script"],
packages=["zeekscript"],
package_data={"zeekscript": ["zeek-language.so"]},
cmdclass={
'build_py': BuildCommand,
'install': InstallPlatlib,
"build_py": BuildCommand,
"install": InstallPlatlib,
},

distclass=BinaryDistribution,

setup_requires=['tree_sitter'],
install_requires=['tree_sitter'],
python_requires='>3.7.0',

setup_requires=["tree_sitter"],
install_requires=["tree_sitter"],
python_requires=">3.7.0",
classifiers=[
'Programming Language :: Python :: 3.7',
'License :: OSI Approved :: BSD License',
'Topic :: Utilities',
"Programming Language :: Python :: 3.7",
"License :: OSI Approved :: BSD License",
"Topic :: Utilities",
],
)
18 changes: 8 additions & 10 deletions tests/data/test1.zeek
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
##! A test script with all kinds of formatting errors.
##!
##! This Zeekygen head comment has multiple lines with more detail
##! This Zeekygen head comment has multiple lines with more detail
##! about this module. It spans two lines.

@load foo/bar/baz.zeek # A "preprocessor" line with comment
Expand All @@ -10,14 +10,14 @@
redef Broker::default_port = to_port(getenv( "ZEEK_PORT"));
@endif

module Test;
module Test;

export {
# A regular comment
# A regular comment
type An::ID: enum {
## A Zeekygen comment
ENUM_VAL1, ##< A Zeekygen post-comment
##< that continues on the next line
##< that continues on the next line
## Anoter Zeekygen comment
PRINTLOG
};
Expand All @@ -30,7 +30,7 @@ module Test;

# Another one that we put on one line. That should also remain
type SingeLineEnum: enum { FOO, BAR };

## A constant.
const a_constant=T &redef ;

Expand All @@ -41,7 +41,7 @@ module Test;
global a_function : function(foo: BAR) :bool;

## A lambda.
const a_lambda: function( foo: string ) = function (foo: string) {
const a_lambda: function( foo: string ) = function (foo: string) {
} &redef;

}
Expand Down Expand Up @@ -150,7 +150,7 @@ function blanklines() {

foo();
bar();

# With one comment
baz(); # and another comment

Expand All @@ -169,5 +169,3 @@ function comments()
function no_comments()
{
}


16 changes: 10 additions & 6 deletions tests/test_all.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,15 @@
import test_dir_recursion
import test_pylint

if __name__ == '__main__':
if __name__ == "__main__":
# Each test() call returns True if successful, so only exit with 0 when they
# all succeed.
sys.exit(not all((
test_formatting.test(),
test_dir_recursion.test(),
test_pylint.test(),
)))
sys.exit(
not all(
(
test_formatting.test(),
test_dir_recursion.test(),
test_pylint.test(),
)
)
)
84 changes: 49 additions & 35 deletions tests/test_dir_recursion.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
from os.path import join

TESTS = os.path.dirname(os.path.realpath(__file__))
ROOT = os.path.normpath(join(TESTS, '..'))
DATA = os.path.normpath(join(TESTS, 'data'))
ROOT = os.path.normpath(join(TESTS, ".."))
DATA = os.path.normpath(join(TESTS, "data"))

# Prepend the tree's root folder to the module searchpath so we find zeekscript
# via it. This allows tests to run without package installation. (We do need a
Expand All @@ -20,21 +20,21 @@

import zeekscript

class TestRecursion(unittest.TestCase):

class TestRecursion(unittest.TestCase):
def setUp(self):
# Set up a small directory tree with Zeek scripts and other files
shutil.rmtree('a', ignore_errors=True)
os.makedirs(join('a', 'b', 'c'))
shutil.rmtree("a", ignore_errors=True)
os.makedirs(join("a", "b", "c"))

shutil.copy(join(DATA, 'test1.zeek'), join('a', 'test1.zeek'))
shutil.copy(join(DATA, 'test1.zeek'), join('a', 'test2.zeek'))
shutil.copy(join(DATA, 'test1.zeek'), join('a', 'b', 'test3.txt'))
shutil.copy(join(DATA, 'test1.zeek'), join('a', 'b', 'test4.zeek'))
shutil.copy(join(DATA, 'test1.zeek'), join('a', 'b', 'c', 'test5.zeek'))
shutil.copy(join(DATA, "test1.zeek"), join("a", "test1.zeek"))
shutil.copy(join(DATA, "test1.zeek"), join("a", "test2.zeek"))
shutil.copy(join(DATA, "test1.zeek"), join("a", "b", "test3.txt"))
shutil.copy(join(DATA, "test1.zeek"), join("a", "b", "test4.zeek"))
shutil.copy(join(DATA, "test1.zeek"), join("a", "b", "c", "test5.zeek"))

def tearDown(self):
shutil.rmtree('a', ignore_errors=True)
shutil.rmtree("a", ignore_errors=True)

def assertEqualContent(self, file1, file2):
with open(file1) as hdl1, open(file2) as hdl2:
Expand All @@ -47,59 +47,72 @@ def assertNotEqualContent(self, file1, file2):
def test_recursive_formatting(self):
parser = argparse.ArgumentParser()
zeekscript.add_format_cmd(parser)
args = parser.parse_args(['-i', '-r', 'a'])
args = parser.parse_args(["-i", "-r", "a"])

# Python < 3.10 does not yet support parenthesized context managers:
with unittest.mock.patch('sys.stdout', new=io.StringIO()) as out, \
unittest.mock.patch('sys.stderr', new=io.StringIO()) as err:
with unittest.mock.patch(
"sys.stdout", new=io.StringIO()
) as out, unittest.mock.patch("sys.stderr", new=io.StringIO()) as err:
ret = args.run_cmd(args)
self.assertEqual(ret, 0)
self.assertEqual(out.getvalue(), '4 files processed, 0 errors\n')
self.assertEqual(out.getvalue(), "4 files processed, 0 errors\n")

self.assertEqualContent(join(DATA, "test1.zeek.out"), join("a", "test1.zeek"))
self.assertEqualContent(join(DATA, "test1.zeek.out"), join("a", "test2.zeek"))
self.assertEqualContent(
join(DATA, 'test1.zeek.out'), join('a', 'test1.zeek'))
self.assertEqualContent(
join(DATA, 'test1.zeek.out'), join('a', 'test2.zeek'))
join(DATA, "test1.zeek.out"), join("a", "b", "test4.zeek")
)
self.assertEqualContent(
join(DATA, 'test1.zeek.out'), join('a', 'b', 'test4.zeek'))
self.assertEqualContent(
join(DATA, 'test1.zeek.out'), join('a', 'b', 'c', 'test5.zeek'))
join(DATA, "test1.zeek.out"), join("a", "b", "c", "test5.zeek")
)

self.assertNotEqualContent(
join(DATA, 'test1.zeek.out'), join('a', 'b', 'test3.txt'))
join(DATA, "test1.zeek.out"), join("a", "b", "test3.txt")
)

def test_recurse_inplace(self):
parser = argparse.ArgumentParser()
zeekscript.add_format_cmd(parser)
args = parser.parse_args(['-ir'])
args = parser.parse_args(["-ir"])

with unittest.mock.patch('sys.stdout', new=io.StringIO()) as out, \
unittest.mock.patch('sys.stderr', new=io.StringIO()) as err:
with unittest.mock.patch(
"sys.stdout", new=io.StringIO()
) as out, unittest.mock.patch("sys.stderr", new=io.StringIO()) as err:
ret = args.run_cmd(args)
self.assertEqual(ret, 0)
self.assertEqual(err.getvalue(), 'warning: cannot use --inplace when reading from stdin, skipping it\n')
self.assertEqual(
err.getvalue(),
"warning: cannot use --inplace when reading from stdin, skipping it\n",
)

def test_dir_without_recurse(self):
parser = argparse.ArgumentParser()
zeekscript.add_format_cmd(parser)
args = parser.parse_args(['-i', 'a'])
args = parser.parse_args(["-i", "a"])

with unittest.mock.patch('sys.stdout', new=io.StringIO()) as out, \
unittest.mock.patch('sys.stderr', new=io.StringIO()) as err:
with unittest.mock.patch(
"sys.stdout", new=io.StringIO()
) as out, unittest.mock.patch("sys.stderr", new=io.StringIO()) as err:
ret = args.run_cmd(args)
self.assertEqual(ret, 0)
self.assertEqual(err.getvalue(), 'warning: "a" is a directory but --recursive not set, skipping it\n')
self.assertEqual(
err.getvalue(),
'warning: "a" is a directory but --recursive not set, skipping it\n',
)

def test_recurse_without_inplace(self):
parser = argparse.ArgumentParser()
zeekscript.add_format_cmd(parser)
args = parser.parse_args(['-r', 'a'])
args = parser.parse_args(["-r", "a"])

with unittest.mock.patch('sys.stdout', new=io.StringIO()) as out, \
unittest.mock.patch('sys.stderr', new=io.StringIO()) as err:
with unittest.mock.patch(
"sys.stdout", new=io.StringIO()
) as out, unittest.mock.patch("sys.stderr", new=io.StringIO()) as err:
ret = args.run_cmd(args)
self.assertEqual(ret, 1)
self.assertEqual(err.getvalue(), 'error: recursive file processing requires --inplace\n')
self.assertEqual(
err.getvalue(), "error: recursive file processing requires --inplace\n"
)


def test():
Expand All @@ -111,5 +124,6 @@ def test():
# This is how unittest.main() implements the exit code itself:
return res.result.wasSuccessful()

if __name__ == '__main__':

if __name__ == "__main__":
sys.exit(not test())
Loading

0 comments on commit 1318eb0

Please sign in to comment.