2016-06-05 16:22:05 -04:00
.. _devguide:
=================
Developer's Guide
=================
2024-06-14 22:26:02 +02:00
.. image :: _static/knight-vs-snail.jpg
2016-06-18 17:32:37 -04:00
:width: 80 %
:alt: knight-vs-snail
:align: center
2016-06-05 16:22:05 -04:00
Welcome to the xonsh developer's guide! This is a place for developers to
place information that does not belong in the user's guide or the library
reference but is useful or necessary for the next people that come along to
develop xonsh.
.. note :: All code changes must go through the pull request review procedure.
2016-07-16 23:20:35 -07:00
Making Your First Change
========================
2024-05-30 10:03:55 -04:00
.. note :: xonsh requires at least `pip 24.0` for `pyproject.toml` support. You can upgrade old versions using `pip install -U pip` .
2016-07-22 11:47:36 +02:00
First, install xonsh from source and open a xonsh shell in your favorite
2020-08-11 12:25:17 -04:00
terminal application. See installation instructions for details, but it
is recommended to do an 'editable' install via `pip'
2016-07-16 23:20:35 -07:00
2024-04-15 17:47:29 +05:30
$ pip install -e .[dev]
2021-03-30 20:40:43 +03:00
2016-07-16 23:20:35 -07:00
Next, make a trivial change (e.g. `` print("hello!") `` in `` main.py `` ).
2016-07-22 11:47:36 +02:00
Finally, run the following commands. You should see the effects of your change
(e.g. `` hello! `` )::
2016-07-16 23:20:35 -07:00
$ $XONSH_DEBUG=1
$ xonsh
2016-06-11 17:49:00 -04:00
Changelog
=========
Pull requests will often have CHANGELOG entries associated with. However,
to avoid excessive merge conflicts, please follow the following procedure:
1. Go into the `` news/ `` directory,
2. Copy the `` TEMPLATE.rst `` file to another file in the `` news/ `` directory.
We suggest using the branchname::
2023-05-16 19:36:50 +06:00
@ cp TEMPLATE.rst branch.rst
2016-06-11 17:49:00 -04:00
3. Add your entries as a bullet pointed lists in your `` branch.rst `` file in
the appropriate category. It is OK to leave the `` None `` entries for later
use.
4. Commit your `` branch.rst `` .
Feel free to update this file whenever you want! Please don't use someone
else's file name. All of the files in this `` news/ `` directory will be merged
automatically at release time. The `` None `` entries will be automatically
filtered out too!
2016-06-05 16:22:05 -04:00
Style Guide
===========
xonsh is a pure Python project, and so we use PEP8 (with some additions) to
ensure consistency throughout the code base.
2022-06-27 22:29:06 -04:00
-----------------
2016-06-05 16:22:05 -04:00
Rules to Write By
2022-06-27 22:29:06 -04:00
-----------------
2016-06-05 16:22:05 -04:00
It is important to refer to things and concepts by their most specific name.
When writing xonsh code or documentation please use technical terms
appropriately. The following rules help provide needed clarity.
***** *****
Interfaces
***** *****
* User-facing APIs should be as generic and robust as possible.
* Tests belong in the top-level `` tests `` directory.
* Documentation belongs in the top-level `` docs `` directory.
***** ***** **
Expectations
***** ***** **
* Code must have associated tests and adequate documentation.
* User-interaction code (such as the Shell class) is hard to test.
Mechanism to test such constructs should be developed over time.
* Have *extreme* empathy for your users.
* Be selfish. Since you will be writing tests you will be your first user.
2022-06-27 22:29:06 -04:00
------------------
2016-06-05 16:22:05 -04:00
Python Style Guide
2022-06-27 22:29:06 -04:00
------------------
xonsh follows `PEP8`_ for all Python code. The following rules apply where
`PEP8`_ is open to interpretation.
2016-06-05 16:22:05 -04:00
* Use absolute imports (`` import xonsh.tools `` ) rather than explicit
relative imports (`` import .tools `` ). Implicit relative imports
(`` import tools `` ) are never allowed.
* We use sphinx with the numpydoc extension to autogenerate API documentation. Follow
the `numpydoc`_ standard for docstrings.
* Simple functions should have simple docstrings.
* Lines should be at most 80 characters long. The 72 and 79 character
recommendations from PEP8 are not required here.
2022-06-27 22:29:06 -04:00
* All Python code should be compliant with Python 3.8+.
2020-05-06 21:23:57 -04:00
* Tests should be written with `pytest <https://docs.pytest.org/> `_ using a procedural style. Do not use
2016-06-05 16:22:05 -04:00
unittest directly or write tests in an object-oriented style.
* Test generators make more dots and the dots must flow!
2024-04-15 17:47:29 +05:30
* We use `pre-commit <https://pre-commit.com/> `_ for linting (ruff-lint) and formatting(ruff-format) the code.
To enable this as a git pre-commit hook::
2019-07-13 14:46:47 -05:00
2023-05-16 19:36:50 +06:00
@ pre-commit install
2019-07-13 14:46:47 -05:00
2022-06-27 22:29:06 -04:00
***** **
2016-06-16 20:58:53 -04:00
Imports
2022-06-27 22:29:06 -04:00
***** **
2022-07-04 01:10:16 -04:00
`` xonsh `` imports should be sorted alphabetically, and by module location. You
can (and should) use `` isort `` either from the command line or use the
`` pre-commit `` hook.
2016-06-16 20:58:53 -04:00
2016-06-05 16:22:05 -04:00
How to Test
2022-06-27 22:29:06 -04:00
===========
2016-06-05 16:22:05 -04:00
2022-06-27 22:29:06 -04:00
------
2016-06-05 16:22:05 -04:00
Docker
2022-06-27 22:29:06 -04:00
------
2016-06-05 16:22:05 -04:00
2016-06-11 17:49:00 -04:00
If you want to run your "work in progress version" without installing
2016-06-05 16:22:05 -04:00
and in a fresh environment you can use Docker. If Docker is installed
you just have to run this::
2023-05-16 19:36:50 +06:00
@ python xonsh-in-docker.py
2016-06-05 16:22:05 -04:00
This will build and run the current state of the repository in an isolated
container (it may take a while the first time you run it). There are two
2017-06-07 11:51:06 -04:00
additional arguments you can pass this script.
2016-06-05 16:22:05 -04:00
* The version of python
* the version of `` prompt_toolkit ``
Example::
2023-05-16 19:36:50 +06:00
@ python docker.py 3.4 0.57
2016-06-05 16:22:05 -04:00
Ensure your cwd is the root directory of the project (i.e., the one containing the
.git directory).
2022-06-27 22:29:06 -04:00
------------
2016-06-05 16:22:05 -04:00
Dependencies
2022-06-27 22:29:06 -04:00
------------
2016-06-05 16:22:05 -04:00
Prep your environment for running the tests::
2023-05-16 19:36:50 +06:00
@ pip install -e '.[dev]'
2016-06-05 16:22:05 -04:00
2022-06-27 22:29:06 -04:00
-------------------------
2016-06-05 16:22:05 -04:00
Running the Tests - Basic
2022-06-27 22:29:06 -04:00
-------------------------
2016-06-05 16:22:05 -04:00
2016-06-23 17:11:45 -04:00
Run all the tests using pytest::
2016-06-05 16:22:05 -04:00
2023-05-16 19:36:50 +06:00
@ pytest -q
2016-06-05 16:22:05 -04:00
2016-07-22 11:47:36 +02:00
Use "-q" to keep pytest from outputting a bunch of info for every test.
2016-06-05 16:22:05 -04:00
2022-06-27 22:29:06 -04:00
----------------------------
2016-06-05 16:22:05 -04:00
Running the Tests - Advanced
2022-06-27 22:29:06 -04:00
----------------------------
2016-06-05 16:22:05 -04:00
To perform all unit tests::
2023-05-16 19:36:50 +06:00
@ pytest
2016-06-05 16:22:05 -04:00
If you want to run specific tests you can specify the test names to
execute. For example to run test_aliases::
2023-05-16 19:36:50 +06:00
@ pytest test_aliases.py
2016-06-05 16:22:05 -04:00
Note that you can pass multiple test names in the above examples::
2023-05-16 19:36:50 +06:00
@ pytest test_aliases.py test_environ.py
2016-06-05 16:22:05 -04:00
2022-06-27 22:29:06 -04:00
----------------------------
2016-07-03 11:43:22 +03:00
Writing the Tests - Advanced
2022-06-27 22:29:06 -04:00
----------------------------
2016-07-03 11:43:22 +03:00
(refer to pytest documentation)
With the Pytest framework you can use bare `assert` statements on
2016-07-03 22:05:49 +03:00
anything you're trying to test, note that the name of the test function
has to be prefixed with `test_` ::
2016-07-03 11:43:22 +03:00
def test_whatever():
assert is_true_or_false
The conftest.py in tests directory defines fixtures for mocking various
parts of xonsh for more test isolation. For a list of the various fixtures::
2023-05-16 19:36:50 +06:00
@ pytest --fixtures
2016-07-03 11:43:22 +03:00
2018-09-30 13:59:08 -07:00
when writing tests it's best to use pytest features i.e. parametrization::
2016-07-03 11:43:22 +03:00
@pytest.mark.parametrize('env', [test_env1, test_env2])
2022-01-08 04:03:22 +05:30
def test_one(env, xession):
# update the environment variables instead of setting the attribute
# which could result in leaks to other tests.
# each run will have the same set of default env variables set.
xession.env.update(env)
2016-07-03 11:43:22 +03:00
...
this will run the test two times each time with the respective `test_env` .
2016-07-03 12:26:52 +03:00
This can be done with a for loop too but the test will run
2016-07-03 11:43:22 +03:00
only once for the different test cases and you get less isolation.
With that in mind, each test should have the least `assert` statements,
preferably one.
At the moment, xonsh doesn't support any pytest plugins.
2016-07-03 12:26:52 +03:00
Happy Testing!
2016-06-05 16:22:05 -04:00
How to Document
2022-06-27 22:29:06 -04:00
===============
2016-06-05 16:22:05 -04:00
Documentation takes many forms. This will guide you through the steps of
successful documentation.
----------
Docstrings
----------
No matter what language you are writing in, you should always have
documentation strings along with you code. This is so important that it is
part of the style guide. When writing in Python, your docstrings should be
in reStructured Text using the `numpydoc`_ format.
------------------------
Auto-Documentation Hooks
------------------------
The docstrings that you have written will automatically be connected to the
website, once the appropriate hooks have been setup. At this stage, all
documentation lives within xonsh's top-level `` docs `` directory.
We uses the sphinx tool to manage and generate the documentation, which
you can learn about from `the sphinx website <http://sphinx-doc.org/> `_ .
If you want to generate the documentation, first xonsh itself must be installed
and then you may run the following command from the `` docs `` dir:
2016-08-18 14:18:57 -04:00
.. code-block :: console
2016-06-05 16:22:05 -04:00
2023-05-16 19:36:50 +06:00
~/xonsh/docs @ make html
2016-06-05 16:22:05 -04:00
For each new
module, you will have to supply the appropriate hooks. This should be done the
first time that the module appears in a pull request. From here, call the
new module `` mymod `` . The following explains how to add hooks.
2022-06-27 22:29:06 -04:00
------------
2016-06-05 16:22:05 -04:00
Python Hooks
2022-06-27 22:29:06 -04:00
------------
2022-01-18 22:08:27 +05:30
Python API documentation is generated for the entries in `` docs/api.rst `` .
2022-05-07 21:26:57 -07:00
`sphinx-autosummary <https://www.sphinx-doc.org/en/master/usage/extensions/autosummary.html> `_
2022-01-18 22:08:27 +05:30
is used to generate documentation for the modules.
Mention your module `` mymod `` under appropriate header.
2016-06-05 16:22:05 -04:00
This will discover all of the docstrings in `` mymod `` and create the
2022-01-18 22:08:27 +05:30
appropriate webpage.
2016-06-05 16:22:05 -04:00
Building the Website
2022-06-27 22:29:06 -04:00
====================
2016-06-05 16:22:05 -04:00
Building the website/documentation requires the following dependencies:
#. `Sphinx <http://sphinx-doc.org/> `_
2022-06-27 22:29:06 -04:00
#. `Furo Theme <https://pradyunsg.me/furo/> `_
#. `numpydoc <https://numpydoc.readthedocs.io/> `_
#. `MyST Parser <https://myst-parser.readthedocs.io> `_
2018-11-06 18:42:59 +11:00
Note that xonsh itself needs to be installed too.
2022-06-27 22:29:06 -04:00
If you have cloned the git repository, you can install all of the doc-related
dependencies by running::
2023-05-16 19:36:50 +06:00
@ pip install -e ".[doc]"
2022-06-27 22:29:06 -04:00
2016-06-05 16:22:05 -04:00
-----------------------------------
Procedure for modifying the website
-----------------------------------
The xonsh website source files are located in the `` docs `` directory.
A developer first makes necessary changes, then rebuilds the website locally
by executing the command::
2023-05-16 19:36:50 +06:00
@ make html
2016-06-05 16:22:05 -04:00
This will generate html files for the website in the `` _build/html/ `` folder.
2022-06-27 22:29:06 -04:00
2022-07-01 21:17:01 +05:30
There is also a helper utility in the `` docs/ `` folder that will watch for changes and automatically rebuild the documentation. You can use this instead of running `` make html `` manually::
2022-06-27 22:29:06 -04:00
2023-05-16 19:36:50 +06:00
@ python docs/serve_docs.py
2022-06-27 22:29:06 -04:00
2016-06-05 16:22:05 -04:00
The developer may view the local changes by opening these files with their
favorite browser, e.g.::
2023-05-16 19:36:50 +06:00
@ firefox _build/html/index.html
2016-06-05 16:22:05 -04:00
Once the developer is satisfied with the changes, the changes should be
2022-06-27 22:29:06 -04:00
committed and pull-requested per usual. The docs are built and deployed using
2022-07-01 21:17:01 +05:30
GitHub Actions.
2016-06-05 16:22:05 -04:00
2022-06-27 22:29:06 -04:00
Docs associated with the latest release are hosted at
https://xon.sh while docs for the current `` main `` branch are available at
https://xon.sh/dev
2016-06-05 16:22:05 -04:00
Branches and Releases
2022-06-27 22:29:06 -04:00
=====================
2021-03-30 20:40:43 +03:00
Mainline xonsh development occurs on the `` main `` branch. Other branches
2016-06-05 16:22:05 -04:00
may be used for feature development (topical branches) or to represent
past and upcoming releases.
2022-06-27 22:29:06 -04:00
-----------------
2016-06-05 16:22:05 -04:00
Maintenance Tasks
2022-06-27 22:29:06 -04:00
-----------------
2016-06-05 16:22:05 -04:00
You can cleanup your local repository of transient files such as \*.pyc files
created by unit testing by running::
2023-05-16 19:36:50 +06:00
@ rm -f xonsh/parser_table.py xonsh/completion_parser_table.py
@ rm -f xonsh/*.pyc tests/* .pyc
@ rm -fr build
2016-06-05 16:22:05 -04:00
2022-06-27 22:29:06 -04:00
----------------------
2016-06-05 16:22:05 -04:00
Performing the Release
2022-06-27 22:29:06 -04:00
----------------------
2022-04-14 18:21:02 +05:30
This is done through the `rever <https://github.com/regro/rever> `_ . To get a list of the
2016-10-01 09:49:21 +03:00
valid options use::
2016-06-05 16:22:05 -04:00
2023-05-16 19:36:50 +06:00
@ pip install re-ver
2016-06-05 16:22:05 -04:00
2016-10-01 09:49:21 +03:00
You can perform a full release::
2016-06-05 16:22:05 -04:00
2023-05-16 19:36:50 +06:00
@ rever check
@ rever <version-number>
2016-06-05 16:22:05 -04:00
2022-06-27 22:29:06 -04:00
----------------------
2021-12-25 19:12:07 +05:30
Cross-platform testing
2022-06-27 22:29:06 -04:00
----------------------
2022-01-18 22:08:27 +05:30
Most of the time, an actual VM machine is needed to test the nuances of cross platform testing.
2021-12-25 19:12:07 +05:30
But alas here are some other ways to test things
2022-01-18 22:08:27 +05:30
1. Windows
2021-12-25 19:12:07 +05:30
2022-01-18 22:08:27 +05:30
- `wine <https://www.winehq.org/> `_ can be used to emulate the development environment. It provides cmd.exe with its default installation.
2021-12-25 19:12:07 +05:30
2. OS X
2022-01-18 22:08:27 +05:30
- `darlinghq <https://www.darlinghq.org/> `_ can be used to emulate the development environment for Linux users.
2021-12-25 19:12:07 +05:30
Windows users can use Linux inside a virtual machine or WSL to run the same.
- `OSX KVM <https://github.com/kholia/OSX-KVM>` can be used for virtualization.
3. Linux
2022-01-18 22:08:27 +05:30
- It far easier to test things for Linux. `docker <https://www.docker.com/> `_ is available on all three platforms.
2021-12-25 19:12:07 +05:30
2022-01-18 22:08:27 +05:30
One can leverage the Github Actions to provide a reverse shell to test things out.
Solutions like `actions-tmate <https://mxschmitt.github.io/action-tmate/> `_ are available,
but they should not in any way violate the Github Action policies.
2021-12-25 19:12:07 +05:30
2016-06-05 16:22:05 -04:00
Document History
2022-06-27 22:29:06 -04:00
================
2016-06-05 16:22:05 -04:00
Portions of this page have been forked from the PyNE documentation,
Copyright 2011-2015, the PyNE Development Team. All rights reserved.
.. _PEP8: https://www.python.org/dev/peps/pep-0008/
2019-03-13 01:33:49 +09:00
.. _numpydoc: https://numpydoc.readthedocs.io/en/latest/format.html#docstring-standard