|  | 
|  | 1 | +--- | 
|  | 2 | +layout: post | 
|  | 3 | +title:  "How to contribute to RustPython by CPython unittest" | 
|  | 4 | +date:   2020-04-05 01:45:00 +0900 | 
|  | 5 | +categories: guideline | 
|  | 6 | +--- | 
|  | 7 | + | 
|  | 8 | +At the very end of 2019, we finally reached one of the short-term goals: CPython unittest support. Due to this enhancement, finding CPython compatibility is easier than before. | 
|  | 9 | +Probably this will be the major source of contribution spots for the new contributors this year. Here is a simple guideline. | 
|  | 10 | + | 
|  | 11 | +## Fix known compatibility bugs | 
|  | 12 | +Let's find an incompatibility issue and fix it. | 
|  | 13 | + | 
|  | 14 | +1. See `Lib/test` directory of the project. There are many `test_` prefixed files like `test_unicode.py`. | 
|  | 15 | +2. Try to open one of them. It might look just fine at a glance - but search for `TODO: RUSTPYTHON` in the files. There are tons of skipped, marked as an expected failure or commented out tests. | 
|  | 16 | +3. Choose one or two interesting bugs. Remove the test blocker - skip, expectedFailure or comments. | 
|  | 17 | +4. Try to fix them. | 
|  | 18 | + | 
|  | 19 | +Here is a quick tip to run single unittest file. | 
|  | 20 | + | 
|  | 21 | +```sh | 
|  | 22 | +$ RUSTPYTHONPATH=Lib cargo run --release Lib/test/test_unicode.py | 
|  | 23 | +``` | 
|  | 24 | + | 
|  | 25 | +## Add a new unittest file | 
|  | 26 | +Because CPython unittest is not perfectly working in RustPython, we are doing this one by one with editings. | 
|  | 27 | +1. Download CPython source code. | 
|  | 28 | +2. Check out a specific version of CPython. For now, 3.8.2 is recommended. (We are testing against CPython 3.8 and 3.8.2 is the most recent version for now) | 
|  | 29 | +3. Copy a file from CPython `Lib/test` | 
|  | 30 | +4. Commit the file without editing. Specify copied CPython version to commit message. | 
|  | 31 | +5. Try to edit it until it runs without a crash or failure. | 
|  | 32 | +6. Commit the changes to make it run. This is the core contribution. | 
|  | 33 | + | 
|  | 34 | +Because RustPython is not perfect, "try to edit it until it runs" doesn't mean to make 100% successful running. The common editing methods here: | 
|  | 35 | +1. At least it must be able to start to run the test. Fix the test code or bug until it runs at least a single unit of the test. Typically, unimplemented stdlib or missing files of unittest can make issues. Sometimes RustPython bugs make issues too. | 
|  | 36 | +2. If any test is not loadable by `SyntaxError`, that part is required to be commented out. | 
|  | 37 | +3. If any test leads to a crash of RustPython, this code is not possible to run. Mark the test to skip. | 
|  | 38 | +4. If any test is run but fails, this is an incompatibility issue. Mark the test as an expected failure. | 
|  | 39 | + | 
|  | 40 | +We prefer the reversed order of upper methods. The later the more strict so easy to detect any progress or regression. | 
|  | 41 | +When we temporarily disable parts of unittest due to RustPython caveats, we mark them to find it out easily later. Please check the examples below or search for `TODO: RUSTPYTHON` in `Lib/test` directory to check actual usage. | 
|  | 42 | + | 
|  | 43 | +Comment out: | 
|  | 44 | +```python | 
|  | 45 | +# TODO: RUSTPYTHON | 
|  | 46 | +# | 
|  | 47 | +# def ...  # commented out tests | 
|  | 48 | +``` | 
|  | 49 | + | 
|  | 50 | +skip: | 
|  | 51 | +```python | 
|  | 52 | +@unittest.skip("TODO: RUSTPYTHON") | 
|  | 53 | +def ...  # skipped tests | 
|  | 54 | +``` | 
|  | 55 | + | 
|  | 56 | +expectedFailure: | 
|  | 57 | +```python | 
|  | 58 | +# TODO: RUSTPYTHON | 
|  | 59 | +@unittest.expectedFailure | 
|  | 60 | +def ...  # failed tests | 
|  | 61 | +``` | 
|  | 62 | + | 
|  | 63 | +## Development guide | 
|  | 64 | +For the general source of the development, please visit the [RustPython development guide](https://github.com/RustPython/RustPython/blob/master/DEVELOPMENT.md) | 
0 commit comments