Files
aji/tests/hosted/source_independent_errors.py
T

99 lines
5.1 KiB
Python

#!/usr/bin/env python3
"""Independent source errors, conservative boundaries, limits and no partial AIR."""
import json
from pathlib import Path
import re
import subprocess
import tempfile
REPO = Path(__file__).resolve().parents[2]
AJI = REPO / 'zig-out/bin/aji'
CORPUS = REPO / 'tests/conformance/source/independent_errors'
checks = 0
def cli(*args, code=1):
global checks
result = subprocess.run([str(AJI), *map(str, args)], capture_output=True, text=True)
assert result.returncode == code, (args, result.returncode, result.stdout[:4000], result.stderr[:4000])
checks += 1
return result.stdout
def records(output):
return [json.loads(line) for line in output.splitlines()]
cache = REPO / '.zig-cache/contract-gate'
cache.mkdir(parents=True, exist_ok=True)
with tempfile.TemporaryDirectory(prefix='independent-', dir=cache) as directory:
root = Path(directory)
lib, entry, manifest = (root / name for name in ('lib.aji', 'main.aji', 'aji.toml'))
entry.write_text('module app\nimport lib.collection;\nfn main() -> void effects [] {}\n')
manifest.write_text('[package]\nname = "collection"\nversion = "1"\nentry = "main.aji"\n'
'[modules]\nlib.collection = "lib.aji"\n')
for path in sorted(CORPUS.glob('*.aji')):
source = path.read_text()
ds = records(cli('check', path))
wanted = re.search(r'// expect: reject (.+)', source)[1].split()
assert sorted(d['code'] for d in ds) == sorted(wanted), (path, ds)
assert len(ds) <= 32 and ds, ds
assert all(d['file'] == str(path) and 'air_line' not in d and 'air_code' not in d for d in ds), ds
# Exactly one diagnostic per code and source location, in declaration order.
assert len({(d['code'], d['file'], d['line'], d['col']) for d in ds}) == len(ds), ds
assert [(d['line'], d['col']) for d in ds] == sorted((d['line'], d['col']) for d in ds), ds
for command in ('lower', 'run', 'report', 'explore'):
assert records(cli(command, path)) == ds
assert records(cli('check', path)) == ds
if ds[0]['code'] == 'E7001':
for mode in ((), ('--check',), ('--write',)):
assert records(cli('fmt', *mode, path)) == ds
assert path.read_text() == source # failed fmt --write cannot modify input
if path.stem == 'three_syntax':
assert [d['repair_candidates'][0]['kind'] for d in ds] == [
'boolean_negation', 'annotate_var_type', 'zero_storage'], ds
elif path.stem in ('limit_syntax', 'limit_bodies'):
assert len(ds) == 32 and ds[-1]['line'] == 36, ds
elif path.stem == 'generic_body':
assert len(ds) == 1 and ds[0]['name'] == 'missing', ds
# Imported parser/body failures preserve the list, spans and advice.
if path.stem in ('three_syntax', 'two_types', 'callee', 'generic_body', 'state_reset'):
lib.write_text(source.replace('module collection', 'module lib.collection')
.replace('fn main(', 'fn user('))
package = records(cli('check', '--manifest', manifest))
assert package == [dict(d, file='lib.aji') for d in ds], package
for command in ('lower', 'run', 'report', 'explore'):
assert records(cli(command, '--manifest', manifest)) == package
# The first failed imported module stops traversal of siblings. Its syntax
# list is retained; a root syntax failure instead stops before imports.
second = root / 'other.aji'
second.write_text('module lib.other\nfn bad() -> bool effects [] { return !false; }\n')
lib.write_text('module lib.collection\nfn a() -> bool effects [] { return !false; }\n'
'fn b() -> bool effects [] { return !true; }\n')
entry.write_text('module app\nimport lib.collection;\nimport lib.other;\n'
'fn main() -> void effects [] {}\n')
manifest.write_text(manifest.read_text() + 'lib.other = "other.aji"\n')
ds = records(cli('check', '--manifest', manifest))
assert len(ds) == 2 and all(d['file'] == 'lib.aji' for d in ds), ds
entry.write_text(entry.read_text().replace('effects [] {}', 'effects [] { let x = !false; }'))
ds = records(cli('check', '--manifest', manifest))
assert len(ds) == 1 and ds[0]['file'] == str(entry), ds
# Unimplemented preserves the previous phase outcome when first, but does
# not suppress independent errors once body collection has begun.
probe = root / 'probe.aji'
unsupported = 'fn unsupported() -> void effects [] { 1u64(); }\n'
bad = 'fn bad(n: u64) -> u32 effects [] { return n as u32; }\n'
main = 'fn main() -> void effects [] {}\n'
probe.write_text('module probe\n' + unsupported + bad + main)
assert cli('check', probe, code=3) == ''
probe.write_text('module probe\n' + bad + unsupported +
'fn later(n: u64) -> u16 effects [] { return n as u16; }\n' + main)
ds = records(cli('check', probe))
assert [d['code'] for d in ds] == ['E7005', 'E7005'], ds
for command in ('lower', 'run', 'report', 'explore'):
assert records(cli(command, probe)) == ds
print(f'independent source errors: {checks} checks passed')