diff options
| author | Xandor Schiefer <me@xandor.co.za> | 2024-09-03 02:44:02 +0200 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-09-02 17:44:02 -0700 |
| commit | f1492683c10cbe4fd5c03e6a75fb0256bd739c2a (patch) | |
| tree | a36012c9f8ac8404aee635a86075a7e44f2f180e /test | |
| parent | 73617ab050b646f099ba7ccb74cbecf94329a67c (diff) | |
Fix `apheleia-npx` in Yarn PnP projects (#301)
* `apheleia-npx` would use an incorrect path for the Yarn PnP ESM
loader.
* `apheleia-npx` did not correctly guard against word splitting.
* `apheleia-npx` was sometimes not able to find formatters in a Yarn PnP
project if there was also a node_modules folder at the root of the
project. Unfortunately, many tools (including
[Prettier](https://github.com/prettier/prettier/issues/13032)) will
create a cache folder in `node_modules` even in Yarn PnP projects. The
presence of any `node_modules` folders are irrelevant when a `.pnp.cjs`
file is present.
---------
Co-authored-by: Radon Rosborough <radon@intuitiveexplanations.com>
Diffstat (limited to 'test')
16 files changed, 167 insertions, 25 deletions
diff --git a/test/formatters/apheleia-ft.el b/test/formatters/apheleia-ft.el index 3855fe0..0409606 100755 --- a/test/formatters/apheleia-ft.el +++ b/test/formatters/apheleia-ft.el @@ -20,6 +20,10 @@ "Root directory of the Git repository. Guaranteed to be absolute and expanded.") +(defvar apheleia-ft--temp-dir + (expand-file-name "apheleia-ft" (temporary-file-directory)) + "Directory for storing temporary files.") + (defun apheleia-ft--relative-truename (path) "Given PATH relative to repo root, resolve symlinks. Return another path relative to repo root." @@ -155,14 +159,68 @@ Return the filename." (defun apheleia-ft--input-files (formatter) "For given FORMATTER, return list of input files used in test cases. -These are absolute filepaths beginning with \"in.\"." - (directory-files +These are absolute filepaths whose basenames begin with \"in.\". +FORMATTER is a string." + (directory-files-recursively (apheleia-ft--path-join apheleia-ft--test-dir "samplecode" formatter) - 'full "^in\\.")) +(defun apheleia-ft--copy-inputs (formatter in-file) + "Prepare FORMATTER for testing by copying IN-FILE and related. +FORMATTER is a string, IN-FILE is an absolute filepath whose +basename begins with \"in.\". All files from the samplecode +subdirectory for FORMATTER are copied to the toplevel of +`apheleia-ft--temp-dir', replicating the directory structure, +except that all the actual \"in.\" and \"out.\" files are not +copied, except for IN-FILE, which is left in the corresponding +place in the directory structure. Any existing files or +directories in `apheleia-ft--temp-dir' are removed. Return the +absolute filepath to which IN-FILE was copied in the temporary +directory." + (delete-directory apheleia-ft--temp-dir 'recursive) + (unless (zerop + (call-process + "cp" nil nil nil + (apheleia-ft--path-join + apheleia-ft--test-dir + "samplecode" formatter) + apheleia-ft--temp-dir + "-RTL")) + (error "cp failed")) + (with-temp-file (expand-file-name ".dir-locals.el" apheleia-ft--temp-dir) + (prin1 '((nil . ((indent-tabs-mode . nil) + (apheleia-formatters-respect-fill-column . nil) + (apheleia-formatters-respect-indent-level . nil)))) + (current-buffer))) + (let ((new-file + (expand-file-name + (replace-regexp-in-string + (concat "^" (regexp-quote + (apheleia-ft--path-join + apheleia-ft--test-dir + "samplecode" formatter)) + "/") + "" in-file) + apheleia-ft--temp-dir))) + (prog1 new-file + (dolist (fname (directory-files-recursively + apheleia-ft--temp-dir + "^\\(in\\|out\\)\\.")) + (unless (string= fname new-file) + (delete-file fname))) + (let ((init-script (expand-file-name + ".apheleia-ft.bash" + (file-name-directory + new-file)))) + (when (file-exists-p init-script) + (let ((default-directory (file-name-directory init-script))) + (unless (zerop + (call-process + "bash" nil nil nil init-script)) + (error "init script failed: %S" init-script)))))))) + (defun apheleia-ft--path-join (component &rest components) "Join COMPONENT and COMPONENTS together, left to right. Return an absolute path." @@ -242,12 +300,7 @@ involve running any formatters." apheleia-ft--test-dir "samplecode" formatter out-file)) (error "Input file %s is has no corresponding output file %s" in-file out-file)) - (push out-file out-files))) - (dolist (file all-files) - (unless (or (member file in-files) - (member file out-files)) - (error "Spurious sample code file at samplecode/%s/%s" - formatter file))))) + (push out-file out-files))))) (dolist (samplecode-dir samplecode-dirs) (unless (member samplecode-dir formatters) (error @@ -270,13 +323,11 @@ returned context." (interactive (unless (or current-prefix-arg noninteractive) (list (completing-read "Formatter: " (apheleia-ft--get-formatters))))) - (setq-default indent-tabs-mode nil) (dolist (formatter (or formatters (apheleia-ft--get-formatters))) (dolist (in-file (apheleia-ft--input-files formatter)) (let* ((extension (file-name-extension in-file)) (in-text (apheleia-ft--read-file in-file)) - (in-temp-file (apheleia-ft--write-temp-file - in-text extension)) + (in-temp-file (apheleia-ft--copy-inputs formatter in-file)) (out-temp-file nil) (command (alist-get (intern formatter) apheleia-formatters)) (syms nil) @@ -294,12 +345,18 @@ returned context." ;; Borrowed with love from Magit (let ((load-suffixes '(".el"))) (locate-library "apheleia")))))) - exec-path))) + exec-path)) + (display-fname + (replace-regexp-in-string + (concat "^" (regexp-quote + (apheleia-ft--path-join + apheleia-ft--test-dir + "samplecode" formatter)) + "/") + "" in-file))) + (when-let ((buf (get-file-buffer in-temp-file))) + (kill-buffer buf)) (with-current-buffer (find-file-noselect in-temp-file) - ;; Some formatters use the current file-name or buffer-name to interpret the - ;; type of file that is being formatted. Some may not be able to determine - ;; this from the contents of the file so we set this to force it. - (rename-buffer (file-name-nondirectory in-file)) (setq stdout-buffer (get-buffer-create (format "*apheleia-ft-stdout-%S%s" formatter extension))) (with-current-buffer stdout-buffer @@ -358,9 +415,12 @@ returned context." (apheleia-ft--print-diff "expected" expected-out-text "actual" out-text) - (error "Formatter %s did not format as expected" formatter))) + (error "Formatter %s did not format %s as expected" + formatter display-fname))) (princ (format "[format-test] success: formatter %s (file %s)\n" - formatter (file-name-nondirectory in-file))))))) + formatter display-fname)) + ;; https://stackoverflow.com/a/66558297 + (set-binary-mode 'stdout nil))))) (provide 'apheleia-ft) diff --git a/test/formatters/installers/prettier-ruby.bash b/test/formatters/installers/prettier-ruby.bash index 9351a44..9f5e244 100644 --- a/test/formatters/installers/prettier-ruby.bash +++ b/test/formatters/installers/prettier-ruby.bash @@ -12,5 +12,4 @@ gem install rbs -v 3.1.3 gem install prettier_print syntax_tree syntax_tree-haml syntax_tree-rbs # Install the plugin -cd /tmp -npm install --save-dev prettier @prettier/plugin-ruby +npm install -g prettier @prettier/plugin-ruby diff --git a/test/formatters/installers/prettier-svelte.bash b/test/formatters/installers/prettier-svelte.bash index 1ab0809..70fae61 100644 --- a/test/formatters/installers/prettier-svelte.bash +++ b/test/formatters/installers/prettier-svelte.bash @@ -1,2 +1 @@ -cd /tmp -npm install --save-dev prettier-plugin-svelte prettier +npm install -g prettier-plugin-svelte prettier diff --git a/test/formatters/samplecode/prettier-ruby/.apheleia-ft.bash b/test/formatters/samplecode/prettier-ruby/.apheleia-ft.bash new file mode 100755 index 0000000..8febad9 --- /dev/null +++ b/test/formatters/samplecode/prettier-ruby/.apheleia-ft.bash @@ -0,0 +1,4 @@ +# Fucking Node devs removed NODE_PATH support so now you have to do +# this bullshit to get import() to work with globally installed +# packages. +ln -s /usr/lib/node_modules ./ diff --git a/test/formatters/samplecode/prettier-ruby/.prettierrc.json b/test/formatters/samplecode/prettier-ruby/.prettierrc.json new file mode 100644 index 0000000..0967ef4 --- /dev/null +++ b/test/formatters/samplecode/prettier-ruby/.prettierrc.json @@ -0,0 +1 @@ +{} diff --git a/test/formatters/samplecode/prettier-svelte/.apheleia-ft.bash b/test/formatters/samplecode/prettier-svelte/.apheleia-ft.bash new file mode 100755 index 0000000..8febad9 --- /dev/null +++ b/test/formatters/samplecode/prettier-svelte/.apheleia-ft.bash @@ -0,0 +1,4 @@ +# Fucking Node devs removed NODE_PATH support so now you have to do +# this bullshit to get import() to work with globally installed +# packages. +ln -s /usr/lib/node_modules ./ diff --git a/test/formatters/samplecode/prettier/test-finds-config-file/.prettierrc.json b/test/formatters/samplecode/prettier/test-finds-config-file/.prettierrc.json new file mode 100644 index 0000000..d7f8afd --- /dev/null +++ b/test/formatters/samplecode/prettier/test-finds-config-file/.prettierrc.json @@ -0,0 +1,3 @@ +{ + "tabWidth": 3 +} diff --git a/test/formatters/samplecode/prettier/test-finds-config-file/in.js b/test/formatters/samplecode/prettier/test-finds-config-file/in.js new file mode 120000 index 0000000..ac3d6dc --- /dev/null +++ b/test/formatters/samplecode/prettier/test-finds-config-file/in.js @@ -0,0 +1 @@ +../../prettier-javascript/in.js
\ No newline at end of file diff --git a/test/formatters/samplecode/prettier/test-finds-config-file/out.js b/test/formatters/samplecode/prettier/test-finds-config-file/out.js new file mode 100644 index 0000000..830baf2 --- /dev/null +++ b/test/formatters/samplecode/prettier/test-finds-config-file/out.js @@ -0,0 +1,10 @@ +function HelloWorld({ + greeting = "hello", + greeted = '"World"', + silent = false, + onMouseOver, +}) { + if (!greeting) { + return null; + } +} diff --git a/test/formatters/samplecode/prettier/test-uses-node-modules/.apheleia-ft.bash b/test/formatters/samplecode/prettier/test-uses-node-modules/.apheleia-ft.bash new file mode 100755 index 0000000..5fdd18f --- /dev/null +++ b/test/formatters/samplecode/prettier/test-uses-node-modules/.apheleia-ft.bash @@ -0,0 +1 @@ +npm install diff --git a/test/formatters/samplecode/prettier/test-uses-node-modules/.gitignore b/test/formatters/samplecode/prettier/test-uses-node-modules/.gitignore new file mode 100644 index 0000000..3c3629e --- /dev/null +++ b/test/formatters/samplecode/prettier/test-uses-node-modules/.gitignore @@ -0,0 +1 @@ +node_modules diff --git a/test/formatters/samplecode/prettier/test-uses-node-modules/in.js b/test/formatters/samplecode/prettier/test-uses-node-modules/in.js new file mode 100644 index 0000000..1b5b832 --- /dev/null +++ b/test/formatters/samplecode/prettier/test-uses-node-modules/in.js @@ -0,0 +1,6 @@ +// Behavior differs between 2.x and 3.x +// https://prettier.io/blog/2023/07/05/3.0.0.html +call( + @dec + class {}, +); diff --git a/test/formatters/samplecode/prettier/test-uses-node-modules/out.js b/test/formatters/samplecode/prettier/test-uses-node-modules/out.js new file mode 100644 index 0000000..0be04cf --- /dev/null +++ b/test/formatters/samplecode/prettier/test-uses-node-modules/out.js @@ -0,0 +1,8 @@ +// Behavior differs between 2.x and 3.x +// https://prettier.io/blog/2023/07/05/3.0.0.html +call( + ( + @dec + class {} + ) +); diff --git a/test/formatters/samplecode/prettier/test-uses-node-modules/package-lock.json b/test/formatters/samplecode/prettier/test-uses-node-modules/package-lock.json new file mode 100644 index 0000000..dacc29d --- /dev/null +++ b/test/formatters/samplecode/prettier/test-uses-node-modules/package-lock.json @@ -0,0 +1,30 @@ +{ + "name": "apheleia-ft-prettier-test-uses-node-modules", + "version": "1.0.0", + "lockfileVersion": 3, + "requires": true, + "packages": { + "": { + "name": "apheleia-ft-prettier-test-uses-node-modules", + "version": "1.0.0", + "license": "MIT", + "dependencies": { + "prettier": "^2.8.8" + } + }, + "node_modules/prettier": { + "version": "2.8.8", + "resolved": "https://registry.npmjs.org/prettier/-/prettier-2.8.8.tgz", + "integrity": "sha512-tdN8qQGvNjw4CHbY+XXk0JgCXn9QiF21a55rBe5LJAU+kDyC4WQn4+awm2Xfk2lQMk5fKup9XgzTZtGkjBdP9Q==", + "bin": { + "prettier": "bin-prettier.js" + }, + "engines": { + "node": ">=10.13.0" + }, + "funding": { + "url": "https://github.com/prettier/prettier?sponsor=1" + } + } + } +} diff --git a/test/formatters/samplecode/prettier/test-uses-node-modules/package.json b/test/formatters/samplecode/prettier/test-uses-node-modules/package.json new file mode 100644 index 0000000..601a9d0 --- /dev/null +++ b/test/formatters/samplecode/prettier/test-uses-node-modules/package.json @@ -0,0 +1,12 @@ +{ + "name": "apheleia-ft-prettier-test-uses-node-modules", + "version": "1.0.0", + "description": "Prettier should be used from node_modules", + "main": "index.js", + "author": "Radian LLC <contact+apheleia@radian.codes>", + "license": "MIT", + "private": true, + "dependencies": { + "prettier": "^2.8.8" + } +} diff --git a/test/shared/run-func.bash b/test/shared/run-func.bash index f5d6f96..8742c95 100755 --- a/test/shared/run-func.bash +++ b/test/shared/run-func.bash @@ -10,5 +10,8 @@ shift cd "$(dirname "$0")/../.." -exec emacs --batch -L . "$@" \ - --eval "(setq debug-on-error t)" -f "${func}" +exec emacs --batch -L . "$@" \ + --eval "(setq debug-on-error t)" \ + --eval "(setq backtrace-line-length 0)" \ + -f "${func}" \ + 2>&1 | sed -uE 's/^(.{320}).+$/\1...[truncated]/' |
