Files
Echoion/scripts/tests/test-frontend-licenses.sh
T
faumaray 639362fbf5 Update
Signed-off-by: faumaray <faumaray@gmail.com>
2026-07-16 19:59:57 +03:00

135 lines
4.5 KiB
Bash
Executable File

#!/usr/bin/env bash
set -euo pipefail
if [[ $# != 1 ]]; then
echo "usage: $0 SOURCE_ROOT" >&2
exit 2
fi
source_root=$(realpath -e -- "$1")
temporary=$(mktemp -d "${TMPDIR:-/tmp}/echoion-frontend-licenses.XXXXXX")
trap 'rm -rf -- "$temporary"' EXIT
node_modules="$temporary/node_modules"
mkdir -p \
"$node_modules/plain/docs" \
"$node_modules/@scope/example" \
"$node_modules/parent/node_modules/duplicate" \
"$node_modules/second/node_modules/duplicate" \
"$node_modules/no-notice"
printf '%s\n' \
'{"name":"plain","version":"1.2.3","license":"MIT"}' \
>"$node_modules/plain/package.json"
printf '%s\n' 'plain license' >"$node_modules/plain/LICENSE"
printf '%s\n' 'ignored nested file' >"$node_modules/plain/docs/LICENSE"
printf '%s\n' '{"name":"not-a-package-root"}' \
>"$node_modules/plain/docs/package.json"
printf '%s\n' \
'{"name":"@scope/example","version":"2.0.0-beta.1","license":"Apache-2.0"}' \
>"$node_modules/@scope/example/package.json"
printf '%s\n' 'scoped license' >"$node_modules/@scope/example/LICENCE.md"
printf '%s\n' 'scoped notice' >"$node_modules/@scope/example/NOTICE-extra"
for package_root in \
"$node_modules/parent/node_modules/duplicate" \
"$node_modules/second/node_modules/duplicate"; do
printf '%s\n' \
'{"name":"duplicate","version":"3.4.5","licenses":[{"type":"BSD-2-Clause"}]}' \
>"$package_root/package.json"
printf '%s\n' 'duplicate copying' >"$package_root/COPYING.txt"
done
printf '%s\n' \
'{"name":"no-notice","version":"4.0.0","license":"ISC"}' \
>"$node_modules/no-notice/package.json"
output_one="$temporary/output-one"
output_two="$temporary/output-two"
(
umask 077
python3 "$source_root/scripts/collect-frontend-licenses.py" \
--node-modules "$node_modules" \
--output "$output_one"
)
python3 "$source_root/scripts/collect-frontend-licenses.py" \
--node-modules "$node_modules" \
--output "$output_two"
diff -r -- "$output_one" "$output_two"
[[ -f $output_one/plain@1.2.3/LICENSE ]]
[[ ! -e $output_one/plain@1.2.3/docs ]]
[[ -f $output_one/@scope%2Fexample@2.0.0-beta.1/LICENCE.md ]]
[[ -f $output_one/@scope%2Fexample@2.0.0-beta.1/NOTICE-extra ]]
[[ -f $output_one/duplicate@3.4.5/COPYING.txt ]]
[[ -d $output_one/no-notice@4.0.0 ]]
if find "$output_one" -type d ! -perm 0755 -print -quit | grep -q .; then
echo "license collection created a directory with an unstable mode" >&2
exit 1
fi
if find "$output_one" -type f ! -perm 0644 -print -quit | grep -q .; then
echo "license collection created a file with an unstable mode" >&2
exit 1
fi
python3 - "$output_one/THIRD-PARTY-NOTICES.txt" <<'PY'
from pathlib import Path
import sys
contents = Path(sys.argv[1]).read_text(encoding="utf-8")
packages = [
line.removeprefix("Package: ")
for line in contents.splitlines()
if line.startswith("Package: ")
]
assert packages == sorted(packages)
assert packages == [
"@scope/example@2.0.0-beta.1",
"duplicate@3.4.5",
"no-notice@4.0.0",
"plain@1.2.3",
]
assert contents.count("Package: duplicate@3.4.5") == 1
assert "Declared license: BSD-2-Clause" in contents
assert "Notice files: none published at package root" in contents
assert "@scope%2Fexample@2.0.0-beta.1/NOTICE-extra" in contents
PY
mkdir -p "$temporary/malformed/node_modules/bad"
printf '%s\n' '{"name":"bad","version":"1.0.0"}' \
>"$temporary/malformed/node_modules/bad/package.json"
error_file="$temporary/malformed.error"
if python3 "$source_root/scripts/collect-frontend-licenses.py" \
--node-modules "$temporary/malformed/node_modules" \
--output "$temporary/malformed-output" \
>/dev/null 2>"$error_file"; then
echo "package without license metadata unexpectedly passed" >&2
exit 1
fi
grep -Fq 'package has no declared license' "$error_file"
mkdir -p \
"$temporary/conflict/node_modules/first/node_modules/same" \
"$temporary/conflict/node_modules/second/node_modules/same"
for package_root in \
"$temporary/conflict/node_modules/first/node_modules/same" \
"$temporary/conflict/node_modules/second/node_modules/same"; do
printf '%s\n' \
'{"name":"same","version":"1.0.0","license":"MIT"}' \
>"$package_root/package.json"
done
printf '%s\n' first \
>"$temporary/conflict/node_modules/first/node_modules/same/LICENSE"
printf '%s\n' second \
>"$temporary/conflict/node_modules/second/node_modules/same/LICENSE"
if python3 "$source_root/scripts/collect-frontend-licenses.py" \
--node-modules "$temporary/conflict/node_modules" \
--output "$temporary/conflict-output" >/dev/null 2>&1; then
echo "conflicting duplicate package contents unexpectedly passed" >&2
exit 1
fi
echo "frontend npm license collection regressions passed"