def test_run(cls, expd_output, expd_retcode): """ See issue 36, tests cmake""" os.makedirs(os.path.join(cls.test_dir, "src"), exist_ok=True) with open(os.path.join(cls.test_dir, "src", "ok.c"), "w") as f: f.write(utils.test_file_strs["ok.c"]) with open(os.path.join(cls.test_dir, "CMakeLists.txt"), "w") as f: f.write(CMAKELISTS) child = sp.run( [ "cmake", "-DCMAKE_EXPORT_COMPILE_COMMANDS=ON", "-Wno-dev", cls.test_dir ], cwd=cls.test_dir, stdout=sp.PIPE, stderr=sp.PIPE, ) if len(child.stderr) != 0 or child.returncode != 0: pytest.fail("Problem occurred when testing iss36:" + child.stderr.decode()) output, retcode = utils.integration_test( "clang-tidy", [os.path.join(cls.test_dir, "src", "ok.c")], ["--fix", "--quiet", "-p=cmake-build-debug"], cls.test_dir, ) utils.assert_equal(expd_output, output) assert expd_retcode == retcode
def test_parse_request_happy(): env = { 'HTTP_ACCEPT': 'text/plain', 'HTTP_HOST': 'example.org', 'CONTENT_TYPE': 'text/plain', 'PATH_INFO': '/foo', 'REQUEST_METHOD': 'GET', 'QUERY_STRING': 'quux=bar', } expected = { 'path': ('foo', ), 'accept': [ MimeType('text', 'plain', {}), ], 'content_type': MimeType('text', 'plain', {}), 'headers': { 'Host': 'example.org', 'Accept': 'text/plain', }, 'query': { 'quux': 'bar' }, 'method': 'GET', 'input': None, } actual = parse_request(env) assert_equal(sorted(expected.items()), sorted(actual.items()))
def test_fmap_fn(): def foo(): return 1 expected = 2 actual = (fmap(lambda x: x + 1, foo))() assert_equal(expected, actual)
def test_prop_lens_set_immutable(): lens = prop_lens("foo") data = {"baz": "quux"} expected = {"baz": "quux"} Lens.set(lens, "bar", data) actual = data assert_equal(expected, actual)
def test_fmap_fn_return_none(): def foo(): return None expected = None actual = (fmap(lambda x: x + 1, foo))() assert_equal(expected, actual)
def test_version(cmd_class, version, expected_stderr, expected_retcode): args = [cmd_class.command + "-hook", "--version", version] sp_child = sp.run(args, stdout=sp.PIPE, stderr=sp.PIPE) actual_stderr = sp_child.stderr actual_retcode = sp_child.returncode utils.assert_equal(actual_stderr, expected_stderr) assert actual_retcode == expected_retcode
def test_hateoas_none(): src = None actual = hateoas(('hello', 'world'), src) expected = { '_parent': {'href': '/hello'}, '_self': {'href': '/hello/world'}, '_top': {'href': '/hello/world'}, } assert_equal(sorted(expected.items()), sorted(actual.items()))
def run_shell_cmd(cmd_class, args, fname, target_output, target_retcode): """Use command generated by setup.py and installed by pip Ex. oclint => oclint-hook for the hook command""" all_args = [cmd_class.command + "-hook", fname, *args] sp_child = sp.run(all_args, stdout=sp.PIPE, stderr=sp.PIPE) actual = str(sp_child.stdout + sp_child.stderr, encoding="utf-8") retcode = sp_child.returncode utils.assert_equal(target_output, actual) assert target_retcode == retcode
def test_hateoas_non_dict_collection(): src = ['bar'] actual = hateoas(('hello', 'world'), src) expected = { '_value': ['bar'], '_parent': {'href': '/hello'}, '_self': {'href': '/hello/world'}, '_top': {'href': '/hello/world'}, } assert_equal(sorted(expected.items()), sorted(actual.items()))
def test_hateoas_string(): src = 'bar' actual = hateoas(('hello', 'world'), src) expected = { '_value': 'bar', '_parent': {'href': '/hello'}, '_self': {'href': '/hello/world'}, '_top': {'href': '/hello/world'}, } assert_equal(sorted(expected.items()), sorted(actual.items()))
def test_hateoas_wrong_type(): src = True actual = hateoas(('hello', 'world'), src) expected = { '_value': True, '_parent': {'href': '/hello'}, '_self': {'href': '/hello/world'}, '_top': {'href': '/hello/world'}, } assert_equal(sorted(expected.items()), sorted(actual.items()))
def test_parse_http_accept_sorted(): src = "text/*;q=0.3,text/plain;q=0.6,*/*;q=0.1" expected = [("text", "plain", { "q": "0.6" }), ("text", "*", { "q": "0.3" }), ("*", "*", { "q": "0.1" })] actual = parse_http_accept(src, sort=True) assert_equal(expected, actual)
def test_rcompose(): def a(i): return i * 7 def b(i): return i + 5 composed = rcompose(a, b) expected = (3 * 7) + 5 actual = composed(3) assert_equal(expected, actual)
def test_hateoas_offset(): src = None actual = hateoas(('hello', 'world'), src, offset=30, count=10) expected = { '_parent': {'href': '/hello'}, '_self': {'href': '/hello/world?count=10&offset=30'}, '_next': {'href': '/hello/world?count=10&offset=40'}, '_prev': {'href': '/hello/world?count=10&offset=20'}, '_top': {'href': '/hello/world?count=10'}, } assert_equal(sorted(expected.items()), sorted(actual.items()))
def test_compose(): def a(i): return i * 7 def b(i): return i + 5 composed = compose(a, b) expected = (3 + 5) * 7 actual = composed(3) assert_equal(expected, actual)
def test_hateoas_paging(): src = None actual = hateoas(('hello', 'world'), src, page=10) expected = { '_parent': {'href': '/hello'}, '_self': {'href': '/hello/world?page=10'}, '_next': {'href': '/hello/world?page=11'}, '_prev': {'href': '/hello/world?page=9'}, '_top': {'href': '/hello/world'}, } assert_equal(sorted(expected.items()), sorted(actual.items()))
def test_get_headers_happy(): env = { 'HTTP_ACCEPT': 'text/plain', 'HTTP_HOST': 'example.org', } expected = { 'Accept': 'text/plain', 'Host': 'example.org', } actual = get_headers(env) assert_equal(expected, actual)
def test_hateoas_happy_with_page(): src = {'foo': 'bar'} actual = hateoas(('hello', 'world'), src, page=2) expected = { 'foo': 'bar', '_parent': {'href': '/hello'}, '_self': {'href': '/hello/world?page=2'}, '_next': {'href': '/hello/world?page=3'}, '_prev': {'href': '/hello/world?page=1'}, '_top': {'href': '/hello/world'}, } assert_equal(sorted(expected.items()), sorted(actual.items()))
def run_shell_cmd(cmd_name, files, args, _, target_output, target_retcode): """Use command generated by setup.py and installed by pip Ex. oclint => oclint-hook for the hook command""" all_args = files + args cmd_to_run = [cmd_name + "-hook", *all_args] sp_child = sp.run(cmd_to_run, stdout=sp.PIPE, stderr=sp.PIPE) actual = sp_child.stdout + sp_child.stderr # Output is unpredictable and platform/version dependent if any([f.endswith("err.cpp") for f in files]) and "-std=c++20" in args: actual = re.sub(rb"[\d,]+ warnings and ", b"", actual) retcode = sp_child.returncode utils.assert_equal(target_output, actual) assert target_retcode == retcode
def run_integration_test(cmd_name, files, args, test_dir, target_output, target_retcode): """Run integration tests by 1. Convert arg lists to .pre-commit-config.yaml text 2. Set the .pre-commit-config.yaml in a directory with test files 3. Run `git init; pre-commit install; git add .; git commit` against the files """ output_actual, actual_returncode = utils.integration_test( cmd_name, files, args, test_dir) if output_actual == b"": pytest.fail("pre-commit should provide output, but none found.") utils.assert_equal(target_output, output_actual) assert target_retcode == actual_returncode
def test_root_get(): class MyResource(object): def get_typed_body(self, *args, **kwargs): return parse_mime_type("text/plain"), "HI!" def application(env, start_response): return serve_resource(MyResource())(env, start_response) expected = { 'status': '200 OK', 'headers': [('Content-type', 'text/plain')], 'body': b'HI!' } actual = mock_request(application, "GET", "/") assert_equal(expected, actual)
def run_cmd_class(cmd_class, args, fname, target_output, target_retcode): """Test the command class in each python hook file""" cmd = cmd_class([fname] + args) if target_retcode == 0: cmd.run() else: with pytest.raises(SystemExit): cmd.run() # If this continues with no system exit, print info print("stdout:`" + cmd.stdout + "`") print("stderr:`" + cmd.stderr + "`") print("returncode:", cmd.returncode) actual = cmd.stdout + cmd.stderr retcode = cmd.returncode utils.assert_equal(target_output, actual) assert target_retcode == retcode
def test_parse_path_root(): expected = () actual = parse_path('/') assert_equal(expected, actual)
def test_parse_path_happy(): expected = ('foo', 'bar', 'baz') actual = parse_path('/foo/bar/baz') assert_equal(expected, actual)
def test_parse_mime_props(): src = "text/plain;charset=utf8;q=0.2" expected = ("text", "plain", {"charset": "utf8", "q": "0.2"}) actual = parse_mime_type(src) assert_equal(expected, actual)
def test_parse_mime_degenerate(): src = "text/plain;charset;q=0.2" expected = ("text", "plain", {"charset": "", "q": "0.2"}) actual = parse_mime_type(src) assert_equal(expected, actual)
def test_parse_path_trailing_slash(): expected = ('foo', 'bar', 'baz') actual = parse_path('/foo/bar/baz/') assert_equal(expected, actual)
def test_parse_path_no_leading_slash(): expected = ('foo', 'bar', 'baz') actual = parse_path('foo/bar/baz') assert_equal(expected, actual)
def test_parse_path_empty(): expected = () actual = parse_path('') assert_equal(expected, actual)
def test_parse_mime_tuple(): src = ("text", "plain") expected = ("text", "plain", {}) actual = parse_mime_type(src) assert_equal(expected, actual)