Check that all V*.elm modules are exposed.

This commit is contained in:
Gavin Panella 2018-10-25 09:13:29 +02:00
parent 01ab7d9421
commit 053778cad7
2 changed files with 70 additions and 1 deletions

View File

@ -6,6 +6,10 @@ SHELL:=env PATH=${PATH} /bin/sh
test: elm-stuff tests/elm-stuff node_modules
elm-test
.PHONY: checks
checks:
scripts/check-exposed.py
.PHONY: diff
diff: node_modules elm-stuff
if (elm-package diff | tee /dev/stderr | grep -q MAJOR); then echo "MAJOR changes are not allowed!"; exit 1; fi
@ -49,4 +53,4 @@ elm-stuff: elm-package.json node_modules
setup: node_modules elm-stuff tests/elm-stuff styleguide-app/elm-stuff
.PHONY: ci
ci: test format documentation.json diff styleguide-app/elm.js
ci: checks test format documentation.json diff styleguide-app/elm.js

65
scripts/check-exposed.py Executable file
View File

@ -0,0 +1,65 @@
#!/usr/bin/env python3
#
# Checks that `elm-package.json` contains an `exposed-modules` entry for every
# module in `src/Nri` (recursively) named `Vn.elm`, where `n` is a decimal
# number.
#
# On success, this emits nothing and exits 0.
#
# On failure, this emits a list of those modules not exposed, and exits 2.
#
import json
import os
import re
import sys
re_v_file = re.compile(r"^V[0-9]+[.]elm$")
re_module = re.compile(
r"""
# Zero or more blank lines or single-line comments.
(?:^$\n|^--.*$\n)*
# Optional port or effect modifier.
^(?:(?:port|effect)\s+)?
# The module declaration itself begins.
module
# Optional newline, comment, newline.
(?:\n\s+--.+\n)?
# After some whitespace, capture the module name.
\s+(?P<module>[^ ]+)\b
""",
flags=re.MULTILINE | re.VERBOSE)
def p_v_file(filename):
"""Is `filename` a versioned widget file?"""
return re_v_file.match(filename) is not None
def read_module(filename):
"""Read the module name from the Elm source."""
with open(filename, "r", encoding="utf-8") as elm:
return re_module.search(elm.read()).group("module")
def find_v_modules():
for dirname, dirnames, filenames in os.walk("src/Nri"):
filenames = filter(p_v_file, filenames)
filepaths = (os.path.join(dirname, filename) for filename in filenames)
yield from map(read_module, filepaths)
def read_exposed_modules():
with open("elm-package.json", "rb") as pkg:
yield from json.load(pkg).get("exposed-modules")
if __name__ == "__main__":
available = set(find_v_modules())
exposed = set(read_exposed_modules())
missing = available - exposed
for module in sorted(missing):
print("Not exposed:", module, file=sys.stderr)
raise SystemExit(
0 if len(missing) == 0 else 2)