Skip to content

Commit

Permalink
Separate compatibility tests (#261)
Browse files Browse the repository at this point in the history
* Separate compatibility tests

Signed-off-by: Shaygan <[email protected]>

* Run compatibility tests on CI

Signed-off-by: Shaygan <[email protected]>

* Update snapshots

* Add exclamation token

* Fix lexer incompatibility with python on mypy source

* Fix clippy warnings

* Ignore mypy master

---------

Signed-off-by: Shaygan <[email protected]>
  • Loading branch information
Glyphack authored Sep 1, 2024
1 parent 4eb6050 commit f744bb3
Show file tree
Hide file tree
Showing 41 changed files with 1,960 additions and 1,776 deletions.
21 changes: 19 additions & 2 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,12 @@ jobs:
with:
rust-version: stable
components: rustfmt, clippy
# Required for compatibility tests
# Required for compatibility unit tests
- uses: actions/setup-python@v5
with:
python-version: '3.12'
- name: Build
run: cargo build

- name: Run tests
env:
RUST_BACKTRACE: 1
Expand All @@ -38,6 +37,24 @@ jobs:
run: make format-check
- name: clippy
run: make lint
compatibility-test-complete:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
with:
submodules: true
- uses: Swatinem/rust-cache@v2
- name: setup toolchain
uses: hecrj/setup-rust-action@v2
with:
rust-version: stable
- uses: actions/setup-python@v5
with:
python-version: '3.12'
- name: Run Compatibility Tests
run: |
cargo run -p enderpy-compat --bin enderpy-compat
# coverage:
# name: coverage
# runs-on: ubuntu-latest
Expand Down
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,5 @@ out/
**/dist/
# vscode extension
*.vsix

mypy-master/
4 changes: 1 addition & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[workspace]
members = ["parser", "enderpy", "typechecker", "lsp", "benchmark"]
members = ["parser", "enderpy", "typechecker", "lsp", "benchmark", "compat"]
resolver = "2"

[workspace.package]
Expand Down Expand Up @@ -31,5 +31,3 @@ opt-level = 3
lto = "fat"
opt-level = 3
# panic = "abort"


23 changes: 23 additions & 0 deletions compat/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
[package]
name = "enderpy-compat"
version = "0.0.0"
authors = { workspace = true }
edition = { workspace = true }
rust-version = { workspace = true }
homepage = { workspace = true }
documentation = { workspace = true }
repository = { workspace = true }
license = { workspace = true }

[dependencies]
serde_json = "1.0"
serde = "1.0"
miette = "5.5"
enderpy_python_parser = { path = "../parser"}
tabled = "0.15"
terminal_size = "0.3"
assert-json-diff = "2.0"
pretty_assertions = "1.4"
which = "6.0.1"
reqwest = { version = "0.11", features = ["blocking"] }
zip = "0.6"
45 changes: 33 additions & 12 deletions parser/ast_python.py → compat/ast_python.py
Original file line number Diff line number Diff line change
@@ -1,25 +1,30 @@
import sys
import ast
from _ast import AST # Python internals I guess?
from _ast import AST # Python internals I guess?
import argparse
import pathlib
import codecs
import json

arg_parser = argparse.ArgumentParser(
description="Parse a Python program to AST."
)
arg_parser = argparse.ArgumentParser(description="Parse a Python program to AST.")
arg_parser.add_argument("--input-file", help="Read and parse input file.")
arg_parser.add_argument("--stdin", action="store_true", help="Read and parse input from stdin.")
arg_parser.add_argument("--type-comments", action="store_true", help="Produce an AST with type comments.")
arg_parser.add_argument(
"--stdin", action="store_true", help="Read and parse input from stdin."
)
arg_parser.add_argument(
"--type-comments", action="store_true", help="Produce an AST with type comments."
)
args = arg_parser.parse_args()

if args.input_file is not None:
source = pathlib.Path(args.input_file).read_text()
elif args.stdin:
source = sys.stdin.read()
else:
print("Missing input parameter. Please specify one of --input-file or --stdin.", file=sys.stderr)
print(
"Missing input parameter. Please specify one of --input-file or --stdin.",
file=sys.stderr,
)
sys.exit(1)

# ----- Begin inline dependency -------------------------------------------------------------------
Expand Down Expand Up @@ -53,16 +58,19 @@

BUILTIN_PURE = (int, float, bool)
BUILTIN_BYTES = (bytearray, bytes)
BUILTIN_STR = (str)
BUILTIN_STR = str


def decode_str(value):
return value


def decode_bytes(value):
try:
return value.decode('utf-8')
return value.decode("utf-8")
except:
return codecs.getencoder('hex_codec')(value)[0].decode('utf-8')
return codecs.getencoder("hex_codec")(value)[0].decode("utf-8")


def ast2json(node):
assert isinstance(node, AST)
Expand All @@ -72,8 +80,13 @@ def ast2json(node):
if attr.startswith("_") or attr == "n" or attr == "s":
continue
to_return[attr] = get_value(getattr(node, attr))
to_return.pop("lineno", None)
to_return.pop("end_lineno", None)
to_return.pop("col_offset", None)
to_return.pop("end_col_offset", None)
return to_return


def get_value(attr_value):
if attr_value is None:
return attr_value
Expand All @@ -92,11 +105,19 @@ def get_value(attr_value):
if isinstance(attr_value, type(Ellipsis)):
return "..."
else:
raise Exception("Unknown case for '%s' of type '%s'" % (attr_value, type(attr_value)))
raise Exception(
"Unknown case for '%s' of type '%s'" % (attr_value, type(attr_value))
)


# -------------------------------------------------------------------- End inline dependency ------


tree = ast.parse(source, filename=args.input_file or "stdin", mode="exec", type_comments=args.type_comments)
tree = ast.parse(
source,
filename=args.input_file or "stdin",
mode="exec",
type_comments=args.type_comments,
)
tree_json = ast2json(tree)
print(json.dumps(tree_json, indent=4))
File renamed without changes.
Loading

0 comments on commit f744bb3

Please sign in to comment.