xref: /linux/scripts/dtc/dt-style-selftest/run.sh (revision bba2c3615bd6cfee7456d1130f2e6b01b3f4e9ba)
1#!/bin/sh
2# SPDX-License-Identifier: GPL-2.0-only
3#
4# Run dt-check-style against fixtures under good/ and bad/.
5# good/ files must produce no output and exit 0 in both modes.
6# bad/ files must produce the expected output (in expected/<name>.txt)
7# and exit 1.
8#
9# The mode used for a bad fixture is whichever produces a violation:
10# trailing-whitespace and tab fixtures use the default (relaxed),
11# the rest use --mode=strict. The expected output files name the
12# mode in their first line.
13
14set -u
15
16here=$(cd "$(dirname "$0")" && pwd)
17tool="$here/../dt-check-style"
18fail=0
19
20run() {
21    file=$1
22    mode=$2
23    "$tool" --mode="$mode" "$file" 2>&1
24}
25
26# good/ -- must exit 0 and produce no output in both modes
27for f in "$here"/good/*; do
28    [ -e "$f" ] || continue
29    for mode in relaxed strict; do
30        out=$(run "$f" "$mode")
31        rc=$?
32        if [ -n "$out" ] || [ "$rc" -ne 0 ]; then
33            echo "FAIL good/$mode: $(basename "$f") (exit $rc, want 0):"
34            echo "$out" | sed 's/^/  /'
35            fail=$((fail + 1))
36        fi
37    done
38done
39
40# bad/ -- must match expected/<name>.txt
41for f in "$here"/bad/*; do
42    [ -e "$f" ] || continue
43    name=$(basename "$f")
44    expected="$here/expected/$name.txt"
45    if [ ! -f "$expected" ]; then
46        echo "FAIL bad: missing $expected"
47        fail=$((fail + 1))
48        continue
49    fi
50    mode=$(head -1 "$expected" | sed 's/^# mode=//')
51    body=$(tail -n +2 "$expected")
52    out=$(run "$f" "$mode")
53    rc=$?
54    # Strip the directory prefix so expected files are portable.
55    out=$(printf '%s\n' "$out" | sed "s|$here/bad/|bad/|g")
56    if [ "$out" != "$body" ] || [ "$rc" -ne 1 ]; then
57        echo "FAIL bad/$mode: $name (exit $rc, want 1):"
58        bf=$(mktemp)
59        printf '%s\n' "$body" > "$bf"
60        printf '%s\n' "$out" | diff -u "$bf" - | sed 's/^/  /'
61        rm -f "$bf"
62        fail=$((fail + 1))
63    fi
64done
65
66if [ "$fail" -eq 0 ]; then
67    echo "PASS"
68    exit 0
69fi
70echo "FAILED ($fail)"
71exit 1
72