Skip to content

Commit 791346c

Browse files
authored
Fix brittany formatter & misc improvements (#222)
* Fix brittany installation, needed the `--reorder-goals` argument to be added so that a version that was not the latest version could be installed so that it was compatible with the base lib version shipped with Ubuntu 20.04. We'll upgrade to 22.04 eventually (before it falls out of LTS). Ref: #221 * Add a `make fmt-build-common` target which allows tagging a docker image containing just the base software and not any formatters, to make it easy to debug formatter installation manually. * Update `apheleia-ft` to also run formatter tests when any files affecting a formatter are changed, which includes the installation script, the sample input/output, and also any scripts (e.g. `apheleia-phpcs`) that it uses. We don't have any logic that will run all formatter tests at once, because that is unwieldy. That can be done manually if making a big change. * Update to actions/checkout@v4 from v2 because the older one was listed as deprecated. * Print full stacktraces when `apheleia-ft` fails with an Elisp error.
1 parent 35f72f6 commit 791346c

File tree

8 files changed

+87
-11
lines changed

8 files changed

+87
-11
lines changed

.github/workflows/formatters.yml

+4-4
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@ jobs:
55
runs-on: ubuntu-latest
66
steps:
77
- name: Checkout pull request
8-
uses: actions/checkout@v2
8+
uses: actions/checkout@v4
99
with:
1010
ref: ${{ github.event.pull_request.head.sha }}
11-
- name: Fetch master
12-
run: |
13-
git fetch
11+
# No shallow clone, we want to be able to compare PR branch
12+
# to main.
13+
fetch-depth: 0
1414
- name: Test changed formatters
1515
run: |
1616
set -euo pipefail

.github/workflows/lint.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ jobs:
1212
emacs_version: [26, 27, 28, "master"]
1313
steps:
1414
- name: Checkout
15-
uses: actions/checkout@v2
15+
uses: actions/checkout@v4
1616
- name: Run linters
1717
env:
1818
VERSION: ${{ matrix.emacs_version }}

Makefile

+5-1
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,11 @@ docker: ## Start a Docker shell; e.g. make docker VERSION=25.3
8080

8181
.PHONY: fmt-build # env vars: FORMATTERS, TAG
8282
fmt-build: ## Build a Docker image with formatters installed
83-
@test/formatters/build-image.bash
83+
@COMMON=0 test/formatters/build-image.bash
84+
85+
.PHONY: fmt-build-common # env var: TAG
86+
fmt-build-common: ## Build a Docker image with just the common base
87+
@COMMON=1 test/formatters/build-image.bash
8488

8589
.PHONY: fmt-docker # env var: TAG
8690
fmt-docker: ## Start a Docker shell for testing formatters

test/formatters/Dockerfile

+6-1
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,15 @@
11
# Ubuntu 20.04 LTS supported until April 2025
2-
FROM ubuntu:20.04
2+
FROM ubuntu:20.04 AS common
33

44
WORKDIR /build
55
COPY install-common.bash /build/
66
RUN ./install-common.bash
77

8+
# Add an intermediate tag so that it is possible to execute debugging
9+
# code before formatter installation. This is necessary because with
10+
# the newer docker build based on buildkit, you can't access
11+
# intermediate image layers by default anymore.
12+
FROM common
813
ARG FORMATTERS
914
COPY install-formatters.bash /build/
1015
COPY installers /build/installers/

test/formatters/apheleia-ft.el

+59
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,26 @@
99

1010
(require 'cl-lib)
1111
(require 'map)
12+
(require 'subr-x)
1213

1314
(defvar apheleia-ft--test-dir
1415
(file-name-directory
1516
(or load-file-name buffer-file-name))
1617
"Directory containing this module.")
1718

19+
(defvar apheleia-ft--repo-dir
20+
(expand-file-name (locate-dominating-file apheleia-ft--test-dir ".git"))
21+
"Root directory of the Git repository.
22+
Guaranteed to be absolute and expanded.")
23+
24+
(defun apheleia-ft--relative-truename (path)
25+
"Given PATH relative to repo root, resolve symlinks.
26+
Return another path relative to repo root."
27+
(string-remove-prefix
28+
apheleia-ft--repo-dir
29+
(file-truename
30+
(expand-file-name path apheleia-ft--repo-dir))))
31+
1832
(defun apheleia-ft--get-formatters (&optional all)
1933
"Return list of strings naming the formatters to run.
2034
This is determined by the environment variable FORMATTERS,
@@ -56,6 +70,44 @@ already in memory on the current branch."
5670
(goto-char (point-min))
5771
(read (current-buffer)))))
5872

73+
(defun apheleia-ft--files-changed-since (ref)
74+
"Get a list of the files changed between REF and HEAD."
75+
(let ((stderr-file (make-temp-file "apheleia-ft-stderr-")))
76+
(with-temp-buffer
77+
(let ((exit-status
78+
(call-process
79+
"git" nil (list (current-buffer) stderr-file) nil
80+
"diff" "--name-only" "--diff-filter=d" (format "%s..." ref))))
81+
(unless (zerop exit-status)
82+
(with-temp-buffer
83+
(insert-file-contents stderr-file)
84+
(princ (buffer-string)))
85+
(error "Failed to 'git diff', got exit status %S" exit-status)))
86+
(split-string (buffer-string)))))
87+
88+
(defun apheleia-ft--formatters-depending-on-file (changed-file)
89+
"Given CHANGED-FILE, return list of formatters affected by it.
90+
Return formatters as string names. This is used to determine
91+
which formatters need tests to be run. CHANGED-FILE should be
92+
relative to repo root, as returned by git diff --name-only."
93+
(setq changed-file (apheleia-ft--relative-truename changed-file))
94+
(save-match-data
95+
(cond
96+
((string-match
97+
"^test/formatters/installers/\\([^/]+\\)\\.bash$" changed-file)
98+
(list (match-string 1 changed-file)))
99+
((string-match
100+
"^test/formatters/samplecode/\\([^/]+\\)/[^/]+$" changed-file)
101+
(list (match-string 1 changed-file)))
102+
((string-match
103+
"^scripts/formatters/\\([^/]+\\)$" changed-file)
104+
(let ((script (match-string 1 changed-file)))
105+
(map-keys
106+
(map-filter
107+
(lambda (fmt def)
108+
(member script def))
109+
apheleia-formatters)))))))
110+
59111
(defun apheleia-ft--get-formatters-for-pull-request ()
60112
"Return list of formatter string names that were touched in this PR.
61113
This means their commands in `apheleia-formatters' are different
@@ -69,6 +121,13 @@ main."
69121
(unless (equal command (alist-get formatter old-formatters))
70122
(push (symbol-name formatter) touched-formatters)))
71123
new-formatters)
124+
(mapc
125+
(lambda (changed-file)
126+
(setq touched-formatters
127+
(nconc
128+
(apheleia-ft--formatters-depending-on-file changed-file)
129+
touched-formatters)))
130+
(apheleia-ft--files-changed-since "origin/main"))
72131
touched-formatters))
73132

74133
(defun apheleia-ft-changed ()

test/formatters/build-image.bash

+9-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@ set -euo pipefail
44

55
echo >&2 "build-image.bash: tagging apheleia-formatters:${TAG:-latest}"
66

7-
if [[ -n "${FORMATTERS:-}" ]]; then
7+
if [[ "${COMMON}" == "1" ]]; then
8+
echo "build-image.bash: will tag common base image only"
9+
elif [[ -n "${FORMATTERS:-}" ]]; then
810
echo "build-image.bash: will install these formatters: ${FORMATTERS}"
911
else
1012
echo "build-image.bash: will install all formatters by default"
@@ -17,6 +19,11 @@ if [[ "$OSTYPE" != darwin* ]] && [[ "$EUID" != 0 ]]; then
1719
docker=(sudo -E "${docker[@]}")
1820
fi
1921

20-
exec "${docker[@]}" build . \
22+
args=()
23+
if [[ "${COMMON}" == "1" ]]; then
24+
args+=(--target=common)
25+
fi
26+
27+
exec "${docker[@]}" build . "${args[@]}" \
2128
-t "apheleia-formatters:${TAG:-latest}" \
2229
--build-arg "FORMATTERS=${FORMATTERS:-}"

test/formatters/installers/brittany.bash

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,5 @@ apt-get install -y cabal-install-3.4 ghc
77
ln -s /opt/cabal/bin/cabal /usr/local/bin/
88

99
cabal v2-update
10-
cabal v2-install brittany
10+
cabal v2-install brittany --reorder-goals
1111
cp -L "$HOME/.cabal/bin/brittany" /usr/local/bin/

test/formatters/run-func.bash

+2-1
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,5 @@ set -euo pipefail
88
cd "$(dirname "$0")"
99
repo="$(cd ../.. && pwd)"
1010

11-
exec emacs --batch -L "${repo}" -L . -l apheleia-ft -f "$1"
11+
exec emacs --batch -L "${repo}" -L . -l apheleia-ft \
12+
--eval "(setq debug-on-error t)" -f "$1"

0 commit comments

Comments
 (0)