mirror of
https://github.com/kuhyx/testsAndMisc.git
synced 2026-11-24 17:36:19 +01:00
Compare commits
No commits in common. "baeb1119d0a3e22e6b5b2a69d3e8ab990b7897c9" and "b9608bbdd86f6a86ce6d05e05032b25bf47714f7" have entirely different histories.
baeb1119d0
...
b9608bbdd8
33
.github/workflows/shell-tests.yml
vendored
33
.github/workflows/shell-tests.yml
vendored
@ -48,39 +48,6 @@ jobs:
|
||||
exit 1
|
||||
fi
|
||||
|
||||
# The lib/tests/ suites that came out of the 250-line-cap splits. These
|
||||
# are DISCOVERED rather than listed: every split adds another
|
||||
# lib/tests/run_all.sh, and a hardcoded list silently leaves each new
|
||||
# suite running only on the author's machine. Four such runners already
|
||||
# existed and none of them ran in CI until this step was added.
|
||||
#
|
||||
# Each runner is invoked directly, not as `bash <runner>`: the same
|
||||
# invocation the coverage wrapper needs, and it keeps the shebang
|
||||
# authoritative.
|
||||
- name: Run lib/tests suites
|
||||
run: |
|
||||
set -euo pipefail
|
||||
mapfile -t runners < <(find . -path ./node_modules -prune -o \
|
||||
-path '*/lib/tests/run_all.sh' -print | sort)
|
||||
if [ "${#runners[@]}" -eq 0 ]; then
|
||||
printf 'No lib/tests/run_all.sh found — the discovery glob is wrong\n'
|
||||
exit 1
|
||||
fi
|
||||
failed=0
|
||||
for r in "${runners[@]}"; do
|
||||
printf '\n=== %s ===\n' "$r"
|
||||
if "$r"; then
|
||||
printf 'PASS %s\n' "$r"
|
||||
else
|
||||
printf 'FAIL %s\n' "$r"
|
||||
failed=$((failed + 1))
|
||||
fi
|
||||
done
|
||||
if [ "$failed" -gt 0 ]; then
|
||||
printf '\n%d lib/tests suite(s) failed\n' "$failed"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
# Tests that assume Arch: they exercise the pacman wrapper, makepkg wrapper
|
||||
# and hosts-guard hooks, so they need an Arch userland rather than Ubuntu.
|
||||
# They run as root here, which is what lets them touch /etc safely — the
|
||||
|
||||
@ -1,20 +0,0 @@
|
||||
{
|
||||
"title": "Split install_leechblock.sh under the 250-line cap",
|
||||
"objective": "Bring linux_configuration/scripts/periodic_background/digital_wellbeing/install_leechblock.sh (485 lines) under the repo-wide 250-line file cap by moving its six install phases into four sourced libs under lib/, preserving behaviour exactly, and defusing the shfmt associative-array corruption that caused the previous attempt at this split to be reverted.",
|
||||
"acceptance_criteria": [
|
||||
"install_leechblock.sh and all four lib/leechblock_*.sh files are each at most 250 lines.",
|
||||
"install_leechblock.sh --help and an unrecognized-flag run are byte-identical to the pre-split baseline, with the same exit codes (0 and 2).",
|
||||
"verify_shell_split.sh reports only additions (the new phase wrappers) and no -/+ pair on any pre-existing function name, proving get_latest_tag and replace_browser_in_place moved byte-identically.",
|
||||
"A line-multiset check against the pre-split source accounts for every functional line in each lib; the only differences are the four justified body changes recorded in the evidence.",
|
||||
"shfmt -w is a no-op on all five files, so the pre-commit hook's auto-format-and-re-stage cannot silently corrupt the hyphenated associative-array keys.",
|
||||
"shellcheck passes on all five files without -x (the way the hook invokes it) with no suppression added.",
|
||||
"The new lib/tests/ suite passes (run_all.sh exits 0) and per-lib coverage is measured and recorded as a real number in the evidence rather than waived."
|
||||
],
|
||||
"out_of_scope": [
|
||||
"Reaching 100% shell coverage for the new libs. A first suite ships in this commit (29 assertions) with measured coverage recorded in the evidence; closing the remaining gap requires shimming sudo/network/pkill externals and is the follow-up.",
|
||||
"Teaching install_pacman_wrapper.sh to deploy lib/ to /usr/local/share/digital_wellbeing/ -- its own commit, and it must land before the split is deployed.",
|
||||
"Fixing install_ublock.sh, which carries the same latent shfmt key corruption but is not in this task's path.",
|
||||
"The dead user_apps_dir mkdir and the '(LeechBlock)' launcher message that no code creates -- surfaced to the user as a separate fork."
|
||||
],
|
||||
"verifier": "bash meta/scripts/check_file_length.sh --all for the cap; bash meta/scripts/verify_shell_split.sh HEAD <old> <old> <new...> for body equivalence; diff of --help/--bogus against the captured baseline; shellcheck and a shfmt -w idempotence check on all five files; pre-commit run --files on the staged set."
|
||||
}
|
||||
@ -1,81 +0,0 @@
|
||||
{
|
||||
"intent": "Bring install_leechblock.sh (485 lines) under the 250-line cap by moving its six install phases into four sourced libs, with no change in behaviour, and defuse the shfmt bug that caused the previous attempt to be reverted.",
|
||||
"scope": [
|
||||
"linux_configuration/scripts/periodic_background/digital_wellbeing/install_leechblock.sh: 485 -> 119 lines",
|
||||
"New: lib/leechblock_fetch.sh (114), lib/leechblock_config.sh (36), lib/leechblock_browsers.sh (184), lib/leechblock_firefox.sh (108)",
|
||||
"In scope after all: a first test suite for the new libs (lib/tests/), with measured coverage recorded below. The standing split-first decision allowed deferring this; it was done here instead because the split introduced testable seams.",
|
||||
"Non-goal: deploying lib/ to /usr/local/share/digital_wellbeing/ (its own commit; must land before the split is deployed)"
|
||||
],
|
||||
"changes": [
|
||||
"Moved six phases into libs: resolve_version + download_extension + get_latest_tag (fetch), inject_default_config (config), detect_browsers + replace_browser_in_place + wire_up_browsers (browsers, kept together because detect_browsers writes the BROWSERS/FIREFOXES arrays that wire_up_browsers reads), install_firefox_policy (firefox).",
|
||||
"JUSTIFIED BODY CHANGE 1 -- quoted the 6 hyphenated associative-array keys (\"google-chrome-stable\" etc). shfmt 3.13.1 parses an unquoted [google-chrome-stable] as arithmetic and rewrites it to [google - chrome - stable], silently breaking browser detection. Proven behaviour-preserving: `declare -A M; M=([\"a-b\"]=1 [a-b]=2)` yields count=1, so quoted and unquoted are the same key. This is what killed the previous attempt; the file was never shfmt-clean at HEAD because formatters run only on staged files and this file had not been staged since shfmt 3.13.1 landed.",
|
||||
"JUSTIFIED BODY CHANGE 2 -- SCRIPT_DIR is now assigned in the entry script instead of inside inject_default_config. Left in the lib, ${BASH_SOURCE[0]} would resolve to lib/, so DEFAULTS_SRC would miss, the whole block would fall to its warn/else branch and default blocking rules would never be seeded -- silently, since --help never reaches that code. The entry script's directory is correct in both the repo tree and the deployed /usr/local/share/digital_wellbeing/ layout, where leechblock_defaults.json and seed_leechblock_storage.js are siblings of the entry script.",
|
||||
"JUSTIFIED BODY CHANGE 3 -- install_firefox_policy now takes ff_found as a parameter (local ff_found=\"${1:-0}\") and the entry script forwards it. ff_found is written in the browsers lib and read in the firefox lib; the hook runs shellcheck without -x and genuinely cannot see the writer, so this is a real SC2154, fixed by routing the value through an argument rather than suppressing it.",
|
||||
"JUSTIFIED BODY CHANGE 4 (defect fix) -- added `require_cmd unzip`. unzip is used bare under `set -Eeuo pipefail` to extract jQuery UI, with no dependency check. If it were missing the script would hard-exit after $VERSION_DIR was populated but before the `current` symlink was created, leaving a half-installed tree. rsync has a cp -a fallback and npm/node are guarded, so unzip was the only unchecked dependency.",
|
||||
"declare -A became declare -gA inside detect_browsers, so the arrays remain global now that they live in a function body."
|
||||
],
|
||||
"verification": [
|
||||
{
|
||||
"command": "bash meta/scripts/check_file_length.sh --all",
|
||||
"result": "pass",
|
||||
"evidence": "Over-cap list went from 5 files to 4; install_leechblock.sh no longer listed. New files: 114/114/36/171/100 lines, all under 250."
|
||||
},
|
||||
{
|
||||
"command": "diff <baseline --help> <post-split --help>; diff <baseline --bogus> <post-split --bogus>",
|
||||
"result": "pass",
|
||||
"evidence": "Both byte-identical, exit codes 0 and 2 unchanged. Re-run after every step including after shfmt. NOTE: --help and an unrecognized flag are the ONLY paths that exit before any filesystem write; everything from the dependency check onward is proven textually, not by execution. This baseline does not exercise any extracted phase body."
|
||||
},
|
||||
{
|
||||
"command": "bash meta/scripts/verify_shell_split.sh HEAD <old> <old> <4 new libs>",
|
||||
"result": "pass",
|
||||
"evidence": "Six additions only (detect_browsers, download_extension, inject_default_config, install_firefox_policy, resolve_version, wire_up_browsers) -- the new phase wrappers. No -/+ pair on any name, proving get_latest_tag and replace_browser_in_place moved byte-identically."
|
||||
},
|
||||
{
|
||||
"command": "python3 line-multiset check of each lib against the pre-split source ranges",
|
||||
"result": "pass",
|
||||
"evidence": "Every functional line accounted for. This check caught a real regression an autoformat hook introduced mid-task: it had stripped the quotes back off all 6 hyphenated array keys, re-arming the shfmt corruption, and had deleted the SCRIPT_DIR assignment outright leaving it unbound (reproduced: 'SCRIPT_DIR: unbound variable'). Both were fixed and re-verified. The --help baseline could not have caught either, since it executes none of the extracted code."
|
||||
},
|
||||
{
|
||||
"command": "for f in <all 5>; do cp $f /tmp/chk.sh; shfmt -w /tmp/chk.sh; diff $f /tmp/chk.sh; done",
|
||||
"result": "pass",
|
||||
"evidence": "shfmt -w is a no-op on all five files. This matters because linux_configuration/.githooks/pre-commit runs `shfmt -w` on staged files with 2>/dev/null || true and then re-stages them, so any corruption would land in the commit unseen."
|
||||
},
|
||||
{
|
||||
"command": "shellcheck install_leechblock.sh lib/leechblock_*.sh (no -x, as the hook invokes it)",
|
||||
"result": "pass",
|
||||
"evidence": "rc=0, no findings, no suppressions added. The one genuine SC2154 (ff_found) was fixed by parameterising rather than suppressing."
|
||||
},
|
||||
{
|
||||
"command": "bash -c 'source libs; detect_browsers; wire_up_browsers; install_firefox_policy \"$ff_found\"'",
|
||||
"result": "pass",
|
||||
"evidence": "BROWSERS has 8 entries and BROWSERS[\"google-chrome-stable\"] resolves to 'Google Chrome', confirming the quoted keys are intact at runtime. ff_found=1 after wire_up_browsers and install_firefox_policy returns 0. Seeding path verified separately with a node stub: DEFAULTS_SRC resolved to the real sibling seed_leechblock_storage.js, proving the SCRIPT_DIR fix works."
|
||||
},
|
||||
{
|
||||
"command": "bash -n on all five files",
|
||||
"result": "pass",
|
||||
"evidence": "All parse cleanly."
|
||||
},
|
||||
{
|
||||
"command": "linux_configuration/.../digital_wellbeing/lib/tests/run_all.sh",
|
||||
"result": "pass",
|
||||
"evidence": "29 assertions, 0 failures (test_leechblock_fetch 14, test_leechblock_browsers 15). Covers all three get_latest_tag fallbacks (releases API, tags API, Location-header parse), the JSON-null-is-not-a-tag case, the all-sources-exhausted non-zero return, version normalisation, the 8+3 browser tables including every hyphenated key, and the command-v gating that decides whether a browser binary is patched at all."
|
||||
},
|
||||
{
|
||||
"command": "bash meta/scripts/shell_coverage.sh <run_all.sh> leechblock_<lib>.sh 0",
|
||||
"result": "measured, below 100%",
|
||||
"evidence": "MEASURED, not rounded: leechblock_fetch.sh 27/70 lines = 38.57%; leechblock_browsers.sh 31/85 = 36.47%; leechblock_config.sh and leechblock_firefox.sh 0% (never sourced by a test). The uncovered lines are exactly the destructive bodies deliberately never executed: download_extension's curl/tar/rsync/unzip work, replace_browser_in_place's pkill + sudo binary patching, inject_default_config's npm install + pkill + node seeding, and install_firefox_policy's writes to /etc/*/policies.json. Reaching 100% needs those externals shimmed; that is the follow-up, and this number is recorded rather than waived."
|
||||
}
|
||||
],
|
||||
"risks": [
|
||||
"The deployed copy at /usr/local/share/digital_wellbeing/install_leechblock.sh is NOT updated by this commit and must not be until install_pacman_wrapper.sh learns to copy lib/. Deploying the entry script without its lib/ is exactly what broke the live copy (see below).",
|
||||
"Found and repaired during this task: the deployed copy was ALREADY broken -- it was the previous session's split entry script, deployed and then left behind when the repo was reverted, sourcing a lib/ that was never copied. It failed with 'No such file or directory' on every invocation. Restored from the repo's pre-split version and verified working (--help). The broken copy is preserved at /tmp/lb_deployed_broken.sh. Severity was latent rather than active: pacman_wrapper.sh:843 returns early when ~/.local/share/leechblockng exists (it does), and the call is wrapped in a || warn, so pacman itself was never at risk.",
|
||||
"install_ublock.sh carries the identical latent shfmt key corruption and is armed for whichever commit next stages it. Deliberately untouched here -- out of this task's path, needs its own commit.",
|
||||
"Coverage for the four new libs is partial and measured (38.57% / 36.47% / 0% / 0%), not 0% across the board as originally planned. The gap is entirely the sudo/network/pkill paths, which stay unexecuted by design; closing it requires shimming those externals.",
|
||||
"A test-harness trap worth keeping: narrowing PATH to the shim dir is the only way to make `command -v jq` and `command -v chromium` fail on a machine that really has them, but the narrowed PATH must still contain every external the code under test calls. A missing mkdir aborted the suite under `set -e` with NO stderr, which under kcov looked like a coverage-tool failure rather than a PATH failure."
|
||||
],
|
||||
"rollback": [
|
||||
"git revert the commit; install_leechblock.sh returns to a single 485-line file and lib/leechblock_*.sh disappear.",
|
||||
"After rollback, re-run `bash install_leechblock.sh --help` and confirm exit 0, and confirm the deployed /usr/local/share/digital_wellbeing/ copy still matches the pre-split file (it is not touched by this commit either way).",
|
||||
"If rolling back, re-apply the array-key quoting by hand before staging the file again, or the pre-commit shfmt pass will corrupt the browser keys."
|
||||
]
|
||||
}
|
||||
@ -1,51 +0,0 @@
|
||||
{
|
||||
"intent": "Teach the pacman-wrapper installer to deploy install_leechblock.sh's lib/ directory alongside the entry script, so the deployed copy at /usr/local/share/digital_wellbeing/ can actually run after the split.",
|
||||
"scope": [
|
||||
"linux_configuration/scripts/periodic_background/digital_wellbeing/pacman/lib/protect_and_extras.sh: install_leechblock_payload also mirrors lib/",
|
||||
"linux_configuration/scripts/periodic_background/digital_wellbeing/pacman/install_pacman_wrapper.sh: two new path variables",
|
||||
"Non-goal: any change to install_leechblock.sh or its libs -- they shipped in 1dceca66 and are untouched here"
|
||||
],
|
||||
"changes": [
|
||||
"install_leechblock_payload now mirrors the four leechblock_*.sh libs into ${LEECHBLOCK_INSTALL_DIR}/lib and marks them executable. Before this, the deployer copied the entry script and three data files but no lib/, so after the split the deployed installer would die on its first `source`.",
|
||||
"The copy is a mirror (rm -rf then recreate), not a merge, so a lib removed upstream does not linger in the deployed tree.",
|
||||
"The glob is leechblock_*.sh, not *.sh, on purpose: lib/ is shared with unrelated music_detect.sh / music_monitor.sh / music_status.sh helpers that this payload has no business shipping to /usr/local.",
|
||||
"A missing lib/ now emits an explicit warning that the deployed installer will not run, rather than silently deploying a broken entry script."
|
||||
],
|
||||
"verification": [
|
||||
{
|
||||
"command": "install_leechblock_payload against a fake root (/tmp/fakeroot) with the real sources",
|
||||
"result": "pass",
|
||||
"evidence": "Deployed tree contains exactly install_leechblock.sh, leechblock_defaults.json, seed_leechblock_storage.js and lib/{browsers,config,fetch,firefox}.sh -- the four leechblock libs and none of the three music_* helpers."
|
||||
},
|
||||
{
|
||||
"command": "bash /tmp/fakeroot/digital_wellbeing/install_leechblock.sh --help",
|
||||
"result": "pass",
|
||||
"evidence": "Exit 0 with the normal usage text, proving the deployed entry script resolves its libs from the deployed lib/ rather than dying on `source`."
|
||||
},
|
||||
{
|
||||
"command": "inject_default_config in the deployed layout with a node stub",
|
||||
"result": "pass",
|
||||
"evidence": "DEFAULTS_SRC and the seeder resolved to /tmp/fakeroot/digital_wellbeing/*, not to lib/. This is the specific silent failure the SCRIPT_DIR fix in 1dceca66 targets, now confirmed in the deployed layout as well as the repo tree."
|
||||
},
|
||||
{
|
||||
"command": "Real deploy to /usr/local/share/digital_wellbeing/ + bash <deployed> --help + sha256sum comparison",
|
||||
"result": "pass",
|
||||
"evidence": "Live deployed copy runs (exit 0, usage text) and its sha256 is identical to the repo's install_leechblock.sh. The deployed lib/ holds the four libs, executable."
|
||||
},
|
||||
{
|
||||
"command": "shellcheck + shfmt idempotence + bash -n on both changed files",
|
||||
"result": "pass",
|
||||
"evidence": "shellcheck rc=0, shfmt -w is a no-op on both, both parse. 70 and 130 lines, under the 250-line cap."
|
||||
}
|
||||
],
|
||||
"risks": [
|
||||
"This commit closes a window that the split itself opened: between 1dceca66 and this change, running the deployer would have overwritten a working deployed copy with a split entry script and no libs. The live copy was repaired by hand during this session and is now deployed correctly from the fixed path.",
|
||||
"The deployer is reached via /etc/pacman.d/hooks/96-restore-pkg-wrappers.hook -> rewrap_pkg_managers.sh on pacman/pacman-git transactions, so the window was real but narrow.",
|
||||
"npm install --prefix still runs against LEECHBLOCK_INSTALL_DIR when package.json is present; unchanged by this commit."
|
||||
],
|
||||
"rollback": [
|
||||
"git revert this commit; install_leechblock_payload returns to copying the entry script without lib/.",
|
||||
"After rollback the deployed installer would break again on its next deploy, so also either revert 1dceca66 or hand-restore the pre-split entry script to /usr/local/share/digital_wellbeing/.",
|
||||
"Validate with: bash /usr/local/share/digital_wellbeing/install_leechblock.sh --help (expect exit 0)."
|
||||
]
|
||||
}
|
||||
@ -1,32 +0,0 @@
|
||||
{
|
||||
"intent": "Make the lib/tests/ suites produced by the 250-line-cap splits actually run in CI. Four such runners existed and none of them were executed by any workflow, so every assertion in them only ever ran on the author's machine.",
|
||||
"scope": [
|
||||
".github/workflows/shell-tests.yml: new 'Run lib/tests suites' step in the shell-tests job",
|
||||
"Non-goal: changing any test, or adding the Arch-only suites to the Ubuntu job"
|
||||
],
|
||||
"changes": [
|
||||
"Added a discovery-based step that finds every */lib/tests/run_all.sh and runs it, failing the job on any non-zero runner. Discovery rather than a hardcoded list: each cap split adds another runner, and a list silently leaves new suites unrun — which is exactly the state this fixes.",
|
||||
"The step fails loudly if discovery matches zero runners, so a future path change cannot degrade into a vacuous green step.",
|
||||
"Each runner is invoked directly rather than as `bash <runner>`, matching how the coverage wrapper must invoke it."
|
||||
],
|
||||
"verification": [
|
||||
{
|
||||
"command": "bash -c 'mapfile -t runners < <(find . -path ./node_modules -prune -o -path \"*/lib/tests/run_all.sh\" -print | sort); ...' (the step's logic, run verbatim under bash)",
|
||||
"result": "pass",
|
||||
"evidence": "Discovered 4 runners (digital_wellbeing, hosts, periodic_background, single_use/utils) and all 4 exited 0. failed=0."
|
||||
},
|
||||
{
|
||||
"command": "grep for Arch-only externals in the suites",
|
||||
"result": "pass",
|
||||
"evidence": "The only `command -v` probe across the harnesses is `unshare`, which exists on ubuntu-latest and is guarded by the probe. No suite invokes pacman, chattr, systemctl or adb at top level, so none of them need the Arch container job."
|
||||
}
|
||||
],
|
||||
"risks": [
|
||||
"The suites have only ever been executed on Arch. If one proves environment-sensitive on ubuntu-latest the shell-tests job goes red; the failure would be a genuine portability defect in that suite rather than a problem with this step.",
|
||||
"Discovery runs whatever it finds, so a future runner that is slow or has side effects would be picked up automatically. The lib/tests convention is side-effect-free by design (that is why these suites exist), and the zero-match guard is the tripwire if the convention moves."
|
||||
],
|
||||
"rollback": [
|
||||
"git revert the commit; the step disappears and the lib/tests suites go back to running only locally.",
|
||||
"After rollback, confirm the shell-tests workflow still parses and the bats + side-effect-free steps still run."
|
||||
]
|
||||
}
|
||||
@ -8,22 +8,6 @@
|
||||
|
||||
set -Eeuo pipefail
|
||||
|
||||
# Each phase of the install lives in a lib beside this file; this script
|
||||
# keeps the flag parsing, the shared configuration and the order the phases
|
||||
# run in.
|
||||
# Resolved here, in the entry script, so it points at the directory holding
|
||||
# leechblock_defaults.json and seed_leechblock_storage.js. Resolving it
|
||||
# inside lib/ would yield lib/ itself and silently skip default seeding.
|
||||
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
|
||||
LIB_DIR="$SCRIPT_DIR/lib"
|
||||
# shellcheck source=lib/leechblock_fetch.sh
|
||||
. "$LIB_DIR/leechblock_fetch.sh"
|
||||
# shellcheck source=lib/leechblock_config.sh
|
||||
. "$LIB_DIR/leechblock_config.sh"
|
||||
# shellcheck source=lib/leechblock_browsers.sh
|
||||
. "$LIB_DIR/leechblock_browsers.sh"
|
||||
# shellcheck source=lib/leechblock_firefox.sh
|
||||
. "$LIB_DIR/leechblock_firefox.sh"
|
||||
SCRIPT_NAME=${0##*/}
|
||||
|
||||
info() { printf "\033[1;34m[INFO]\033[0m %s\n" "$*"; }
|
||||
@ -92,7 +76,6 @@ require_cmd tar
|
||||
require_cmd find
|
||||
require_cmd sed
|
||||
require_cmd awk
|
||||
require_cmd unzip
|
||||
if ! command -v jq >/dev/null 2>&1; then
|
||||
warn "jq not found — will fall back to a simpler tag detection method."
|
||||
fi
|
||||
@ -108,12 +91,395 @@ REPO_NAME_CHROME="LeechBlockNG-chrome"
|
||||
# Use Chrome repo for Chromium-based browsers (the default target)
|
||||
REPO_NAME="$REPO_NAME_CHROME"
|
||||
|
||||
resolve_version
|
||||
download_extension
|
||||
get_latest_tag() {
|
||||
local repo="$1"
|
||||
local tag
|
||||
if command -v jq >/dev/null 2>&1; then
|
||||
tag=$(curl -fsSL "https://api.github.com/repos/${REPO_OWNER}/${repo}/releases/latest" | jq -r '.tag_name // empty' || true)
|
||||
if [[ -n $tag && $tag != "null" ]]; then
|
||||
echo "$tag"
|
||||
return 0
|
||||
fi
|
||||
# Fallback: try tags endpoint
|
||||
tag=$(curl -fsSL "https://api.github.com/repos/${REPO_OWNER}/${repo}/tags?per_page=1" | jq -r '.[0].name // empty' || true)
|
||||
if [[ -n $tag && $tag != "null" ]]; then
|
||||
echo "$tag"
|
||||
return 0
|
||||
fi
|
||||
fi
|
||||
# Fallback: follow redirect for /releases/latest to extract tag
|
||||
tag=$(curl -fsSLI "https://github.com/${REPO_OWNER}/${repo}/releases/latest" | awk -F'/tag/' '/^location:/I {print $2}' | tr -d '\r\n' || true)
|
||||
if [[ -n $tag ]]; then
|
||||
echo "$tag"
|
||||
return 0
|
||||
fi
|
||||
return 1
|
||||
}
|
||||
|
||||
if [[ -z $VERSION ]]; then
|
||||
info "Resolving latest release tag from GitHub…"
|
||||
if ! VERSION=$(get_latest_tag "$REPO_NAME"); then
|
||||
err "Failed to determine latest version tag"
|
||||
exit 1
|
||||
fi
|
||||
fi
|
||||
|
||||
if [[ ! $VERSION =~ ^v?[0-9]+(\.[0-9]+)*$ ]]; then
|
||||
warn "Version tag '$VERSION' doesn't look like vX[.Y[.Z]] — continuing anyway."
|
||||
fi
|
||||
|
||||
VERSION=${VERSION#v} # strip leading v for folder names
|
||||
TAG="v${VERSION}"
|
||||
|
||||
XDG_DATA_HOME=${XDG_DATA_HOME:-"$HOME/.local/share"}
|
||||
INSTALL_ROOT="$XDG_DATA_HOME/leechblockng"
|
||||
VERSION_DIR="$INSTALL_ROOT/$VERSION"
|
||||
CURRENT_LINK="$INSTALL_ROOT/current"
|
||||
|
||||
if [[ -d $VERSION_DIR && $FORCE -ne 1 ]]; then
|
||||
info "LeechBlockNG $VERSION already present at $VERSION_DIR (use --force to reinstall)."
|
||||
else
|
||||
info "Downloading LeechBlockNG $TAG source from GitHub…"
|
||||
tmpdir=$(mktemp -d)
|
||||
trap 'rm -rf "$tmpdir"' EXIT
|
||||
ARCHIVE_URL="https://github.com/${REPO_OWNER}/${REPO_NAME}/archive/refs/tags/${TAG}.tar.gz"
|
||||
ARCHIVE_FILE="$tmpdir/${REPO_NAME}-${TAG}.tar.gz"
|
||||
curl -fL --retry 3 -o "$ARCHIVE_FILE" "$ARCHIVE_URL"
|
||||
info "Extracting…"
|
||||
mkdir -p "$tmpdir/extract"
|
||||
tar -xzf "$ARCHIVE_FILE" -C "$tmpdir/extract"
|
||||
# The archive usually extracts to REPO_NAME-TAG/ …
|
||||
src_root=$(find "$tmpdir/extract" -maxdepth 1 -type d -name "${REPO_NAME}-*" | head -n1 || true)
|
||||
[[ -z $src_root ]] && {
|
||||
err "Could not locate extracted source root"
|
||||
exit 1
|
||||
}
|
||||
|
||||
# Find the extension manifest (support a couple of common layouts)
|
||||
manifest_path=$(find "$src_root" -maxdepth 5 -type f -name manifest.json | head -n1 || true)
|
||||
if [[ -z $manifest_path ]]; then
|
||||
err "manifest.json not found in the extracted archive. The project layout may have changed."
|
||||
exit 1
|
||||
fi
|
||||
ext_dir=$(dirname "$manifest_path")
|
||||
|
||||
mkdir -p "$INSTALL_ROOT"
|
||||
rm -rf "$VERSION_DIR"
|
||||
info "Installing to $VERSION_DIR…"
|
||||
mkdir -p "$VERSION_DIR"
|
||||
# Copy the extension directory as-is (avoid bringing tests or build scripts)
|
||||
rsync -a --delete "$ext_dir/" "$VERSION_DIR/" 2>/dev/null || cp -a "$ext_dir/." "$VERSION_DIR/"
|
||||
|
||||
# Download jQuery UI (not included in repo — listed in .gitignore)
|
||||
# The extension's options.html expects:
|
||||
# jquery-ui/jquery-ui.min.css
|
||||
# jquery-ui/external/jquery/jquery.js
|
||||
# jquery-ui/jquery-ui.min.js
|
||||
info "Downloading jQuery UI…"
|
||||
jqui_version="1.14.1"
|
||||
jqui_url="https://jqueryui.com/resources/download/jquery-ui-${jqui_version}.zip"
|
||||
jqui_zip="$tmpdir/jquery-ui.zip"
|
||||
curl -fL --retry 3 -o "$jqui_zip" "$jqui_url"
|
||||
mkdir -p "$tmpdir/jqui-extract"
|
||||
unzip -q "$jqui_zip" -d "$tmpdir/jqui-extract"
|
||||
jqui_src=$(find "$tmpdir/jqui-extract" -maxdepth 1 -type d -name "jquery-ui-*" | head -n1 || true)
|
||||
if [[ -n $jqui_src ]]; then
|
||||
mkdir -p "$VERSION_DIR/jquery-ui/external/jquery"
|
||||
cp "$jqui_src/jquery-ui.min.css" "$VERSION_DIR/jquery-ui/" 2>/dev/null || true
|
||||
cp "$jqui_src/jquery-ui.min.js" "$VERSION_DIR/jquery-ui/" 2>/dev/null || true
|
||||
cp "$jqui_src/external/jquery/jquery.js" "$VERSION_DIR/jquery-ui/external/jquery/" 2>/dev/null || true
|
||||
info "✓ jQuery UI ${jqui_version} installed into extension"
|
||||
else
|
||||
warn "Could not extract jQuery UI — options page may not work correctly"
|
||||
fi
|
||||
|
||||
ln -sfn "$VERSION_DIR" "$CURRENT_LINK"
|
||||
fi
|
||||
|
||||
EXT_PATH="$CURRENT_LINK" # stable path used by wrappers
|
||||
|
||||
inject_default_config
|
||||
detect_browsers
|
||||
wire_up_browsers
|
||||
install_firefox_policy "$ff_found"
|
||||
# ── Inject default blocking configuration ─────────────────────────────
|
||||
# Write default blocking rules directly into Chrome's LevelDB extension
|
||||
# storage via Node.js (classic-level). This is content-verification-proof:
|
||||
# we never touch any extension JS file, so Chrome cannot detect tampering.
|
||||
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
|
||||
DEFAULTS_SRC="$SCRIPT_DIR/leechblock_defaults.json"
|
||||
|
||||
if [[ -f $DEFAULTS_SRC ]]; then
|
||||
|
||||
# Ensure classic-level is available next to this script.
|
||||
if [[ ! -d "$SCRIPT_DIR/node_modules/classic-level" ]]; then
|
||||
info "Installing classic-level npm package into $SCRIPT_DIR ..."
|
||||
npm install --prefix "$SCRIPT_DIR" 2>&1 | grep -v '^npm warn' || true
|
||||
fi
|
||||
|
||||
# Chrome locks its LevelDB files while running — close all Chromium browsers
|
||||
# so the write succeeds.
|
||||
pkill -f 'google-chrome|chromium|brave-browser|vivaldi|thorium' 2>/dev/null || true
|
||||
sleep 1
|
||||
|
||||
# Seed defaults into every Chrome/Chromium profile found on this machine.
|
||||
if node "$SCRIPT_DIR/seed_leechblock_storage.js" "$DEFAULTS_SRC"; then
|
||||
info "Seeded default LeechBlock settings into browser storage"
|
||||
else
|
||||
warn "Could not seed LeechBlock defaults — run manually after install:"
|
||||
warn " node $SCRIPT_DIR/seed_leechblock_storage.js $DEFAULTS_SRC"
|
||||
fi
|
||||
else
|
||||
warn "leechblock_defaults.json not found at $DEFAULTS_SRC — skipping default config"
|
||||
fi
|
||||
|
||||
# Detect browsers
|
||||
declare -A BROWSERS
|
||||
BROWSERS=(
|
||||
[chromium]="Chromium"
|
||||
[google-chrome-stable]="Google Chrome"
|
||||
[google-chrome]="Google Chrome"
|
||||
[brave-browser]="Brave"
|
||||
[vivaldi-stable]="Vivaldi"
|
||||
[vivaldi]="Vivaldi"
|
||||
[opera]="Opera"
|
||||
[thorium-browser]="Thorium"
|
||||
)
|
||||
|
||||
declare -A FIREFOXES
|
||||
FIREFOXES=(
|
||||
[firefox]="Firefox"
|
||||
[firefox-developer-edition]="Firefox Developer Edition"
|
||||
[librewolf]="LibreWolf"
|
||||
)
|
||||
|
||||
found_any=0
|
||||
|
||||
# Create a user desktop entry
|
||||
user_apps_dir="${XDG_DATA_HOME:-$HOME/.local/share}/applications"
|
||||
mkdir -p "$user_apps_dir"
|
||||
|
||||
# Inject --load-extension into a browser launcher so every launch includes LeechBlock.
|
||||
# Handles two cases:
|
||||
# 1) The binary is a shell script with an "exec" line — patch it in-place.
|
||||
# 2) The binary is a compiled ELF — wrap it with a shell script.
|
||||
# Follows symlinks only one level to avoid breaking shared wrapper scripts
|
||||
# (e.g. browser-preexec-wrapper used by multiple browser symlinks).
|
||||
# Requires sudo for system paths.
|
||||
replace_browser_in_place() {
|
||||
local bin="$1"
|
||||
shift
|
||||
local pretty="$1"
|
||||
shift
|
||||
|
||||
local bin_path
|
||||
bin_path=$(command -v "$bin" || true)
|
||||
[[ -z $bin_path ]] && return
|
||||
|
||||
# Resolve symlinks to find the actual file to patch.
|
||||
# Use readlink -f to get the canonical path.
|
||||
local real_bin
|
||||
real_bin=$(readlink -f "$bin_path")
|
||||
|
||||
local load_ext_flag="--load-extension=\"$EXT_PATH\""
|
||||
|
||||
# If already patched, skip (idempotent)
|
||||
if grep -q -- "$load_ext_flag" "$real_bin" 2>/dev/null; then
|
||||
info "$pretty ($bin) already has LeechBlock --load-extension — skipping"
|
||||
found_any=1
|
||||
return
|
||||
fi
|
||||
|
||||
# Case 1: Shell script with an exec line — patch the exec line directly.
|
||||
# This preserves the original script's logic (e.g. basename routing,
|
||||
# hosts enforcement) and avoids breaking shared wrapper scripts.
|
||||
if file "$real_bin" 2>/dev/null | grep -qi 'text\|script'; then
|
||||
if grep -qE '^exec ' "$real_bin"; then
|
||||
info "Patching exec line in $real_bin to add LeechBlock…"
|
||||
|
||||
# Kill running instances so the patched script takes effect
|
||||
pkill -f "$real_bin" 2>/dev/null || true
|
||||
sleep 1
|
||||
|
||||
# Back up before patching (only once)
|
||||
local orig_backup="${real_bin}.orig"
|
||||
if [[ ! -f $orig_backup ]]; then
|
||||
info "Backing up $real_bin → $orig_backup"
|
||||
sudo cp -a "$real_bin" "$orig_backup"
|
||||
fi
|
||||
|
||||
# Insert --load-extension right after "exec <command>" on the exec line.
|
||||
# Matches: exec "$real_bin" "$@" or exec /path/to/bin $FLAGS "$@"
|
||||
sudo sed -i "s|^exec \(.*\) \"\\\$@\"|exec \1 $load_ext_flag \"\\\$@\"|" "$real_bin"
|
||||
|
||||
info "✓ $pretty exec line patched with LeechBlock"
|
||||
found_any=1
|
||||
return
|
||||
fi
|
||||
fi
|
||||
|
||||
# Case 2: Binary or script without a recognisable exec line — wrap it.
|
||||
local orig_backup="${real_bin}.orig"
|
||||
|
||||
# Kill running instances
|
||||
info "Killing running $pretty instances…"
|
||||
pkill -f "$real_bin" 2>/dev/null || true
|
||||
pkill -f "$(basename "$real_bin")" 2>/dev/null || true
|
||||
sleep 1
|
||||
|
||||
# Back up original
|
||||
if [[ ! -f $orig_backup ]]; then
|
||||
info "Backing up $real_bin → $orig_backup"
|
||||
sudo cp -a "$real_bin" "$orig_backup"
|
||||
else
|
||||
info "Backup already exists: $orig_backup"
|
||||
fi
|
||||
|
||||
# Write replacement wrapper
|
||||
info "Replacing $real_bin with LeechBlock wrapper…"
|
||||
sudo tee "$real_bin" >/dev/null <<WRAP
|
||||
#!/usr/bin/env bash
|
||||
# __LEECHBLOCK_WRAPPER__ — auto-generated by install_leechblock.sh
|
||||
# Original backed up at: $orig_backup
|
||||
exec "$orig_backup" --load-extension="$EXT_PATH" "\$@"
|
||||
WRAP
|
||||
sudo chmod +x "$real_bin"
|
||||
|
||||
info "✓ $pretty now always launches with LeechBlock"
|
||||
found_any=1
|
||||
}
|
||||
|
||||
info "Detecting installed browsers…"
|
||||
for bin in "${!BROWSERS[@]}"; do
|
||||
if command -v "$bin" >/dev/null 2>&1; then
|
||||
replace_browser_in_place "$bin" "${BROWSERS[$bin]}"
|
||||
fi
|
||||
done
|
||||
|
||||
ff_found=0
|
||||
for bin in "${!FIREFOXES[@]}"; do
|
||||
if command -v "$bin" >/dev/null 2>&1; then
|
||||
ff_found=1
|
||||
fi
|
||||
done
|
||||
|
||||
echo
|
||||
if [[ $found_any -eq 1 ]]; then
|
||||
info "Chromium-based integration complete. Launch the browser via its '(LeechBlock)' launcher."
|
||||
warn "Chromium will mark it as a developer extension; this is expected for unpacked installs."
|
||||
fi
|
||||
|
||||
if [[ $ff_found -eq 1 ]]; then
|
||||
echo
|
||||
warn "Detected Firefox-based browser(s). Permanent install from GitHub source isn't possible on stable builds due to required signing."
|
||||
cat <<FF
|
||||
Options:
|
||||
1) Install from Mozilla Add-ons (recommended):
|
||||
https://addons.mozilla.org/firefox/addon/leechblock-ng/
|
||||
2) For testing with Developer Edition or Nightly, you can set xpinstall.signatures.required=false
|
||||
and install a built XPI. We'll still keep the downloaded source at:
|
||||
$VERSION_DIR
|
||||
|
||||
To load temporarily for testing (session-only), open 'about:debugging#/runtime/this-firefox' and "Load Temporary Add-on…" then select $VERSION_DIR/manifest.json.
|
||||
|
||||
Tip: Re-run this script with --install-firefox to auto-install from AMO via enterprise policy (requires sudo).
|
||||
FF
|
||||
fi
|
||||
|
||||
if [[ $found_any -eq 0 && $ff_found -eq 0 ]]; then
|
||||
warn "No supported browsers detected. We placed the extension at: $VERSION_DIR"
|
||||
echo "Supported (auto-wired): ${!BROWSERS[*]}. Detected Firefox variants will show guidance only."
|
||||
fi
|
||||
|
||||
echo
|
||||
info "Done. Version: $VERSION (tag $TAG) installed under $VERSION_DIR"
|
||||
|
||||
# If requested, attempt automatic install on Firefox via enterprise policies
|
||||
if [[ $AUTO_FIREFOX -eq 1 && $ff_found -eq 1 ]]; then
|
||||
echo
|
||||
info "Attempting Firefox auto-install via Enterprise Policies (requires sudo)."
|
||||
# AMO info
|
||||
ADDON_ID="leechblockng@proginosko.com"
|
||||
ADDON_AMO_URL="https://addons.mozilla.org/firefox/downloads/latest/leechblock-ng/latest.xpi"
|
||||
|
||||
# Determine policy directories for detected Firefox-like browsers
|
||||
declare -a POLICY_DIRS
|
||||
POLICY_DIRS=()
|
||||
if command -v firefox >/dev/null 2>&1; then
|
||||
POLICY_DIRS+=("/etc/firefox/policies" "/usr/lib/firefox/distribution")
|
||||
fi
|
||||
if command -v firefox-developer-edition >/dev/null 2>&1; then
|
||||
POLICY_DIRS+=("/etc/firefox-developer-edition/policies" "/usr/lib/firefox-developer-edition/distribution")
|
||||
fi
|
||||
if command -v librewolf >/dev/null 2>&1; then
|
||||
POLICY_DIRS+=("/etc/librewolf/policies" "/usr/lib/librewolf/distribution")
|
||||
fi
|
||||
# Generic mozilla path as fallback
|
||||
POLICY_DIRS+=("/usr/lib/mozilla/distribution")
|
||||
|
||||
updated_any=0
|
||||
for pol_target in "${POLICY_DIRS[@]}"; do
|
||||
tmp_pol=$(mktemp)
|
||||
existing="${pol_target}/policies.json"
|
||||
if sudo test -f "$existing"; then
|
||||
info "Merging into existing policies.json at $existing"
|
||||
sudo cp "$existing" "$tmp_pol"
|
||||
if command -v jq >/dev/null 2>&1; then
|
||||
merged=$(jq --arg id "$ADDON_ID" --arg url "$ADDON_AMO_URL" '
|
||||
.policies |= (. // {}) |
|
||||
.policies.ExtensionSettings |= (. // {}) |
|
||||
.policies.ExtensionSettings."*" |= (. // {"installation_mode":"allowed"}) |
|
||||
.policies.ExtensionSettings[$id] |= (. // {}) |
|
||||
.policies.ExtensionSettings[$id].installation_mode = "force_installed" |
|
||||
.policies.ExtensionSettings[$id].install_url = $url
|
||||
' "$tmp_pol") || merged=""
|
||||
if [[ -n $merged ]]; then
|
||||
printf '%s\n' "$merged" >"$tmp_pol"
|
||||
else
|
||||
warn "jq merge failed; skipping $pol_target"
|
||||
rm -f "$tmp_pol"
|
||||
continue
|
||||
fi
|
||||
else
|
||||
warn "jq not available; creating minimal policies.json (existing file will be backed up)."
|
||||
sudo cp "$existing" "${existing}.bak.$(date +%s)"
|
||||
cat >"$tmp_pol" <<JSON
|
||||
{
|
||||
"policies": {
|
||||
"ExtensionSettings": {
|
||||
"*": { "installation_mode": "allowed" },
|
||||
"$ADDON_ID": {
|
||||
"installation_mode": "force_installed",
|
||||
"install_url": "$ADDON_AMO_URL"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
JSON
|
||||
fi
|
||||
else
|
||||
info "Creating new policies.json at $pol_target"
|
||||
cat >"$tmp_pol" <<JSON
|
||||
{
|
||||
"policies": {
|
||||
"ExtensionSettings": {
|
||||
"*": { "installation_mode": "allowed" },
|
||||
"$ADDON_ID": {
|
||||
"installation_mode": "force_installed",
|
||||
"install_url": "$ADDON_AMO_URL"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
JSON
|
||||
fi
|
||||
|
||||
sudo mkdir -p "$pol_target"
|
||||
sudo cp "$tmp_pol" "$pol_target/policies.json"
|
||||
rm -f "$tmp_pol"
|
||||
updated_any=1
|
||||
done
|
||||
|
||||
if [[ $updated_any -eq 1 ]]; then
|
||||
info "Firefox policies updated. Restart Firefox/LibreWolf to complete installation of LeechBlock NG."
|
||||
else
|
||||
warn "No Firefox policy locations updated. You may not have a supported Firefox installed."
|
||||
fi
|
||||
info "Firefox policy updated. Restart Firefox to complete installation of LeechBlock NG."
|
||||
fi
|
||||
|
||||
@ -1,184 +0,0 @@
|
||||
#!/usr/bin/env bash
|
||||
# Browser detection and launcher wiring for install_leechblock.sh.
|
||||
# Sourced by the installer; inherits its strict mode and info/warn helpers.
|
||||
#
|
||||
# detect_browsers, replace_browser_in_place and wire_up_browsers live in ONE
|
||||
# file on purpose: detect_browsers writes BROWSERS/FIREFOXES, which the other
|
||||
# two read. Splitting them would make those globals write-only across a file
|
||||
# boundary — a genuine SC2034 the hook cannot see past (it runs shellcheck
|
||||
# without -x), and suppressions are forbidden.
|
||||
|
||||
detect_browsers() {
|
||||
# Detect browsers
|
||||
declare -gA BROWSERS
|
||||
BROWSERS=(
|
||||
[chromium]="Chromium"
|
||||
["google-chrome-stable"]="Google Chrome"
|
||||
["google-chrome"]="Google Chrome"
|
||||
["brave-browser"]="Brave"
|
||||
["vivaldi-stable"]="Vivaldi"
|
||||
[vivaldi]="Vivaldi"
|
||||
[opera]="Opera"
|
||||
["thorium-browser"]="Thorium"
|
||||
)
|
||||
|
||||
declare -gA FIREFOXES
|
||||
FIREFOXES=(
|
||||
[firefox]="Firefox"
|
||||
["firefox-developer-edition"]="Firefox Developer Edition"
|
||||
[librewolf]="LibreWolf"
|
||||
)
|
||||
|
||||
found_any=0
|
||||
|
||||
# Create a user desktop entry
|
||||
user_apps_dir="${XDG_DATA_HOME:-$HOME/.local/share}/applications"
|
||||
mkdir -p "$user_apps_dir"
|
||||
}
|
||||
|
||||
# Inject --load-extension into a browser launcher so every launch includes LeechBlock.
|
||||
# Handles two cases:
|
||||
# 1) The binary is a shell script with an "exec" line — patch it in-place.
|
||||
# 2) The binary is a compiled ELF — wrap it with a shell script.
|
||||
# Follows symlinks only one level to avoid breaking shared wrapper scripts
|
||||
# (e.g. browser-preexec-wrapper used by multiple browser symlinks).
|
||||
# Requires sudo for system paths.
|
||||
replace_browser_in_place() {
|
||||
local bin="$1"
|
||||
shift
|
||||
local pretty="$1"
|
||||
shift
|
||||
|
||||
local bin_path
|
||||
bin_path=$(command -v "$bin" || true)
|
||||
[[ -z $bin_path ]] && return
|
||||
|
||||
# Resolve symlinks to find the actual file to patch.
|
||||
# Use readlink -f to get the canonical path.
|
||||
local real_bin
|
||||
real_bin=$(readlink -f "$bin_path")
|
||||
|
||||
local load_ext_flag="--load-extension=\"$EXT_PATH\""
|
||||
|
||||
# If already patched, skip (idempotent)
|
||||
if grep -q -- "$load_ext_flag" "$real_bin" 2>/dev/null; then
|
||||
info "$pretty ($bin) already has LeechBlock --load-extension — skipping"
|
||||
found_any=1
|
||||
return
|
||||
fi
|
||||
|
||||
# Case 1: Shell script with an exec line — patch the exec line directly.
|
||||
# This preserves the original script's logic (e.g. basename routing,
|
||||
# hosts enforcement) and avoids breaking shared wrapper scripts.
|
||||
if file "$real_bin" 2>/dev/null | grep -qi 'text\|script'; then
|
||||
if grep -qE '^exec ' "$real_bin"; then
|
||||
info "Patching exec line in $real_bin to add LeechBlock…"
|
||||
|
||||
# Kill running instances so the patched script takes effect
|
||||
pkill -f "$real_bin" 2>/dev/null || true
|
||||
sleep 1
|
||||
|
||||
# Back up before patching (only once)
|
||||
local orig_backup="${real_bin}.orig"
|
||||
if [[ ! -f $orig_backup ]]; then
|
||||
info "Backing up $real_bin → $orig_backup"
|
||||
sudo cp -a "$real_bin" "$orig_backup"
|
||||
fi
|
||||
|
||||
# Insert --load-extension right after "exec <command>" on the exec line.
|
||||
# Matches: exec "$real_bin" "$@" or exec /path/to/bin $FLAGS "$@"
|
||||
sudo sed -i "s|^exec \(.*\) \"\\\$@\"|exec \1 $load_ext_flag \"\\\$@\"|" "$real_bin"
|
||||
|
||||
info "✓ $pretty exec line patched with LeechBlock"
|
||||
found_any=1
|
||||
return
|
||||
fi
|
||||
fi
|
||||
|
||||
# Case 2: Binary or script without a recognisable exec line — wrap it.
|
||||
local orig_backup="${real_bin}.orig"
|
||||
|
||||
# Kill running instances
|
||||
info "Killing running $pretty instances…"
|
||||
pkill -f "$real_bin" 2>/dev/null || true
|
||||
pkill -f "$(basename "$real_bin")" 2>/dev/null || true
|
||||
sleep 1
|
||||
|
||||
# Back up original
|
||||
if [[ ! -f $orig_backup ]]; then
|
||||
info "Backing up $real_bin → $orig_backup"
|
||||
sudo cp -a "$real_bin" "$orig_backup"
|
||||
else
|
||||
info "Backup already exists: $orig_backup"
|
||||
fi
|
||||
|
||||
# Write replacement wrapper
|
||||
info "Replacing $real_bin with LeechBlock wrapper…"
|
||||
sudo tee "$real_bin" >/dev/null <<WRAP
|
||||
#!/usr/bin/env bash
|
||||
# __LEECHBLOCK_WRAPPER__ — auto-generated by install_leechblock.sh
|
||||
# Original backed up at: $orig_backup
|
||||
exec "$orig_backup" --load-extension="$EXT_PATH" "\$@"
|
||||
WRAP
|
||||
sudo chmod +x "$real_bin"
|
||||
|
||||
info "✓ $pretty now always launches with LeechBlock"
|
||||
found_any=1
|
||||
}
|
||||
|
||||
wire_up_browsers() {
|
||||
# The Firefox guidance and the closing summary interpolate the install
|
||||
# layout. Those are set by resolve_version, which the entry script always
|
||||
# runs first; defaulting them keeps this function callable on its own
|
||||
# without aborting under `set -u` on an unbound VERSION_DIR.
|
||||
local version_dir="${VERSION_DIR:-<not yet resolved>}"
|
||||
local version="${VERSION:-<unknown>}"
|
||||
local tag="${TAG:-<unknown>}"
|
||||
|
||||
info "Detecting installed browsers…"
|
||||
for bin in "${!BROWSERS[@]}"; do
|
||||
if command -v "$bin" >/dev/null 2>&1; then
|
||||
replace_browser_in_place "$bin" "${BROWSERS[$bin]}"
|
||||
fi
|
||||
done
|
||||
|
||||
# Global on purpose: written and read only within this file, and the entry
|
||||
# script forwards it to install_firefox_policy as an argument.
|
||||
ff_found=0
|
||||
for bin in "${!FIREFOXES[@]}"; do
|
||||
if command -v "$bin" >/dev/null 2>&1; then
|
||||
ff_found=1
|
||||
fi
|
||||
done
|
||||
|
||||
echo
|
||||
if [[ $found_any -eq 1 ]]; then
|
||||
info "Chromium-based integration complete. Launch the browser via its '(LeechBlock)' launcher."
|
||||
warn "Chromium will mark it as a developer extension; this is expected for unpacked installs."
|
||||
fi
|
||||
|
||||
if [[ $ff_found -eq 1 ]]; then
|
||||
echo
|
||||
warn "Detected Firefox-based browser(s). Permanent install from GitHub source isn't possible on stable builds due to required signing."
|
||||
cat <<FF
|
||||
Options:
|
||||
1) Install from Mozilla Add-ons (recommended):
|
||||
https://addons.mozilla.org/firefox/addon/leechblock-ng/
|
||||
2) For testing with Developer Edition or Nightly, you can set xpinstall.signatures.required=false
|
||||
and install a built XPI. We'll still keep the downloaded source at:
|
||||
$version_dir
|
||||
|
||||
To load temporarily for testing (session-only), open 'about:debugging#/runtime/this-firefox' and "Load Temporary Add-on…" then select $version_dir/manifest.json.
|
||||
|
||||
Tip: Re-run this script with --install-firefox to auto-install from AMO via enterprise policy (requires sudo).
|
||||
FF
|
||||
fi
|
||||
|
||||
if [[ $found_any -eq 0 && $ff_found -eq 0 ]]; then
|
||||
warn "No supported browsers detected. We placed the extension at: $version_dir"
|
||||
echo "Supported (auto-wired): ${!BROWSERS[*]}. Detected Firefox variants will show guidance only."
|
||||
fi
|
||||
|
||||
echo
|
||||
info "Done. Version: $version (tag $tag) installed under $version_dir"
|
||||
}
|
||||
@ -1,36 +0,0 @@
|
||||
#!/usr/bin/env bash
|
||||
# Default-configuration seeding for install_leechblock.sh.
|
||||
# Sourced by the installer; inherits its strict mode and info/warn helpers.
|
||||
# Reads SCRIPT_DIR, writes the browser LevelDB storage via the Node seeder.
|
||||
|
||||
inject_default_config() {
|
||||
# ── Inject default blocking configuration ─────────────────────────────
|
||||
# Write default blocking rules directly into Chrome's LevelDB extension
|
||||
# storage via Node.js (classic-level). This is content-verification-proof:
|
||||
# we never touch any extension JS file, so Chrome cannot detect tampering.
|
||||
DEFAULTS_SRC="$SCRIPT_DIR/leechblock_defaults.json"
|
||||
|
||||
if [[ -f $DEFAULTS_SRC ]]; then
|
||||
|
||||
# Ensure classic-level is available next to this script.
|
||||
if [[ ! -d "$SCRIPT_DIR/node_modules/classic-level" ]]; then
|
||||
info "Installing classic-level npm package into $SCRIPT_DIR ..."
|
||||
npm install --prefix "$SCRIPT_DIR" 2>&1 | grep -v '^npm warn' || true
|
||||
fi
|
||||
|
||||
# Chrome locks its LevelDB files while running — close all Chromium browsers
|
||||
# so the write succeeds.
|
||||
pkill -f 'google-chrome|chromium|brave-browser|vivaldi|thorium' 2>/dev/null || true
|
||||
sleep 1
|
||||
|
||||
# Seed defaults into every Chrome/Chromium profile found on this machine.
|
||||
if node "$SCRIPT_DIR/seed_leechblock_storage.js" "$DEFAULTS_SRC"; then
|
||||
info "Seeded default LeechBlock settings into browser storage"
|
||||
else
|
||||
warn "Could not seed LeechBlock defaults — run manually after install:"
|
||||
warn " node $SCRIPT_DIR/seed_leechblock_storage.js $DEFAULTS_SRC"
|
||||
fi
|
||||
else
|
||||
warn "leechblock_defaults.json not found at $DEFAULTS_SRC — skipping default config"
|
||||
fi
|
||||
}
|
||||
@ -1,114 +0,0 @@
|
||||
#!/usr/bin/env bash
|
||||
# Release-tag resolution and source download for install_leechblock.sh.
|
||||
# Sourced by the installer; inherits its strict mode and its info/warn/err
|
||||
# helpers. Reads REPO_OWNER/REPO_NAME/FORCE, writes VERSION/TAG and the
|
||||
# INSTALL_ROOT layout globals the later phases consume.
|
||||
|
||||
get_latest_tag() {
|
||||
local repo="$1"
|
||||
local tag
|
||||
if command -v jq >/dev/null 2>&1; then
|
||||
tag=$(curl -fsSL "https://api.github.com/repos/${REPO_OWNER}/${repo}/releases/latest" | jq -r '.tag_name // empty' || true)
|
||||
if [[ -n $tag && $tag != "null" ]]; then
|
||||
echo "$tag"
|
||||
return 0
|
||||
fi
|
||||
# Fallback: try tags endpoint
|
||||
tag=$(curl -fsSL "https://api.github.com/repos/${REPO_OWNER}/${repo}/tags?per_page=1" | jq -r '.[0].name // empty' || true)
|
||||
if [[ -n $tag && $tag != "null" ]]; then
|
||||
echo "$tag"
|
||||
return 0
|
||||
fi
|
||||
fi
|
||||
# Fallback: follow redirect for /releases/latest to extract tag
|
||||
tag=$(curl -fsSLI "https://github.com/${REPO_OWNER}/${repo}/releases/latest" | awk -F'/tag/' '/^location:/I {print $2}' | tr -d '\r\n' || true)
|
||||
if [[ -n $tag ]]; then
|
||||
echo "$tag"
|
||||
return 0
|
||||
fi
|
||||
return 1
|
||||
}
|
||||
|
||||
resolve_version() {
|
||||
if [[ -z $VERSION ]]; then
|
||||
info "Resolving latest release tag from GitHub…"
|
||||
if ! VERSION=$(get_latest_tag "$REPO_NAME"); then
|
||||
err "Failed to determine latest version tag"
|
||||
exit 1
|
||||
fi
|
||||
fi
|
||||
|
||||
if [[ ! $VERSION =~ ^v?[0-9]+(\.[0-9]+)*$ ]]; then
|
||||
warn "Version tag '$VERSION' doesn't look like vX[.Y[.Z]] — continuing anyway."
|
||||
fi
|
||||
|
||||
VERSION=${VERSION#v} # strip leading v for folder names
|
||||
TAG="v${VERSION}"
|
||||
|
||||
XDG_DATA_HOME=${XDG_DATA_HOME:-"$HOME/.local/share"}
|
||||
INSTALL_ROOT="$XDG_DATA_HOME/leechblockng"
|
||||
VERSION_DIR="$INSTALL_ROOT/$VERSION"
|
||||
CURRENT_LINK="$INSTALL_ROOT/current"
|
||||
}
|
||||
|
||||
download_extension() {
|
||||
if [[ -d $VERSION_DIR && $FORCE -ne 1 ]]; then
|
||||
info "LeechBlockNG $VERSION already present at $VERSION_DIR (use --force to reinstall)."
|
||||
else
|
||||
info "Downloading LeechBlockNG $TAG source from GitHub…"
|
||||
tmpdir=$(mktemp -d)
|
||||
trap 'rm -rf "$tmpdir"' EXIT
|
||||
ARCHIVE_URL="https://github.com/${REPO_OWNER}/${REPO_NAME}/archive/refs/tags/${TAG}.tar.gz"
|
||||
ARCHIVE_FILE="$tmpdir/${REPO_NAME}-${TAG}.tar.gz"
|
||||
curl -fL --retry 3 -o "$ARCHIVE_FILE" "$ARCHIVE_URL"
|
||||
info "Extracting…"
|
||||
mkdir -p "$tmpdir/extract"
|
||||
tar -xzf "$ARCHIVE_FILE" -C "$tmpdir/extract"
|
||||
# The archive usually extracts to REPO_NAME-TAG/ …
|
||||
src_root=$(find "$tmpdir/extract" -maxdepth 1 -type d -name "${REPO_NAME}-*" | head -n1 || true)
|
||||
[[ -z $src_root ]] && {
|
||||
err "Could not locate extracted source root"
|
||||
exit 1
|
||||
}
|
||||
|
||||
# Find the extension manifest (support a couple of common layouts)
|
||||
manifest_path=$(find "$src_root" -maxdepth 5 -type f -name manifest.json | head -n1 || true)
|
||||
if [[ -z $manifest_path ]]; then
|
||||
err "manifest.json not found in the extracted archive. The project layout may have changed."
|
||||
exit 1
|
||||
fi
|
||||
ext_dir=$(dirname "$manifest_path")
|
||||
|
||||
mkdir -p "$INSTALL_ROOT"
|
||||
rm -rf "$VERSION_DIR"
|
||||
info "Installing to $VERSION_DIR…"
|
||||
mkdir -p "$VERSION_DIR"
|
||||
# Copy the extension directory as-is (avoid bringing tests or build scripts)
|
||||
rsync -a --delete "$ext_dir/" "$VERSION_DIR/" 2>/dev/null || cp -a "$ext_dir/." "$VERSION_DIR/"
|
||||
|
||||
# Download jQuery UI (not included in repo — listed in .gitignore)
|
||||
# The extension's options.html expects:
|
||||
# jquery-ui/jquery-ui.min.css
|
||||
# jquery-ui/external/jquery/jquery.js
|
||||
# jquery-ui/jquery-ui.min.js
|
||||
info "Downloading jQuery UI…"
|
||||
jqui_version="1.14.1"
|
||||
jqui_url="https://jqueryui.com/resources/download/jquery-ui-${jqui_version}.zip"
|
||||
jqui_zip="$tmpdir/jquery-ui.zip"
|
||||
curl -fL --retry 3 -o "$jqui_zip" "$jqui_url"
|
||||
mkdir -p "$tmpdir/jqui-extract"
|
||||
unzip -q "$jqui_zip" -d "$tmpdir/jqui-extract"
|
||||
jqui_src=$(find "$tmpdir/jqui-extract" -maxdepth 1 -type d -name "jquery-ui-*" | head -n1 || true)
|
||||
if [[ -n $jqui_src ]]; then
|
||||
mkdir -p "$VERSION_DIR/jquery-ui/external/jquery"
|
||||
cp "$jqui_src/jquery-ui.min.css" "$VERSION_DIR/jquery-ui/" 2>/dev/null || true
|
||||
cp "$jqui_src/jquery-ui.min.js" "$VERSION_DIR/jquery-ui/" 2>/dev/null || true
|
||||
cp "$jqui_src/external/jquery/jquery.js" "$VERSION_DIR/jquery-ui/external/jquery/" 2>/dev/null || true
|
||||
info "✓ jQuery UI ${jqui_version} installed into extension"
|
||||
else
|
||||
warn "Could not extract jQuery UI — options page may not work correctly"
|
||||
fi
|
||||
|
||||
ln -sfn "$VERSION_DIR" "$CURRENT_LINK"
|
||||
fi
|
||||
}
|
||||
@ -1,108 +0,0 @@
|
||||
#!/usr/bin/env bash
|
||||
# Firefox enterprise-policy installation for install_leechblock.sh.
|
||||
# Sourced by the installer; inherits its strict mode and info/warn helpers.
|
||||
# Reads AUTO_FIREFOX; writes policies.json under system paths.
|
||||
#
|
||||
# Whether a Firefox-family browser was detected arrives as $1 rather than as
|
||||
# the ff_found global the pre-split code used: that global is written in
|
||||
# leechblock_browsers.sh, and reading it here would be a cross-file global the
|
||||
# linter genuinely cannot resolve (it runs without -x), i.e. a real SC2154
|
||||
# rather than noise. Suppressions are forbidden, so the value is passed
|
||||
# explicitly instead.
|
||||
install_firefox_policy() {
|
||||
local ff_found="${1:-0}"
|
||||
|
||||
# If requested, attempt automatic install on Firefox via enterprise policies
|
||||
if [[ $AUTO_FIREFOX -eq 1 && $ff_found -eq 1 ]]; then
|
||||
echo
|
||||
info "Attempting Firefox auto-install via Enterprise Policies (requires sudo)."
|
||||
# AMO info
|
||||
ADDON_ID="leechblockng@proginosko.com"
|
||||
ADDON_AMO_URL="https://addons.mozilla.org/firefox/downloads/latest/leechblock-ng/latest.xpi"
|
||||
|
||||
# Determine policy directories for detected Firefox-like browsers
|
||||
declare -a POLICY_DIRS
|
||||
POLICY_DIRS=()
|
||||
if command -v firefox >/dev/null 2>&1; then
|
||||
POLICY_DIRS+=("/etc/firefox/policies" "/usr/lib/firefox/distribution")
|
||||
fi
|
||||
if command -v firefox-developer-edition >/dev/null 2>&1; then
|
||||
POLICY_DIRS+=("/etc/firefox-developer-edition/policies" "/usr/lib/firefox-developer-edition/distribution")
|
||||
fi
|
||||
if command -v librewolf >/dev/null 2>&1; then
|
||||
POLICY_DIRS+=("/etc/librewolf/policies" "/usr/lib/librewolf/distribution")
|
||||
fi
|
||||
# Generic mozilla path as fallback
|
||||
POLICY_DIRS+=("/usr/lib/mozilla/distribution")
|
||||
|
||||
updated_any=0
|
||||
for pol_target in "${POLICY_DIRS[@]}"; do
|
||||
tmp_pol=$(mktemp)
|
||||
existing="${pol_target}/policies.json"
|
||||
if sudo test -f "$existing"; then
|
||||
info "Merging into existing policies.json at $existing"
|
||||
sudo cp "$existing" "$tmp_pol"
|
||||
if command -v jq >/dev/null 2>&1; then
|
||||
merged=$(jq --arg id "$ADDON_ID" --arg url "$ADDON_AMO_URL" '
|
||||
.policies |= (. // {}) |
|
||||
.policies.ExtensionSettings |= (. // {}) |
|
||||
.policies.ExtensionSettings."*" |= (. // {"installation_mode":"allowed"}) |
|
||||
.policies.ExtensionSettings[$id] |= (. // {}) |
|
||||
.policies.ExtensionSettings[$id].installation_mode = "force_installed" |
|
||||
.policies.ExtensionSettings[$id].install_url = $url
|
||||
' "$tmp_pol") || merged=""
|
||||
if [[ -n $merged ]]; then
|
||||
printf '%s\n' "$merged" >"$tmp_pol"
|
||||
else
|
||||
warn "jq merge failed; skipping $pol_target"
|
||||
rm -f "$tmp_pol"
|
||||
continue
|
||||
fi
|
||||
else
|
||||
warn "jq not available; creating minimal policies.json (existing file will be backed up)."
|
||||
sudo cp "$existing" "${existing}.bak.$(date +%s)"
|
||||
cat >"$tmp_pol" <<JSON
|
||||
{
|
||||
"policies": {
|
||||
"ExtensionSettings": {
|
||||
"*": { "installation_mode": "allowed" },
|
||||
"$ADDON_ID": {
|
||||
"installation_mode": "force_installed",
|
||||
"install_url": "$ADDON_AMO_URL"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
JSON
|
||||
fi
|
||||
else
|
||||
info "Creating new policies.json at $pol_target"
|
||||
cat >"$tmp_pol" <<JSON
|
||||
{
|
||||
"policies": {
|
||||
"ExtensionSettings": {
|
||||
"*": { "installation_mode": "allowed" },
|
||||
"$ADDON_ID": {
|
||||
"installation_mode": "force_installed",
|
||||
"install_url": "$ADDON_AMO_URL"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
JSON
|
||||
fi
|
||||
|
||||
sudo mkdir -p "$pol_target"
|
||||
sudo cp "$tmp_pol" "$pol_target/policies.json"
|
||||
rm -f "$tmp_pol"
|
||||
updated_any=1
|
||||
done
|
||||
|
||||
if [[ $updated_any -eq 1 ]]; then
|
||||
info "Firefox policies updated. Restart Firefox/LibreWolf to complete installation of LeechBlock NG."
|
||||
else
|
||||
warn "No Firefox policy locations updated. You may not have a supported Firefox installed."
|
||||
fi
|
||||
info "Firefox policy updated. Restart Firefox to complete installation of LeechBlock NG."
|
||||
fi
|
||||
}
|
||||
@ -1,150 +0,0 @@
|
||||
#!/usr/bin/env bash
|
||||
# lib/tests/leechblock_harness.sh — shared fixture for the install_leechblock.sh split.
|
||||
#
|
||||
# Sourced, not executed. Scope is deliberately narrow: the phases that download
|
||||
# archives, pkill browsers, patch system binaries under sudo and write
|
||||
# /etc/*/policies.json are never executed here — running them would rewrite
|
||||
# this machine's browsers. Those phases are covered by the textual
|
||||
# wrap-verification recorded in the evidence instead.
|
||||
#
|
||||
# What IS executed: the pure logic that decides *what* to do — tag resolution
|
||||
# across its three curl fallbacks, version normalisation, and browser-table
|
||||
# construction. curl/jq are shimmed on PATH so no network call is made.
|
||||
set -euo pipefail
|
||||
|
||||
PASS=0
|
||||
FAIL=0
|
||||
|
||||
_t_pass() {
|
||||
PASS=$((PASS + 1))
|
||||
printf ' OK: %s\n' "$1"
|
||||
}
|
||||
|
||||
_t_fail() {
|
||||
FAIL=$((FAIL + 1))
|
||||
printf ' FAIL: %s\n' "$1"
|
||||
}
|
||||
|
||||
_t_eq() {
|
||||
local want="$1"
|
||||
local got="$2"
|
||||
local what="$3"
|
||||
if [[ $got == "$want" ]]; then
|
||||
_t_pass "$what"
|
||||
else
|
||||
_t_fail "$what (want '${want}', got '${got}')"
|
||||
fi
|
||||
}
|
||||
|
||||
_t_has() {
|
||||
local haystack="$1"
|
||||
local needle="$2"
|
||||
local what="$3"
|
||||
if [[ $haystack == *"$needle"* ]]; then
|
||||
_t_pass "$what"
|
||||
else
|
||||
_t_fail "$what (want a substring '${needle}')"
|
||||
fi
|
||||
}
|
||||
|
||||
# A function that calls `exit` would kill the test script if invoked directly
|
||||
# in an `if` condition, so failures are asserted inside a subshell.
|
||||
_t_exits_nonzero() {
|
||||
local what="$2"
|
||||
if ("$1" >/dev/null 2>&1); then
|
||||
_t_fail "$what (expected a non-zero exit, got 0)"
|
||||
else
|
||||
_t_pass "$what"
|
||||
fi
|
||||
}
|
||||
|
||||
LEECHBLOCK_LIB_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
|
||||
export LEECHBLOCK_LIB_DIR
|
||||
|
||||
# The libs expect these logging helpers from the entry script.
|
||||
info() { printf '[INFO] %s\n' "$*"; }
|
||||
warn() { printf '[WARN] %s\n' "$*"; }
|
||||
err() { printf '[ERR ] %s\n' "$*"; }
|
||||
|
||||
# Build a throwaway PATH front so `curl`, `jq` and friends are controllable.
|
||||
# Every shim writes what it was asked for into $TEST_TMPDIR so assertions can
|
||||
# inspect the call, and reads its canned reply from a per-test file.
|
||||
_t_setup_shims() {
|
||||
TEST_TMPDIR="$(mktemp -d)"
|
||||
SHIM_DIR="$TEST_TMPDIR/bin"
|
||||
mkdir -p "$SHIM_DIR"
|
||||
|
||||
cat >"$SHIM_DIR/curl" <<'SHIM'
|
||||
#!/usr/bin/env bash
|
||||
# Records argv, then replays a canned body chosen by the caller.
|
||||
printf '%s\n' "$*" >>"$TEST_TMPDIR/curl.calls"
|
||||
if [[ -n ${CURL_FAIL_ALL:-} ]]; then
|
||||
exit 22
|
||||
fi
|
||||
if [[ $* == *"-fsSLI"* ]]; then
|
||||
[[ -f "$TEST_TMPDIR/curl.head" ]] && cat "$TEST_TMPDIR/curl.head"
|
||||
exit 0
|
||||
fi
|
||||
if [[ $* == *"/releases/latest"* ]]; then
|
||||
[[ -f "$TEST_TMPDIR/curl.releases" ]] && cat "$TEST_TMPDIR/curl.releases"
|
||||
exit 0
|
||||
fi
|
||||
if [[ $* == *"/tags"* ]]; then
|
||||
[[ -f "$TEST_TMPDIR/curl.tags" ]] && cat "$TEST_TMPDIR/curl.tags"
|
||||
exit 0
|
||||
fi
|
||||
exit 0
|
||||
SHIM
|
||||
chmod +x "$SHIM_DIR/curl"
|
||||
|
||||
# _t_hide_jq narrows PATH to SHIM_DIR alone, so every external command still
|
||||
# needed on that path must be reachable from here: awk/tr for the
|
||||
# redirect-header fallback, env+bash because the curl shim's own shebang is
|
||||
# `#!/usr/bin/env bash`, and rm/cat/mktemp for the harness itself.
|
||||
# Without these the fallback would fail for the wrong reason and the test
|
||||
# would be asserting nothing.
|
||||
# mkdir is load-bearing: detect_browsers creates the desktop-entry dir, and
|
||||
# a missing mkdir aborts the whole suite under `set -e` with no stderr at
|
||||
# all — which under kcov looks like a coverage-tool problem rather than a
|
||||
# PATH problem. Keep this list in sync with the externals the libs call.
|
||||
local tool
|
||||
for tool in awk tr cat mkdir mktemp rm env bash sed grep wc readlink; do
|
||||
ln -sf "$(command -v "$tool")" "$SHIM_DIR/$tool"
|
||||
done
|
||||
|
||||
export TEST_TMPDIR
|
||||
PATH="$SHIM_DIR:$PATH"
|
||||
export PATH
|
||||
}
|
||||
|
||||
# Narrow PATH to the shim dir alone. Two tests need this and neither can be
|
||||
# written any other way:
|
||||
# * hiding jq — shadowing it with a non-executable shim is NOT enough,
|
||||
# because `command -v` keeps searching PATH and finds the real jq;
|
||||
# * hiding browsers — this machine genuinely has Chromium and Firefox
|
||||
# installed, so "no browser detected" is otherwise untestable.
|
||||
# _t_setup_shims pre-seeds the shim dir with the utilities the code under test
|
||||
# calls, so narrowing hides only what the test means to hide.
|
||||
_t_isolate_path() {
|
||||
_T_SAVED_PATH="$PATH"
|
||||
PATH="$SHIM_DIR"
|
||||
export PATH
|
||||
}
|
||||
|
||||
_t_restore_path() {
|
||||
PATH="$_T_SAVED_PATH"
|
||||
export PATH
|
||||
}
|
||||
|
||||
# Readable aliases for the jq-specific use of the same mechanism.
|
||||
_t_hide_jq() { _t_isolate_path; }
|
||||
_t_show_jq() { _t_restore_path; }
|
||||
|
||||
_t_teardown() {
|
||||
[[ -n ${TEST_TMPDIR:-} && -d ${TEST_TMPDIR} ]] && rm -rf "$TEST_TMPDIR"
|
||||
}
|
||||
|
||||
_t_report() {
|
||||
printf '\n%s: %d passed, %d failed\n' "${1:-tests}" "$PASS" "$FAIL"
|
||||
[[ $FAIL -eq 0 ]]
|
||||
}
|
||||
@ -1,18 +0,0 @@
|
||||
#!/usr/bin/env bash
|
||||
# Runs every leechblock lib test. Used directly as the coverage subject:
|
||||
# meta/scripts/shell_coverage.sh wants the test script itself, not `bash <it>`.
|
||||
set -euo pipefail
|
||||
|
||||
HERE="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
|
||||
|
||||
rc=0
|
||||
for t in "$HERE"/test_leechblock_*.sh; do
|
||||
printf '\n=== %s ===\n' "$(basename "$t")"
|
||||
# Each suite exits non-zero on failure; keep going so one red suite does not
|
||||
# hide the state of the others, then fail the run as a whole.
|
||||
# Invoked directly, NOT as `bash "$t"`: under kcov the latter instruments
|
||||
# bash itself and reports zero lines for every lib.
|
||||
"$t" || rc=1
|
||||
done
|
||||
|
||||
exit "$rc"
|
||||
@ -1,85 +0,0 @@
|
||||
#!/usr/bin/env bash
|
||||
# Covers lib/leechblock_browsers.sh's detection table and dispatch logic.
|
||||
#
|
||||
# replace_browser_in_place is NOT executed: it pkills browsers and rewrites
|
||||
# system binaries under sudo. What is asserted here is everything that decides
|
||||
# *whether* it runs and *what* it would be handed — the table itself, the
|
||||
# global-scope contract, and the command-v gating in wire_up_browsers.
|
||||
set -euo pipefail
|
||||
|
||||
HERE="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
|
||||
# shellcheck source=leechblock_harness.sh
|
||||
source "$HERE/leechblock_harness.sh"
|
||||
# shellcheck source=../leechblock_browsers.sh
|
||||
source "$LEECHBLOCK_LIB_DIR/leechblock_browsers.sh"
|
||||
|
||||
_t_setup_shims
|
||||
trap _t_teardown EXIT
|
||||
|
||||
HOME="$TEST_TMPDIR/home"
|
||||
XDG_DATA_HOME="$TEST_TMPDIR/home/.local/share"
|
||||
|
||||
# Assigned by wire_up_browsers in the sourced lib. Declared here so the
|
||||
# assertions below read as checking a known variable rather than tripping
|
||||
# SC2154 on something shellcheck cannot trace across the source boundary.
|
||||
ff_found=0
|
||||
|
||||
printf 'detect_browsers: the tables survive as globals\n'
|
||||
# declare -A inside a function is function-local; the pre-split code ran at top
|
||||
# level. If the -g were ever dropped these counts would be 0 and every browser
|
||||
# would silently go unwired, so assert the count rather than just a lookup.
|
||||
detect_browsers
|
||||
_t_eq "8" "${#BROWSERS[@]}" "all eight Chromium-family browsers are present"
|
||||
_t_eq "3" "${#FIREFOXES[@]}" "all three Firefox-family browsers are present"
|
||||
|
||||
printf '\ndetect_browsers: hyphenated keys resolve\n'
|
||||
# The subscripts are QUOTED deliberately. shfmt -w rewrites an unquoted
|
||||
# ${BROWSERS[google-chrome-stable]} into arithmetic
|
||||
# (the key becomes a subtraction expression), which resolves to the empty key and
|
||||
# makes every one of these assertions read an absent entry. Reproduced in this
|
||||
# very file during the split; quoting is what makes shfmt leave it alone.
|
||||
_t_eq "Google Chrome" "${BROWSERS["google-chrome-stable"]}" "google-chrome-stable maps to its pretty name"
|
||||
_t_eq "Brave" "${BROWSERS["brave-browser"]}" "brave-browser maps to its pretty name"
|
||||
_t_eq "Vivaldi" "${BROWSERS["vivaldi-stable"]}" "vivaldi-stable maps to its pretty name"
|
||||
_t_eq "Thorium" "${BROWSERS["thorium-browser"]}" "thorium-browser maps to its pretty name"
|
||||
_t_eq "Firefox Developer Edition" "${FIREFOXES["firefox-developer-edition"]}" "firefox-developer-edition maps to its pretty name"
|
||||
|
||||
printf '\ndetect_browsers: it creates the user applications directory\n'
|
||||
if [[ -d "$XDG_DATA_HOME/applications" ]]; then
|
||||
_t_pass "the desktop-entry directory is created"
|
||||
else
|
||||
_t_fail "the desktop-entry directory is missing"
|
||||
fi
|
||||
_t_eq "0" "$found_any" "found_any starts at zero"
|
||||
|
||||
printf '\nwire_up_browsers: no installed browser means nothing is patched\n'
|
||||
# This machine really does have Chromium and Firefox installed, so PATH must be
|
||||
# narrowed to the shim dir for "nothing installed" to be testable at all.
|
||||
# _t_setup_shims seeded that dir with the utilities the function needs, so
|
||||
# narrowing hides the browsers without breaking the code under test.
|
||||
_t_isolate_path
|
||||
replace_browser_in_place() {
|
||||
printf 'PATCHED %s\n' "$1" >>"$TEST_TMPDIR/patched"
|
||||
found_any=1
|
||||
}
|
||||
: >"$TEST_TMPDIR/patched"
|
||||
wire_up_browsers >"$TEST_TMPDIR/wire.out" 2>&1
|
||||
_t_restore_path
|
||||
_t_eq "0" "$(wc -l <"$TEST_TMPDIR/patched")" "no browser present means no binary is touched"
|
||||
_t_eq "0" "$ff_found" "no Firefox present leaves ff_found at zero"
|
||||
_t_has "$(cat "$TEST_TMPDIR/wire.out")" "No supported browsers detected" "it reports that nothing was found"
|
||||
|
||||
printf '\nwire_up_browsers: an installed browser is dispatched with its pretty name\n'
|
||||
# SHIM_DIR is first on PATH, so these stand in for a real installed browser.
|
||||
printf '#!/usr/bin/env bash\nexit 0\n' >"$SHIM_DIR/chromium"
|
||||
chmod +x "$SHIM_DIR/chromium"
|
||||
printf '#!/usr/bin/env bash\nexit 0\n' >"$SHIM_DIR/firefox"
|
||||
chmod +x "$SHIM_DIR/firefox"
|
||||
: >"$TEST_TMPDIR/patched"
|
||||
found_any=0
|
||||
wire_up_browsers >"$TEST_TMPDIR/wire2.out" 2>&1
|
||||
_t_has "$(cat "$TEST_TMPDIR/patched")" "PATCHED chromium" "an installed Chromium is handed to the patcher"
|
||||
_t_eq "1" "$ff_found" "an installed Firefox sets ff_found"
|
||||
_t_has "$(cat "$TEST_TMPDIR/wire2.out")" "Detected Firefox-based browser" "it prints the Firefox guidance"
|
||||
|
||||
_t_report "test_leechblock_browsers"
|
||||
@ -1,88 +0,0 @@
|
||||
#!/usr/bin/env bash
|
||||
# Covers lib/leechblock_fetch.sh's tag resolution and version normalisation.
|
||||
#
|
||||
# get_latest_tag has three fallbacks in priority order (releases API → tags
|
||||
# API → redirect-header parse). A silent failure there installs the WRONG
|
||||
# version of a blocking extension, so each fallback is exercised separately,
|
||||
# including the path where jq is absent entirely.
|
||||
set -euo pipefail
|
||||
|
||||
HERE="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
|
||||
# shellcheck source=leechblock_harness.sh
|
||||
source "$HERE/leechblock_harness.sh"
|
||||
# shellcheck source=../leechblock_fetch.sh
|
||||
source "$LEECHBLOCK_LIB_DIR/leechblock_fetch.sh"
|
||||
|
||||
# Exported because get_latest_tag reads them from the environment of the
|
||||
# sourced lib rather than taking them as arguments; a plain assignment reads as
|
||||
# unused here (SC2034) even though the code under test depends on it.
|
||||
export REPO_OWNER="proginosko"
|
||||
export REPO_NAME="LeechBlockNG-chrome"
|
||||
|
||||
_t_setup_shims
|
||||
trap _t_teardown EXIT
|
||||
|
||||
printf 'get_latest_tag: releases API wins when it answers\n'
|
||||
printf '{"tag_name":"v1.2.3"}\n' >"$TEST_TMPDIR/curl.releases"
|
||||
# A subshell would strand nothing here (the value is echoed), but the call is
|
||||
# routed through a file so a future assertion on side effects still works.
|
||||
get_latest_tag "$REPO_NAME" >"$TEST_TMPDIR/out"
|
||||
_t_eq "v1.2.3" "$(cat "$TEST_TMPDIR/out")" "the releases endpoint supplies the tag"
|
||||
_t_has "$(cat "$TEST_TMPDIR/curl.calls")" "releases/latest" "it queried the releases endpoint"
|
||||
|
||||
printf '\nget_latest_tag: falls back to the tags API when releases is empty\n'
|
||||
: >"$TEST_TMPDIR/curl.calls"
|
||||
printf '{}\n' >"$TEST_TMPDIR/curl.releases"
|
||||
printf '[{"name":"v9.9.9"}]\n' >"$TEST_TMPDIR/curl.tags"
|
||||
get_latest_tag "$REPO_NAME" >"$TEST_TMPDIR/out"
|
||||
_t_eq "v9.9.9" "$(cat "$TEST_TMPDIR/out")" "an empty releases reply falls through to tags"
|
||||
_t_has "$(cat "$TEST_TMPDIR/curl.calls")" "tags?per_page=1" "it queried the tags endpoint"
|
||||
|
||||
printf '\nget_latest_tag: a literal null is treated as absent, not as a tag\n'
|
||||
printf '{"tag_name":null}\n' >"$TEST_TMPDIR/curl.releases"
|
||||
printf '[{"name":"v4.5.6"}]\n' >"$TEST_TMPDIR/curl.tags"
|
||||
get_latest_tag "$REPO_NAME" >"$TEST_TMPDIR/out"
|
||||
_t_eq "v4.5.6" "$(cat "$TEST_TMPDIR/out")" "a JSON null does not become the version"
|
||||
|
||||
printf '\nget_latest_tag: redirect-header fallback when jq is unavailable\n'
|
||||
_t_hide_jq
|
||||
printf 'location: https://github.com/x/y/releases/tag/v7.7.7\r\n' >"$TEST_TMPDIR/curl.head"
|
||||
get_latest_tag "$REPO_NAME" >"$TEST_TMPDIR/out"
|
||||
_t_eq "v7.7.7" "$(cat "$TEST_TMPDIR/out")" "the Location header supplies the tag without jq"
|
||||
|
||||
printf '\nget_latest_tag: reports failure when every source is exhausted\n'
|
||||
# Still inside the hidden-jq PATH: with no Location header either, all three
|
||||
# fallbacks are exhausted. The caller aborts the install on this non-zero
|
||||
# status, so returning 0 with an empty tag would install a bogus version.
|
||||
: >"$TEST_TMPDIR/curl.head"
|
||||
if get_latest_tag "$REPO_NAME" >"$TEST_TMPDIR/out" 2>&1; then
|
||||
_t_fail "an exhausted lookup must return non-zero"
|
||||
else
|
||||
_t_pass "an exhausted lookup returns non-zero rather than an empty version"
|
||||
fi
|
||||
_t_show_jq
|
||||
|
||||
printf '\nresolve_version: normalises the tag and derives the install layout\n'
|
||||
VERSION="v2.0.1"
|
||||
HOME="$TEST_TMPDIR/home"
|
||||
XDG_DATA_HOME="$TEST_TMPDIR/home/.local/share"
|
||||
resolve_version
|
||||
_t_eq "2.0.1" "$VERSION" "the leading v is stripped for directory names"
|
||||
_t_eq "v2.0.1" "$TAG" "the tag keeps its leading v"
|
||||
_t_eq "$XDG_DATA_HOME/leechblockng/2.0.1" "$VERSION_DIR" "VERSION_DIR is derived from the stripped version"
|
||||
_t_eq "$XDG_DATA_HOME/leechblockng/current" "$CURRENT_LINK" "CURRENT_LINK is the stable wrapper path"
|
||||
|
||||
printf '\nresolve_version: a non-standard tag warns but still installs\n'
|
||||
VERSION="nightly-build"
|
||||
# warn() prints to stdout, not stderr, so redirect stdout to capture it.
|
||||
resolve_version >"$TEST_TMPDIR/warn" 2>&1
|
||||
_t_has "$(cat "$TEST_TMPDIR/warn")" "doesn't look like" "an odd tag produces a warning"
|
||||
_t_eq "nightly-build" "$VERSION" "an odd tag is still honoured rather than rejected"
|
||||
|
||||
printf '\nresolve_version: an unset VERSION resolves from GitHub\n'
|
||||
printf '{"tag_name":"v3.3.3"}\n' >"$TEST_TMPDIR/curl.releases"
|
||||
VERSION=""
|
||||
resolve_version >/dev/null
|
||||
_t_eq "3.3.3" "$VERSION" "an empty VERSION is filled from the releases endpoint"
|
||||
|
||||
_t_report "test_leechblock_fetch"
|
||||
@ -59,8 +59,6 @@ LEECHBLOCK_INSTALL_DIR="/usr/local/share/digital_wellbeing"
|
||||
LEECHBLOCK_INSTALLER_DEST="${LEECHBLOCK_INSTALL_DIR}/install_leechblock.sh"
|
||||
LEECHBLOCK_DEFAULTS_DEST="${LEECHBLOCK_INSTALL_DIR}/leechblock_defaults.json"
|
||||
LEECHBLOCK_SEEDER_DEST="${LEECHBLOCK_INSTALL_DIR}/seed_leechblock_storage.js"
|
||||
LEECHBLOCK_LIB_SOURCE="$(dirname "$0")/../lib"
|
||||
LEECHBLOCK_LIB_DEST="${LEECHBLOCK_INSTALL_DIR}/lib"
|
||||
VBOX_ENFORCE_SOURCE="$(dirname "$0")/../virtualbox/enforce_vbox_hosts.sh"
|
||||
VBOX_INSTALL_DIR="/usr/local/share/digital_wellbeing/virtualbox"
|
||||
VBOX_ENFORCE_DEST="${VBOX_INSTALL_DIR}/enforce_vbox_hosts.sh"
|
||||
|
||||
@ -22,21 +22,6 @@ install_leechblock_payload() {
|
||||
echo -e "${BLUE}Installing LeechBlock installer to ${LEECHBLOCK_INSTALLER_DEST}...${NC}"
|
||||
cp "$LEECHBLOCK_INSTALLER_SOURCE" "$LEECHBLOCK_INSTALLER_DEST"
|
||||
chmod +x "$LEECHBLOCK_INSTALLER_DEST"
|
||||
# The installer sources every install phase from lib/ beside itself, so
|
||||
# the directory has to travel with it. Deploying the entry script alone
|
||||
# leaves a copy that dies on its first `source`. Mirror rather than
|
||||
# merge, so a lib deleted upstream does not linger here.
|
||||
if [ -d "$LEECHBLOCK_LIB_SOURCE" ]; then
|
||||
rm -rf "$LEECHBLOCK_LIB_DEST"
|
||||
mkdir -p "$LEECHBLOCK_LIB_DEST"
|
||||
# Only the leechblock_* libs: lib/ is shared with unrelated scripts
|
||||
# (the music_* helpers) that this payload has no business shipping.
|
||||
cp "$LEECHBLOCK_LIB_SOURCE"/leechblock_*.sh "$LEECHBLOCK_LIB_DEST"/
|
||||
chmod +x "$LEECHBLOCK_LIB_DEST"/leechblock_*.sh
|
||||
echo -e "${GREEN}LeechBlock libs deployed to ${LEECHBLOCK_LIB_DEST}${NC}"
|
||||
else
|
||||
echo -e "${YELLOW}LeechBlock lib/ not found at ${LEECHBLOCK_LIB_SOURCE}; the deployed installer will not run${NC}"
|
||||
fi
|
||||
echo -e "${GREEN}LeechBlock installer deployed to ${LEECHBLOCK_INSTALLER_DEST}${NC}"
|
||||
else
|
||||
echo -e "${YELLOW}LeechBlock installer not found at ${LEECHBLOCK_INSTALLER_SOURCE}, skipping...${NC}"
|
||||
|
||||
Loading…
Reference in New Issue
Block a user