Skip to content

Commit

Permalink
Rewrite diff workflow to make it compare properly (#1405)
Browse files Browse the repository at this point in the history
* change workflow

* simplify return values

* exit properly oops

* Update .github/workflows/build.yml

* Fix result check

* Run `black`

---------

Co-authored-by: Robin Avery <[email protected]>
  • Loading branch information
thefoxcam and ribbanya authored Jul 29, 2024
1 parent e80e81a commit 2afbcda
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 14 deletions.
19 changes: 5 additions & 14 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ jobs:
expected="$root/expected-report.json"
actual="$root/actual-report.json"
changes="$root/changes-report.json"
head=$(git rev-parse HEAD)
ninja all_source
objdiff-cli report generate -o "$actual"
Expand All @@ -134,24 +135,14 @@ jobs:
;;
*) exit 1 ;;
esac
ninja all_source
objdiff-cli report generate -o "$expected"
git checkout "$head"
objdiff-cli report changes -o "$changes" "$expected" "$actual"
results="$(jq '
.units[]
| select(.functions[]
| { from: .from.fuzzy_match_percent,
to: .to.fuzzy_match_percent }
| .from == 100 and .to < .from)
| { (.name): ([.functions[] | .name]) }' \
"$changes")"
status="$(echo "$results" | jq -s 'if . == [] then 0 else 1 end')"
if [ "$status" -ne 0 ]; then
echo "::error::One or more functions were broken!"
echo "$results"
fi
python tools/diff_changes.py "$changes"
status="$?"
exit "$status"
;;
*) exit 1 ;;
Expand Down
59 changes: 59 additions & 0 deletions tools/diff_changes.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import argparse
import json
import sys
from typing import Optional


def process_json(data):
result = {}
for unit in data.get("units", []):
matching_functions = []
for function in unit.get("functions", []):
from_fuzzy = function.get("from", {}).get("fuzzy_match_percent")
to_fuzzy = function.get("to", {}).get("fuzzy_match_percent")
if from_fuzzy == 100 and to_fuzzy < from_fuzzy:
func_name = function.get("name")
matching_functions.append(f"{func_name} ({from_fuzzy}% -> {to_fuzzy})")

if matching_functions:
result[unit.get("name")] = matching_functions

return result


def process_input(input_file: Optional[str]) -> None:
content = ""
if input_file == "-" or not input_file:
content = json.load(sys.stdin)
else:
with open(input_file, "r") as f:
content = json.load(f)

result = process_json(content)
if not result:
print("No functions broken.")
sys.exit(0)
else:
print("::error::One or more functions were broken!")
sys.stdout.write(json.dumps(result, indent=2))
sys.exit(1)


if __name__ == "__main__":
parser = argparse.ArgumentParser(
description="List broken functions from the output of objdiff's `changes` command."
)
parser.add_argument(
"input_file",
metavar="INPUT_FILE",
nargs="?",
default="",
help='input file to process (use "-" for stdin)',
)
args = parser.parse_args()

if args.input_file != "":
process_input(args.input_file)
else:
print("No argument provided to diff_changes.py.")
sys.exit(1)

0 comments on commit 2afbcda

Please sign in to comment.