Skip to content

Commit

Permalink
Add initial tests workflow
Browse files Browse the repository at this point in the history
  • Loading branch information
rootmos committed Nov 15, 2023
1 parent ce4eb55 commit 31ae88c
Show file tree
Hide file tree
Showing 3 changed files with 132 additions and 1 deletion.
18 changes: 18 additions & 0 deletions .github/workflows/tests.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
name: Tests
on:
push:
schedule:
- cron: "* * 7 * *"

jobs:
build:
runs-on: ubuntu-latest
steps:
- name: Check out repository code
uses: actions/checkout@v4

- name: Build
run: make build

- name: Check README.md
run: tools/is-clean --make --root=doc README.md
2 changes: 1 addition & 1 deletion doc/Makefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
THIS := $(shell dirname $(realpath $(firstword $(MAKEFILE_LIST))))
ROOT ?= $(realpath $(THIS)/..)
ROOT ?= ..
TOOLS ?= $(ROOT)/tools

export EXE ?= $(ROOT)/src/cli.exe
Expand Down
113 changes: 113 additions & 0 deletions tools/is-clean
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
#!/usr/bin/env python3

import argparse
import hashlib
import os
import subprocess
import sys
import tempfile

whoami = "is-clean"
env_prefix = whoami.upper().replace("-", "_") + "_"
def env(var, default=None):
return os.environ.get(env_prefix + var, default)

import logging
logger = logging.getLogger(whoami)

def is_clean(path, make=False, root=None, target=None, show_diff=False):
logger.info(f"checking: {path}")

digest = "sha256"
with open(path, "rb") as f:
current_digest = hashlib.new(digest, f.read()).hexdigest()
logger.info(f"current; {digest.upper()}: {current_digest}")

if make:
root = root or os.path.dirname(path) or os.getcwd()
logger.debug(f"root: {root}")

target = target or os.path.relpath(path, root)
logger.debug(f"target: {target}")

cmdline = [ os.environ.get("MAKE", "make") ]
cmdline += [ "-C", root ]
cmdline += [ target ]
logger.debug(f"running: {cmdline}")
subprocess.check_call(cmdline)

with open(path, "rb") as f:
generated_digest = hashlib.file_digest(f, digest).hexdigest()
logger.info(f"generated; {digest.upper()}: {generated_digest}")
current_digest = generated_digest

with tempfile.TemporaryDirectory(prefix=f"{whoami}-") as tmp:
head = os.path.join(tmp, "head")

with open(head, "xb") as f:
rel = os.path.relpath(path, start=root)
cmdline = ["git", "show", "HEAD:./" + rel]
logger.debug(f"running: {cmdline}")
bs = subprocess.check_output(cmdline, cwd=root)
head_digest = hashlib.new(digest, bs).hexdigest()
f.write(bs)

logger.info(f"HEAD; {digest.upper()}: {head_digest}")

if current_digest == head_digest:
return True

if show_diff:
subprocess.run(["diff", path, head])

return False

def setup_logger(level, logger=None):
level = level.upper()
if logger is None:
logger = logging.getLogger()
logger.setLevel(level)

handler = logging.StreamHandler()
handler.setLevel(level)
logger.addHandler(handler)

fmt = logging.Formatter(fmt="%(asctime)s:%(name)s:%(levelname)s %(message)s", datefmt="%Y-%m-%dT%H:%M:%S%z")
handler.setFormatter(fmt)

def parse_args():
parser = argparse.ArgumentParser(
description="Freshness checker",
formatter_class=argparse.ArgumentDefaultsHelpFormatter,
)

parser.add_argument("--log", default=env("LOG_LEVEL", "INFO"), help="set log level")

parser.add_argument("-m", "--make", action="store_true")
parser.add_argument("-r", "--root", metavar="PATH")
parser.add_argument("-t", "--target", metavar="TARGET")

parser.add_argument("-D", "--no-diff", action="store_true")

parser.add_argument("path", metavar="PATH")

return parser.parse_args()

def main():
args = parse_args()
setup_logger(args.log, logger)
logger.debug(f"args: {args}")

r = is_clean(
path = args.path,
make = args.make,
root = args.root,
target = args.target,
show_diff = not args.no_diff,
)

if not r:
sys.exit(1)

if __name__ == "__main__":
main()

0 comments on commit 31ae88c

Please sign in to comment.