├── .github
├── ISSUE_TEMPLATE
│ └── bug_report.md
├── dependabot.yml
└── workflows
│ └── ci.yml
├── .gitignore
├── Cask
├── LICENSE
├── Makefile
├── README.rst
├── pet.el
└── test
├── pet-test.el
└── undercover-init.el
/.github/ISSUE_TEMPLATE/bug_report.md:
--------------------------------------------------------------------------------
1 | ---
2 | name: Bug report
3 | about: Create a report to help us improve
4 | title: ''
5 | labels: ''
6 | assignees: ''
7 |
8 | ---
9 |
10 | **Description**
11 |
12 |
13 | **Reproduction steps**
14 |
20 |
21 | **Expected behavior**
22 |
23 |
24 | **PET version**
25 |
26 | **Emacs version**
27 |
28 | **Desktop (please complete the following information):**
29 | - OS: [e.g. macOS]
30 | - Version [e.g. 12.5]
31 |
32 | **System tools versions**
33 | - dasel [e.g. v1.26.0]
34 | - sqlite3 [e.g. 3.39.2]
35 | - yq [e.g. 4.27.2]
36 | - toml [e.g. v2.0.2]
37 |
38 | **Additional context**
39 |
40 |
--------------------------------------------------------------------------------
/.github/dependabot.yml:
--------------------------------------------------------------------------------
1 | # To get started with Dependabot version updates, you'll need to specify which
2 | # package ecosystems to update and where the package manifests are located.
3 | # Please see the documentation for all configuration options:
4 | # https://docs.github.com/github/administering-a-repository/configuration-options-for-dependency-updates
5 |
6 | version: 2
7 | updates:
8 | - package-ecosystem: ["github-actions"] # See documentation for possible values
9 | directory: "/" # Location of package manifests
10 | schedule:
11 | interval: "weekly"
12 |
--------------------------------------------------------------------------------
/.github/workflows/ci.yml:
--------------------------------------------------------------------------------
1 | name: "CI"
2 | on:
3 | push:
4 | paths-ignore:
5 | - "README.rst"
6 | branches:
7 | - "main"
8 | pull_request:
9 | branches:
10 | - "*"
11 |
12 | jobs:
13 | ci:
14 | runs-on: "ubuntu-latest"
15 | continue-on-error: "${{ matrix.experimental }}"
16 | strategy:
17 | fail-fast: false
18 | matrix:
19 | emacs-version:
20 | - "26.1"
21 | - "26.2"
22 | - "26.3"
23 | - "27.1"
24 | - "27.2"
25 | - "28.1"
26 | - "28.2"
27 | - "29.1"
28 | - "29.2"
29 | - "29.3"
30 | - "29.4"
31 | - "30.1"
32 | - "release-snapshot"
33 | experimental: [false]
34 | # include:
35 | # - emacs-version: "snapshot"
36 | # experimental: true
37 |
38 | steps:
39 | - uses: "actions/checkout@v4"
40 |
41 | - uses: "purcell/setup-emacs@master"
42 | with:
43 | version: "${{ matrix.emacs-version }}"
44 |
45 | - uses: "cask/setup-cask@master"
46 | with:
47 | version: "snapshot"
48 |
49 | - name: "Compile"
50 | run: |
51 | make compile
52 |
53 | - uses: "actions/setup-go@v5"
54 | with:
55 | go-version: "stable"
56 | check-latest: true
57 | cache: false
58 |
59 | - name: "Install tomljson"
60 | run: |
61 | go install github.com/pelletier/go-toml/v2/cmd/tomljson@latest
62 |
63 | - name: "Test"
64 | run: |
65 | make test
66 |
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | # Created by https://www.toptal.com/developers/gitignore/api/elisp
2 | # Edit at https://www.toptal.com/developers/gitignore?templates=elisp
3 |
4 | ### Elisp ###
5 | # Compiled
6 | *.elc
7 |
8 | # Packaging
9 | .cask
10 |
11 | # Backup files
12 | *~
13 |
14 | # Undo-tree save-files
15 | *.~undo-tree
16 |
17 | # End of https://www.toptal.com/developers/gitignore/api/elisp
18 | coverage
19 |
--------------------------------------------------------------------------------
/Cask:
--------------------------------------------------------------------------------
1 | (source gnu)
2 | (source melpa)
3 |
4 | (depends-on "f" "0.6.0")
5 | (depends-on "map" "3.3.1")
6 | (depends-on "seq" "2.24")
7 |
8 | (development
9 | (depends-on "buttercup")
10 | (depends-on "undercover"))
11 |
12 | (package-file "pet.el")
13 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | GNU GENERAL PUBLIC LICENSE
2 | Version 3, 29 June 2007
3 |
4 | Copyright (C) 2007 Free Software Foundation, Inc.
5 | Everyone is permitted to copy and distribute verbatim copies
6 | of this license document, but changing it is not allowed.
7 |
8 | Preamble
9 |
10 | The GNU General Public License is a free, copyleft license for
11 | software and other kinds of works.
12 |
13 | The licenses for most software and other practical works are designed
14 | to take away your freedom to share and change the works. By contrast,
15 | the GNU General Public License is intended to guarantee your freedom to
16 | share and change all versions of a program--to make sure it remains free
17 | software for all its users. We, the Free Software Foundation, use the
18 | GNU General Public License for most of our software; it applies also to
19 | any other work released this way by its authors. You can apply it to
20 | your programs, too.
21 |
22 | When we speak of free software, we are referring to freedom, not
23 | price. Our General Public Licenses are designed to make sure that you
24 | have the freedom to distribute copies of free software (and charge for
25 | them if you wish), that you receive source code or can get it if you
26 | want it, that you can change the software or use pieces of it in new
27 | free programs, and that you know you can do these things.
28 |
29 | To protect your rights, we need to prevent others from denying you
30 | these rights or asking you to surrender the rights. Therefore, you have
31 | certain responsibilities if you distribute copies of the software, or if
32 | you modify it: responsibilities to respect the freedom of others.
33 |
34 | For example, if you distribute copies of such a program, whether
35 | gratis or for a fee, you must pass on to the recipients the same
36 | freedoms that you received. You must make sure that they, too, receive
37 | or can get the source code. And you must show them these terms so they
38 | know their rights.
39 |
40 | Developers that use the GNU GPL protect your rights with two steps:
41 | (1) assert copyright on the software, and (2) offer you this License
42 | giving you legal permission to copy, distribute and/or modify it.
43 |
44 | For the developers' and authors' protection, the GPL clearly explains
45 | that there is no warranty for this free software. For both users' and
46 | authors' sake, the GPL requires that modified versions be marked as
47 | changed, so that their problems will not be attributed erroneously to
48 | authors of previous versions.
49 |
50 | Some devices are designed to deny users access to install or run
51 | modified versions of the software inside them, although the manufacturer
52 | can do so. This is fundamentally incompatible with the aim of
53 | protecting users' freedom to change the software. The systematic
54 | pattern of such abuse occurs in the area of products for individuals to
55 | use, which is precisely where it is most unacceptable. Therefore, we
56 | have designed this version of the GPL to prohibit the practice for those
57 | products. If such problems arise substantially in other domains, we
58 | stand ready to extend this provision to those domains in future versions
59 | of the GPL, as needed to protect the freedom of users.
60 |
61 | Finally, every program is threatened constantly by software patents.
62 | States should not allow patents to restrict development and use of
63 | software on general-purpose computers, but in those that do, we wish to
64 | avoid the special danger that patents applied to a free program could
65 | make it effectively proprietary. To prevent this, the GPL assures that
66 | patents cannot be used to render the program non-free.
67 |
68 | The precise terms and conditions for copying, distribution and
69 | modification follow.
70 |
71 | TERMS AND CONDITIONS
72 |
73 | 0. Definitions.
74 |
75 | "This License" refers to version 3 of the GNU General Public License.
76 |
77 | "Copyright" also means copyright-like laws that apply to other kinds of
78 | works, such as semiconductor masks.
79 |
80 | "The Program" refers to any copyrightable work licensed under this
81 | License. Each licensee is addressed as "you". "Licensees" and
82 | "recipients" may be individuals or organizations.
83 |
84 | To "modify" a work means to copy from or adapt all or part of the work
85 | in a fashion requiring copyright permission, other than the making of an
86 | exact copy. The resulting work is called a "modified version" of the
87 | earlier work or a work "based on" the earlier work.
88 |
89 | A "covered work" means either the unmodified Program or a work based
90 | on the Program.
91 |
92 | To "propagate" a work means to do anything with it that, without
93 | permission, would make you directly or secondarily liable for
94 | infringement under applicable copyright law, except executing it on a
95 | computer or modifying a private copy. Propagation includes copying,
96 | distribution (with or without modification), making available to the
97 | public, and in some countries other activities as well.
98 |
99 | To "convey" a work means any kind of propagation that enables other
100 | parties to make or receive copies. Mere interaction with a user through
101 | a computer network, with no transfer of a copy, is not conveying.
102 |
103 | An interactive user interface displays "Appropriate Legal Notices"
104 | to the extent that it includes a convenient and prominently visible
105 | feature that (1) displays an appropriate copyright notice, and (2)
106 | tells the user that there is no warranty for the work (except to the
107 | extent that warranties are provided), that licensees may convey the
108 | work under this License, and how to view a copy of this License. If
109 | the interface presents a list of user commands or options, such as a
110 | menu, a prominent item in the list meets this criterion.
111 |
112 | 1. Source Code.
113 |
114 | The "source code" for a work means the preferred form of the work
115 | for making modifications to it. "Object code" means any non-source
116 | form of a work.
117 |
118 | A "Standard Interface" means an interface that either is an official
119 | standard defined by a recognized standards body, or, in the case of
120 | interfaces specified for a particular programming language, one that
121 | is widely used among developers working in that language.
122 |
123 | The "System Libraries" of an executable work include anything, other
124 | than the work as a whole, that (a) is included in the normal form of
125 | packaging a Major Component, but which is not part of that Major
126 | Component, and (b) serves only to enable use of the work with that
127 | Major Component, or to implement a Standard Interface for which an
128 | implementation is available to the public in source code form. A
129 | "Major Component", in this context, means a major essential component
130 | (kernel, window system, and so on) of the specific operating system
131 | (if any) on which the executable work runs, or a compiler used to
132 | produce the work, or an object code interpreter used to run it.
133 |
134 | The "Corresponding Source" for a work in object code form means all
135 | the source code needed to generate, install, and (for an executable
136 | work) run the object code and to modify the work, including scripts to
137 | control those activities. However, it does not include the work's
138 | System Libraries, or general-purpose tools or generally available free
139 | programs which are used unmodified in performing those activities but
140 | which are not part of the work. For example, Corresponding Source
141 | includes interface definition files associated with source files for
142 | the work, and the source code for shared libraries and dynamically
143 | linked subprograms that the work is specifically designed to require,
144 | such as by intimate data communication or control flow between those
145 | subprograms and other parts of the work.
146 |
147 | The Corresponding Source need not include anything that users
148 | can regenerate automatically from other parts of the Corresponding
149 | Source.
150 |
151 | The Corresponding Source for a work in source code form is that
152 | same work.
153 |
154 | 2. Basic Permissions.
155 |
156 | All rights granted under this License are granted for the term of
157 | copyright on the Program, and are irrevocable provided the stated
158 | conditions are met. This License explicitly affirms your unlimited
159 | permission to run the unmodified Program. The output from running a
160 | covered work is covered by this License only if the output, given its
161 | content, constitutes a covered work. This License acknowledges your
162 | rights of fair use or other equivalent, as provided by copyright law.
163 |
164 | You may make, run and propagate covered works that you do not
165 | convey, without conditions so long as your license otherwise remains
166 | in force. You may convey covered works to others for the sole purpose
167 | of having them make modifications exclusively for you, or provide you
168 | with facilities for running those works, provided that you comply with
169 | the terms of this License in conveying all material for which you do
170 | not control copyright. Those thus making or running the covered works
171 | for you must do so exclusively on your behalf, under your direction
172 | and control, on terms that prohibit them from making any copies of
173 | your copyrighted material outside their relationship with you.
174 |
175 | Conveying under any other circumstances is permitted solely under
176 | the conditions stated below. Sublicensing is not allowed; section 10
177 | makes it unnecessary.
178 |
179 | 3. Protecting Users' Legal Rights From Anti-Circumvention Law.
180 |
181 | No covered work shall be deemed part of an effective technological
182 | measure under any applicable law fulfilling obligations under article
183 | 11 of the WIPO copyright treaty adopted on 20 December 1996, or
184 | similar laws prohibiting or restricting circumvention of such
185 | measures.
186 |
187 | When you convey a covered work, you waive any legal power to forbid
188 | circumvention of technological measures to the extent such circumvention
189 | is effected by exercising rights under this License with respect to
190 | the covered work, and you disclaim any intention to limit operation or
191 | modification of the work as a means of enforcing, against the work's
192 | users, your or third parties' legal rights to forbid circumvention of
193 | technological measures.
194 |
195 | 4. Conveying Verbatim Copies.
196 |
197 | You may convey verbatim copies of the Program's source code as you
198 | receive it, in any medium, provided that you conspicuously and
199 | appropriately publish on each copy an appropriate copyright notice;
200 | keep intact all notices stating that this License and any
201 | non-permissive terms added in accord with section 7 apply to the code;
202 | keep intact all notices of the absence of any warranty; and give all
203 | recipients a copy of this License along with the Program.
204 |
205 | You may charge any price or no price for each copy that you convey,
206 | and you may offer support or warranty protection for a fee.
207 |
208 | 5. Conveying Modified Source Versions.
209 |
210 | You may convey a work based on the Program, or the modifications to
211 | produce it from the Program, in the form of source code under the
212 | terms of section 4, provided that you also meet all of these conditions:
213 |
214 | a) The work must carry prominent notices stating that you modified
215 | it, and giving a relevant date.
216 |
217 | b) The work must carry prominent notices stating that it is
218 | released under this License and any conditions added under section
219 | 7. This requirement modifies the requirement in section 4 to
220 | "keep intact all notices".
221 |
222 | c) You must license the entire work, as a whole, under this
223 | License to anyone who comes into possession of a copy. This
224 | License will therefore apply, along with any applicable section 7
225 | additional terms, to the whole of the work, and all its parts,
226 | regardless of how they are packaged. This License gives no
227 | permission to license the work in any other way, but it does not
228 | invalidate such permission if you have separately received it.
229 |
230 | d) If the work has interactive user interfaces, each must display
231 | Appropriate Legal Notices; however, if the Program has interactive
232 | interfaces that do not display Appropriate Legal Notices, your
233 | work need not make them do so.
234 |
235 | A compilation of a covered work with other separate and independent
236 | works, which are not by their nature extensions of the covered work,
237 | and which are not combined with it such as to form a larger program,
238 | in or on a volume of a storage or distribution medium, is called an
239 | "aggregate" if the compilation and its resulting copyright are not
240 | used to limit the access or legal rights of the compilation's users
241 | beyond what the individual works permit. Inclusion of a covered work
242 | in an aggregate does not cause this License to apply to the other
243 | parts of the aggregate.
244 |
245 | 6. Conveying Non-Source Forms.
246 |
247 | You may convey a covered work in object code form under the terms
248 | of sections 4 and 5, provided that you also convey the
249 | machine-readable Corresponding Source under the terms of this License,
250 | in one of these ways:
251 |
252 | a) Convey the object code in, or embodied in, a physical product
253 | (including a physical distribution medium), accompanied by the
254 | Corresponding Source fixed on a durable physical medium
255 | customarily used for software interchange.
256 |
257 | b) Convey the object code in, or embodied in, a physical product
258 | (including a physical distribution medium), accompanied by a
259 | written offer, valid for at least three years and valid for as
260 | long as you offer spare parts or customer support for that product
261 | model, to give anyone who possesses the object code either (1) a
262 | copy of the Corresponding Source for all the software in the
263 | product that is covered by this License, on a durable physical
264 | medium customarily used for software interchange, for a price no
265 | more than your reasonable cost of physically performing this
266 | conveying of source, or (2) access to copy the
267 | Corresponding Source from a network server at no charge.
268 |
269 | c) Convey individual copies of the object code with a copy of the
270 | written offer to provide the Corresponding Source. This
271 | alternative is allowed only occasionally and noncommercially, and
272 | only if you received the object code with such an offer, in accord
273 | with subsection 6b.
274 |
275 | d) Convey the object code by offering access from a designated
276 | place (gratis or for a charge), and offer equivalent access to the
277 | Corresponding Source in the same way through the same place at no
278 | further charge. You need not require recipients to copy the
279 | Corresponding Source along with the object code. If the place to
280 | copy the object code is a network server, the Corresponding Source
281 | may be on a different server (operated by you or a third party)
282 | that supports equivalent copying facilities, provided you maintain
283 | clear directions next to the object code saying where to find the
284 | Corresponding Source. Regardless of what server hosts the
285 | Corresponding Source, you remain obligated to ensure that it is
286 | available for as long as needed to satisfy these requirements.
287 |
288 | e) Convey the object code using peer-to-peer transmission, provided
289 | you inform other peers where the object code and Corresponding
290 | Source of the work are being offered to the general public at no
291 | charge under subsection 6d.
292 |
293 | A separable portion of the object code, whose source code is excluded
294 | from the Corresponding Source as a System Library, need not be
295 | included in conveying the object code work.
296 |
297 | A "User Product" is either (1) a "consumer product", which means any
298 | tangible personal property which is normally used for personal, family,
299 | or household purposes, or (2) anything designed or sold for incorporation
300 | into a dwelling. In determining whether a product is a consumer product,
301 | doubtful cases shall be resolved in favor of coverage. For a particular
302 | product received by a particular user, "normally used" refers to a
303 | typical or common use of that class of product, regardless of the status
304 | of the particular user or of the way in which the particular user
305 | actually uses, or expects or is expected to use, the product. A product
306 | is a consumer product regardless of whether the product has substantial
307 | commercial, industrial or non-consumer uses, unless such uses represent
308 | the only significant mode of use of the product.
309 |
310 | "Installation Information" for a User Product means any methods,
311 | procedures, authorization keys, or other information required to install
312 | and execute modified versions of a covered work in that User Product from
313 | a modified version of its Corresponding Source. The information must
314 | suffice to ensure that the continued functioning of the modified object
315 | code is in no case prevented or interfered with solely because
316 | modification has been made.
317 |
318 | If you convey an object code work under this section in, or with, or
319 | specifically for use in, a User Product, and the conveying occurs as
320 | part of a transaction in which the right of possession and use of the
321 | User Product is transferred to the recipient in perpetuity or for a
322 | fixed term (regardless of how the transaction is characterized), the
323 | Corresponding Source conveyed under this section must be accompanied
324 | by the Installation Information. But this requirement does not apply
325 | if neither you nor any third party retains the ability to install
326 | modified object code on the User Product (for example, the work has
327 | been installed in ROM).
328 |
329 | The requirement to provide Installation Information does not include a
330 | requirement to continue to provide support service, warranty, or updates
331 | for a work that has been modified or installed by the recipient, or for
332 | the User Product in which it has been modified or installed. Access to a
333 | network may be denied when the modification itself materially and
334 | adversely affects the operation of the network or violates the rules and
335 | protocols for communication across the network.
336 |
337 | Corresponding Source conveyed, and Installation Information provided,
338 | in accord with this section must be in a format that is publicly
339 | documented (and with an implementation available to the public in
340 | source code form), and must require no special password or key for
341 | unpacking, reading or copying.
342 |
343 | 7. Additional Terms.
344 |
345 | "Additional permissions" are terms that supplement the terms of this
346 | License by making exceptions from one or more of its conditions.
347 | Additional permissions that are applicable to the entire Program shall
348 | be treated as though they were included in this License, to the extent
349 | that they are valid under applicable law. If additional permissions
350 | apply only to part of the Program, that part may be used separately
351 | under those permissions, but the entire Program remains governed by
352 | this License without regard to the additional permissions.
353 |
354 | When you convey a copy of a covered work, you may at your option
355 | remove any additional permissions from that copy, or from any part of
356 | it. (Additional permissions may be written to require their own
357 | removal in certain cases when you modify the work.) You may place
358 | additional permissions on material, added by you to a covered work,
359 | for which you have or can give appropriate copyright permission.
360 |
361 | Notwithstanding any other provision of this License, for material you
362 | add to a covered work, you may (if authorized by the copyright holders of
363 | that material) supplement the terms of this License with terms:
364 |
365 | a) Disclaiming warranty or limiting liability differently from the
366 | terms of sections 15 and 16 of this License; or
367 |
368 | b) Requiring preservation of specified reasonable legal notices or
369 | author attributions in that material or in the Appropriate Legal
370 | Notices displayed by works containing it; or
371 |
372 | c) Prohibiting misrepresentation of the origin of that material, or
373 | requiring that modified versions of such material be marked in
374 | reasonable ways as different from the original version; or
375 |
376 | d) Limiting the use for publicity purposes of names of licensors or
377 | authors of the material; or
378 |
379 | e) Declining to grant rights under trademark law for use of some
380 | trade names, trademarks, or service marks; or
381 |
382 | f) Requiring indemnification of licensors and authors of that
383 | material by anyone who conveys the material (or modified versions of
384 | it) with contractual assumptions of liability to the recipient, for
385 | any liability that these contractual assumptions directly impose on
386 | those licensors and authors.
387 |
388 | All other non-permissive additional terms are considered "further
389 | restrictions" within the meaning of section 10. If the Program as you
390 | received it, or any part of it, contains a notice stating that it is
391 | governed by this License along with a term that is a further
392 | restriction, you may remove that term. If a license document contains
393 | a further restriction but permits relicensing or conveying under this
394 | License, you may add to a covered work material governed by the terms
395 | of that license document, provided that the further restriction does
396 | not survive such relicensing or conveying.
397 |
398 | If you add terms to a covered work in accord with this section, you
399 | must place, in the relevant source files, a statement of the
400 | additional terms that apply to those files, or a notice indicating
401 | where to find the applicable terms.
402 |
403 | Additional terms, permissive or non-permissive, may be stated in the
404 | form of a separately written license, or stated as exceptions;
405 | the above requirements apply either way.
406 |
407 | 8. Termination.
408 |
409 | You may not propagate or modify a covered work except as expressly
410 | provided under this License. Any attempt otherwise to propagate or
411 | modify it is void, and will automatically terminate your rights under
412 | this License (including any patent licenses granted under the third
413 | paragraph of section 11).
414 |
415 | However, if you cease all violation of this License, then your
416 | license from a particular copyright holder is reinstated (a)
417 | provisionally, unless and until the copyright holder explicitly and
418 | finally terminates your license, and (b) permanently, if the copyright
419 | holder fails to notify you of the violation by some reasonable means
420 | prior to 60 days after the cessation.
421 |
422 | Moreover, your license from a particular copyright holder is
423 | reinstated permanently if the copyright holder notifies you of the
424 | violation by some reasonable means, this is the first time you have
425 | received notice of violation of this License (for any work) from that
426 | copyright holder, and you cure the violation prior to 30 days after
427 | your receipt of the notice.
428 |
429 | Termination of your rights under this section does not terminate the
430 | licenses of parties who have received copies or rights from you under
431 | this License. If your rights have been terminated and not permanently
432 | reinstated, you do not qualify to receive new licenses for the same
433 | material under section 10.
434 |
435 | 9. Acceptance Not Required for Having Copies.
436 |
437 | You are not required to accept this License in order to receive or
438 | run a copy of the Program. Ancillary propagation of a covered work
439 | occurring solely as a consequence of using peer-to-peer transmission
440 | to receive a copy likewise does not require acceptance. However,
441 | nothing other than this License grants you permission to propagate or
442 | modify any covered work. These actions infringe copyright if you do
443 | not accept this License. Therefore, by modifying or propagating a
444 | covered work, you indicate your acceptance of this License to do so.
445 |
446 | 10. Automatic Licensing of Downstream Recipients.
447 |
448 | Each time you convey a covered work, the recipient automatically
449 | receives a license from the original licensors, to run, modify and
450 | propagate that work, subject to this License. You are not responsible
451 | for enforcing compliance by third parties with this License.
452 |
453 | An "entity transaction" is a transaction transferring control of an
454 | organization, or substantially all assets of one, or subdividing an
455 | organization, or merging organizations. If propagation of a covered
456 | work results from an entity transaction, each party to that
457 | transaction who receives a copy of the work also receives whatever
458 | licenses to the work the party's predecessor in interest had or could
459 | give under the previous paragraph, plus a right to possession of the
460 | Corresponding Source of the work from the predecessor in interest, if
461 | the predecessor has it or can get it with reasonable efforts.
462 |
463 | You may not impose any further restrictions on the exercise of the
464 | rights granted or affirmed under this License. For example, you may
465 | not impose a license fee, royalty, or other charge for exercise of
466 | rights granted under this License, and you may not initiate litigation
467 | (including a cross-claim or counterclaim in a lawsuit) alleging that
468 | any patent claim is infringed by making, using, selling, offering for
469 | sale, or importing the Program or any portion of it.
470 |
471 | 11. Patents.
472 |
473 | A "contributor" is a copyright holder who authorizes use under this
474 | License of the Program or a work on which the Program is based. The
475 | work thus licensed is called the contributor's "contributor version".
476 |
477 | A contributor's "essential patent claims" are all patent claims
478 | owned or controlled by the contributor, whether already acquired or
479 | hereafter acquired, that would be infringed by some manner, permitted
480 | by this License, of making, using, or selling its contributor version,
481 | but do not include claims that would be infringed only as a
482 | consequence of further modification of the contributor version. For
483 | purposes of this definition, "control" includes the right to grant
484 | patent sublicenses in a manner consistent with the requirements of
485 | this License.
486 |
487 | Each contributor grants you a non-exclusive, worldwide, royalty-free
488 | patent license under the contributor's essential patent claims, to
489 | make, use, sell, offer for sale, import and otherwise run, modify and
490 | propagate the contents of its contributor version.
491 |
492 | In the following three paragraphs, a "patent license" is any express
493 | agreement or commitment, however denominated, not to enforce a patent
494 | (such as an express permission to practice a patent or covenant not to
495 | sue for patent infringement). To "grant" such a patent license to a
496 | party means to make such an agreement or commitment not to enforce a
497 | patent against the party.
498 |
499 | If you convey a covered work, knowingly relying on a patent license,
500 | and the Corresponding Source of the work is not available for anyone
501 | to copy, free of charge and under the terms of this License, through a
502 | publicly available network server or other readily accessible means,
503 | then you must either (1) cause the Corresponding Source to be so
504 | available, or (2) arrange to deprive yourself of the benefit of the
505 | patent license for this particular work, or (3) arrange, in a manner
506 | consistent with the requirements of this License, to extend the patent
507 | license to downstream recipients. "Knowingly relying" means you have
508 | actual knowledge that, but for the patent license, your conveying the
509 | covered work in a country, or your recipient's use of the covered work
510 | in a country, would infringe one or more identifiable patents in that
511 | country that you have reason to believe are valid.
512 |
513 | If, pursuant to or in connection with a single transaction or
514 | arrangement, you convey, or propagate by procuring conveyance of, a
515 | covered work, and grant a patent license to some of the parties
516 | receiving the covered work authorizing them to use, propagate, modify
517 | or convey a specific copy of the covered work, then the patent license
518 | you grant is automatically extended to all recipients of the covered
519 | work and works based on it.
520 |
521 | A patent license is "discriminatory" if it does not include within
522 | the scope of its coverage, prohibits the exercise of, or is
523 | conditioned on the non-exercise of one or more of the rights that are
524 | specifically granted under this License. You may not convey a covered
525 | work if you are a party to an arrangement with a third party that is
526 | in the business of distributing software, under which you make payment
527 | to the third party based on the extent of your activity of conveying
528 | the work, and under which the third party grants, to any of the
529 | parties who would receive the covered work from you, a discriminatory
530 | patent license (a) in connection with copies of the covered work
531 | conveyed by you (or copies made from those copies), or (b) primarily
532 | for and in connection with specific products or compilations that
533 | contain the covered work, unless you entered into that arrangement,
534 | or that patent license was granted, prior to 28 March 2007.
535 |
536 | Nothing in this License shall be construed as excluding or limiting
537 | any implied license or other defenses to infringement that may
538 | otherwise be available to you under applicable patent law.
539 |
540 | 12. No Surrender of Others' Freedom.
541 |
542 | If conditions are imposed on you (whether by court order, agreement or
543 | otherwise) that contradict the conditions of this License, they do not
544 | excuse you from the conditions of this License. If you cannot convey a
545 | covered work so as to satisfy simultaneously your obligations under this
546 | License and any other pertinent obligations, then as a consequence you may
547 | not convey it at all. For example, if you agree to terms that obligate you
548 | to collect a royalty for further conveying from those to whom you convey
549 | the Program, the only way you could satisfy both those terms and this
550 | License would be to refrain entirely from conveying the Program.
551 |
552 | 13. Use with the GNU Affero General Public License.
553 |
554 | Notwithstanding any other provision of this License, you have
555 | permission to link or combine any covered work with a work licensed
556 | under version 3 of the GNU Affero General Public License into a single
557 | combined work, and to convey the resulting work. The terms of this
558 | License will continue to apply to the part which is the covered work,
559 | but the special requirements of the GNU Affero General Public License,
560 | section 13, concerning interaction through a network will apply to the
561 | combination as such.
562 |
563 | 14. Revised Versions of this License.
564 |
565 | The Free Software Foundation may publish revised and/or new versions of
566 | the GNU General Public License from time to time. Such new versions will
567 | be similar in spirit to the present version, but may differ in detail to
568 | address new problems or concerns.
569 |
570 | Each version is given a distinguishing version number. If the
571 | Program specifies that a certain numbered version of the GNU General
572 | Public License "or any later version" applies to it, you have the
573 | option of following the terms and conditions either of that numbered
574 | version or of any later version published by the Free Software
575 | Foundation. If the Program does not specify a version number of the
576 | GNU General Public License, you may choose any version ever published
577 | by the Free Software Foundation.
578 |
579 | If the Program specifies that a proxy can decide which future
580 | versions of the GNU General Public License can be used, that proxy's
581 | public statement of acceptance of a version permanently authorizes you
582 | to choose that version for the Program.
583 |
584 | Later license versions may give you additional or different
585 | permissions. However, no additional obligations are imposed on any
586 | author or copyright holder as a result of your choosing to follow a
587 | later version.
588 |
589 | 15. Disclaimer of Warranty.
590 |
591 | THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY
592 | APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT
593 | HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY
594 | OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO,
595 | THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
596 | PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM
597 | IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF
598 | ALL NECESSARY SERVICING, REPAIR OR CORRECTION.
599 |
600 | 16. Limitation of Liability.
601 |
602 | IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
603 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS
604 | THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY
605 | GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE
606 | USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF
607 | DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD
608 | PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS),
609 | EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF
610 | SUCH DAMAGES.
611 |
612 | 17. Interpretation of Sections 15 and 16.
613 |
614 | If the disclaimer of warranty and limitation of liability provided
615 | above cannot be given local legal effect according to their terms,
616 | reviewing courts shall apply local law that most closely approximates
617 | an absolute waiver of all civil liability in connection with the
618 | Program, unless a warranty or assumption of liability accompanies a
619 | copy of the Program in return for a fee.
620 |
621 | END OF TERMS AND CONDITIONS
622 |
623 | How to Apply These Terms to Your New Programs
624 |
625 | If you develop a new program, and you want it to be of the greatest
626 | possible use to the public, the best way to achieve this is to make it
627 | free software which everyone can redistribute and change under these terms.
628 |
629 | To do so, attach the following notices to the program. It is safest
630 | to attach them to the start of each source file to most effectively
631 | state the exclusion of warranty; and each file should have at least
632 | the "copyright" line and a pointer to where the full notice is found.
633 |
634 |
635 | Copyright (C)
636 |
637 | This program is free software: you can redistribute it and/or modify
638 | it under the terms of the GNU General Public License as published by
639 | the Free Software Foundation, either version 3 of the License, or
640 | (at your option) any later version.
641 |
642 | This program is distributed in the hope that it will be useful,
643 | but WITHOUT ANY WARRANTY; without even the implied warranty of
644 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
645 | GNU General Public License for more details.
646 |
647 | You should have received a copy of the GNU General Public License
648 | along with this program. If not, see .
649 |
650 | Also add information on how to contact you by electronic and paper mail.
651 |
652 | If the program does terminal interaction, make it output a short
653 | notice like this when it starts in an interactive mode:
654 |
655 | Copyright (C)
656 | This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'.
657 | This is free software, and you are welcome to redistribute it
658 | under certain conditions; type `show c' for details.
659 |
660 | The hypothetical commands `show w' and `show c' should show the appropriate
661 | parts of the General Public License. Of course, your program's commands
662 | might be different; for a GUI interface, you would use an "about box".
663 |
664 | You should also get your employer (if you work as a programmer) or school,
665 | if any, to sign a "copyright disclaimer" for the program, if necessary.
666 | For more information on this, and how to apply and follow the GNU GPL, see
667 | .
668 |
669 | The GNU General Public License does not permit incorporating your program
670 | into proprietary programs. If your program is a subroutine library, you
671 | may consider it more useful to permit linking proprietary applications with
672 | the library. If this is what you want to do, use the GNU Lesser General
673 | Public License instead of this License. But first, please read
674 | .
675 |
--------------------------------------------------------------------------------
/Makefile:
--------------------------------------------------------------------------------
1 | export EMACS ?= $(shell which emacs)
2 | CASK_DIR := $(shell cask package-directory)
3 |
4 | $(CASK_DIR): Cask
5 | cask eval '(progn (when (version< emacs-version "27") (setq package-check-signature nil)) (cask-cli/install))'
6 | @touch $(CASK_DIR)
7 |
8 | .PHONY: cask
9 | cask: $(CASK_DIR)
10 |
11 | clean:
12 | cask clean-elc
13 |
14 | .PHONY: compile
15 | compile: cask clean
16 | cask build
17 |
18 | .PHONY: test
19 | test: cask clean
20 | cask exec buttercup --traceback full -L . test
21 |
22 | .PHONY: coverage
23 | coverage: cask clean
24 | mkdir -p coverage
25 | UNDERCOVER_FORCE=true cask exec buttercup --traceback full -L . test
26 |
--------------------------------------------------------------------------------
/README.rst:
--------------------------------------------------------------------------------
1 | .. image:: https://github.com/wyuenho/emacs-pet/actions/workflows/ci.yml/badge.svg
2 | :target: https://github.com/wyuenho/emacs-pet/actions/workflows/ci.yml
3 | :alt: CI Status
4 |
5 | **P** ython **E** xecutable **T** racker
6 | ========================================
7 |
8 | Greatings fellow Pythonistas and Emacs users!
9 |
10 | Have you ever worked on a project that uses one of the many Python package
11 | managers and/or virtual environments, where all the linters, formatters and
12 | commit hooks are set up meticulously, and then when you fire up Emacs, packages
13 | like `flycheck `_ or `lsp-mode
14 | `_ are either unable to find the binary
15 | in your virtualenv, or are using the wrong one?
16 |
17 | Have you ever tried one of the 11+ Emacs virtualenv packages to help you fix
18 | this problem, but are still at a lost at why your other favorite Emacs packages
19 | still can't find the right binaries, or they stop working when you switch to a
20 | different project using a different flavor of virtualenv?
21 |
22 | If you answer "yes" for any of these questions, you've come to the right place.
23 |
24 |
25 | How does ``pet`` work?
26 | ----------------------
27 |
28 | The first key insight is to recognize the paths to the executables of many
29 | Python linting and formatting Emacs packages are configurable.
30 |
31 | The second key insight is Emacs allows you to setup a different value for the
32 | exectuable path on a per buffer basis, and that these packages work with these
33 | buffer-local values.
34 |
35 | The hardest problem is finding the correct executable, this is what ``pet``
36 | tries to solve.
37 |
38 | As long as you use one of the supported Python virtualenv tools, ``pet`` will be
39 | able to find the virtualenv root and binary you ask for, with **zero Emacs
40 | configuration** necessary.
41 |
42 | ``pet`` works well with popular source code project management packages such as
43 | `Projectile `_ and the
44 | built-in ``project.el``. The first time you call one the few ``pet`` helper
45 | functions, it will use Projectile or project.el to detect the root of your
46 | project, search for the configuration files for the many supported Python
47 | virtualenv tools, and then lookup the location of the virtualenv based on the
48 | content of the configuration files. Once a virtualenv is found, all executables
49 | are found by looking into its ``bin`` directory.
50 |
51 |
52 | Supported Python Virtual Environment Tools
53 | ------------------------------------------
54 |
55 | - `pre-commit `_
56 | - `poetry `_
57 | - `pipenv `_
58 | - `direnv `_
59 | - `venv `_, `virtualenv
60 | `_ or `virtualenvwrapper
61 | `_ (`virtualenvwrapper caveats`_)
62 | - `maturin `_
63 | - `uv `_
64 | - `pdm `_
65 | - `pipx `_
66 | - `pyenv `_ (very poorly maintained, don't use
67 | it unless you are using Homebrew on macOS)
68 | - `docker `_
69 | - `conda `_ (no support for conda-project yet)
70 | - `mamba
71 | `_
72 | - `micromamba
73 | `_
74 | - Whatever is on your ``VIRTUAL_ENV`` environment variable
75 | - Even when you aren't in a virtual environment
76 |
77 |
78 | Supported Emacs Packages
79 | ------------------------
80 |
81 | - Built-in `project.el `_
82 | - `projectile `_
83 | - `envrc `_ (`direnv caveats`_)
84 | - `eglot `_
85 | - `dape `_
86 | - `flycheck `_
87 | - `lsp-jedi `_
88 | - `lsp-pyright `_
89 | - `dap-python `_
90 | - `blacken `_
91 | - `yapfify `_
92 | - `python-black `_
93 | - `python-isort `_
94 | - `python-pytest `_
95 | - `ruff-format `_
96 | - `py-autopep8 `_
97 | - `auto-virtualenvwrapper `_
98 |
99 |
100 | System Requirements
101 | -------------------
102 |
103 | Currently ``pet`` requires a program to convert TOML to JSON, a program to
104 | convert YAML to JSON, and if you are using Emacs < 29, the ``sqlite3`` command
105 | to be installed on your system.
106 |
107 | By default, both the TOML to JSON and YAML to JSON converters are configured to
108 | use `dasel `_. If you are on Linux, it may
109 | be more convenient to use `tomljson
110 | `_ and `yq
111 | `_ since both of which are likely to be
112 | available from the system package management system.
113 |
114 | When a suitable Emacs Lisp YAML and TOML parser becomes available, ``dasel``
115 | will be made optional.
116 |
117 |
118 | Usage
119 | -----
120 |
121 | If you are using Emacs on macOS, to get the most out of ``pet``, it is best to
122 | install `exec-path-from-shell
123 | `_ first to ensure all of the
124 | `Supported Python Virtual Environment Tools`_ are available in your
125 | ``exec-path``. Once your ``exec-path`` is synced up to your shell's ``$PATH``
126 | environment variable, you can use the following ways to help you setup the rest
127 | of your Emacs packages **properly**.
128 |
129 |
130 | Basic Setup
131 | +++++++++++
132 |
133 | Generally, the following snippet is all you'll need:
134 |
135 | .. code-block:: elisp
136 |
137 | (require 'pet)
138 |
139 | ;; Emacs < 26
140 | ;; You have to make sure this function is added to the hook last so it's
141 | ;; called first
142 | (add-hook 'python-mode-hook 'pet-mode)
143 |
144 | ;; Emacs 27+
145 | ;; The -10 tells `add-hook' to makes sure the function is called as early as
146 | ;; possible whenever it is added to the hook variable
147 | (add-hook 'python-mode-hook 'pet-mode -10)
148 |
149 | ;; Emacs 29+
150 | ;; This will turn on `pet-mode' on `python-mode' and `python-ts-mode'
151 | (add-hook 'python-base-mode-hook 'pet-mode -10)
152 |
153 | Or, if you use `use-package `_:
154 |
155 | .. code-block:: elisp
156 |
157 | (use-package pet
158 | :config
159 | (add-hook 'python-base-mode-hook 'pet-mode -10))
160 |
161 |
162 | This will setup the buffer local variables for all of the `Supported Emacs
163 | Packages`_.
164 |
165 |
166 | Advanced Usage
167 | ++++++++++++++
168 |
169 | If you need to configure a package that ``pet`` doesn't support, or only want to
170 | configure a couple of packages instead of all of the supported ones, ``pet``
171 | offers 2 autoloaded functions to help you find the correct path to the
172 | executable and virtualenv directory:
173 |
174 | - ``(pet-executable-find EXECUTABLE)``
175 | - ``(pet-virtualenv-root)``
176 |
177 | For example, to set up ``python-mode`` to use the correct interpreter when you
178 | execute ``M-x run-python``:
179 |
180 | .. code-block:: elisp
181 |
182 | (add-hook 'python-mode-hook
183 | (lambda ()
184 | (setq-local python-shell-interpreter (pet-executable-find "python")
185 | python-shell-virtualenv-root (pet-virtualenv-root))))
186 |
187 |
188 | For ``flycheck``, due to its complexity, ``pet`` also comes with another
189 | autoloaded function to help you setup the ``flake8``, ``pylint`` and ``mypy``
190 | checkers:
191 |
192 | .. code-block:: elisp
193 |
194 | (add-hook 'python-mode-hook 'pet-flycheck-setup)
195 |
196 |
197 | Complete Example
198 | ++++++++++++++++
199 |
200 | .. code-block:: elisp
201 |
202 | (use-package exec-path-from-shell
203 | :if (memq (window-system) '(mac ns))
204 | :config (exec-path-from-shell-initialize))
205 |
206 | (use-package flycheck)
207 |
208 | (use-package lsp)
209 |
210 | (use-package lsp-jedi
211 | :after lsp)
212 |
213 | (use-package lsp-pyright
214 | :after lsp)
215 |
216 | (use-package dap-python
217 | :after lsp)
218 |
219 | (use-package eglot)
220 |
221 | (use-package python-pytest)
222 |
223 | (use-package python-black)
224 |
225 | (use-package python-isort)
226 |
227 | (use-package ruff-format)
228 |
229 | (use-package pet
230 | :ensure-system-package (dasel sqlite3)
231 | :config
232 | (add-hook 'python-mode-hook
233 | (lambda ()
234 | (setq-local python-shell-interpreter (pet-executable-find "python")
235 | python-shell-virtualenv-root (pet-virtualenv-root))
236 |
237 | ;; (pet-eglot-setup)
238 | ;; (eglot-ensure)
239 |
240 | (pet-flycheck-setup)
241 | (flycheck-mode)
242 |
243 | (setq-local lsp-jedi-executable-command
244 | (pet-executable-find "jedi-language-server"))
245 |
246 | (setq-local lsp-pyright-python-executable-cmd python-shell-interpreter
247 | lsp-pyright-venv-path python-shell-virtualenv-root)
248 |
249 | (lsp)
250 |
251 | (setq-local dap-python-executable python-shell-interpreter)
252 |
253 | (setq-local python-pytest-executable (pet-executable-find "pytest"))
254 |
255 | (when-let ((ruff-executable (pet-executable-find "ruff")))
256 | (setq-local ruff-format-command ruff-executable)
257 | (ruff-format-on-save-mode))
258 |
259 | (when-let ((black-executable (pet-executable-find "black")))
260 | (setq-local python-black-command black-executable)
261 | (python-black-on-save-mode))
262 |
263 | (when-let ((isort-executable (pet-executable-find "isort")))
264 | (setq-local python-isort-command isort-executable)
265 | (python-isort-on-save-mode)))))
266 |
267 |
268 | FAQ
269 | ---
270 |
271 | .. _direnv caveats:
272 |
273 | How do I get ``pet`` to pick up the virtualenv or PATH created by ``direnv``?
274 | +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
275 |
276 | Short answer:
277 |
278 | Use `envrc `_.
279 |
280 | .. code-block:: elisp
281 |
282 | (require 'envrc)
283 | (add-hook 'change-major-mode-after-body-hook 'envrc-mode)
284 |
285 |
286 | Longer answer:
287 |
288 | There are a number of packages similar to ``envrc`` such as ``direnv`` and
289 | ``buffer-env`` that claim to be able to configure ``direnv`` in Emacs. However,
290 | they all suffer from various problems such as changing the environment and
291 | ``exec-path`` for the entire Emacs process, unable to activate early enough or
292 | being too general to support direnv tightly.
293 |
294 | Because ``pet`` needs to configure the buffer local variables **before** the
295 | rest of the minor modes are activated, but **after** ``exec-path`` has been set
296 | up by direnv, one must take care of choosing a minor mode package that allows
297 | the user to customize when it takes effect. This requirement rules out
298 | ``direnv.el`` [1]_.
299 |
300 | .. [1] Earlier versions of ``pet`` suggested ``direnv.el`` as a solution, it is
301 | no longer recommended due to this reason.
302 |
303 | .. _virtualenvwrapper caveats:
304 |
305 | My project uses ``virtualenvwrapper``, how do I get ``pet`` to pick up the virtualenv?
306 | ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
307 |
308 | You can use ``envrc`` + `this direnv configuration
309 | `_ to activate
310 | your virtualenv or `auto-virtualenvwrapper
311 | `_. Note that in
312 | any case, your virtualenv must be activated before turning on ``pet-mode`` in
313 | order to make the environment variable ``VIRTUAL_ENV`` available to it. For
314 | example:
315 |
316 | .. code-block:: elisp
317 |
318 | (require 'auto-virtualenvwrapper)
319 | (require 'pet)
320 |
321 | (add-hook 'python-base-mode-hook
322 | (lambda ()
323 | (auto-virtualenvwrapper-activate)
324 | (pet-mode))
325 | -10)
326 | (add-hook 'window-configuration-change-hook #'auto-virtualenvwrapper-activate)
327 | (add-hook 'focus-in-hook #'auto-virtualenvwrapper-activate)
328 |
329 |
330 | Why didn't ``pet`` set up the executable variables on a fresh Python project clone?
331 | ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
332 |
333 | ``Pet`` does not automatically create virtualenvs for you. If you have a fresh
334 | clone, you must create the virtualenv and install your development dependencies
335 | into it first. Once it is done, the next time you open a Python file buffer
336 | ``pet`` will automatically set up the executable variables for you.
337 |
338 | To find out how to do it, please find the virtualenv tool in question from
339 | `Supported Python Virtual Environment Tools`_, and visit its documentation for
340 | details.
341 |
342 |
343 | Why doesn't ``pet`` simply set a buffer-local ``exec-path``?
344 | ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
345 |
346 | The reason is mainly due to the fact that many Python projects use development
347 | tools located in different virtualenvs. This means ``exec-path`` needs to be
348 | prepended with all of the virtualenvs for all of the dev tools, and always kept
349 | in the correct order. An example where this approach may cause issues is dealing
350 | with projects that use ``pre-commit`` and ``direnv``. A typical ``pre-commit``
351 | configuration may include many "hooks", where each of them is isolated in its
352 | own virtualenv. While prepending many directories to ``exec-path`` is not
353 | problematic in itself, playing well with other Emacs packages that mutate
354 | ``exec-path`` reliably is non-trivial. Providing an absolute path to executable
355 | variables conveniently sidesteps this complexity, while being slightly more
356 | performant.
357 |
358 | In addition, there are Emacs packages, most prominantly ``flycheck`` that by
359 | default require dev tools to be installed into the same virtualenv as the first
360 | ``python`` executable found on ``exec-path``. Changing this behavior requires
361 | setting the corresponding ``flycheck`` checker executable variable to the
362 | intended absolute path.
363 |
364 |
365 | My package didn't pick up the correct paths, how do I debug ``pet``?
366 | +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
367 |
368 | You can turn on ``pet-debug`` and watch what comes out in the ``*Messages*``
369 | buffer. In addition, you can use ``M-x pet-verify-setup`` in your Python buffers
370 | to find out what was detected.
371 |
372 | For ``lsp``, use ``lsp-describe-session``.
373 |
374 | For ``eglot``, use ``eglot-show-workspace-configuration``.
375 |
376 | For ``flycheck``, use ``flycheck-verify-setup``.
377 |
378 |
379 | Do I still need any of the 11+ virtualenv Emacs packages?
380 | +++++++++++++++++++++++++++++++++++++++++++++++++++++++++
381 |
382 | Nope. You can uninstall them all. This is the raison d'être of this package.
383 |
384 |
385 | License
386 | -------
387 |
388 | `GPLv3 <./LICENSE>`_
389 |
--------------------------------------------------------------------------------
/pet.el:
--------------------------------------------------------------------------------
1 | ;;; pet.el --- Executable and virtualenv tracker for python-mode -*- lexical-binding: t -*-
2 |
3 | ;; Author: Jimmy Yuen Ho Wong
4 | ;; Maintainer: Jimmy Yuen Ho Wong
5 | ;; Version: 3.1.0
6 | ;; Package-Requires: ((emacs "26.1") (f "0.6.0") (map "3.3.1") (seq "2.24"))
7 | ;; Homepage: https://github.com/wyuenho/emacs-pet/
8 | ;; Keywords: tools
9 |
10 |
11 | ;; This file is not part of GNU Emacs
12 |
13 | ;; This program is free software: you can redistribute it and/or modify
14 | ;; it under the terms of the GNU General Public License as published by
15 | ;; the Free Software Foundation, either version 3 of the License, or
16 | ;; (at your option) any later version.
17 |
18 | ;; This program is distributed in the hope that it will be useful,
19 | ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
20 | ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 | ;; GNU General Public License for more details.
22 |
23 | ;; You should have received a copy of the GNU General Public License
24 | ;; along with this program. If not, see .
25 |
26 |
27 | ;;; Commentary:
28 |
29 | ;; __P__ython __E__xecutable __T__racker. Tracks downs the correct Python
30 | ;; executables from the various virtualenv management tools and assign them to
31 | ;; buffer local variables. The package to end all Emacs virtualenv packages.
32 |
33 | ;;; Code:
34 |
35 |
36 | (require 'cl-lib)
37 | (require 'f)
38 | (require 'filenotify)
39 | (require 'let-alist)
40 | (require 'map)
41 | (require 'pcase)
42 | (require 'project)
43 | (require 'python)
44 | (require 'seq)
45 | (require 'subr-x)
46 | (require 'tramp)
47 |
48 | (when (< emacs-major-version 27)
49 | (require 'json))
50 |
51 | (defgroup pet nil
52 | "Customization group for `pet'."
53 | :group 'python
54 | :prefix "pet-")
55 |
56 | (defcustom pet-debug nil
57 | "Whether to turn on debug messages."
58 | :group 'pet
59 | :type 'boolean)
60 |
61 | (defcustom pet-toml-to-json-program "dasel"
62 | "Name of the program to convert TOML to JSON.
63 |
64 | The program must accept input from STDIN and output a JSON to
65 | STDOUT.
66 |
67 | You can customize the arguments that will be passed to the
68 | program by adjusting `pet-toml-to-json-program-arguments'"
69 | :group 'pet
70 | :type '(choice (const "dasel")
71 | (const "tomljson")
72 | (string :tag "Other")))
73 |
74 | (defcustom pet-toml-to-json-program-arguments '("-f" "-" "-r" "toml" "-w" "json")
75 | "Arguments for `pet-toml-to-json-program'."
76 | :group 'pet
77 | :type '(repeat string))
78 |
79 | (defcustom pet-yaml-to-json-program "dasel"
80 | "Name of the program to convert YAML to JSON.
81 |
82 | The program must accept input from STDIN and output a JSON to
83 | STDOUT.
84 |
85 | You can customize the arguments that will be passed to the
86 | program by adjusting `pet-yaml-to-json-program-arguments'"
87 | :group 'pet
88 | :type '(choice (const "dasel")
89 | (const "yq")
90 | (string :tag "Other")))
91 |
92 | (defcustom pet-yaml-to-json-program-arguments '("-f" "-" "-r" "yaml" "-w" "json")
93 | "Arguments for `pet-yaml-to-json-program'."
94 | :group 'pet
95 | :type '(repeat string))
96 |
97 | (defcustom pet-find-file-functions '(pet-find-file-from-project-root
98 | pet-locate-dominating-file
99 | pet-find-file-from-project-root-recursively)
100 | "Order in which `pet-find-file-from-project' should search for a config file.
101 |
102 | Each function should take a file name as its sole argument and
103 | return an absolute path to the file found in the current project
104 | and nil otherwise."
105 | :group 'pet
106 | :type '(repeat (choice (const pet-find-file-from-project-root)
107 | (const pet-locate-dominating-file)
108 | (const pet-find-file-from-project-root-recursively)
109 | function)))
110 |
111 | (defcustom pet-venv-dir-names '(".venv" "venv" "env")
112 | "Directory names to search for when looking for a virtualenv at the project root."
113 | :group 'pet
114 | :type '(repeat string))
115 |
116 | (defcustom pet-fd-command "fd"
117 | "The \"fd\" command in the system."
118 | :type 'string
119 | :group 'pet)
120 |
121 | (defcustom pet-fd-command-args '("-tf" "-cnever" "-H" "-a" "-g")
122 | "The arguments to pass to the \"fd\" command."
123 | :type '(repeat string)
124 | :group 'pet)
125 |
126 | (defcustom pet-search-globally t
127 | "Whether `pet-executable-find' should search outside of the project's virtualenvs.
128 |
129 | If you need to configure this value, it is likely that you'd want a
130 | different value depending the project you are working on. If so, you
131 | can safely set this variable in a `.dir-locals.el' file at the root of
132 | the project directory."
133 | :group 'pet
134 | :type 'boolean
135 | :safe t)
136 |
137 |
138 |
139 | (defun pet--executable-find (command &optional remote)
140 | "Like Emacs 27's `executable-find', ignore REMOTE on Emacs 26.
141 |
142 | See `executable-find' for the meaning of COMMAND and REMOTE."
143 | (if (>= emacs-major-version 27)
144 | (executable-find command remote)
145 | (executable-find command)))
146 |
147 | (defun pet-system-bin-dir ()
148 | "Determine the correct script directory based on `system-type'."
149 | (if (eq (if (file-remote-p default-directory)
150 | (tramp-get-connection-property
151 | (tramp-dissect-file-name default-directory)
152 | "uname"
153 | 'windows-nt)
154 | system-type)
155 | 'windows-nt)
156 | "Scripts" "bin"))
157 |
158 | (defun pet-report-error (err)
159 | "Report ERR to the minibuffer.
160 |
161 | Only reports to the minibuffer if `pet-debug' is non-nil."
162 | (when pet-debug
163 | (minibuffer-message (error-message-string err)))
164 | nil)
165 |
166 | (defun pet-project-root ()
167 | "Return the path of root of the project.
168 |
169 | If `projectile' is available, the function
170 | `projectile-project-root' is used to find the project root.
171 | Otherwise, `project-root' is used."
172 | (or (and (functionp 'projectile-project-root)
173 | (projectile-project-root))
174 | (when-let*((project (project-current)))
175 | (or (and (functionp 'project-root)
176 | (expand-file-name (project-root project)))
177 | (and (functionp 'project-roots)
178 | (when-let*((root (car (project-roots project))))
179 | (expand-file-name root)))))))
180 |
181 | (defun pet-find-file-from-project-root (file)
182 | "Find FILE from the current project's root.
183 |
184 | FILE is a file name or a wildcard.
185 |
186 | Return absolute path to FILE if found in the project root, nil
187 | otherwise."
188 | (when-let*((root (pet-project-root)))
189 | (car (file-expand-wildcards (concat (file-name-as-directory root) file) t))))
190 |
191 | (defun pet-locate-dominating-file (file)
192 | "Find FILE by walking up `default-directory' until the current project's root.
193 |
194 | FILE is a file name or a wildcard.
195 |
196 | Return absolute path to FILE if found, nil otherwise."
197 | (when-let* ((root (pet-project-root))
198 | (dir (locate-dominating-file
199 | default-directory
200 | (lambda (dir)
201 | (car
202 | (file-expand-wildcards
203 | (concat (file-name-as-directory dir) file))))))
204 | (dir (expand-file-name dir)))
205 | (when (string-prefix-p root dir)
206 | (car (file-expand-wildcards (concat (file-name-as-directory dir) file) t)))))
207 |
208 | (defun pet-find-file-from-project-root-recursively (file)
209 | "Find FILE by recursively searching down from the current project's root.
210 |
211 | FILE is a file name or a wildcard.
212 |
213 | Return absolute path to FILE if found, nil otherwise."
214 | (condition-case err
215 | (when-let*((root (pet-project-root)))
216 | (if (executable-find pet-fd-command)
217 | (car (cl-remove-if
218 | #'string-empty-p
219 | (apply #'process-lines `(,pet-fd-command ,@pet-fd-command-args ,file ,root))))
220 | (when-let*((fileset
221 | (cond ((functionp 'projectile-dir-files)
222 | (mapcar (apply-partially #'concat root)
223 | (projectile-dir-files (pet-project-root))))
224 | ((functionp 'project-files)
225 | (project-files (project-current)))
226 | (t (directory-files-recursively
227 | (pet-project-root)
228 | (wildcard-to-regexp file))))))
229 | (seq-find (lambda (f)
230 | (string-match-p
231 | (wildcard-to-regexp file)
232 | (file-name-nondirectory f)))
233 | (sort fileset 'string<)))))
234 | (error (pet-report-error err))))
235 |
236 | (defun pet-find-file-from-project (file)
237 | "Find FILE from the current project.
238 |
239 | Try each function in `pet-find-file-functions' in order and
240 | return the absolute path found by the first function, nil
241 | otherwise."
242 | (seq-some (lambda (fn) (funcall fn file)) pet-find-file-functions))
243 |
244 | (defun pet-parse-json (str)
245 | "Parse JSON STR to an alist. Arrays are converted to lists."
246 | (if (functionp 'json-parse-string)
247 | (json-parse-string str :object-type 'alist :array-type 'list)
248 | (let ((json-array-type 'list))
249 | (json-read-from-string str))))
250 |
251 | (defun pet-parse-config-file (file-path)
252 | "Parse a configuration file at FILE-PATH into JSON alist."
253 | (condition-case err
254 | (let* ((ext (downcase (or (file-name-extension file-path) "")))
255 | (auto-mode-alist-matcher (lambda (entry)
256 | (pcase-let ((`(,pat . ,mode) entry))
257 | (when (string-match-p pat file-path)
258 | mode))))
259 | (mode (seq-some auto-mode-alist-matcher auto-mode-alist))
260 | (json-p (or (equal ext "json")
261 | (eq 'json-mode mode)
262 | (eq 'json-ts-mode mode)
263 | (eq 'jsonian-mode mode)))
264 | (toml-p (or (equal ext "toml")
265 | (eq 'conf-toml-mode mode)
266 | (eq 'toml-ts-mode mode)))
267 | (yaml-p (or (string-match-p "ya?ml" ext)
268 | (eq 'yaml-mode mode)
269 | (eq 'yaml-ts-mode mode))))
270 |
271 | (let ((output (get-buffer-create " *pet parser output*")))
272 | (unwind-protect
273 | (let ((exit-code
274 | (when (or toml-p yaml-p)
275 | (condition-case err
276 | (apply #'process-file
277 | (cond (toml-p pet-toml-to-json-program)
278 | (yaml-p pet-yaml-to-json-program))
279 | file-path
280 | output
281 | nil
282 | (cond (toml-p pet-toml-to-json-program-arguments)
283 | (yaml-p pet-yaml-to-json-program-arguments)))
284 | (error (error-message-string err))))))
285 |
286 | (cond ((and (integerp exit-code) (zerop exit-code))
287 | (with-current-buffer output
288 | (pet-parse-json (buffer-string))))
289 | (json-p
290 | (with-temp-buffer
291 | (insert-file-contents file-path)
292 | (pet-parse-json (buffer-string))))
293 | (t
294 | (error (if (stringp exit-code)
295 | exit-code
296 | (with-current-buffer output
297 | (buffer-string)))))))
298 | (kill-buffer output))))
299 |
300 | (error (pet-report-error err))))
301 |
302 | (defvar pet-watched-config-files nil)
303 |
304 | (defun pet-make-config-file-change-callback (cache-var parser)
305 | "Make callback for `file-notify-add-watch'.
306 |
307 | Return a callback with CACHE-VAR and PARSER captured in its environment.
308 | CACHE-VAR is the symbol to the cache variable to update. PARSER is the
309 | symbol to the parser to parse the file.
310 |
311 | When invoked, the callback returned will parse the file with
312 | PARSER and cache the result in CACHE-VAR if the file was changed.
313 | If the file was deleted or renamed, remove the file's watcher,
314 | and delete the file entry from CACHE-VAR and
315 | `pet-watched-config-files'."
316 | (lambda (event)
317 | (pcase-let ((`(,_ ,action ,file . ,_) event))
318 | (pcase action
319 | ((or 'deleted 'renamed)
320 | (file-notify-rm-watch (assoc-default file pet-watched-config-files))
321 | (setf (alist-get file (symbol-value cache-var) nil t 'equal) nil)
322 | (setf (alist-get file pet-watched-config-files nil t 'equal) nil))
323 | ('changed
324 | (setf (alist-get file (symbol-value cache-var) nil nil 'equal)
325 | (funcall parser file)))))))
326 |
327 | (defun pet-watch-config-file (config-file cache-var parser)
328 | "Keep cache fresh by watching for change in the config file.
329 |
330 | CONFIG-FILE is the path to the configuration file to watch for
331 | changes. CACHE-VAR is the symbol to the variable where the
332 | parsed configuration file content is stored. PARSER is the
333 | symbol to a function that takes a file path and parses its
334 | content into an alist."
335 | (unless (assoc-default config-file pet-watched-config-files)
336 | (push (cons config-file
337 | (file-notify-add-watch
338 | config-file
339 | '(change)
340 | (pet-make-config-file-change-callback cache-var parser)))
341 | pet-watched-config-files)))
342 |
343 | (cl-defmacro pet-def-config-accessor (name &key file-name parser)
344 | "Create a function for reading the content of a config file.
345 |
346 | NAME will be used to create a memorized funcion named `pet-NAME'
347 | to return the content of the configuration file FILE-NAME.
348 | FILE-NAME is the name or glob pattern of the configuration file
349 | that will be searched in the project. The content of the file
350 | will be parsed by PARSER and then cached in a variable called
351 | `pet-NAME-cache'.
352 |
353 | Changes to the file will automatically update the cached content
354 | See `pet-watch-config-file' for details."
355 | (let* ((accessor-name (concat "pet-" (symbol-name name)))
356 | (path-accessor-name (concat accessor-name "-path"))
357 | (cache-var (intern (concat accessor-name "-cache")))
358 | (accessor-docstring
359 | (format "Accessor for `%s' in the current Python project.
360 |
361 | If the file is found in the current Python project, cache its
362 | content in `%s' and return it.
363 |
364 | If the file content change, it is parsed again and the cache is
365 | refreshed automatically. If it is renamed or deleted, the cache
366 | entry is deleted.
367 | "
368 | name (symbol-name cache-var)))
369 | (path-accessor-docstring (format "Path of `%s' in the current Python project.
370 |
371 | Return nil if the file is not found." file-name))
372 | (cache-var-docstring
373 | (format "Cache for `%s'.
374 |
375 | This variable is an alist where the key is the absolute path to a
376 | `%s' in some Python project and the value is the parsed content.
377 | " name name)))
378 | `(progn
379 | (defvar ,cache-var nil ,cache-var-docstring)
380 |
381 | (defun ,(intern path-accessor-name) ()
382 | ,path-accessor-docstring
383 | (pet-find-file-from-project ,file-name))
384 |
385 | (defun ,(intern accessor-name) ()
386 | ,accessor-docstring
387 | (when-let*((config-file (,(intern path-accessor-name))))
388 | (if-let* ((cached-content (assoc-default config-file ,cache-var)))
389 | cached-content
390 | (pet-watch-config-file config-file ',cache-var #',parser)
391 | (when-let*((content (funcall #',parser config-file)))
392 | (push (cons config-file content) ,cache-var)
393 | content)))))))
394 |
395 | (pet-def-config-accessor pre-commit-config
396 | :file-name ".pre-commit-config.yaml"
397 | :parser pet-parse-config-file)
398 |
399 | (pet-def-config-accessor pyproject
400 | :file-name "pyproject.toml"
401 | :parser pet-parse-config-file)
402 |
403 | (pet-def-config-accessor python-version
404 | :file-name ".python-version"
405 | :parser f-read-text)
406 |
407 | (pet-def-config-accessor pipfile
408 | :file-name "Pipfile"
409 | :parser pet-parse-config-file)
410 |
411 | ;; So `pet-parse-config-file' knows Pipfile can be parsed with `pet-toml-to-json-program'.
412 | (add-to-list 'auto-mode-alist '("/Pipfile\\'" . conf-toml-mode))
413 |
414 | (pet-def-config-accessor environment
415 | :file-name "environment*.y*ml"
416 | :parser pet-parse-config-file)
417 |
418 | (defun pet-use-pre-commit-p ()
419 | "Whether the current project is using `pre-commit'.
420 |
421 | Returns the path to the `pre-commit' executable."
422 | (and (pet-pre-commit-config)
423 | (or (pet--executable-find "pre-commit" t)
424 | (and (when-let* ((venv (pet-virtualenv-root))
425 | (exec-path (list (concat (file-name-as-directory venv) (pet-system-bin-dir))))
426 | (process-environment (copy-sequence process-environment)))
427 | (setenv "PATH" (string-join exec-path path-separator))
428 | (pet--executable-find "pre-commit" t))))))
429 |
430 | (defun pet-use-conda-p ()
431 | "Whether the current project is using `conda'.
432 |
433 | Returns the path to the `conda' executable variant found."
434 | (and (pet-environment)
435 | (or (pet--executable-find "conda" t)
436 | (pet--executable-find "mamba" t)
437 | (pet--executable-find "micromamba" t))))
438 |
439 | (defun pet-use-poetry-p ()
440 | "Whether the current project is using `poetry'.
441 |
442 | Returns the path to the `poetry' executable."
443 | (and (string-match-p
444 | "poetry"
445 | (or (let-alist (pet-pyproject)
446 | .build-system.build-backend)
447 | ""))
448 | (pet--executable-find "poetry" t)))
449 |
450 | (defun pet-use-pyenv-p ()
451 | "Whether the current project is using `pyenv'.
452 |
453 | Returns the path to the `pyenv' executable."
454 | (and (pet-python-version)
455 | (pet--executable-find "pyenv" t)))
456 |
457 | (defun pet-use-pipenv-p ()
458 | "Whether the current project is using `pipenv'.
459 |
460 | Returns the path to the `pipenv' executable."
461 | (and (pet-pipfile)
462 | (pet--executable-find "pipenv" t)))
463 |
464 | (defun pet-pre-commit-config-has-hook-p (id)
465 | "Determine if the `pre-commit' configuration has a hook.
466 |
467 | Return non-nil if the `pre-commit' configuration for the current
468 | project has hook ID set up."
469 | (member id (cl-loop for repo in (let-alist (pet-pre-commit-config) .repos)
470 | append (cl-loop for hook in (let-alist repo .hooks)
471 | collect (let-alist hook .id)))))
472 |
473 | (defun pet-parse-pre-commit-db (db-file)
474 | "Parse `pre-commit' database.
475 |
476 | Read the pre-commit SQLite database located at DB-FILE into an alist."
477 | (if (and (functionp 'sqlite-available-p)
478 | (sqlite-available-p))
479 | (let ((db (sqlite-open db-file)))
480 | (unwind-protect
481 | (let* ((result-set (sqlite-select db "select * from repos" nil 'set))
482 | result
483 | row)
484 | (while (setq row (sqlite-next result-set))
485 | (setq result (cons (seq-mapn (lambda (a b) (cons (intern a) b))
486 | (sqlite-columns result-set)
487 | row)
488 | result)))
489 | (sqlite-finalize result-set)
490 | result)
491 | (sqlite-close db)))
492 |
493 | (condition-case err
494 | (with-temp-buffer
495 | (process-file "sqlite3" nil t nil "-json" db-file "select * from repos")
496 | (pet-parse-json (buffer-string)))
497 | (error (pet-report-error err)))))
498 |
499 | (defvar pet-pre-commit-database-cache nil)
500 |
501 | (defun pet-pre-commit-virtualenv-path (hook-id)
502 | "Find the virtualenv location from the `pre-commit' database.
503 |
504 | If the `pre-commit' hook HOOK-ID is found in the current Python
505 | project's `.pre-commit-config.yaml' file, the hook ID and its
506 | additional dependencies are used to construct a key for looking
507 | up a virtualenv for the hook from the pre-commit database.
508 |
509 | In order to find the hook virtualenv, `pre-commit' and the hooks
510 | must both be installed into the current project first."
511 | (when-let* ((db-file
512 | (concat
513 | (expand-file-name
514 | (file-name-as-directory
515 | (or (getenv "PRE_COMMIT_HOME")
516 | (getenv "XDG_CACHE_HOME")
517 | "~/.cache/")))
518 | (unless (getenv "PRE_COMMIT_HOME") "pre-commit/")
519 | "db.db"))
520 |
521 | (db
522 | (or (assoc-default db-file pet-pre-commit-database-cache)
523 | (when (file-exists-p db-file)
524 | (pet-watch-config-file db-file 'pet-pre-commit-database-cache 'pet-parse-pre-commit-db)
525 | (when-let*((content (pet-parse-pre-commit-db db-file)))
526 | (push (cons db-file content) pet-pre-commit-database-cache)
527 | content))))
528 |
529 | (repo-config
530 | (seq-find
531 | (lambda (repo)
532 | (seq-find
533 | (lambda (hook)
534 | (equal (let-alist hook .id) hook-id))
535 | (let-alist repo .hooks)))
536 | (let-alist (pet-pre-commit-config) .repos)))
537 |
538 | (repo-url
539 | (let-alist repo-config .repo))
540 |
541 | (repo-dir
542 | (let* ((additional-deps
543 | (let-alist repo-config
544 | (let-alist (seq-find (lambda (hook) (let-alist hook (equal .id hook-id))) .hooks)
545 | .additional_dependencies)))
546 | (unsorted-repo-url (concat repo-url ":" (string-join additional-deps ",")))
547 | (sorted-repo-url (concat repo-url ":" (string-join (sort (copy-sequence additional-deps) 'string<) ","))))
548 | (let-alist (seq-find
549 | (lambda (row)
550 | (let-alist row
551 | (and (if additional-deps
552 | (or (equal .repo unsorted-repo-url)
553 | (equal .repo sorted-repo-url))
554 | (equal .repo repo-url))
555 | (equal .ref (let-alist repo-config .rev)))))
556 | db)
557 | .path))))
558 |
559 | (car
560 | (last
561 | (file-expand-wildcards
562 | (concat (file-name-as-directory repo-dir) "py_env-*")
563 | t)))))
564 |
565 |
566 |
567 | ;;;###autoload
568 | (defun pet-executable-find (executable &optional search-globally)
569 | "Find the correct EXECUTABLE for the current Python project.
570 |
571 | Search for EXECUTABLE first in the `pre-commit' virtualenv, then
572 | whatever environment is found by `pet-virtualenv-root'.
573 |
574 | If SEARCH-GLOBALLY or `pet-search-globally' is non-nil, the
575 | search continues to look in `pyenv', then finally from
576 | `exec-path'."
577 |
578 | (catch 'done
579 | (cond ((and (pet-use-pre-commit-p)
580 | (not (string-prefix-p "python" executable))
581 | (pet-pre-commit-config-has-hook-p executable))
582 | (condition-case err
583 | (let* ((venv (or (pet-pre-commit-virtualenv-path executable)
584 | (user-error "`pre-commit' is configured but the hook `%s' does not appear to be installed" executable)))
585 | (bin-dir (concat (file-name-as-directory venv) (pet-system-bin-dir)))
586 | (bin-path (concat bin-dir "/" executable)))
587 | (if (file-exists-p bin-path)
588 | bin-path
589 | (user-error "`pre-commit' is configured but `%s' is not found in %s" executable bin-dir)))
590 | (error (pet-report-error err))))
591 | ((when-let* ((venv (pet-virtualenv-root))
592 | (path (list (concat (file-name-as-directory venv)
593 | (unless (and (string-prefix-p "python" executable)
594 | (pet-use-conda-p)
595 | (eq system-type 'windows-nt))
596 | (pet-system-bin-dir)))))
597 | (exec-path path)
598 | (tramp-remote-path path)
599 | (process-environment (copy-sequence process-environment)))
600 | (setenv "PATH" (string-join exec-path path-separator))
601 | (pet--executable-find executable t)))
602 | ((if (or search-globally pet-search-globally)
603 | nil
604 | (throw 'done nil)))
605 | ((pet--executable-find "pyenv" t)
606 | (condition-case err
607 | (car (process-lines "pyenv" "which" executable))
608 | (error (pet-report-error err))))
609 | (t (or (pet--executable-find executable t)
610 | (pet--executable-find (concat executable "3") t))))))
611 |
612 | (defvar pet-project-virtualenv-cache nil)
613 |
614 | ;;;###autoload
615 | (defun pet-virtualenv-root ()
616 | "Find the path to the virtualenv for the current Python project.
617 |
618 | Selects a virtualenv in the follow order:
619 |
620 | 1. The value of the environment variable `VIRTUAL_ENV' if defined.
621 | 2. If the current project is using any `conda' variant, return the absolute path
622 | to the virtualenv directory for the current project.
623 | 3. Ditta for `poetry'.
624 | 4. Ditto for `pipenv'.
625 | 5. A directory in `pet-venv-dir-names' in the project root if found.
626 | 6. If the current project is using `pyenv', return the path to the virtualenv
627 | directory by looking up the prefix from `.python-version'."
628 | (let ((root (pet-project-root)))
629 | (or (assoc-default root pet-project-virtualenv-cache)
630 | (when-let*((ev (getenv "VIRTUAL_ENV")))
631 | (expand-file-name ev))
632 | (let ((venv-path
633 | (cond ((when-let* ((program (pet-use-conda-p))
634 | (default-directory (file-name-directory (pet-environment-path))))
635 | (condition-case err
636 | (with-temp-buffer
637 | (let ((exit-code (process-file program nil t nil "info" "--json"))
638 | (output (string-trim (buffer-string))))
639 | (if (zerop exit-code)
640 | (let* ((json-output (pet-parse-json output))
641 | (env-dirs (or (let-alist json-output .envs_dirs)
642 | (let-alist json-output .envs\ directories)))
643 | (env-name (alist-get 'name (pet-environment)))
644 | (env (seq-find 'file-directory-p
645 | (seq-map (lambda (dir)
646 | (file-name-as-directory
647 | (concat
648 | (file-name-as-directory dir)
649 | env-name)))
650 | env-dirs))))
651 | (or env
652 | (user-error "Please create the environment with `$ %s create --file %s' first" program (pet-environment-path))))
653 | (user-error (buffer-string)))))
654 | (error (pet-report-error err)))))
655 | ((when-let*((program (pet-use-poetry-p))
656 | (default-directory (file-name-directory (pet-pyproject-path))))
657 | (condition-case err
658 | (with-temp-buffer
659 | (let ((exit-code (process-file program nil t nil "env" "info" "--no-ansi" "--path"))
660 | (output (string-trim (buffer-string))))
661 | (if (zerop exit-code)
662 | output
663 | (user-error (buffer-string)))))
664 | (error (pet-report-error err)))))
665 | ((when-let*((program (pet-use-pipenv-p))
666 | (default-directory (file-name-directory (pet-pipfile-path))))
667 | (condition-case err
668 | (with-temp-buffer
669 | (let ((exit-code (process-file program nil '(t nil) nil "--quiet" "--venv"))
670 | (output (string-trim (buffer-string))))
671 | (if (zerop exit-code)
672 | output
673 | (user-error (buffer-string)))))
674 | (error (pet-report-error err)))))
675 | ((when-let*((dir (cl-loop for name in pet-venv-dir-names
676 | with dir = nil
677 | if (setq dir (locate-dominating-file default-directory name))
678 | return (file-name-as-directory (concat dir name)))))
679 | (expand-file-name dir)))
680 | ((when-let*((program (pet-use-pyenv-p))
681 | (default-directory (file-name-directory (pet-python-version-path))))
682 | (condition-case err
683 | (with-temp-buffer
684 | (let ((exit-code (process-file program nil t nil "prefix"))
685 | (output (string-trim (buffer-string))))
686 | (if (zerop exit-code)
687 | (file-truename output)
688 | (user-error (buffer-string)))))
689 | (error (pet-report-error err))))))))
690 | ;; root maybe nil when not in a project, this avoids caching a nil
691 | (when root
692 | (setf (alist-get root pet-project-virtualenv-cache nil nil 'equal) venv-path))
693 | venv-path))))
694 |
695 |
696 |
697 | (defvar flycheck-mode)
698 | (defvar flycheck-python-mypy-config)
699 | (defvar flycheck-pylintrc)
700 | (defvar flycheck-python-flake8-executable)
701 | (defvar flycheck-python-pylint-executable)
702 | (defvar flycheck-python-mypy-executable)
703 | (defvar flycheck-python-pyright-executable)
704 | (defvar flycheck-python-pycompile-executable)
705 | (defvar flycheck-python-ruff-executable)
706 |
707 | (defun pet-flycheck-python-pylint-find-pylintrc ()
708 | "Polyfill `flycheck-pylintrc'.
709 |
710 | Find the correct `pylint' configuration file according to the
711 | algorithm described at
712 | `https://pylint.pycqa.org/en/latest/user_guide/usage/run.html'."
713 | (let* ((pylintrc '("pylintrc" ".pylintrc" "pyproject.toml" "setup.cfg"))
714 | (found (cond ((cl-loop for f in pylintrc
715 | with path = nil
716 | do (setq path (concat default-directory f))
717 | if (file-exists-p path)
718 | return (expand-file-name path)))
719 | ((and (buffer-file-name)
720 | (file-exists-p (concat (file-name-directory (buffer-file-name)) "__init__.py")))
721 | (when-let*((path (cl-loop for f in pylintrc
722 | with dir = nil
723 | do (setq dir (locate-dominating-file default-directory f))
724 | if dir
725 | return (concat dir f))))
726 | (expand-file-name path))))))
727 | (if found
728 | found
729 | (cond ((when-let* ((ev (getenv "PYLINTRC"))
730 | (path (expand-file-name ev)))
731 | (and (file-exists-p path) path)))
732 | ((let* ((ev (getenv "XDG_CONFIG_HOME"))
733 | (config-dir
734 | (or (and ev (file-name-as-directory ev))
735 | "~/.config/"))
736 | (xdg-file-path (expand-file-name (concat config-dir "pylintrc"))))
737 | (and (file-exists-p xdg-file-path) xdg-file-path)))
738 | ((let ((home-dir-pylintrc (expand-file-name "~/.pylintrc")))
739 | (and (file-exists-p home-dir-pylintrc) home-dir-pylintrc)))
740 | (t "/etc/pylintrc")))))
741 |
742 | (defun pet-flycheck-toggle-local-vars ()
743 | "Toggle buffer local variables for `flycheck' Python checkers.
744 |
745 | When `flycheck-mode' is non-nil, set up all supported Python
746 | checker executable variables buffer-locally. Reset them to
747 | default otherwise."
748 | (if (bound-and-true-p flycheck-mode)
749 | (progn
750 | (when (derived-mode-p (if (functionp 'python-base-mode) 'python-base-mode 'python-mode))
751 | (setq-local flycheck-python-mypy-config `("mypy.ini" ".mypy.ini" "pyproject.toml" "setup.cfg"
752 | ,(expand-file-name
753 | (concat
754 | (or (when-let*((xdg-config-home (getenv "XDG_CONFIG_HOME")))
755 | (file-name-as-directory xdg-config-home))
756 | "~/.config/")
757 | "mypy/config"))
758 | ,(expand-file-name "~/.mypy.ini")))
759 | (setq-local flycheck-pylintrc (pet-flycheck-python-pylint-find-pylintrc))
760 | (setq-local flycheck-python-flake8-executable (pet-executable-find "flake8"))
761 | (setq-local flycheck-python-pylint-executable (pet-executable-find "pylint"))
762 | (setq-local flycheck-python-mypy-executable (pet-executable-find "mypy"))
763 | (setq-local flycheck-python-pyright-executable (pet-executable-find "pyright"))
764 | (setq-local flycheck-python-ruff-executable (pet-executable-find "ruff"))
765 | (let ((python-path (pet-executable-find "python")))
766 | (setq-local flycheck-python-mypy-python-executable python-path)
767 | (setq-local flycheck-python-pycompile-executable python-path))))
768 | (kill-local-variable 'flycheck-python-mypy-config)
769 | (kill-local-variable 'flycheck-pylintrc)
770 | (kill-local-variable 'flycheck-python-flake8-executable)
771 | (kill-local-variable 'flycheck-python-pylint-executable)
772 | (kill-local-variable 'flycheck-python-mypy-executable)
773 | (kill-local-variable 'flycheck-python-mypy-python-executable)
774 | (kill-local-variable 'flycheck-python-pyright-executable)
775 | (kill-local-variable 'flycheck-python-pycompile-executable)
776 | (kill-local-variable 'flycheck-python-ruff-executable)))
777 |
778 | (defun pet-flycheck-python-find-project-root-advice (_)
779 | "Delegate `flycheck-python-find-project-root' to `pet-virtualenv-root'."
780 | (pet-virtualenv-root))
781 |
782 | ;;;###autoload
783 | (defun pet-flycheck-setup ()
784 | "Set up all `flycheck' Python checker configuration."
785 | (advice-add 'flycheck-python-find-project-root :override #'pet-flycheck-python-find-project-root-advice)
786 | (add-hook 'flycheck-mode-hook #'pet-flycheck-toggle-local-vars))
787 |
788 | ;;;###autoload
789 | (defun pet-flycheck-teardown ()
790 | "Reset all `flycheck' Python checker configuration to default."
791 | (advice-remove 'flycheck-python-find-project-root #'pet-flycheck-python-find-project-root-advice)
792 | (remove-hook 'flycheck-mode-hook #'pet-flycheck-toggle-local-vars)
793 | (kill-local-variable 'flycheck-python-mypy-config)
794 | (kill-local-variable 'flycheck-pylintrc)
795 | (kill-local-variable 'flycheck-python-flake8-executable)
796 | (kill-local-variable 'flycheck-python-pylint-executable)
797 | (kill-local-variable 'flycheck-python-mypy-executable)
798 | (kill-local-variable 'flycheck-python-mypy-python-executable)
799 | (kill-local-variable 'flycheck-python-pyright-executable)
800 | (kill-local-variable 'flycheck-python-pycompile-executable)
801 | (kill-local-variable 'flycheck-python-ruff-executable))
802 |
803 |
804 |
805 | (defvar eglot-workspace-configuration)
806 | (declare-function jsonrpc--process "ext:jsonrpc")
807 | (declare-function eglot--executable-find "ext:eglot")
808 | (declare-function eglot--workspace-configuration-plist "ext:eglot")
809 | (declare-function eglot--guess-contact "ext:eglot")
810 |
811 | (defun pet-eglot--executable-find-advice (fn &rest args)
812 | "Look up Python language servers using `pet-executable-find'.
813 |
814 | FN is `eglot--executable-find', ARGS is the arguments to
815 | `eglot--executable-find'."
816 | (pcase-let ((`(,command . ,_) args))
817 | (if (member command '("pylsp" "pyls" "pyright-langserver" "jedi-language-server" "ruff-lsp"))
818 | (pet-executable-find command)
819 | (apply fn args))))
820 |
821 | (defun pet-lookup-eglot-server-initialization-options (command)
822 | "Return LSP initializationOptions for Eglot.
823 |
824 | COMMAND is the name of the Python language server command."
825 | (cond
826 | ((not
827 | (stringp command))
828 | 'nil)
829 | ((string-match "pylsp" command)
830 | (let nil
831 | `(:pylsp
832 | (:plugins
833 | (:jedi
834 | (:environment ,(pet-virtualenv-root))
835 | :ruff
836 | (:executable ,(pet-executable-find "ruff"))
837 | :pylsp_mypy
838 | (:overrides
839 | ["--python-executable"
840 | (\,
841 | (pet-executable-find "python"))
842 | t])
843 | :flake8
844 | (:executable ,(pet-executable-find "flake8"))
845 | :pylint
846 | (:executable ,(pet-executable-find "pylint")))))))
847 | ((string-match "pyls" command)
848 | (let nil
849 | `(:pyls
850 | (:plugins
851 | (:jedi
852 | (:environment ,(pet-virtualenv-root))
853 | :pylint
854 | (:executable ,(pet-executable-find "pylint")))))))
855 | ((string-match "pyright-langserver" command)
856 | (let nil
857 | `(:python
858 | (:pythonPath ,(pet-executable-find "python")
859 | :venvPath ,(pet-virtualenv-root)))))
860 | ((string-match "jedi-language-server" command)
861 | (let nil
862 | `(:jedi
863 | (:executable
864 | (:command ,(pet-executable-find "jedi-language-server"))
865 | :workspace
866 | (:environmentPath ,(pet-executable-find "python"))))))
867 | ((string-match "ruff-lsp" command)
868 | (let nil
869 | `(:settings
870 | (:interpreter ,(pet-executable-find "python")
871 | :path ,(pet-executable-find "ruff")))))
872 | (t 'nil)))
873 |
874 | (defalias 'pet--proper-list-p 'proper-list-p)
875 | (eval-when-compile
876 | (when (and (not (functionp 'proper-list-p))
877 | (functionp 'format-proper-list-p))
878 | (defun pet--proper-list-p (l)
879 | (and (format-proper-list-p l)
880 | (length l)))))
881 |
882 | (defun pet--plistp (object)
883 | "Non-nil if and only if OBJECT is a valid plist."
884 | (let ((len (pet--proper-list-p object)))
885 | (and len
886 | (zerop (% len 2))
887 | (seq-every-p
888 | (lambda (kvp)
889 | (keywordp (car kvp)))
890 | (seq-split object 2)))))
891 |
892 | (defun pet-merge-eglot-initialization-options (a b)
893 | "Deep merge plists A and B."
894 | (map-merge-with 'plist
895 | (lambda (c d)
896 | (cond ((and (pet--plistp c) (pet--plistp d))
897 | (pet-merge-eglot-initialization-options c d))
898 | ((and (vectorp c) (vectorp d))
899 | (vconcat (seq-union c d)))
900 | (t d)))
901 | (copy-tree a t)
902 | (copy-tree b t)))
903 |
904 | (defun pet-eglot--workspace-configuration-plist-advice (fn &rest args)
905 | "Enrich `eglot-workspace-configuration' with paths found by `pet'.
906 |
907 | FN is `eglot--workspace-configuration-plist', ARGS is the
908 | arguments to `eglot--workspace-configuration-plist'."
909 | (let* ((path (cadr args))
910 | (canonical-path (if (and path (file-directory-p path))
911 | (file-name-as-directory path)
912 | path))
913 | (server (car args))
914 | (command (process-command (jsonrpc--process server)))
915 | (program (and (listp command) (car command)))
916 | (pet-config (pet-lookup-eglot-server-initialization-options program))
917 | (user-config (apply fn server (and canonical-path (cons canonical-path (cddr args))))))
918 | (pet-merge-eglot-initialization-options user-config pet-config)))
919 |
920 | (defun pet-eglot--guess-contact-advice (fn &rest args)
921 | "Enrich `eglot--guess-contact' with paths found by `pet'.
922 |
923 | FN is `eglot--guess-contact', ARGS is the arguments to
924 | `eglot--guess-contact'."
925 | (let* ((result (apply fn args))
926 | (contact (nth 3 result))
927 | (probe (seq-position contact :initializationOptions))
928 | (program-with-args (seq-subseq contact 0 (or probe (length contact))))
929 | (program (car program-with-args))
930 | (init-opts (plist-get (seq-subseq contact (or probe 0)) :initializationOptions)))
931 | (if init-opts
932 | (append (seq-subseq result 0 3)
933 | (list
934 | (append
935 | program-with-args
936 | (list
937 | :initializationOptions
938 | (pet-merge-eglot-initialization-options
939 | init-opts
940 | (pet-lookup-eglot-server-initialization-options
941 | program)))))
942 | (seq-subseq result 4))
943 | result)))
944 |
945 | (defun pet-eglot-setup ()
946 | "Set up Eglot to use server executables and virtualenvs found by PET."
947 | (advice-add 'eglot--executable-find :around #'pet-eglot--executable-find-advice)
948 | (advice-add 'eglot--workspace-configuration-plist :around #'pet-eglot--workspace-configuration-plist-advice)
949 | (advice-add 'eglot--guess-contact :around #'pet-eglot--guess-contact-advice))
950 |
951 | (defun pet-eglot-teardown ()
952 | "Tear down PET advices to Eglot."
953 | (advice-remove 'eglot--executable-find #'pet-eglot--executable-find-advice)
954 | (advice-remove 'eglot--workspace-configuration-plist #'pet-eglot--workspace-configuration-plist-advice)
955 | (advice-remove 'eglot--guess-contact #'pet-eglot--guess-contact-advice))
956 |
957 |
958 | (defvar dape-command)
959 | (defvar dape-cwd-function)
960 |
961 | (defun pet-dape-setup ()
962 | "Set up the buffer local variables for `dape'."
963 | (if-let* ((main (pet-find-file-from-project-root-recursively "__main__.py"))
964 | (module (let* ((dir (file-name-directory main))
965 | (dir-file-name (directory-file-name dir))
966 | (module))
967 | (while (file-exists-p (concat dir "__init__.py"))
968 | (push (file-name-nondirectory dir-file-name) module)
969 | (setq dir (file-name-directory dir-file-name))
970 | (setq dir-file-name (directory-file-name dir)))
971 | (string-join module "."))))
972 | (setq-local dape-command `(debugpy-module command ,(pet-executable-find "python") :module ,module))
973 | (setq-local dape-command `(debugpy command ,(pet-executable-find "python"))))
974 | (setq-local dape-cwd-function #'pet-project-root))
975 |
976 | (defun pet-dape-teardown ()
977 | "Tear down the buffer local variables for `dape'."
978 | (kill-local-variable 'dape-command)
979 | (kill-local-variable 'dape-cwd-function))
980 |
981 |
982 |
983 | (defvar lsp-jedi-executable-command)
984 | (defvar lsp-pyls-plugins-jedi-environment)
985 | (defvar lsp-pylsp-plugins-jedi-environment)
986 | (defvar lsp-pyright-python-executable-cmd)
987 | (defvar lsp-pyright-venv-path)
988 | (defvar lsp-ruff-server-command)
989 | (defvar lsp-ruff-python-path)
990 | (defvar dap-python-executable)
991 | (defvar dap-variables-project-root-function)
992 | (defvar python-pytest-executable)
993 | (defvar python-black-command)
994 | (defvar python-isort-command)
995 | (defvar ruff-format-command)
996 | (defvar blacken-executable)
997 | (defvar yapfify-executable)
998 | (defvar py-autopep8-command)
999 |
1000 | (defun pet-buffer-local-vars-setup ()
1001 | "Set up the buffer local variables for Python tools.
1002 |
1003 | Assign all supported Python tooling executable variables to
1004 | buffer local values."
1005 | (setq-local python-shell-interpreter (pet-executable-find "python"))
1006 | (setq-local python-shell-virtualenv-root (pet-virtualenv-root))
1007 |
1008 | (pet-flycheck-setup)
1009 |
1010 | (setq-local lsp-jedi-executable-command
1011 | (pet-executable-find "jedi-language-server"))
1012 | (setq-local lsp-pyls-plugins-jedi-environment python-shell-virtualenv-root)
1013 | (setq-local lsp-pylsp-plugins-jedi-environment python-shell-virtualenv-root)
1014 | (setq-local lsp-pyright-venv-path python-shell-virtualenv-root)
1015 | (setq-local lsp-pyright-python-executable-cmd python-shell-interpreter)
1016 | (setq-local lsp-ruff-server-command (list (pet-executable-find "ruff") "server"))
1017 | (setq-local lsp-ruff-python-path python-shell-interpreter)
1018 | (setq-local dap-python-executable python-shell-interpreter)
1019 | (setq-local dap-variables-project-root-function #'pet-project-root)
1020 | (setq-local python-pytest-executable (pet-executable-find "pytest"))
1021 | (setq-local python-black-command (pet-executable-find "black"))
1022 | (setq-local python-isort-command (pet-executable-find "isort"))
1023 | (setq-local ruff-format-command (pet-executable-find "ruff"))
1024 | (setq-local blacken-executable python-black-command)
1025 | (setq-local yapfify-executable (pet-executable-find "yapf"))
1026 | (setq-local py-autopep8-command (pet-executable-find "autopep8"))
1027 |
1028 | (pet-eglot-setup)
1029 | (pet-dape-setup))
1030 |
1031 | (defun pet-buffer-local-vars-teardown ()
1032 | "Reset all supported buffer local variable values to default."
1033 |
1034 | (kill-local-variable 'python-shell-interpreter)
1035 | (kill-local-variable 'python-shell-virtualenv-root)
1036 |
1037 | (pet-flycheck-teardown)
1038 |
1039 | (kill-local-variable 'lsp-jedi-executable-command)
1040 | (kill-local-variable 'lsp-pyls-plugins-jedi-environment)
1041 | (kill-local-variable 'lsp-pylsp-plugins-jedi-environment)
1042 | (kill-local-variable 'lsp-pyright-venv-path)
1043 | (kill-local-variable 'lsp-pyright-python-executable-cmd)
1044 | (kill-local-variable 'lsp-ruff-python-path)
1045 | (kill-local-variable 'lsp-ruff-server-command)
1046 | (kill-local-variable 'dap-python-executable)
1047 | (kill-local-variable 'dap-variables-project-root-function)
1048 | (kill-local-variable 'python-pytest-executable)
1049 | (kill-local-variable 'python-black-command)
1050 | (kill-local-variable 'python-isort-command)
1051 | (kill-local-variable 'ruff-format-command)
1052 | (kill-local-variable 'blacken-executable)
1053 | (kill-local-variable 'yapfify-executable)
1054 | (kill-local-variable 'py-autopep8-command)
1055 |
1056 | (pet-eglot-teardown)
1057 | (pet-dape-teardown))
1058 |
1059 | (defun pet-verify-setup ()
1060 | "Verify the values of buffer local variables visually.
1061 |
1062 | Print all of the buffer local variable values `pet-mode'
1063 | has assigned to."
1064 | (interactive)
1065 |
1066 | (unless (derived-mode-p 'python-base-mode 'python-mode)
1067 | (user-error "You are not in python-mode!"))
1068 |
1069 | (let ((kvp (mapcar (lambda (sym)
1070 | (cons sym
1071 | (if (boundp sym)
1072 | (let ((val (symbol-value sym)))
1073 | (if (consp val)
1074 | (apply #'string-join
1075 | (mapcar (apply-partially #'abbreviate-file-name)
1076 | (mapcar (apply-partially #'format "%s") val))
1077 | (list ", "))
1078 | (abbreviate-file-name (format "%s" val))))
1079 | 'unbound)))
1080 | '(python-shell-interpreter
1081 | python-shell-virtualenv-root
1082 | flycheck-python-flake8-executable
1083 | flycheck-pylintrc
1084 | flycheck-python-pylint-executable
1085 | flycheck-python-mypy-executable
1086 | flycheck-python-mypy-config
1087 | flycheck-python-mypy-python-executable
1088 | flycheck-python-pyright-executable
1089 | flycheck-python-pycompile-executable
1090 | flycheck-python-ruff-executable
1091 | lsp-jedi-executable-command
1092 | lsp-pyls-plugins-jedi-environment
1093 | lsp-pylsp-plugins-jedi-environment
1094 | lsp-pyright-python-executable-cmd
1095 | lsp-pyright-venv-path
1096 | lsp-ruff-server-command
1097 | lsp-ruff-python-path
1098 | dap-python-executable
1099 | dap-variables-project-root-function
1100 | dape-command
1101 | dape-cwd-function
1102 | python-pytest-executable
1103 | python-black-command
1104 | blacken-executable
1105 | python-isort-command
1106 | ruff-format-command
1107 | yapfify-executable
1108 | py-autopep8-command))))
1109 |
1110 | (with-current-buffer-window "*pet info*" nil nil
1111 | (mapc (pcase-lambda (`(,key . ,value))
1112 | (insert (propertize (format "%-40s" (concat (symbol-name key) ":")) 'face 'font-lock-variable-name-face))
1113 | (insert (format "%s" value))
1114 | (insert "\n"))
1115 | kvp)
1116 | (insert (propertize (format "%-40s"
1117 | (concat (symbol-name (if (file-remote-p default-directory)
1118 | 'tramp-remote-path
1119 | 'exec-path))
1120 | ":"))
1121 | 'face 'font-lock-variable-name-face) "\n")
1122 | (mapc (lambda (dir)
1123 | (insert (abbreviate-file-name (format "%s" dir)) "\n"))
1124 | (if (file-remote-p default-directory)
1125 | tramp-remote-path
1126 | exec-path))
1127 | (special-mode))))
1128 |
1129 | ;;;###autoload
1130 | (define-minor-mode pet-mode
1131 | "Minor mode to set up buffer local variables for Python tools."
1132 | :lighter " Pet"
1133 | :group 'pet
1134 | (if pet-mode
1135 | (progn
1136 | (pet-buffer-local-vars-setup)
1137 | (add-hook 'kill-buffer-hook #'pet-cleanup-watchers-and-caches t))
1138 | (pet-buffer-local-vars-teardown)
1139 | (remove-hook 'kill-buffer-hook #'pet-cleanup-watchers-and-caches t)))
1140 |
1141 | (defun pet-cleanup-watchers-and-caches ()
1142 | "Clean up configuration file caches and watchers.
1143 |
1144 | Delete configuration file caches and watchers when all
1145 | `python-mode' buffers of a project have been closed."
1146 | (when (and (buffer-file-name)
1147 | (derived-mode-p 'python-base-mode 'python-mode))
1148 | (when-let*((root (pet-project-root)))
1149 | (when (null (cl-loop for buf in (buffer-list)
1150 | if (and (not (equal buf (current-buffer)))
1151 | (string-prefix-p root (buffer-file-name buf)))
1152 | return buf))
1153 |
1154 | (setf (alist-get root pet-project-virtualenv-cache nil t 'equal) nil)
1155 |
1156 | (pcase-dolist (`(,config-file . ,watcher) pet-watched-config-files)
1157 | (when (string-prefix-p root config-file)
1158 | (file-notify-rm-watch watcher)
1159 | (setf (alist-get config-file pet-watched-config-files nil t 'equal) nil)))
1160 |
1161 | (dolist (cache '(pet-pre-commit-config-cache
1162 | pet-pyproject-cache
1163 | pet-python-version-cache
1164 | pet-pipfile-cache
1165 | pet-environment-cache))
1166 | (pcase-dolist (`(,key . ,_) (symbol-value cache))
1167 | (when (string-prefix-p root key)
1168 | (setf (alist-get key (symbol-value cache) nil t 'equal) nil))))))))
1169 |
1170 | (provide 'pet)
1171 |
1172 | ;;; pet.el ends here
1173 |
--------------------------------------------------------------------------------
/test/pet-test.el:
--------------------------------------------------------------------------------
1 | ;; -*- lisp-indent-offset: 2; lexical-binding: t; -*-
2 |
3 | (unless (< emacs-major-version 27)
4 | (load-file "test/undercover-init.el"))
5 |
6 | (require 'pet)
7 |
8 | ;; (setq pet-debug t)
9 |
10 | (setq python-indent-guess-indent-offset nil)
11 |
12 | (describe "pet-system-bin-dir"
13 | (describe "when called on Windows"
14 | (before-each
15 | (setq-local system-type 'windows-nt)
16 | ;; Github actions triggers some weird Emacs 26 code path that calls
17 | ;; `file-truename' after setting `system-type' and when `flycheck' is not
18 | ;; in the `load-path'. WTF indeed.
19 | (spy-on 'w32-long-file-name))
20 |
21 | (after-each
22 | (kill-local-variable 'system-type))
23 |
24 | (it "should return Scripts"
25 | (expect (pet-system-bin-dir) :to-equal "Scripts")))
26 |
27 | (describe "when called on non-Windows"
28 | (before-each
29 | (setq-local system-type 'gnu/linux))
30 |
31 | (after-each
32 | (kill-local-variable 'system-type))
33 |
34 | (it "should return bin"
35 | (expect (pet-system-bin-dir) :to-equal "bin"))))
36 |
37 | (describe "pet-report-error"
38 | (describe "when `pet-debug' is t"
39 | (before-each
40 | (setq-local pet-debug t))
41 |
42 | (after-each
43 | (kill-local-variable 'pet-debug))
44 |
45 | (it "should call minibuffer-message"
46 | (buttercup-suppress-warning-capture
47 | (spy-on 'minibuffer-message :and-call-fake 'ignore))
48 | (pet-report-error '(error . ("error")))
49 | (expect 'minibuffer-message :to-have-been-called-with "error")))
50 |
51 | (it "should not call minibuffer-message when `pet-debug' is nil"
52 | (pet-report-error '(error . ("error")))
53 | (expect 'minibuffer-message :not :to-have-been-called)))
54 |
55 | (describe "pet-project-root"
56 | (it "should find project root with `projectile'"
57 | (spy-on 'projectile-project-root :and-return-value "/")
58 | (expect (pet-project-root) :to-equal "/"))
59 |
60 | (it "should find project root with `project.el'"
61 | (spy-on 'projectile-project-root)
62 | (spy-on 'project-current :and-return-value (if (< emacs-major-version 29) (cons 'vc "/") '(vc Git "/")))
63 | (expect (pet-project-root) :to-equal "/"))
64 |
65 | (it "should return nil when Python file does not appear to be in a project"
66 | (spy-on 'projectile-project-root)
67 | (spy-on 'project-current)
68 | (expect (pet-project-root) :to-be nil)))
69 |
70 | (describe "pet-find-file-from-project-root"
71 | (it "should find file from project root"
72 | (spy-on 'pet-project-root :and-return-value "/etc")
73 | (expect (pet-find-file-from-project-root "passwd") :to-equal "/etc/passwd"))
74 |
75 | (it "should return nil when file not found from project root"
76 | (spy-on 'pet-project-root :and-return-value "/etc")
77 | (expect (pet-find-file-from-project-root "idontexist") :to-be nil))
78 |
79 | (it "should return nil when not under a project"
80 | (spy-on 'pet-project-root)
81 | (expect (pet-find-file-from-project-root "foo") :to-be nil)))
82 |
83 | (describe "pet-locate-dominating-file"
84 | :var ((old-default-directory default-directory)
85 | (home (getenv "HOME"))
86 | (process-environment (copy-sequence process-environment)))
87 |
88 | (before-each
89 | (setenv "HOME" "/home/user/")
90 | (setq-local default-directory "~/project/src/")
91 | (spy-on 'pet-project-root :and-return-value "/home/user/project/"))
92 |
93 | (after-each
94 | (setenv "HOME" home)
95 | (setq-local default-directory old-default-directory))
96 |
97 | (it "should find file from `default-directory'"
98 | (spy-on 'locate-dominating-file :and-return-value "~/project/src/package")
99 | (spy-on 'file-expand-wildcards :and-call-fake
100 | (lambda (pattern &optional full)
101 | (cond
102 | ((and full (equal pattern "/home/user/project/src/package/.pylintrc"))
103 | (list (expand-file-name pattern)))
104 | ((and (not full) (equal pattern "~/project/src/package/.pylintrc"))
105 | (list pattern)))))
106 | (expect (pet-locate-dominating-file ".pylintrc") :to-equal "/home/user/project/src/package/.pylintrc"))
107 |
108 | (it "should find file from parent directory"
109 | (spy-on 'locate-dominating-file :and-return-value "~/project/src/")
110 | (spy-on 'file-expand-wildcards :and-call-fake
111 | (lambda (pattern &optional full)
112 | (cond
113 | ((and full (equal pattern "/home/user/project/src/.pylintrc"))
114 | (list (expand-file-name pattern)))
115 | ((and (not full) (equal pattern "~/project/src/.pylintrc"))
116 | (list pattern)))))
117 | (expect (pet-locate-dominating-file ".pylintrc") :to-equal "/home/user/project/src/.pylintrc"))
118 |
119 | (it "should return `nil' if found file is outside of project root"
120 | (spy-on 'file-expand-wildcards :and-call-fake
121 | (lambda (dir &optional _)
122 | (when (equal dir "~/")
123 | (list "~/"))))
124 | (expect (pet-locate-dominating-file ".pylintrc") :to-be nil)))
125 |
126 | (describe "pet-find-file-from-project-root-recursively"
127 | :var ((old-default-directory default-directory)
128 | (home (getenv "HOME"))
129 | (process-environment (copy-sequence process-environment)))
130 |
131 | (before-each
132 | (setenv "HOME" "/home/user/")
133 | (setq-local default-directory "~/project/src/")
134 | (spy-on 'pet-project-root :and-return-value "/home/user/project/"))
135 |
136 | (after-each
137 | (setenv "HOME" home)
138 | (setq-local default-directory old-default-directory))
139 |
140 | (describe "when using projectile"
141 | (it "should return the absolute path of the first file in a project that matches the file wildcard"
142 | (spy-on 'projectile-dir-files :and-return-value '("environment-dev.yaml"))
143 | (expect (pet-find-file-from-project-root-recursively "environment*.y*ml") :to-equal "/home/user/project/environment-dev.yaml"))
144 |
145 | (it "should return `nil' if the project is empty"
146 | (spy-on 'projectile-dir-files :and-return-value nil)
147 | (expect (pet-find-file-from-project-root-recursively "environment*.y*ml") :to-be nil))
148 |
149 | (it "should return `nil' if no file in the project matches the file wildcard"
150 | (spy-on 'projectile-dir-files :and-return-value '("setup.py"))
151 | (expect (pet-find-file-from-project-root-recursively "environment*.y*ml") :to-be nil)))
152 |
153 | (describe "when using project.el"
154 | :var ((projectile-dir-files-def (symbol-function 'projectile-dir-files)))
155 |
156 | (before-each
157 | (fset 'projectile-dir-files nil))
158 |
159 | (after-each
160 | (fset 'projectile-dir-files projectile-dir-files-def))
161 |
162 | (it "should return the absolute path of the first file in a project that matches the file wildcard"
163 | (spy-on 'project-files :and-return-value '("/home/user/project/environment-dev.yaml"))
164 | (expect (pet-find-file-from-project-root-recursively "environment*.y*ml") :to-equal "/home/user/project/environment-dev.yaml"))
165 |
166 | (it "should return `nil' if the project is empty"
167 | (spy-on 'project-files :and-return-value nil)
168 | (expect (pet-find-file-from-project-root-recursively "environment*.y*ml") :to-be nil))
169 |
170 | (it "should return `nil' if no file in the project matches the file wildcard"
171 | (spy-on 'project-files :and-return-value '("setup.py"))
172 | (expect (pet-find-file-from-project-root-recursively "environment*.y*ml") :to-be nil)))
173 |
174 | (describe "when using `directory-files-recursively'"
175 | :var ((projectile-dir-files-def (symbol-function 'projectile-dir-files))
176 | (project-files-def (symbol-function 'project-files)))
177 |
178 | (before-each
179 | (fset 'projectile-dir-files nil)
180 | (fset 'project-files nil))
181 |
182 | (after-each
183 | (fset 'projectile-dir-files projectile-dir-files-def)
184 | (fset 'project-files project-files-def))
185 |
186 | (it "should return the absolute path of the first file in a project that matches the file wildcard"
187 | (spy-on 'directory-files-recursively :and-return-value '("/home/user/project/environment-dev.yaml"))
188 | (expect (pet-find-file-from-project-root-recursively "environment*.y*ml") :to-equal "/home/user/project/environment-dev.yaml"))
189 |
190 | (it "should return `nil' if the project is empty"
191 | (spy-on 'directory-files-recursively :and-return-value nil)
192 | (expect (pet-find-file-from-project-root-recursively "environment*.y*ml") :to-be nil))
193 |
194 | (it "should return `nil' if no file in the project matches the file wildcard"
195 | (spy-on 'directory-files-recursively :and-return-value '("setup.py"))
196 | (expect (pet-find-file-from-project-root-recursively "environment*.y*ml") :to-be nil))))
197 |
198 | (describe "pet-parse-json"
199 | (it "should parse a JSON string to an alist"
200 | (expect (pet-parse-json "{\"foo\":\"bar\",\"baz\":[\"buz\",1]}") :to-equal '((foo . "bar") (baz "buz" 1)))))
201 |
202 | (describe "pet-parse-config-file"
203 | :var* ((yaml-content "foo: bar\nbaz:\n - buz\n - 1\n")
204 | (toml-content "foo = \"bar\"\nbaz = [\"buz\", 1]\n")
205 | (json-content "{\"foo\":\"bar\",\"baz\":[\"buz\",1]}")
206 | (yaml-file (make-temp-file "pet-test" nil ".yaml" yaml-content))
207 | (toml-file (make-temp-file "pet-test" nil ".toml" toml-content))
208 | (json-file (make-temp-file "pet-test" nil ".json" json-content))
209 | (toml-file-sans-ext (make-temp-file "pet-test" nil nil toml-content))
210 | (yaml-file-sans-ext (make-temp-file "pet-test" nil nil yaml-content))
211 | (json-file-sans-ext (make-temp-file "pet-test" nil nil json-content))
212 | (jsonian-file-sans-ext (make-temp-file "pet-test" nil nil json-content)))
213 |
214 | (after-all
215 | (delete-file yaml-file)
216 | (delete-file toml-file)
217 | (delete-file json-file)
218 | (delete-file toml-file-sans-ext)
219 | (delete-file yaml-file-san-ext)
220 | (delete-file json-file-san-ext))
221 |
222 | (before-each
223 | (setq-local pet-toml-to-json-program "tomljson")
224 | (setq-local pet-toml-to-json-program-arguments nil)
225 | (setq-local pet-yaml-to-json-program "yq")
226 | (setq-local pet-yaml-to-json-program-arguments '("--output-format" "json"))
227 | (make-local-variable 'auto-mode-alist)
228 | (add-to-list 'auto-mode-alist (cons (concat (file-name-nondirectory toml-file-sans-ext) "\\'") 'conf-toml-mode))
229 | (add-to-list 'auto-mode-alist (cons (concat (file-name-nondirectory yaml-file-sans-ext) "\\'") 'yaml-mode))
230 | (add-to-list 'auto-mode-alist (cons (concat (file-name-nondirectory json-file-sans-ext) "\\'") 'json-mode))
231 | (add-to-list 'auto-mode-alist (cons (concat (file-name-nondirectory jsonian-file-sans-ext) "\\'") 'jsonian-mode)))
232 |
233 | (after-each
234 | (kill-local-variable 'pet-toml-to-json-program)
235 | (kill-local-variable 'pet-toml-to-json-program-arguments)
236 | (kill-local-variable 'pet-yaml-to-json-program)
237 | (kill-local-variable 'pet-yaml-to-json-program-arguments)
238 | (kill-local-variable 'auto-mode-alist))
239 |
240 | (it "should parse a YAML file content to alist"
241 | (expect (pet-parse-config-file yaml-file) :to-have-same-items-as '((foo . "bar") (baz "buz" 1)))
242 | (expect (get-buffer " *pet parser output*") :to-be nil))
243 |
244 | (it "should parse a TOML file content to alist if the file name matches a key in `auto-mode-alist' and the value is `yaml-mode'"
245 | (expect (pet-parse-config-file yaml-file-sans-ext) :to-have-same-items-as '((foo . "bar") (baz "buz" 1)))
246 | (expect (get-buffer " *pet parser output*") :to-be nil))
247 |
248 | (it "should parse a TOML file content to alist"
249 | (expect (pet-parse-config-file toml-file) :to-have-same-items-as '((foo . "bar") (baz "buz" 1)))
250 | (expect (get-buffer " *pet parser output*") :to-be nil))
251 |
252 | (it "should parse a TOML file content to alist if the file name matches a key in `auto-mode-alist' and the value is `conf-toml-mode'"
253 | (expect (pet-parse-config-file toml-file-sans-ext) :to-have-same-items-as '((foo . "bar") (baz "buz" 1)))
254 | (expect (get-buffer " *pet parser output*") :to-be nil))
255 |
256 | (it "should parse a JSON file content to alist"
257 | (expect (pet-parse-config-file json-file) :to-have-same-items-as '((foo . "bar") (baz "buz" 1)))
258 | (expect (get-buffer " *pet parser output*") :to-be nil))
259 |
260 | (it "should parse a JSON file content to alist if the file name matches a key in `auto-mode-alist' and the value is `json-mode'"
261 | (expect (pet-parse-config-file json-file-sans-ext) :to-have-same-items-as '((foo . "bar") (baz "buz" 1)))
262 | (expect (get-buffer " *pet parser output*") :to-be nil))
263 |
264 | (it "should parse a JSON file content to alist if the file name matches a key in `auto-mode-alist' and the value is `jsonian-mode'"
265 | (expect (pet-parse-config-file jsonian-file-sans-ext) :to-have-same-items-as '((foo . "bar") (baz "buz" 1)))
266 | (expect (get-buffer " *pet parser output*") :to-be nil)))
267 |
268 | (describe "pet-make-config-file-change-callback"
269 | (it "should return a function"
270 | (expect (functionp (pet-make-config-file-change-callback 'cache 'parser)) :to-be-truthy))
271 |
272 | (describe "when received deleted event"
273 | :var* ((descriptor 1)
274 | (file "/home/user/project/tox.ini")
275 | (event `((,file . ,descriptor))))
276 |
277 | (before-each
278 | (spy-on 'file-notify-rm-watch)
279 | (setq-local pet-watched-config-files event)
280 | (defvar cache `((,file . "content")))
281 | (defvar callback (pet-make-config-file-change-callback 'cache nil))
282 | (funcall callback `(,descriptor deleted ,file)))
283 |
284 | (after-each
285 | (kill-local-variable 'pet-watched-config-files)
286 | (makunbound 'cache)
287 | (unintern 'cache)
288 | (makunbound 'callback)
289 | (unintern 'callback))
290 |
291 | (it "should remove file watcher"
292 | (expect 'file-notify-rm-watch :to-have-been-called-with descriptor))
293 |
294 | (it "should remove entry from cache"
295 | (expect (assoc-default file cache) :not :to-be-truthy))
296 |
297 | (it "should remove entry from `pet-watched-config-files'"
298 | (expect (assoc-default file pet-watched-config-files) :not :to-be-truthy)))
299 |
300 | (describe "when received changed event"
301 | :var ((file "/home/user/project/tox.ini"))
302 |
303 | (before-each
304 | (defvar cache nil)
305 | (defun parser (file)
306 | "content")
307 |
308 | (spy-on 'parser :and-call-through)
309 |
310 | (defvar callback (pet-make-config-file-change-callback 'cache 'parser))
311 | (funcall callback `(1 changed ,file)))
312 |
313 | (after-each
314 | (makunbound 'cache)
315 | (unintern 'cache)
316 | (fmakunbound 'parser)
317 | (unintern 'parser)
318 | (makunbound 'callback)
319 | (unintern 'callback))
320 |
321 | (it "parse the file again"
322 | (expect 'parser :to-have-been-called-with file)
323 | (expect (spy-context-return-value (spy-calls-most-recent 'parser)) :to-equal "content"))
324 |
325 | (it "should set parsed value to cache"
326 | (expect (assoc-default file cache) :to-equal "content"))))
327 |
328 | (describe "pet-watch-config-file"
329 | :var ((file "/home/user/project/tox.ini"))
330 |
331 | (describe "when the file is being watched"
332 | (before-each
333 | (spy-on 'file-notify-add-watch)
334 | (setq-local pet-watched-config-files `((,file . 1))))
335 |
336 | (after-each
337 | (kill-local-variable 'pet-watched-config-files))
338 |
339 | (it "should do nothing"
340 | (expect (pet-watch-config-file file nil nil) :to-be nil)
341 | (expect 'file-notify-add-watch :not :to-have-been-called)))
342 |
343 | (describe "when the file isn't being watched"
344 | :var ((callback (lambda ())))
345 |
346 | (before-each
347 | (spy-on 'file-notify-add-watch :and-return-value 1)
348 | (spy-on 'pet-make-config-file-change-callback :and-return-value callback)
349 | (defvar pet-tox-ini-cache nil)
350 | (defun parser (file) "content"))
351 |
352 | (after-each
353 | (makunbound 'pet-tox-ini-cache)
354 | (unintern 'pet-tox-ini-cache)
355 | (fmakunbound 'parser)
356 | (unintern 'parser))
357 |
358 | (it "should add an entry to the watched files cache"
359 | (pet-watch-config-file file 'pet-tox-ini-cache 'parser)
360 | (expect 'file-notify-add-watch :to-have-been-called-with file '(change) callback)
361 | (expect 'pet-make-config-file-change-callback :to-have-been-called-with 'pet-tox-ini-cache 'parser)
362 | (expect (assoc-default file pet-watched-config-files) :to-equal 1))))
363 |
364 | (describe "pet-def-config-accessor"
365 | (before-each
366 | (defun parser (file) "content"))
367 |
368 | (after-all
369 | (fmakunbound 'parser)
370 | (unintern 'parser))
371 |
372 | (before-each
373 | (pet-def-config-accessor tox-ini :file-name "tox.ini" :parser parser))
374 |
375 | (after-each
376 | (fmakunbound 'pet-tox-ini)
377 | (unintern 'pet-tox-ini)
378 | (makunbound 'pet-tox-ini-cache)
379 | (unintern 'pet-tox-ini-cache))
380 |
381 | (it "should create cache variable"
382 | (expect (boundp 'pet-tox-ini-cache) :to-be t))
383 |
384 | (it "should create cache access function"
385 | (expect (fboundp 'pet-tox-ini) :to-be t))
386 |
387 | (describe "the cache access function"
388 | (before-each
389 | (spy-on 'pet-find-file-from-project :and-return-value "/home/user/project/tox.ini")
390 | (spy-on 'pet-watch-config-file :and-call-fake (lambda (_ __ ___)))
391 | (spy-on 'parser :and-call-through))
392 |
393 | (after-each
394 | (setq pet-tox-ini-cache nil))
395 |
396 | (it "should return cached value if it exists"
397 | (push (cons "/home/user/project/tox.ini" "cached content") pet-tox-ini-cache)
398 | (expect (pet-tox-ini) :to-equal "cached content")
399 | (expect 'pet-watch-config-file :not :to-have-been-called)
400 | (expect 'parser :not :to-have-been-called))
401 |
402 | (describe "when the config file content has not been cached"
403 | (it "should return parsed file content"
404 | (expect (pet-tox-ini) :to-equal "content"))
405 |
406 | (it "should watch file"
407 | (pet-tox-ini)
408 | (expect 'pet-watch-config-file :to-have-been-called-with "/home/user/project/tox.ini" 'pet-tox-ini-cache 'parser))
409 |
410 | (it "should cache config file content"
411 | (pet-tox-ini)
412 | (expect (alist-get "/home/user/project/tox.ini" pet-tox-ini-cache nil nil 'equal) :to-equal "content")))))
413 |
414 | (describe "pet-use-pre-commit-p"
415 | (describe "when the project has a `.pre-commit-config.yaml' file"
416 | (before-each
417 | (spy-on 'pet-pre-commit-config :and-return-value t))
418 |
419 | (it "should return `pre-commit' path if `pre-commit' is found"
420 | (spy-on 'executable-find :and-return-value "/usr/bin/pre-commit")
421 | (expect (pet-use-pre-commit-p) :to-equal "/usr/bin/pre-commit")
422 |
423 | (spy-on 'pet-virtualenv-root :and-return-value "/home/user/venv/test")
424 | (let ((call-count 0))
425 | (spy-on 'executable-find :and-call-fake (lambda (&rest _)
426 | (setq call-count (1+ call-count))
427 | (when (= call-count 2)
428 | "/home/user/venv/test/bin/pre-commit"))))
429 |
430 | (expect (pet-use-pre-commit-p) :to-equal "/home/user/venv/test/bin/pre-commit"))
431 |
432 | (it "should return nil if `pre-commit' is not found"
433 | (spy-on 'executable-find :and-return-value "/usr/bin/pre-commit")
434 | (expect (pet-use-pre-commit-p) :to-equal "/usr/bin/pre-commit")))
435 |
436 | (describe "when the project does not have a `.pre-commit-config.yaml' file"
437 | (before-each
438 | (spy-on 'pet-pre-commit-config))
439 |
440 | (it "should return nil if `pre-commit' is found"
441 | (spy-on 'executable-find :and-return-value "/usr/bin/pre-commit")
442 | (expect (pet-use-pre-commit-p) :to-be nil))
443 |
444 | (it "should return nil if `pre-commit' is not found"
445 | (spy-on 'executable-find)
446 | (expect (pet-use-pre-commit-p) :to-be nil))))
447 |
448 | (describe "pet-use-conda-p"
449 | (describe "when the project has an `environment[a-zA-Z0-9-_].yaml' file"
450 | (before-each
451 | (spy-on 'pet-environment :and-return-value t))
452 |
453 | (it "should return `conda' path if `conda' is found"
454 | (spy-on 'executable-find :and-call-fake (lambda (exe &optional _) (when (equal exe "conda") "/usr/bin/conda")))
455 | (expect (pet-use-conda-p) :to-equal "/usr/bin/conda"))
456 |
457 | (it "should return `mamba' path if `mamba' is found"
458 | (spy-on 'executable-find :and-call-fake (lambda (exe &optional _) (when (equal exe "mamba") "/usr/bin/mamba")))
459 | (expect (pet-use-conda-p) :to-equal "/usr/bin/mamba"))
460 |
461 | (it "should return `micromamba' path if `micromamba' is found"
462 | (spy-on 'executable-find :and-call-fake (lambda (exe &optional _) (when (equal exe "micromamba") "/usr/bin/micromamba")))
463 | (expect (pet-use-conda-p) :to-equal "/usr/bin/micromamba"))
464 |
465 | (it "should return nil if none of `conda' or `mamba' or `micromamba' is found"
466 | (spy-on 'executable-find)
467 | (expect (pet-use-conda-p) :to-be nil)))
468 |
469 | (describe "when the project does not have a `environment[a-zA-Z0-9-_].yaml' file"
470 | (before-each
471 | (spy-on 'pet-environment))
472 |
473 | (it "should return nil if `conda' is found"
474 | (spy-on 'executable-find :and-call-fake (lambda (exe &optional _) (when (equal exe "conda") "/usr/bin/conda")))
475 | (expect (pet-use-conda-p) :to-be nil))
476 |
477 | (it "should return nil if `mamba' is found"
478 | (spy-on 'executable-find :and-call-fake (lambda (exe &optional _) (when (equal exe "mamba") "/usr/bin/mamba")))
479 | (expect (pet-use-conda-p) :to-be nil))
480 |
481 | (it "should return nil if `microconda' is found"
482 | (spy-on 'executable-find :and-call-fake (lambda (exe &optional _) (when (equal exe "microconda") "/usr/bin/microconda")))
483 | (expect (pet-use-conda-p) :to-be nil))
484 |
485 | (it "should return nil if none of `conda' or `mamba' or `micromamba' is found"
486 | (spy-on 'executable-find)
487 | (expect (pet-use-conda-p) :to-be nil))))
488 |
489 | (describe "pet-use-poetry-p"
490 | (describe "when the `pyproject.toml' file in the project declares `poetry' as the build system"
491 | (before-each
492 | (spy-on 'pet-pyproject :and-return-value '((build-system (build-backend . "poetry.core.masonry.api")))))
493 |
494 | (it "should return `poetry' path if `poetry' is found"
495 | (spy-on 'executable-find :and-return-value "/usr/bin/poetry")
496 | (expect (pet-use-poetry-p) :to-equal "/usr/bin/poetry"))
497 |
498 | (it "should return nil if `poetry' is not found"
499 | (spy-on 'executable-find)
500 | (expect (pet-use-poetry-p) :to-be nil)))
501 |
502 | (describe "when the `pyproject.toml' file in the project does not declare `poetry' as the build system"
503 | (before-each
504 | (spy-on 'pet-pyproject :and-return-value '((build-system (build-backend . "pdm")))))
505 |
506 | (it "should return nil if `poetry' is found"
507 | (expect (pet-use-poetry-p) :to-be nil))
508 |
509 | (it "should return nil if `poetry' is not found"
510 | (spy-on 'executable-find :and-return-value "/usr/bin/poetry")
511 | (expect (pet-use-poetry-p) :to-be nil)))
512 |
513 | (describe "when the project does not have a `pyproject.toml' file"
514 | (before-each
515 | (spy-on 'pet-pyproject))
516 |
517 | (it "should return nil if `poetry' is found"
518 | (expect (pet-use-poetry-p) :to-be nil))
519 |
520 | (it "should return nil if `poetry' is not found"
521 | (spy-on 'executable-find :and-return-value "/usr/bin/poetry")
522 | (expect (pet-use-poetry-p) :to-be nil))))
523 |
524 | (describe "pet-use-pyenv-p"
525 | (describe "when the project has a `.python-version' file"
526 | (before-each
527 | (spy-on 'pet-python-version :and-return-value t))
528 |
529 | (it "should return `pyenv' path if `pyenv' is found"
530 | (spy-on 'executable-find :and-return-value "/usr/bin/pyenv")
531 | (expect (pet-use-pyenv-p) :to-equal "/usr/bin/pyenv"))
532 |
533 | (it "should return `pyenv' path if `pyenv' is not found"
534 | (spy-on 'executable-find)
535 | (expect (pet-use-pyenv-p) :to-be nil)))
536 |
537 | (describe "when the project does not have a `.python-version' file"
538 | (before-each
539 | (spy-on 'pet-python-version))
540 |
541 | (it "should return nil if `pyenv' is found"
542 | (spy-on 'executable-find :and-return-value "/usr/bin/pyenv")
543 | (expect (pet-use-pyenv-p) :to-be nil))
544 |
545 | (it "should return nil if `pyenv' is not found"
546 | (spy-on 'executable-find)
547 | (expect (pet-use-pyenv-p) :to-be nil))))
548 |
549 | (describe "pet-use-pipenv-p"
550 | (describe "when the project has a `Pipfile' file"
551 | (before-each
552 | (spy-on 'pet-pipfile :and-return-value t))
553 |
554 | (it "should return `pipenv' path if `pipenv' is found"
555 | (spy-on 'executable-find :and-return-value "/usr/bin/pipenv")
556 | (expect (pet-use-pipenv-p) :to-equal "/usr/bin/pipenv"))
557 |
558 | (it "should return nil path if `pipenv' is not found"
559 | (spy-on 'executable-find)
560 | (expect (pet-use-pipenv-p) :to-be nil)))
561 |
562 | (describe "when the project does not have a `Pipfile' file"
563 | (before-each
564 | (spy-on 'pet-pipfile))
565 |
566 | (it "should return nil path if `pipenv' is found"
567 | (spy-on 'executable-find :and-return-value "/usr/bin/pipenv")
568 | (expect (pet-use-pipenv-p) :to-be nil))
569 |
570 | (it "should return nil path if `pipenv' is not found"
571 | (spy-on 'executable-find)
572 | (expect (pet-use-pipenv-p) :to-be nil))))
573 |
574 | (describe "pet-pre-commit-config-has-hook-p"
575 | :var ((pre-commit-config-content
576 | '((repos
577 | ((hooks ((id . "black"))) (repo . "https://github.com/psf/black") (rev . "22.6.0"))))))
578 |
579 | (before-each
580 | (spy-on 'pet-pre-commit-config :and-return-value pre-commit-config-content))
581 |
582 | (it "should return t if `.pre-commit-config.yaml' has hook declared"
583 | (expect (pet-pre-commit-config-has-hook-p "black") :to-be-truthy))
584 |
585 | (it "should return nil if `.pre-commit-config.yaml' does not have hook declared"
586 | (expect (pet-pre-commit-config-has-hook-p "isort") :not :to-be-truthy)))
587 |
588 | (describe "pet-parse-pre-commit-db"
589 | (before-all
590 | (call-process "sqlite3" nil nil nil "some.db" "CREATE TABLE repos (repo TEXT NOT NULL, ref TEXT NOT NULL, path TEXT NOT NULL, PRIMARY KEY (repo, ref)); INSERT INTO repos VALUES('https://github.com/pycqa/flake8','5.0.0','/home/user/project/flake8');"))
591 |
592 | (after-all
593 | (call-process "rm" nil nil nil "some.db"))
594 |
595 | (it "should parse `pre-commit' database to alist"
596 | (expect (pet-parse-pre-commit-db "some.db") :to-equal '(((repo . "https://github.com/pycqa/flake8")
597 | (ref . "5.0.0")
598 | (path . "/home/user/project/flake8"))))))
599 |
600 | (describe "pet-pre-commit-virtualenv-path"
601 | :var ((pre-commit-db-content '(((repo . "https://github.com/pycqa/flake8:flake8-comprehensions==3.10.0,flake8-no-implicit-concat==0.3.3")
602 | (ref . "bd1656c")
603 | (path . "/home/user/.cache/pre-commit/repofurqd1rq"))
604 | ((repo . "https://github.com/psf/black")
605 | (ref . "22.6.0")
606 | (path . "/home/user/.cache/pre-commit/repo85no_p81"))))
607 | (pre-commit-config-content '((repos
608 | ((hooks
609 | ((additional_dependencies "flake8-no-implicit-concat==0.3.3" "flake8-comprehensions==3.10.0")
610 | (id . "flake8")))
611 | (repo . "https://github.com/pycqa/flake8")
612 | (rev . "bd1656c"))
613 | ((hooks
614 | ((id . "black")))
615 | (repo . "https://github.com/psf/black")
616 | (rev . "22.6.0")))))
617 | (old-default-directory default-directory)
618 | (home (getenv "HOME"))
619 | (orig-getenv (symbol-function 'getenv))
620 | (process-environment (copy-sequence process-environment)))
621 |
622 | (before-each
623 | (setenv "HOME" "/home/user/")
624 | (setq-local default-directory "~/project/src/")
625 | (spy-on 'getenv :and-call-fake
626 | (lambda (name)
627 | (unless (member name '("PRE_COMMIT_HOME" "XDG_CACHE_HOME"))
628 | (funcall orig-getenv name))))
629 | (spy-on 'pet-pre-commit-config :and-return-value pre-commit-config-content))
630 |
631 | (after-each
632 | (setenv "HOME" home)
633 | (setq-local default-directory old-default-directory))
634 |
635 | (describe "when `pre-commit' database content is not cached"
636 | (before-each
637 | (spy-on 'file-exists-p :and-call-fake (lambda (name) (equal name "/home/user/.cache/pre-commit/db.db")))
638 | (spy-on 'pet-watch-config-file)
639 | (spy-on 'pet-parse-pre-commit-db :and-return-value pre-commit-db-content))
640 |
641 | (it "should return absolute path to the virtualenv of a `pre-commit' hook with additional dependencies"
642 | (spy-on 'file-expand-wildcards :and-return-value '("/home/user/.cache/pre-commit/repofurqd1rq/py_env-python3.9"
643 | "/home/user/.cache/pre-commit/repofurqd1rq/py_env-python3.10"))
644 | (expect (pet-pre-commit-virtualenv-path "flake8") :to-equal "/home/user/.cache/pre-commit/repofurqd1rq/py_env-python3.10"))
645 |
646 | (it "should return absolute path to the virtualenv of a `pre-commit' hook with no additional dependencies"
647 | (spy-on 'file-expand-wildcards :and-return-value '("/home/user/.cache/pre-commit/repo85no_p81/py_env-python3.9"))
648 | (expect (pet-pre-commit-virtualenv-path "black") :to-equal "/home/user/.cache/pre-commit/repo85no_p81/py_env-python3.9")))
649 |
650 | (describe "when `pre-commit' database content is cached"
651 | (before-each
652 | (setq-local pet-pre-commit-database-cache `(("/home/user/.cache/pre-commit/db.db" ,@pre-commit-db-content))))
653 |
654 | (after-each
655 | (kill-local-variable 'pet-pre-commit-database-cache))
656 |
657 | (it "should return absolute path to the virtualenv of a `pre-commit' hook with additional dependencies"
658 | (spy-on 'file-expand-wildcards :and-return-value '("/home/user/.cache/pre-commit/repofurqd1rq/py_env-python3.9"
659 | "/home/user/.cache/pre-commit/repofurqd1rq/py_env-python3.10"))
660 | (expect (pet-pre-commit-virtualenv-path "flake8") :to-equal "/home/user/.cache/pre-commit/repofurqd1rq/py_env-python3.10"))
661 |
662 | (it "should return absolute path to the virtualenv of a `pre-commit' hook with no additional dependencies"
663 | (spy-on 'file-expand-wildcards :and-return-value '("/home/user/.cache/pre-commit/repo85no_p81/py_env-python3.9"))
664 | (expect (pet-pre-commit-virtualenv-path "black") :to-equal "/home/user/.cache/pre-commit/repo85no_p81/py_env-python3.9"))))
665 |
666 | (describe "pet-executable-find"
667 |
668 | (describe "when using `pre-commit'"
669 | (before-each
670 | (spy-on 'pet-use-pre-commit-p :and-return-value "/usr/bin/pre-commit")
671 | (spy-on 'pet--executable-find :and-return-value nil))
672 |
673 | (it "should return the absolute path to the executable if hook and hook repo are found and the executable is found in hook repo"
674 | (spy-on 'pet-pre-commit-config-has-hook-p :and-return-value t)
675 | (spy-on 'pet-pre-commit-virtualenv-path :and-return-value "/home/user/.cache/pre-commit/repoblack")
676 | (spy-on 'file-exists-p :and-call-fake (lambda (path) (equal path "/home/user/.cache/pre-commit/repoblack/bin/black")))
677 | (expect (pet-executable-find "black") :to-equal "/home/user/.cache/pre-commit/repoblack/bin/black"))
678 |
679 | (it "should return nil if the hook is not found in config"
680 | (spy-on 'pet-pre-commit-config-has-hook-p :and-return-value nil)
681 | (expect (pet-executable-find "black") :to-be nil))
682 |
683 | (it "should return nil if the hook repo is not found"
684 | (spy-on 'pet-pre-commit-config-has-hook-p :and-return-value t)
685 | (spy-on 'pet-pre-commit-virtualenv-path :and-return-value nil)
686 | (expect (pet-executable-find "black") :to-be nil))
687 |
688 | (it "should return nil if the executable is not found in hook repo"
689 | (spy-on 'pet-pre-commit-config-has-hook-p :and-return-value t)
690 | (spy-on 'pet-pre-commit-virtualenv-path :and-return-value "/home/user/.cache/pre-commit/repoblack")
691 | (spy-on 'file-exists-p :and-call-fake (lambda (path) (not (equal path "/home/user/.cache/pre-commit/repoblack/bin/black"))))
692 | (expect (pet-executable-find "black") :to-be nil)))
693 |
694 | (describe "when on *nix"
695 | (it "should return the absolute path to the python executable for a project if its virtualenv is found"
696 | (spy-on 'pet-use-pre-commit-p :and-return-value nil)
697 | (spy-on 'pet-virtualenv-root :and-return-value "/home/user/project/.venv/")
698 | (spy-on 'pet-use-conda-p :and-return-value nil)
699 | (spy-on 'pet-system-bin-dir)
700 | (spy-on 'executable-find :and-return-value "/home/user/project/.venv/bin/python")
701 | (expect (pet-executable-find "python") :to-equal "/home/user/project/.venv/bin/python")
702 | (expect 'pet-system-bin-dir :to-have-been-called-times 1))
703 |
704 | (it "should return the absolute path to the python executable for a conda project if its virtualenv is found"
705 | (spy-on 'pet-use-pre-commit-p :and-return-value nil)
706 | (spy-on 'pet-virtualenv-root :and-return-value "/home/user/anaconda/envs/project/")
707 | (spy-on 'pet-use-conda-p :and-return-value t)
708 | (spy-on 'pet-system-bin-dir)
709 | (spy-on 'executable-find :and-return-value "/home/user/anaconda/envs/project/bin/python")
710 | (expect (pet-executable-find "python") :to-equal "/home/user/anaconda/envs/project/bin/python")
711 | (expect 'pet-system-bin-dir :to-have-been-called-times 1)))
712 |
713 | (describe "when on windows"
714 | (before-each
715 | (setq-local system-type 'windows-nt))
716 |
717 | (after-each
718 | (kill-local-variable 'system-type))
719 |
720 | (it "should return the absolute path to the python executable for a project if its virtualenv is found"
721 | (spy-on 'pet-use-pre-commit-p :and-return-value nil)
722 | (spy-on 'pet-virtualenv-root :and-return-value "C:/Users/user/project/.venv/")
723 | (spy-on 'pet-use-conda-p :and-return-value nil)
724 | (spy-on 'pet-system-bin-dir)
725 | (spy-on 'executable-find :and-return-value "C:/Users/user/project/.venv/bin/python")
726 | (expect (pet-executable-find "python") :to-equal "C:/Users/user/project/.venv/bin/python")
727 | (expect 'pet-system-bin-dir :to-have-been-called-times 1))
728 |
729 | (it "should return the absolute path to the python executable for a conda project if its virtualenv is found"
730 | (spy-on 'pet-use-pre-commit-p :and-return-value nil)
731 | (spy-on 'pet-virtualenv-root :and-return-value "C:/Users/user/Anaconda3/envs/project/")
732 | (spy-on 'pet-use-conda-p :and-return-value t)
733 | (spy-on 'pet-system-bin-dir)
734 | (spy-on 'executable-find :and-return-value "C:/Users/user/Anaconda3/envs/project/python")
735 | (expect (pet-executable-find "python") :to-equal "C:/Users/user/Anaconda3/envs/project/python")
736 | (expect 'pet-system-bin-dir :not :to-have-been-called)))
737 |
738 | (describe "when `pet-search-globally' is t"
739 | (it "should return the absolute path of the result of `pyenv which EXECUTABLE' if no virtualenv is found but `pyenv' is in `exec-path'"
740 | (spy-on 'pet-use-pre-commit-p :and-return-value nil)
741 | (spy-on 'pet-virtualenv-root :and-return-value nil)
742 | (spy-on 'pet--executable-find :and-call-fake (lambda (executable &optional _)
743 | (when (equal executable "pyenv")
744 | "/usr/bin/pyenv")))
745 | (spy-on 'process-lines :and-return-value '("/home/user/.pyenv/versions/3.10.5/bin/python"))
746 | (expect (pet-executable-find "python" t) :to-equal "/home/user/.pyenv/versions/3.10.5/bin/python")
747 | (expect 'process-lines :to-have-been-called-with "pyenv" "which" "python")
748 | (expect 'pet--executable-find :to-have-been-called-times 1))
749 |
750 | (it "should return the absolute path the executable for a project from `exec-path'"
751 | (spy-on 'pet-use-pre-commit-p :and-return-value nil)
752 | (spy-on 'pet-virtualenv-root :and-return-value nil)
753 | (spy-on 'pet--executable-find :and-call-fake (lambda (executable &optional _)
754 | (when (equal executable "black")
755 | "/home/user/project/.venv/bin/black")))
756 | (expect (pet-executable-find "black" t) :to-equal "/home/user/project/.venv/bin/black")
757 | (expect 'pet--executable-find :to-have-been-called-times 2)))
758 |
759 | (describe "when `pet-search-globally' is nil"
760 | (before-each
761 | (setq-local pet-search-globally nil))
762 |
763 | (after-each
764 | (kill-local-variable 'pet-search-globally))
765 |
766 | (it "should not return the absolute path of the result of `pyenv which EXECUTABLE' if no virtualenv is found but `pyenv' is in `exec-path'"
767 | (spy-on 'pet-use-pre-commit-p :and-return-value nil)
768 | (spy-on 'pet-virtualenv-root :and-return-value nil)
769 | (spy-on 'pet--executable-find :and-call-fake (lambda (executable &optional _)
770 | (when (equal executable "pyenv")
771 | "/usr/bin/pyenv")))
772 | (spy-on 'process-lines :and-return-value '("/home/user/.pyenv/versions/3.10.5/bin/python"))
773 |
774 | (expect (pet-executable-find "python" nil) :to-equal nil)
775 | (expect 'process-lines :not :to-have-been-called-with "pyenv" "which" "python")
776 | (expect 'pet--executable-find :to-have-been-called-times 0))
777 |
778 | (it "should not return the absolute path the executable for a project from `exec-path'"
779 | (spy-on 'pet-use-pre-commit-p :and-return-value nil)
780 | (spy-on 'pet-virtualenv-root :and-return-value nil)
781 | (spy-on 'pet--executable-find :and-call-fake (lambda (executable &optional _)
782 | (when (equal executable "black")
783 | "/home/user/project/.venv/bin/black")))
784 |
785 | (expect (pet-executable-find "black" nil) :to-equal nil)
786 | (expect 'pet--executable-find :to-have-been-called-times 0))))
787 |
788 | (describe "pet-virtualenv-root"
789 | :var ((project-root "/home/user/project/")
790 | (conda-path "/usr/bin/conda")
791 | (conda-env-dirs '("/home/user/miniforge3/envs" "/home/user/.conda/envs"))
792 | (conda-virtualenv "/home/user/miniforge3/envs/project/")
793 | (poetry-path "/usr/bin/poetry")
794 | (poetry-virtualenv "/home/user/.cache/pypoetry/virtualenvs/project/")
795 | (pipenv-path "/usr/bin/pipenv")
796 | (pipenv-virtualenv "/home/user/.local/share/virtualenvs/project/")
797 | (venv-virtualenv "/home/user/project/.venv/")
798 | (pyenv-path "/usr/bin/pyenv")
799 | (pyenv-virtualenv "/home/user/.pyenv/versions/project/")
800 | (pyenv-virtualenv-truename "/home/user/.pyenv/versions/3.8/envs/project/")
801 | (old-default-directory default-directory)
802 | (home (getenv "HOME"))
803 | (process-environment (copy-sequence process-environment)))
804 |
805 | (before-each
806 | (setenv "HOME" "/home/user/")
807 | (setq-local default-directory "/home/user/project")
808 | (spy-on 'pet-project-root :and-return-value project-root)
809 | (setq pet-project-virtualenv-cache nil))
810 |
811 | (after-each
812 | (setenv "HOME" home)
813 | (setq-local default-directory old-default-directory)
814 | (setq pet-project-virtualenv-cache nil))
815 |
816 | (it "should not cache nil when not in a project"
817 | (spy-on 'pet-project-root :and-return-value nil)
818 | (expect (pet-virtualenv-root) :to-be nil)
819 | (expect pet-project-virtualenv-cache :to-be nil))
820 |
821 | (it "should return the absolute path of the virtualenv for a project from `VIRTUAL_ENV'"
822 | (spy-on 'getenv :and-call-fake (lambda (name) (when (equal name "VIRTUAL_ENV") "/home/user/.venvs/project")))
823 | (expect (pet-virtualenv-root) :to-equal "/home/user/.venvs/project"))
824 |
825 | (it "should return the absolute path of the virtualenv for a project using `conda'"
826 | (spy-on 'pet-use-conda-p :and-return-value conda-path)
827 | (spy-on 'pet-environment-path :and-return-value "/home/user/project/environment.yml")
828 | (spy-on 'call-process :and-call-fake
829 | (lambda (&rest _)
830 | (insert
831 | (format "{\"envs_dirs\": [%s]}"
832 | (string-join
833 | (seq-map
834 | (lambda (dir)
835 | (format "\"%s\"" dir))
836 | conda-env-dirs)
837 | ",")))
838 | 0))
839 | (spy-on 'pet-environment :and-return-value `((name . "project")))
840 | (spy-on 'file-directory-p :and-call-fake (lambda (filename) (equal filename conda-virtualenv)))
841 | (expect (pet-virtualenv-root) :to-equal conda-virtualenv)
842 | (expect (assoc-default project-root pet-project-virtualenv-cache) :to-equal conda-virtualenv)
843 | (expect 'call-process :to-have-been-called-with conda-path nil t nil "info" "--json"))
844 |
845 | (it "should return the absolute path of the virtualenv for a project using `poetry'"
846 | (spy-on 'pet-use-conda-p :and-return-value nil)
847 | (spy-on 'pet-use-poetry-p :and-return-value poetry-path)
848 | (spy-on 'pet-pyproject-path :and-return-value "/home/user/project/pyproject.toml")
849 | (spy-on 'call-process :and-call-fake (lambda (&rest _) (insert poetry-virtualenv) 0))
850 | (expect (pet-virtualenv-root) :to-equal poetry-virtualenv)
851 | (expect (assoc-default project-root pet-project-virtualenv-cache) :to-equal poetry-virtualenv)
852 | (expect 'call-process :to-have-been-called-with poetry-path nil t nil "env" "info" "--no-ansi" "--path"))
853 |
854 | (it "should return the absolute path of the virtualenv for a project using `pipenv'"
855 | (spy-on 'pet-use-conda-p :and-return-value nil)
856 | (spy-on 'pet-use-poetry-p :and-return-value nil)
857 | (spy-on 'pet-use-pipenv-p :and-return-value pipenv-path)
858 | (spy-on 'pet-pipfile-path :and-return-value "/home/user/project/Pipfile")
859 | (spy-on 'call-process :and-call-fake (lambda (&rest _) (insert pipenv-virtualenv) 0))
860 | (expect (pet-virtualenv-root) :to-equal pipenv-virtualenv)
861 | (expect (assoc-default project-root pet-project-virtualenv-cache) :to-equal pipenv-virtualenv)
862 | (expect 'call-process :to-have-been-called-with pipenv-path nil '(t nil) nil "--quiet" "--venv"))
863 |
864 | (it "should return the absolute path of the `.venv' or `venv' directory in a project"
865 | (spy-on 'pet-use-conda-p :and-return-value nil)
866 | (spy-on 'pet-use-poetry-p :and-return-value nil)
867 | (spy-on 'pet-use-pipenv-p :and-return-value nil)
868 | (spy-on 'locate-dominating-file :and-return-value project-root)
869 | (expect (pet-virtualenv-root) :to-equal venv-virtualenv)
870 | (expect (assoc-default project-root pet-project-virtualenv-cache) :to-equal venv-virtualenv))
871 |
872 | (it "should return the absolute path of the virtualenv for a project using `pyenv'"
873 | (spy-on 'pet-use-conda-p :and-return-value nil)
874 | (spy-on 'pet-use-poetry-p :and-return-value nil)
875 | (spy-on 'pet-use-pipenv-p :and-return-value nil)
876 | (spy-on 'locate-dominating-file :and-return-value nil)
877 | (spy-on 'pet-use-pyenv-p :and-return-value pyenv-path)
878 | (spy-on 'pet-python-version-path :and-return-value "/home/user/project/.python-version")
879 | (spy-on 'call-process :and-call-fake (lambda (&rest _) (insert pyenv-virtualenv) 0))
880 | (spy-on 'file-truename :and-call-fake (lambda (name) (when (equal name pyenv-virtualenv) pyenv-virtualenv-truename)))
881 | (expect (pet-virtualenv-root) :to-equal pyenv-virtualenv-truename)
882 | (expect (assoc-default project-root pet-project-virtualenv-cache) :to-equal pyenv-virtualenv-truename)
883 | (expect 'call-process :to-have-been-called-with pyenv-path nil t nil "prefix"))
884 |
885 | (it "should return the absolute path of the virtualenv for a project if the root is found in cache"
886 | (setq pet-project-virtualenv-cache `((,project-root . "/home/user/.venvs/env/")))
887 | (expect (pet-virtualenv-root) :to-equal "/home/user/.venvs/env/")))
888 |
889 | (describe "pet-flycheck-python-pylint-find-pylintrc"
890 | :var ((old-default-directory default-directory)
891 | (home (getenv "HOME"))
892 | (orig-getenv (symbol-function 'getenv))
893 | (process-environment (copy-sequence process-environment)))
894 |
895 | (before-each
896 | (setenv "HOME" "/home/user/")
897 | (setq-local default-directory "~/project/src/"))
898 |
899 | (after-each
900 | (setenv "HOME" home)
901 | (setq-local default-directory old-default-directory))
902 |
903 | (it "should not error when run inside a non-file buffer"
904 | (expect (with-temp-buffer (pet-flycheck-python-pylint-find-pylintrc)) :not :to-throw))
905 |
906 | (it "should return the absolute path to `pylintrc' from `default-directory'"
907 | (spy-on 'file-exists-p :and-call-fake (lambda (path) (equal path "~/project/src/.pylintrc")))
908 | (expect (pet-flycheck-python-pylint-find-pylintrc) :to-equal "/home/user/project/src/.pylintrc"))
909 |
910 | (it "should return the absolute path to `pylintrc' from a python package directory hierarchy"
911 | (spy-on 'buffer-file-name :and-return-value "/home/user/project/src/foo.py")
912 | (spy-on 'file-exists-p :and-call-fake (lambda (path) (equal path "/home/user/project/src/__init__.py")))
913 | (spy-on 'locate-dominating-file :and-call-fake (lambda (file name)
914 | (when (equal name ".pylintrc")
915 | "/home/user/project/src/")))
916 | (expect (pet-flycheck-python-pylint-find-pylintrc) :to-equal "/home/user/project/src/.pylintrc"))
917 |
918 | (it "should return the absolute path to `pylintrc' from `PYLINTRC'"
919 | (spy-on 'buffer-file-name :and-return-value "/home/user/project/src/foo.py")
920 | (spy-on 'file-exists-p :and-call-fake (lambda (path) (equal path "/home/user/project/pyproject.toml")))
921 | (spy-on 'getenv :and-call-fake (lambda (name)
922 | (if (equal name "PYLINTRC")
923 | "/home/user/project/pyproject.toml"
924 | (funcall orig-getenv name))))
925 |
926 | (expect (pet-flycheck-python-pylint-find-pylintrc) :to-equal "/home/user/project/pyproject.toml"))
927 |
928 | (it "should return the absolute path to `pylintrc' from `XDG_CONFIG_HOME'"
929 | (spy-on 'buffer-file-name :and-return-value "/home/user/project/src/foo.py")
930 | (spy-on 'file-exists-p :and-call-fake (lambda (path) (equal path "/home/user/.config/pylintrc")))
931 | (spy-on 'getenv :and-call-fake (lambda (name)
932 | (if (equal name "XDG_CONFIG_HOME")
933 | "/home/user/.config"
934 | (funcall orig-getenv name))))
935 |
936 | (expect (pet-flycheck-python-pylint-find-pylintrc) :to-equal "/home/user/.config/pylintrc"))
937 |
938 | (it "should return the absolute path to `pylintrc' from `HOME'"
939 | (spy-on 'file-exists-p :and-call-fake (lambda (path) (equal path "/home/user/.pylintrc")))
940 | (expect (pet-flycheck-python-pylint-find-pylintrc) :to-equal "/home/user/.pylintrc"))
941 |
942 | (it "should return the absolute path to `pylintrc' from `/etc'"
943 | (spy-on 'file-exists-p :and-return-value nil)
944 | (expect (pet-flycheck-python-pylint-find-pylintrc) :to-equal "/etc/pylintrc")))
945 |
946 | (describe "pet-flycheck-toggle-local-vars"
947 | :var ((old-default-directory default-directory)
948 | (home (getenv "HOME"))
949 | (orig-getenv (symbol-function 'getenv))
950 | (process-environment (copy-sequence process-environment)))
951 |
952 | (before-each
953 | (setenv "HOME" "/home/user/")
954 | (setq-local default-directory "/home/user/")
955 | (defvar flycheck-mode t)
956 | (spy-on 'getenv :and-call-fake
957 | (lambda (name)
958 | (unless (member name '("XDG_CONFIG_HOME"))
959 | (funcall orig-getenv name)))))
960 |
961 | (after-each
962 | (setenv "HOME" home)
963 | (setq-local default-directory old-default-directory)
964 | (makunbound 'flycheck-mode)
965 | (unintern 'flycheck-mode))
966 |
967 | (it "should set `flycheck' Python checkers variables to buffer-local when `flycheck-mode' is t"
968 | (spy-on 'pet-flycheck-python-pylint-find-pylintrc :and-return-value "/etc/pylintrc")
969 | (spy-on 'pet-executable-find :and-call-fake (lambda (name)
970 | (pcase name
971 | ("flake8" "/home/user/project/.venv/bin/flake8")
972 | ("pylint" "/home/user/project/.venv/bin/pylint")
973 | ("mypy" "/home/user/project/.venv/bin/mypy")
974 | ("python" "/home/user/project/.venv/bin/python")
975 | ("pyright" "/home/user/project/.venv/bin/pyright")
976 | ("ruff" "/home/user/project/.venv/bin/ruff"))))
977 | (spy-on 'derived-mode-p :and-return-value t)
978 | (pet-flycheck-toggle-local-vars)
979 | (expect flycheck-python-mypy-config :to-equal `("mypy.ini" ".mypy.ini" "pyproject.toml" "setup.cfg" "/home/user/.config/mypy/config" "/home/user/.mypy.ini"))
980 | (expect flycheck-pylintrc :to-equal "/etc/pylintrc")
981 | (expect flycheck-python-flake8-executable :to-equal "/home/user/project/.venv/bin/flake8")
982 | (expect flycheck-python-pylint-executable :to-equal "/home/user/project/.venv/bin/pylint")
983 | (expect flycheck-python-mypy-executable :to-equal "/home/user/project/.venv/bin/mypy")
984 | (expect flycheck-python-mypy-python-executable :to-equal "/home/user/project/.venv/bin/python")
985 | (expect flycheck-python-pyright-executable :to-equal "/home/user/project/.venv/bin/pyright")
986 | (expect flycheck-python-pycompile-executable :to-equal flycheck-python-mypy-python-executable)
987 | (expect flycheck-python-ruff-executable :to-equal "/home/user/project/.venv/bin/ruff"))
988 |
989 | (it "should reset `flycheck' Python checkers variables to default when `flycheck-mode' is nil"
990 | (spy-on 'pet-flycheck-python-pylint-find-pylintrc :and-return-value "/etc/pylintrc")
991 | (spy-on 'pet-executable-find :and-call-fake (lambda (name)
992 | (pcase name
993 | ("flake8" "/home/user/project/.venv/bin/flake8")
994 | ("pylint" "/home/user/project/.venv/bin/pylint")
995 | ("mypy" "/home/user/project/.venv/bin/mypy")
996 | ("python" "/home/user/project/.venv/bin/python")
997 | ("pyright" "/home/user/project/.venv/bin/pyright")
998 | ("ruff" "/home/user/project/.venv/bin/ruff"))))
999 | (spy-on 'derived-mode-p :and-return-value t)
1000 | (pet-flycheck-toggle-local-vars)
1001 | (setq-local flycheck-mode nil)
1002 |
1003 | (pet-flycheck-toggle-local-vars)
1004 | (expect (local-variable-p 'flycheck-pylintrc) :to-be nil)
1005 | (expect (local-variable-p 'flycheck-python-flake8-executable) :to-be nil)
1006 | (expect (local-variable-p 'flycheck-python-pylint-executable) :to-be nil)
1007 | (expect (local-variable-p 'flycheck-python-mypy-executable) :to-be nil)
1008 | (expect (local-variable-p 'flycheck-python-mypy-python-executable) :to-be nil)
1009 | (expect (local-variable-p 'flycheck-python-pyright-executable) :to-be nil)
1010 | (expect (local-variable-p 'flycheck-python-pycompile-executable) :to-be nil)
1011 | (expect (local-variable-p 'flycheck-python-ruff-executable) :to-be nil)
1012 |
1013 | (kill-local-variable 'flycheck-mode)))
1014 |
1015 | (describe "pet-flycheck-setup"
1016 | :var ((old-default-directory default-directory)
1017 | (home (getenv "HOME"))
1018 | (process-environment (copy-sequence process-environment)))
1019 |
1020 | (before-each
1021 | (setenv "HOME" "/home/user/")
1022 | (setq-local default-directory "/home/user/")
1023 | (defun flycheck-checker-get (checker property)))
1024 |
1025 | (after-each
1026 | (setenv "HOME" home)
1027 | (setq-local default-directory old-default-directory)
1028 | (fmakunbound 'flycheck-checker-get)
1029 | (unintern 'flycheck-checker-get))
1030 |
1031 | (it "should add `pet-flycheck-toggle-local-vars' to `flycheck-mode-hook'"
1032 | (pet-flycheck-setup)
1033 | (expect (member 'pet-flycheck-toggle-local-vars flycheck-mode-hook) :to-be-truthy))
1034 |
1035 | (it "should advice `flycheck-python-find-project-root'"
1036 | (pet-flycheck-setup)
1037 | (expect
1038 | (advice-member-p 'pet-flycheck-python-find-project-root-advice 'flycheck-python-find-project-root)
1039 | :to-be-truthy)))
1040 |
1041 | (describe "pet-flycheck-teardown"
1042 | (before-each
1043 | (pet-flycheck-setup)
1044 | (pet-flycheck-teardown)
1045 | (defun flycheck-checker-get (checker property)))
1046 |
1047 | (after-each
1048 | (fmakunbound 'flycheck-checker-get)
1049 | (unintern 'flycheck-checker-get))
1050 |
1051 | (it "should remove advice on `flycheck-python-find-project-root'"
1052 | (expect
1053 | (advice-member-p 'pet-flycheck-python-find-project-root-advice 'flycheck-python-find-project-root)
1054 | :not :to-be-truthy))
1055 |
1056 | (it "should remove `pet-flycheck-toggle-local-vars' from `flycheck-mode-hook'"
1057 | (expect (member 'pet-flycheck-toggle-local-vars flycheck-mode-hook) :not :to-be-truthy))
1058 |
1059 | (it "should reset `flycheck' Python checkers variables to default"
1060 | (expect (local-variable-p 'flycheck-pylintrc) :not :to-be-truthy)
1061 | (expect (local-variable-p 'flycheck-python-flake8-executable) :not :to-be-truthy)
1062 | (expect (local-variable-p 'flycheck-python-pylint-executable) :not :to-be-truthy)
1063 | (expect (local-variable-p 'flycheck-python-mypy-executable) :not :to-be-truthy)
1064 | (expect (local-variable-p 'flycheck-python-pyright-executable) :not :to-be-truthy)
1065 | (expect (local-variable-p 'flycheck-python-pycompile-executable) :not :to-be-truthy)
1066 | (expect (local-variable-p 'flycheck-python-ruff-executable) :not :to-be-truthy)))
1067 |
1068 | (describe "pet-eglot--executable-find-advice"
1069 | (it "should delegate to `pet-executable-find' for Python LSP servers"
1070 | (spy-on 'eglot--executable-find :and-call-fake (lambda (&rest args) (string-join args " ")))
1071 | (spy-on 'pet-executable-find :and-call-fake 'identity)
1072 |
1073 | (expect (pet-eglot--executable-find-advice 'eglot--executable-find "pylsp") :to-equal "pylsp")
1074 | (expect (spy-context-return-value (spy-calls-most-recent 'pet-executable-find)) :to-equal "pylsp")
1075 | (expect 'eglot--executable-find :not :to-have-been-called)
1076 |
1077 | (expect (pet-eglot--executable-find-advice 'eglot--executable-find "pyls") :to-equal "pyls")
1078 | (expect (spy-context-return-value (spy-calls-most-recent 'pet-executable-find)) :to-equal "pyls")
1079 | (expect 'eglot--executable-find :not :to-have-been-called)
1080 |
1081 | (expect (pet-eglot--executable-find-advice 'eglot--executable-find "pyright-langserver") :to-equal "pyright-langserver")
1082 | (expect (spy-context-return-value (spy-calls-most-recent 'pet-executable-find)) :to-equal "pyright-langserver")
1083 | (expect 'eglot--executable-find :not :to-have-been-called)
1084 |
1085 | (expect (pet-eglot--executable-find-advice 'eglot--executable-find "jedi-language-server") :to-equal "jedi-language-server")
1086 | (expect (spy-context-return-value (spy-calls-most-recent 'pet-executable-find)) :to-equal "jedi-language-server")
1087 | (expect 'eglot--executable-find :not :to-have-been-called)
1088 |
1089 | (expect (pet-eglot--executable-find-advice 'eglot--executable-find "ruff-lsp") :to-equal "ruff-lsp")
1090 | (expect (spy-context-return-value (spy-calls-most-recent 'pet-executable-find)) :to-equal "ruff-lsp")
1091 | (expect 'eglot--executable-find :not :to-have-been-called)
1092 |
1093 | (expect (pet-eglot--executable-find-advice 'eglot--executable-find "sh" "-c") :to-equal "sh -c")
1094 | (expect 'eglot--executable-find :to-have-been-called-with "sh" "-c")))
1095 |
1096 | (describe "pet-eglot--workspace-configuration-plist-advice"
1097 | (before-each
1098 | (spy-on 'jsonrpc--process))
1099 |
1100 | (it "should pass canonicalized PATH to FN if it's a directory"
1101 | (spy-on 'mock-eglot--workspace-configuration-plist)
1102 | (spy-on 'process-command :and-return-value '("/usr/bin/jedi-language-server"))
1103 | (spy-on 'file-directory-p :and-return-value t)
1104 |
1105 | (pet-eglot--workspace-configuration-plist-advice
1106 | 'mock-eglot--workspace-configuration-plist
1107 | "server" "/home/users/project")
1108 |
1109 | (expect 'mock-eglot--workspace-configuration-plist :to-have-been-called-with "server" "/home/users/project/"))
1110 |
1111 | (it "should pass PATH to FN directly if it's a not directory"
1112 | (spy-on 'mock-eglot--workspace-configuration-plist)
1113 | (spy-on 'process-command :and-return-value '("/usr/bin/jedi-language-server"))
1114 | (spy-on 'file-directory-p :and-return-value nil)
1115 |
1116 | (pet-eglot--workspace-configuration-plist-advice
1117 | 'mock-eglot--workspace-configuration-plist
1118 | "server" "/home/users/project/file")
1119 |
1120 | (expect 'mock-eglot--workspace-configuration-plist :to-have-been-called-with "server" "/home/users/project/file"))
1121 |
1122 | (it "should return `nil' when no dir local variables and pet server initialization options"
1123 | (spy-on 'mock-eglot--workspace-configuration-plist)
1124 | (spy-on 'process-command :and-return-value '("/usr/bin/some-lsp-server"))
1125 |
1126 | (expect (pet-eglot--workspace-configuration-plist-advice
1127 | 'mock-eglot--workspace-configuration-plist
1128 | "server")
1129 | :not :to-be-truthy)
1130 |
1131 | (expect 'mock-eglot--workspace-configuration-plist :to-have-been-called))
1132 |
1133 | (it "should return pet server initialization options when no dir local variables"
1134 | (spy-on 'mock-eglot--workspace-configuration-plist)
1135 | (spy-on 'process-command :and-return-value '("/usr/bin/pyright-langserver"))
1136 | (spy-on 'pet-lookup-eglot-server-initialization-options
1137 | :and-return-value
1138 | '(:python
1139 | (:pythonPath
1140 | "/usr/bin/python"
1141 | :venvPath
1142 | "/home/user/project/")))
1143 |
1144 | (expect (pet-eglot--workspace-configuration-plist-advice
1145 | 'mock-eglot--workspace-configuration-plist
1146 | "server")
1147 | :to-equal '(:python
1148 | (:pythonPath
1149 | "/usr/bin/python"
1150 | :venvPath
1151 | "/home/user/project/")))
1152 |
1153 | (expect 'mock-eglot--workspace-configuration-plist :to-have-been-called))
1154 |
1155 | (it "should return dir local variables when pet server initialization options"
1156 | (spy-on 'mock-eglot--workspace-configuration-plist
1157 | :and-return-value
1158 | '(:python
1159 | (:pythonPath
1160 | "/usr/bin/python"
1161 | :venvPath
1162 | "/home/user/project/")))
1163 | (spy-on 'process-command :and-return-value '("/usr/bin/pyright-langserver"))
1164 | (spy-on 'pet-lookup-eglot-server-initialization-options)
1165 |
1166 | (expect (pet-eglot--workspace-configuration-plist-advice
1167 | 'mock-eglot--workspace-configuration-plist
1168 | "server")
1169 | :to-equal '(:python
1170 | (:pythonPath
1171 | "/usr/bin/python"
1172 | :venvPath
1173 | "/home/user/project/")))
1174 |
1175 | (expect 'mock-eglot--workspace-configuration-plist :to-have-been-called))
1176 |
1177 | (it "should return dir local variables and pet server initialization options when both available"
1178 | (spy-on 'mock-eglot--workspace-configuration-plist
1179 | :and-return-value
1180 | '(:python
1181 | (:pythonPath
1182 | "/usr/bin/python")))
1183 | (spy-on 'process-command :and-return-value '("/usr/bin/pyright-langserver"))
1184 | (spy-on 'pet-lookup-eglot-server-initialization-options
1185 | :and-return-value
1186 | '(:python
1187 | (:venvPath
1188 | "/home/user/project/")))
1189 |
1190 | (expect (pet-eglot--workspace-configuration-plist-advice
1191 | 'mock-eglot--workspace-configuration-plist
1192 | "server")
1193 | :to-equal '(:python
1194 | (:pythonPath
1195 | "/usr/bin/python"
1196 | :venvPath
1197 | "/home/user/project/")))
1198 |
1199 | (expect 'mock-eglot--workspace-configuration-plist :to-have-been-called)))
1200 |
1201 | (describe "pet-eglot--guess-contact-advice"
1202 | (it "should return output unchanged when there's no pet server initialization"
1203 | (spy-on 'eglot--guess-contact :and-return-value
1204 | '((python-mode python-ts-mode)
1205 | "project"
1206 | 'eglot-lsp-server
1207 | ("pyright-langserver" "--stdio")
1208 | "python-ts"))
1209 |
1210 | (spy-on 'pet-lookup-eglot-server-initialization-options)
1211 |
1212 | (expect
1213 | (pet-eglot--guess-contact-advice 'eglot--guess-contact)
1214 | :to-equal
1215 | '((python-mode python-ts-mode)
1216 | "project"
1217 | 'eglot-lsp-server
1218 | ("pyright-langserver" "--stdio")
1219 | "python-ts")))
1220 |
1221 | (it "should return contact with default server initialization when there's no pet server initialization"
1222 | (spy-on 'eglot--guess-contact :and-return-value
1223 | '((python-mode python-ts-mode)
1224 | "project"
1225 | 'eglot-lsp-server
1226 | ("jedi-language-server"
1227 | :initializationOptions
1228 | (:jedi
1229 | (:executable
1230 | (:args ["--ws"]))))
1231 | "python-ts"))
1232 |
1233 | (spy-on 'pet-lookup-eglot-server-initialization-options :and-return-value
1234 | '(:jedi
1235 | (:executable
1236 | (:command
1237 | "/home/user/project/env/bin/jedi-language-server")
1238 | :workspace
1239 | (:environmentPath
1240 | "/home/user/project/env/bin/python"))))
1241 |
1242 | (expect
1243 | (pet-eglot--guess-contact-advice 'eglot--guess-contact)
1244 | :to-equal
1245 | '((python-mode python-ts-mode)
1246 | "project"
1247 | 'eglot-lsp-server
1248 | ("jedi-language-server"
1249 | :initializationOptions
1250 | (:jedi
1251 | (:executable
1252 | (:args
1253 | ["--ws"]
1254 | :command
1255 | "/home/user/project/env/bin/jedi-language-server")
1256 | :workspace
1257 | (:environmentPath
1258 | "/home/user/project/env/bin/python"))))
1259 | "python-ts"))))
1260 |
1261 | (describe "pet-lookup-eglot-server-initialization-options"
1262 | (before-each
1263 | (spy-on 'pet-virtualenv-root :and-return-value "/home/user/project/")
1264 | (spy-on 'pet-executable-find :and-call-fake
1265 | (lambda (command)
1266 | (assoc-default command
1267 | '(("flake8" . "/usr/bin/flake8")
1268 | ("pylint" . "/usr/bin/pylint")
1269 | ("python" . "/usr/bin/python")
1270 | ("jedi-language-server" . "/home/user/.local/bin/jedi-language-server")
1271 | ("ruff" . "/usr/bin/ruff"))))))
1272 |
1273 | (it "should return eglot initialization options for pylsp"
1274 | (expect (pet-lookup-eglot-server-initialization-options "/home/user/.local/bin/pylsp") :to-equal
1275 | '(:pylsp
1276 | (:plugins
1277 | (:jedi
1278 | (:environment
1279 | "/home/user/project/")
1280 | :ruff
1281 | (:executable
1282 | "/usr/bin/ruff")
1283 | :pylsp_mypy
1284 | (:overrides
1285 | ["--python-executable" "/usr/bin/python" t])
1286 | :flake8
1287 | (:executable
1288 | "/usr/bin/flake8")
1289 | :pylint
1290 | (:executable
1291 | "/usr/bin/pylint"))))))
1292 |
1293 | (it "should return eglot initialization options for pyls"
1294 | (expect (pet-lookup-eglot-server-initialization-options "/home/user/.local/bin/pyls") :to-equal
1295 | '(:pyls
1296 | (:plugins
1297 | (:jedi
1298 | (:environment
1299 | "/home/user/project/")
1300 | :pylint
1301 | (:executable
1302 | "/usr/bin/pylint"))))))
1303 |
1304 | (it "should return eglot initialization options for pyright"
1305 | (expect (pet-lookup-eglot-server-initialization-options "/home/user/.local/bin/pyright-langserver") :to-equal
1306 | `(:python
1307 | (:pythonPath
1308 | "/usr/bin/python"
1309 | :venvPath
1310 | "/home/user/project/"))))
1311 |
1312 | (it "should return eglot initialization options for jedi-language-server"
1313 | (expect (pet-lookup-eglot-server-initialization-options "jedi-language-server") :to-equal
1314 | '(:jedi
1315 | (:executable
1316 | (:command
1317 | "/home/user/.local/bin/jedi-language-server")
1318 | :workspace
1319 | (:environmentPath
1320 | "/usr/bin/python")))))
1321 |
1322 | (it "should return eglot initialization options for ruff-lsp"
1323 | (expect (pet-lookup-eglot-server-initialization-options "ruff-lsp") :to-equal
1324 | '(:settings
1325 | (:interpreter
1326 | "/usr/bin/python"
1327 | :path
1328 | "/usr/bin/ruff")))))
1329 |
1330 | (describe "pet-merge-eglot-initialization-options"
1331 | (it "should deeply merge 2 plists"
1332 | (expect
1333 | (pet-merge-eglot-initialization-options
1334 | '(:a (:b [1 2] :c 0 :d "hello" :f :json-null))
1335 | '(:a (:b [3 4] :c 9 :e "world" :g :json-false)))
1336 | :to-equal
1337 | '(:a (:b [1 2 3 4] :c 9 :d "hello" :f :json-null :e "world" :g :json-false)))))
1338 |
1339 | (describe "pet-eglot-setup"
1340 | (before-each
1341 | (pet-eglot-setup))
1342 |
1343 | (after-each
1344 | (pet-eglot-teardown))
1345 |
1346 | (it "should advice eglot functions"
1347 | (pet-eglot-setup)
1348 | (expect (advice-member-p 'pet-eglot--workspace-configuration-plist-advice 'eglot--workspace-configuration-plist) :to-be-truthy)
1349 | (expect (advice-member-p 'pet-eglot--executable-find-advice 'eglot--executable-find) :to-be-truthy)
1350 | (expect (advice-member-p 'pet-eglot--guess-contact-advice 'eglot--guess-contact) :to-be-truthy)))
1351 |
1352 | (describe "pet-eglot-teardown"
1353 | (it "should remove `pet' advices from eglot functions"
1354 | (pet-eglot-setup)
1355 | (pet-eglot-teardown)
1356 | (expect (advice-member-p 'pet-eglot--workspace-configuration-plist-advice 'eglot--workspace-configuration-plist) :to-be nil)
1357 | (expect (advice-member-p 'pet-eglot--executable-find-advice 'eglot--executable-find) :to-be nil)
1358 | (expect (advice-member-p 'pet-eglot--guess-contact-advice 'eglot--guess-contact) :to-be nil)))
1359 |
1360 | (describe "pet-dape-setup"
1361 | (before-each
1362 | (spy-on 'pet-find-file-from-project-root-recursively)
1363 | (spy-on 'pet-executable-find :and-return-value "/usr/bin/python"))
1364 |
1365 | (it "should set up buffer local variable dape-command when no __main__.py is found"
1366 | (pet-dape-setup)
1367 | (expect (local-variable-p 'dape-command) :to-be-truthy)
1368 | (expect dape-command :to-equal '(debugpy command "/usr/bin/python")))
1369 |
1370 | (it "should set up buffer local variable dape-command when a __main__.py is found"
1371 | (spy-on 'pet-find-file-from-project-root-recursively :and-return-value "/home/user/project/src/foo/bar/__main__.py")
1372 | (spy-on 'file-exists-p :and-call-fake
1373 | (lambda (path)
1374 | (member path
1375 | '("/home/user/project/src/foo/bar/__init__.py" "/home/user/project/src/foo/__init__.py"))))
1376 | (pet-dape-setup)
1377 | (expect (local-variable-p 'dape-command) :to-be-truthy)
1378 | (expect dape-command :to-equal '(debugpy-module command "/usr/bin/python" :module "foo.bar")))
1379 |
1380 | (it "should set up buffer local variable dape-cwd-function"
1381 | (pet-dape-setup)
1382 | (expect (local-variable-p 'dape-cwd-function) :to-be-truthy)
1383 | (expect dape-cwd-function :to-equal 'pet-project-root)))
1384 |
1385 | (describe "pet-dape-teardown"
1386 | (it "should tear down bufer local variables for dape"
1387 | (spy-on 'pet-find-file-from-project-root-recursively)
1388 | (spy-on 'pet-executable-find :and-return-value "/usr/bin/python")
1389 | (pet-dape-setup)
1390 | (pet-dape-teardown)
1391 | (expect (local-variable-p 'dape-command) :not :to-be-truthy)
1392 | (expect (local-variable-p 'dape-cwd-function) :not :to-be-truthy)))
1393 |
1394 | (describe "pet-buffer-local-vars-setup"
1395 | (after-each
1396 | (kill-local-variable 'python-shell-interpreter)
1397 | (kill-local-variable 'python-shell-virtualenv-root)
1398 | (kill-local-variable 'lsp-jedi-executable-command)
1399 | (kill-local-variable 'lsp-pyls-plugins-jedi-environment)
1400 | (kill-local-variable 'lsp-pylsp-plugins-jedi-environment)
1401 | (kill-local-variable 'lsp-pyright-venv-path)
1402 | (kill-local-variable 'lsp-pyright-python-executable-cmd)
1403 | (kill-local-variable 'lsp-ruff-server-command)
1404 | (kill-local-variable 'lsp-ruff-python-path)
1405 | (kill-local-variable 'dap-python-executable)
1406 | (kill-local-variable 'dap-variables-project-root-function)
1407 | (kill-local-variable 'python-pytest-executable)
1408 | (kill-local-variable 'python-black-command)
1409 | (kill-local-variable 'python-isort-command)
1410 | (kill-local-variable 'blacken-executable)
1411 | (kill-local-variable 'yapfify-executable)
1412 | (kill-local-variable 'ruff-format-command)
1413 | (kill-local-variable 'py-autopep8-command))
1414 |
1415 | (it "should set up all buffer local variables for supported packages"
1416 | (spy-on 'pet-executable-find :and-call-fake
1417 | (lambda (exec)
1418 | (pcase exec
1419 | ("python"
1420 | "/usr/bin/python")
1421 | ("jedi-language-server"
1422 | "/usr/bin/jedi-language-server")
1423 | ("pytest"
1424 | "/usr/bin/pytest")
1425 | ("black"
1426 | "/usr/bin/black")
1427 | ("isort"
1428 | "/usr/bin/isort")
1429 | ("yapf"
1430 | "/usr/bin/yapf")
1431 | ("ruff"
1432 | "/usr/bin/ruff")
1433 | ("autopep8"
1434 | "/usr/bin/autopep8"))))
1435 | (spy-on 'pet-virtualenv-root :and-return-value "/home/user/project/.venv/")
1436 | (spy-on 'pet-flycheck-setup)
1437 | (spy-on 'pet-eglot-setup)
1438 | (spy-on 'pet-dape-setup)
1439 |
1440 | (pet-buffer-local-vars-setup)
1441 |
1442 | (expect 'pet-flycheck-setup :to-have-been-called)
1443 | (expect 'pet-eglot-setup :to-have-been-called)
1444 | (expect 'pet-dape-setup :to-have-been-called)
1445 |
1446 | (expect (local-variable-p 'python-shell-interpreter) :to-be-truthy)
1447 | (expect (local-variable-p 'python-shell-virtualenv-root) :to-be-truthy)
1448 | (expect (local-variable-p 'lsp-jedi-executable-command) :to-be-truthy)
1449 | (expect (local-variable-p 'lsp-pyls-plugins-jedi-environment) :to-be-truthy)
1450 | (expect (local-variable-p 'lsp-pylsp-plugins-jedi-environment) :to-be-truthy)
1451 | (expect (local-variable-p 'lsp-pyright-venv-path) :to-be-truthy)
1452 | (expect (local-variable-p 'lsp-pyright-python-executable-cmd) :to-be-truthy)
1453 | (expect (local-variable-p 'lsp-ruff-server-command) :to-be-truthy)
1454 | (expect (local-variable-p 'lsp-ruff-python-path) :to-be-truthy)
1455 | (expect (local-variable-p 'dap-python-executable) :to-be-truthy)
1456 | (expect (local-variable-p 'dap-variables-project-root-function) :to-be-truthy)
1457 | (expect (local-variable-p 'python-pytest-executable) :to-be-truthy)
1458 | (expect (local-variable-p 'python-black-command) :to-be-truthy)
1459 | (expect (local-variable-p 'python-isort-command) :to-be-truthy)
1460 | (expect (local-variable-p 'blacken-executable) :to-be-truthy)
1461 | (expect (local-variable-p 'yapfify-executable) :to-be-truthy)
1462 | (expect (local-variable-p 'ruff-format-command) :to-be-truthy)
1463 | (expect (local-variable-p 'py-autopep8-command) :to-be-truthy)
1464 |
1465 | (expect python-shell-interpreter :to-equal "/usr/bin/python")
1466 | (expect python-shell-virtualenv-root :to-equal "/home/user/project/.venv/")
1467 | (expect lsp-jedi-executable-command :to-equal "/usr/bin/jedi-language-server")
1468 | (expect lsp-pyls-plugins-jedi-environment :to-equal "/home/user/project/.venv/")
1469 | (expect lsp-pylsp-plugins-jedi-environment :to-equal "/home/user/project/.venv/")
1470 | (expect lsp-pyright-venv-path :to-equal "/home/user/project/.venv/")
1471 | (expect lsp-pyright-python-executable-cmd :to-equal "/usr/bin/python")
1472 | (expect lsp-ruff-server-command :to-equal '("/usr/bin/ruff" "server"))
1473 | (expect lsp-ruff-python-path :to-equal "/usr/bin/python")
1474 | (expect dap-python-executable :to-equal "/usr/bin/python")
1475 | (expect dap-variables-project-root-function :to-equal #'pet-project-root)
1476 | (expect python-pytest-executable :to-equal "/usr/bin/pytest")
1477 | (expect python-black-command :to-equal "/usr/bin/black")
1478 | (expect python-isort-command :to-equal "/usr/bin/isort")
1479 | (expect blacken-executable :to-equal "/usr/bin/black")
1480 | (expect yapfify-executable :to-equal "/usr/bin/yapf")
1481 | (expect ruff-format-command :to-equal "/usr/bin/ruff")
1482 | (expect yapfify-executable :to-equal "/usr/bin/yapf")
1483 | (expect py-autopep8-command :to-equal "/usr/bin/autopep8")))
1484 |
1485 | (describe "pet-buffer-local-vars-teardown"
1486 | (after-each
1487 | (kill-local-variable 'python-shell-interpreter)
1488 | (kill-local-variable 'python-shell-virtualenv-root)
1489 | (kill-local-variable 'lsp-jedi-executable-command)
1490 | (kill-local-variable 'lsp-pyls-plugins-jedi-environment)
1491 | (kill-local-variable 'lsp-pylsp-plugins-jedi-environment)
1492 | (kill-local-variable 'lsp-pyright-venv-path)
1493 | (kill-local-variable 'lsp-pyright-python-executable-cmd)
1494 | (kill-local-variable 'lsp-ruff-server-command)
1495 | (kill-local-variable 'lsp-ruff-python-path)
1496 | (kill-local-variable 'dap-python-executable)
1497 | (kill-local-variable 'dap-variables-project-root-function)
1498 | (kill-local-variable 'python-pytest-executable)
1499 | (kill-local-variable 'python-black-command)
1500 | (kill-local-variable 'python-isort-command)
1501 | (kill-local-variable 'blacken-executable)
1502 | (kill-local-variable 'yapfify-executable)
1503 | (kill-local-variable 'ruff-format-command)
1504 | (kill-local-variable 'py-autopep8-command))
1505 |
1506 | (it "should reset all buffer local variables for supported packages to default"
1507 | (spy-on 'pet-flycheck-teardown)
1508 | (spy-on 'pet-eglot-teardown)
1509 | (spy-on 'pet-dape-teardown)
1510 |
1511 | (pet-buffer-local-vars-setup)
1512 | (pet-buffer-local-vars-teardown)
1513 |
1514 | (expect 'pet-flycheck-teardown :to-have-been-called)
1515 | (expect 'pet-eglot-teardown :to-have-been-called)
1516 | (expect 'pet-dape-teardown :to-have-been-called)
1517 |
1518 | (expect (local-variable-p 'python-shell-interpreter) :not :to-be-truthy)
1519 | (expect (local-variable-p 'python-shell-virtualenv-root) :not :to-be-truthy)
1520 | (expect (local-variable-p 'lsp-jedi-executable-command) :not :to-be-truthy)
1521 | (expect (local-variable-p 'lsp-pyls-plugins-jedi-environment) :not :to-be-truthy)
1522 | (expect (local-variable-p 'lsp-pylsp-plugins-jedi-environment) :not :to-be-truthy)
1523 | (expect (local-variable-p 'lsp-pyright-venv-path) :not :to-be-truthy)
1524 | (expect (local-variable-p 'lsp-pyright-python-executable-cmd) :not :to-be-truthy)
1525 | (expect (local-variable-p 'lsp-ruff-server-command) :not :to-be-truthy)
1526 | (expect (local-variable-p 'lsp-ruff-python-path) :not :to-be-truthy)
1527 | (expect (local-variable-p 'dap-python-executable) :not :to-be-truthy)
1528 | (expect (local-variable-p 'dap-variables-project-root-function) :not :to-be-truthy)
1529 | (expect (local-variable-p 'python-pytest-executable) :not :to-be-truthy)
1530 | (expect (local-variable-p 'python-black-command) :not :to-be-truthy)
1531 | (expect (local-variable-p 'python-isort-command) :not :to-be-truthy)
1532 | (expect (local-variable-p 'blacken-executable) :not :to-be-truthy)
1533 | (expect (local-variable-p 'yapfify-executable) :not :to-be-truthy)
1534 | (expect (local-variable-p 'ruff-format-command) :not :to-be-truthy)
1535 | (expect (local-variable-p 'py-autopep8-command) :not :to-be-truthy)))
1536 |
1537 | (describe "pet-verify-setup"
1538 | :var ((old-default-directory default-directory)
1539 | (home (getenv "HOME"))
1540 | (orig-getenv (symbol-function 'getenv))
1541 | (process-environment (copy-sequence process-environment)))
1542 |
1543 | (before-each
1544 | (setenv "HOME" "/home/user/")
1545 | (setq-local default-directory "~/project/"))
1546 |
1547 | (after-each
1548 | (setenv "HOME" home)
1549 | (setq-local default-directory old-default-directory))
1550 |
1551 | (it "should error when not in python mode"
1552 | (expect (pet-verify-setup) :to-throw 'user-error))
1553 |
1554 | (it "should display unbound values"
1555 | (with-temp-buffer
1556 | (python-mode)
1557 | (pet-verify-setup)
1558 | (expect
1559 | (with-current-buffer "*pet info*"
1560 | (re-search-forward "lsp-jedi-executable-command:\s+\\(.+\\)")
1561 | (match-string 1))
1562 | :to-equal "unbound")))
1563 |
1564 | (it "should display bound values"
1565 | (with-temp-buffer
1566 | (python-mode)
1567 | (pet-verify-setup)
1568 | (expect
1569 | (with-current-buffer "*pet info*"
1570 | (re-search-forward "python-shell-interpreter:\s+\\(.+\\)")
1571 | (match-string 1))
1572 | :to-equal (if (< emacs-major-version 28) "python" "python3"))))
1573 |
1574 | (it "should display list as comma-separated values"
1575 | (spy-on 'pet-flycheck-python-pylint-find-pylintrc)
1576 | (spy-on 'pet-executable-find)
1577 | (spy-on 'getenv :and-call-fake (lambda (name)
1578 | (unless (equal name "XDG_CONFIG_HOME")
1579 | (funcall orig-getenv name))))
1580 | (with-temp-buffer
1581 | (python-mode)
1582 | (setq-local flycheck-mode t)
1583 | (pet-flycheck-toggle-local-vars)
1584 | (pet-verify-setup)
1585 | (expect
1586 | (split-string (with-current-buffer "*pet info*"
1587 | (re-search-forward "flycheck-python-mypy-config:\s+\\(.+\\)")
1588 | (match-string 1))
1589 | "," t split-string-default-separators)
1590 | :to-have-same-items-as '("mypy.ini" ".mypy.ini" "pyproject.toml" "setup.cfg" "/home/user/.config/mypy/config" "/home/user/.mypy.ini")))))
1591 |
1592 | (describe "pet-mode"
1593 | (it "should set up all buffer local variables for supported packages if `pet-mode' is t"
1594 | (spy-on 'pet-buffer-local-vars-setup)
1595 | (pet-mode 1)
1596 | (expect 'pet-buffer-local-vars-setup :to-have-been-called))
1597 |
1598 | (it "should reset all buffer local variables for supported packages to default if `pet-mode' is nil"
1599 | (spy-on 'pet-buffer-local-vars-teardown)
1600 | (pet-mode -1)
1601 | (expect 'pet-buffer-local-vars-teardown :to-have-been-called)))
1602 |
1603 | (describe "pet-cleanup-watchers-and-caches"
1604 | :var ((last-project-buf-file-name "/home/user/project/src/__init__.py")
1605 | (buffers (mapcar
1606 | (lambda (file)
1607 | (with-current-buffer (create-file-buffer file)
1608 | (set-visited-file-name file)
1609 | (python-mode)
1610 | (set-buffer-modified-p nil)
1611 | (current-buffer)))
1612 | '("/home/user/foo.py" "/home/user/project2/src/bar.py"))))
1613 |
1614 | (after-all
1615 | (dolist (buf buffers)
1616 | (kill-buffer buf)))
1617 |
1618 | (before-each
1619 | (setq pet-project-virtualenv-cache '(("/home/user/project/" . "/home/user/project/.venv/")))
1620 | (setq pet-pre-commit-config-cache '(("/home/user/project/.pre-commit-config.yaml" . "whatever")))
1621 | (setq pet-pyproject-cache '(("/home/user/project/pyproject.toml" . "whatever")))
1622 | (setq pet-pipfile-cache '(("/home/user/project/Pipfile" . "whatever")))
1623 | (setq pet-python-version-cache '(("/home/user/project/.python-verion" . "whatever")))
1624 | (setq pet-environment-cache '(("/home/user/project/environment.yml" . "whatever")))
1625 | (setq pet-watched-config-files '(("/home/user/.cache/pre-commit/db.db" . 1)
1626 | ("/home/user/project/.pre-commit-config.yaml" . 2)
1627 | ("/home/user/project/pyproject.toml" . 3)
1628 | ("/home/user/project/Pipfile" . 4)
1629 | ("/home/user/project/.python-verion" . 5)
1630 | ("/home/user/project/environment.yml" . 6)))
1631 |
1632 | (spy-on 'pet-project-root :and-return-value "/home/user/project/")
1633 | (spy-on 'buffer-list :and-return-value buffers)
1634 | (spy-on 'file-notify-rm-watch))
1635 |
1636 | (describe "when a buffer is killed"
1637 | (it "should do nothing if it's not a file visiting buffer"
1638 | (kill-buffer (with-current-buffer (get-buffer-create "*pet test*")
1639 | (python-mode)
1640 | (current-buffer)))
1641 |
1642 | (expect pet-project-virtualenv-cache :to-equal '(("/home/user/project/" . "/home/user/project/.venv/")))
1643 | (expect pet-pre-commit-config-cache :to-equal '(("/home/user/project/.pre-commit-config.yaml" . "whatever")))
1644 | (expect pet-pyproject-cache :to-equal '(("/home/user/project/pyproject.toml" . "whatever")))
1645 | (expect pet-pipfile-cache :to-equal '(("/home/user/project/Pipfile" . "whatever")))
1646 | (expect pet-python-version-cache :to-equal '(("/home/user/project/.python-verion" . "whatever")))
1647 | (expect pet-environment-cache :to-equal '(("/home/user/project/environment.yml" . "whatever")))
1648 | (expect pet-watched-config-files :to-equal '(("/home/user/.cache/pre-commit/db.db" . 1)
1649 | ("/home/user/project/.pre-commit-config.yaml" . 2)
1650 | ("/home/user/project/pyproject.toml" . 3)
1651 | ("/home/user/project/Pipfile" . 4)
1652 | ("/home/user/project/.python-verion" . 5)
1653 | ("/home/user/project/environment.yml" . 6))))
1654 |
1655 | (it "should do nothing if it's not a python mode buffer"
1656 | (kill-buffer (with-current-buffer (create-file-buffer "/home/user/.emacs")
1657 | (emacs-lisp-mode)
1658 | (set-buffer-modified-p nil)
1659 | (current-buffer)))
1660 |
1661 | (expect pet-project-virtualenv-cache :to-equal '(("/home/user/project/" . "/home/user/project/.venv/")))
1662 | (expect pet-pre-commit-config-cache :to-equal '(("/home/user/project/.pre-commit-config.yaml" . "whatever")))
1663 | (expect pet-pyproject-cache :to-equal '(("/home/user/project/pyproject.toml" . "whatever")))
1664 | (expect pet-pipfile-cache :to-equal '(("/home/user/project/Pipfile" . "whatever")))
1665 | (expect pet-python-version-cache :to-equal '(("/home/user/project/.python-verion" . "whatever")))
1666 | (expect pet-environment-cache :to-equal '(("/home/user/project/environment.yml" . "whatever")))
1667 | (expect pet-watched-config-files :to-equal '(("/home/user/.cache/pre-commit/db.db" . 1)
1668 | ("/home/user/project/.pre-commit-config.yaml" . 2)
1669 | ("/home/user/project/pyproject.toml" . 3)
1670 | ("/home/user/project/Pipfile" . 4)
1671 | ("/home/user/project/.python-verion" . 5)
1672 | ("/home/user/project/environment.yml" . 6))))
1673 |
1674 | (it "should do nothing if it does not belong in a project"
1675 | (spy-calls-reset 'pet-project-root)
1676 | (spy-on 'pet-project-root)
1677 |
1678 | (kill-buffer (with-current-buffer (create-file-buffer "/home/user/baz.py")
1679 | (set-visited-file-name "/home/user/baz.py")
1680 | (python-mode)
1681 | (set-buffer-modified-p nil)
1682 | (current-buffer)))
1683 |
1684 | (expect pet-project-virtualenv-cache :to-equal '(("/home/user/project/" . "/home/user/project/.venv/")))
1685 | (expect pet-pre-commit-config-cache :to-equal '(("/home/user/project/.pre-commit-config.yaml" . "whatever")))
1686 | (expect pet-pyproject-cache :to-equal '(("/home/user/project/pyproject.toml" . "whatever")))
1687 | (expect pet-pipfile-cache :to-equal '(("/home/user/project/Pipfile" . "whatever")))
1688 | (expect pet-python-version-cache :to-equal '(("/home/user/project/.python-verion" . "whatever")))
1689 | (expect pet-environment-cache :to-equal '(("/home/user/project/environment.yml" . "whatever")))
1690 | (expect pet-watched-config-files :to-equal '(("/home/user/.cache/pre-commit/db.db" . 1)
1691 | ("/home/user/project/.pre-commit-config.yaml" . 2)
1692 | ("/home/user/project/pyproject.toml" . 3)
1693 | ("/home/user/project/Pipfile" . 4)
1694 | ("/home/user/project/.python-verion" . 5)
1695 | ("/home/user/project/environment.yml" . 6)))))
1696 |
1697 | (describe "when the last Python buffer for a project is killed"
1698 | (before-each
1699 | (kill-buffer (with-current-buffer (create-file-buffer last-project-buf-file-name)
1700 | (set-visited-file-name last-project-buf-file-name)
1701 | (python-mode)
1702 | (set-buffer-modified-p nil)
1703 | (current-buffer))))
1704 |
1705 | (it "should clean `pet-project-virtualenv-cache'"
1706 | (expect (assoc-default "/home/user/project/" pet-project-virtualenv-cache) :to-be nil))
1707 |
1708 | (it "should clear all watched files except `pre-commit' db"
1709 | (expect pet-watched-config-files :to-equal '(("/home/user/.cache/pre-commit/db.db" . 1))))
1710 |
1711 | (it "should clear all config file caches"
1712 | (expect pet-project-virtualenv-cache :to-be nil)
1713 | (expect pet-pre-commit-config-cache :to-be nil)
1714 | (expect pet-pyproject-cache :to-be nil)
1715 | (expect pet-pipfile-cache :to-be nil)
1716 | (expect pet-python-version-cache :to-be nil)
1717 | (expect pet-environment-cache :to-be nil))))
1718 |
1719 | ;; Local Variables:
1720 | ;; eval: (buttercup-minor-mode 1)
1721 | ;; End:
1722 |
--------------------------------------------------------------------------------
/test/undercover-init.el:
--------------------------------------------------------------------------------
1 | ;; -*- lexical-binding: t; -*-
2 |
3 | (when (require 'undercover nil t)
4 | (undercover "*.el" (:report-format 'simplecov) (:send-report nil)))
5 |
--------------------------------------------------------------------------------