Skip to content
This repository was archived by the owner on Jan 21, 2020. It is now read-only.

Commit abd0893

Browse files
authored
Introduce a check links source files with documentation about them. (#307)
Signed-off-by: Bill Farner <[email protected]>
1 parent 61b254a commit abd0893

File tree

5 files changed

+66
-1
lines changed

5 files changed

+66
-1
lines changed

Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ lint:
5959
check-docs:
6060
@echo "+ $@"
6161
find . -name '*.md' | grep -v vendor/ | blockcheck
62+
./scripts/doc-source-check
6263

6364
build:
6465
@echo "+ $@"

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ environments while using shared components and consistent interfaces.
2525
_InfraKit_ makes extensive use of _Plugins_ to manage arbitrary systems in diverse environments, which can be composed
2626
to meet different needs.
2727

28-
See the [plugins](docs/plugins.md) documentation for more details.
28+
See the [plugins](docs/plugins) documentation for more details.
2929

3030

3131
## Building
File renamed without changes.

docs/plugins/instance.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Instance plugin
2+
3+
<!-- SOURCE-CHECKSUM pkg/spi/instance/* 0c778e96cbeb32043532709412e15e6cc86778d7153c65b1d7422b9f0b5901c89362666bfdd75de7 -->
4+
5+
## API

scripts/doc-source-check

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
#!/usr/bin/env bash
2+
3+
# Validate that documentation is kept up to date with the sources being documented.
4+
#
5+
# This is a low-tech approach to ensure that documentation is validated when relevant sources
6+
# are changed. A markdown file can 'call out' the sources being referenced using an HTML comment.
7+
# For example:
8+
#
9+
# <!-- SOURCE-CHECKSUM pkg/spi/instance/* ABCDEF -->
10+
#
11+
# This instructs the script to compute a checksum of the files under pkg/spi/instance and check
12+
# that it matches ABCDEF.
13+
#
14+
# The goal is to remind developers to update documentation when code changes. Hashing the source
15+
# files undoubtedly raises false positives, but mitigates stale documentation.
16+
17+
set -o errexit
18+
set -o nounset
19+
20+
HERE="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
21+
cd "$HERE/.."
22+
23+
# Check all markdown files we own.
24+
for f in $(find . -name '*.md' -not -path './vendor/*')
25+
do
26+
# Look for the sentinel string.
27+
doc_source_link=$(grep '<!-- SOURCE-CHECKSUM' "$f" || true)
28+
if [ "$doc_source_link" == "" ]
29+
then
30+
continue
31+
fi
32+
33+
echo -n "Validating source checksum in $f: "
34+
35+
# Split the command arguments.
36+
IFS=' ' read -r -a link_cmd <<< "$doc_source_link"
37+
if [ "${link_cmd[1]}" != "SOURCE-CHECKSUM" -o "${#link_cmd[@]}" -lt 5 ]
38+
then
39+
echo "FAIL"
40+
echo "Invalid SOURCE-CHECKSUM command"
41+
echo "<!-- SOURCE-CHECKSUM file-pattern checksum -->"
42+
exit 1
43+
fi
44+
45+
file_pattern="${link_cmd[2]}"
46+
checksum="${link_cmd[3]}"
47+
48+
# Use git to hash the source files specified, concatenating them to form a single hash string.
49+
hash=$(git hash-object $file_pattern | tr -d '\n')
50+
if [ "$hash" == "$checksum" ]
51+
then
52+
echo "PASS"
53+
else
54+
echo "FAIL"
55+
echo "Expected: $checksum"
56+
echo "Actual: $hash"
57+
exit 1
58+
fi
59+
done

0 commit comments

Comments
 (0)