|
| 1 | +Running mypy and mypyc tests in a Docker container |
| 2 | +================================================== |
| 3 | + |
| 4 | +This directory contains scripts for running mypy and mypyc tests in a |
| 5 | +Linux Docker container. This allows running Linux tests on a different |
| 6 | +operating system that supports Docker, or running tests in an |
| 7 | +isolated, predictable environment on a Linux host operating system. |
| 8 | + |
| 9 | +Why use Docker? |
| 10 | +--------------- |
| 11 | + |
| 12 | +Mypyc tests can be significantly faster in a Docker container than |
| 13 | +running natively on macOS. |
| 14 | + |
| 15 | +Also, if it's inconvient to install the necessary dependencies on the |
| 16 | +host operating system, or there are issues getting some tests to pass |
| 17 | +on the host operating system, using a container can be an easy |
| 18 | +workaround. |
| 19 | + |
| 20 | +Prerequisites |
| 21 | +------------- |
| 22 | + |
| 23 | +First install Docker. On macOS, both Docker Desktop (proprietary, but |
| 24 | +with a free of charge subscription for some use cases) and Colima (MIT |
| 25 | +license) should work as runtimes. |
| 26 | + |
| 27 | +You may have to explicitly start the runtime first. Colima example |
| 28 | +(replace '8' with the number of CPU cores you have): |
| 29 | + |
| 30 | +``` |
| 31 | +$ colima start -c 8 |
| 32 | +
|
| 33 | +``` |
| 34 | + |
| 35 | +How to run tests |
| 36 | +---------------- |
| 37 | + |
| 38 | +You need to build the container with all necessary dependencies before |
| 39 | +you can run tests: |
| 40 | + |
| 41 | +``` |
| 42 | +$ python3 misc/docker/build.py |
| 43 | +``` |
| 44 | + |
| 45 | +This creates a `mypy-test` Docker container that you can use to run |
| 46 | +tests. |
| 47 | + |
| 48 | +You may need to run the script as root: |
| 49 | + |
| 50 | +``` |
| 51 | +$ sudo python3 misc/docker/build.py |
| 52 | +``` |
| 53 | + |
| 54 | +If you have a stale container which isn't up-to-date, use `--no-cache` |
| 55 | +`--pull` to force rebuilding everything: |
| 56 | + |
| 57 | +``` |
| 58 | +$ python3 misc/docker/build.py --no-cache --pull |
| 59 | +``` |
| 60 | + |
| 61 | +Now you can run tests by using the `misc/docker/run.sh` script. Give |
| 62 | +it the pytest command line you want to run as arguments. For example, |
| 63 | +you can run mypyc tests like this: |
| 64 | + |
| 65 | +``` |
| 66 | +$ misc/docker/run.sh pytest mypyc |
| 67 | +``` |
| 68 | + |
| 69 | +You can also use `-k <filter>`, `-n0`, `-q`, etc. |
| 70 | + |
| 71 | +Again, you may need to run `run.sh` as root: |
| 72 | + |
| 73 | +``` |
| 74 | +$ sudo misc/docker/run.sh pytest mypyc |
| 75 | +``` |
| 76 | + |
| 77 | +You can also use `runtests.py` in the container. Example: |
| 78 | + |
| 79 | +``` |
| 80 | +$ misc/docker/run.sh ./runtests.py self lint |
| 81 | +``` |
| 82 | + |
| 83 | +Notes |
| 84 | +----- |
| 85 | + |
| 86 | +File system changes within the container are not visible to the host |
| 87 | +system. You can't use the container to format code using Black, for |
| 88 | +example. |
| 89 | + |
| 90 | +On a mac, you may want to give additional CPU to the VM used to run |
| 91 | +the container. The default allocation may be way too low (e.g. 2 CPU |
| 92 | +cores). For example, use the `-c` option when starting the VM if you |
| 93 | +use Colima: |
| 94 | + |
| 95 | +``` |
| 96 | +$ colima start -c 8 |
| 97 | +``` |
| 98 | + |
| 99 | +Giving access to all available CPUs to the Linux VM tends to provide |
| 100 | +the best performance. This is not needed on a Linux host, since the |
| 101 | +container is not run in a VM. |
0 commit comments