What this is about
You have a small Python tool. It reads some input and prints a result. You want the same tool in Mojo, and you want proof that it behaves the same.
This article shows the way we do it. You write the Mojo version, you write a list of test cases, and a tool called mojo-port runs every case through both programs. If the outputs match, the port passes. Then the tool damages your port on purpose to see if your cases notice.
Why it matters
A port that "looks right" is not enough. Small tools often hide small rules, such as the exit code for a missing file. If you skip one of those rules, scripts that call your tool can break without a warning.
A good port is also a smaller task than it sounds. Start with a tool that fits in one file and uses only Python's standard library. Those are the easy wins, and mojo-port can sort your tools into that group for you.
How to do it, step by step
We use the demo that ships with mojo-port: wcl, a line and word counter.
1. Install the checker. You need Mojo 1.2 and Python 3.11 or newer for the oracle. Python is the trusted original, and it only needs the standard library. Then:
git clone https://github.com/amarbarosys/mojo-port && cd mojo-port
mkdir -p .bin && mojo build mojo-port.mojo -o .bin/mojo-port
mojo must be on your PATH. The checker itself is one Mojo file of about 600 lines.
2. Lay out a tool folder. Each tool gets one folder with these parts:
wcl/
tool.toml name = "wcl", entry = "wcl.py"
wcl.py the original Python program
wcl.mojo your Mojo port
tests/cases.txt one argument string per line
3. Write cases. Each line of cases.txt is a set of arguments to the tool. Lines that begin with # are skipped. You need at least three cases. A good set has the usual file, an empty file, two files at once and a file that does not exist. The wcl demo has 9 cases.
4. Write the port. Start from the Python. Keep the names. Do not be clever. The goal is the same output, not the shortest code. You can also ask a model for a first draft with mojo-port auto. It tries up to five times and runs verify after each, but the checks are plain programs and never trust the model.
5. Run verify. For the demo:
$ MOJO_PORT_ROOT=examples .bin/mojo-port verify wcl
wcl : 9 / 9 cases match
verify builds your Mojo file, runs every case through both programs and compares what they print and the exit status.
6. Break it on purpose. Change the exit code for an unreadable file from status = 1 to status = 0 and run verify again. The README shows the result: 7 of 9 cases match, and the tool prints the two failing cases with the Python result and the Mojo result side by side. That is the moment the tool earns its place. You see exactly which input disagreed. Restore the file after the test.
7. Run the mutant check.
$ MOJO_PORT_ROOT=examples .bin/mojo-port mutants wcl
wcl : mutants 8 / 8 killed (8 by a case, 0 did not compile)
A mutant is your port with one operator flipped. If your cases catch all of them, the cases are looking at most of your code. If one survives, add a case that would notice it.
8. Fix the gaps one at a time. Each failed case is a small question. Fix one, run verify again, and move on. Do not edit three things at once, or you will not know which edit helped.
Which tool should you port first
Not every tool is a good first port. mojo-port has a triage command that looks at the tool folders under your root and gives each one a tag.
port: the tool uses only Python's standard library. This is the easy group.wrap: the tool imports a third-party package. A port would need a library for it.keep: the tool is a shell script, so it stays as it is.done: the tool is already in Mojo.
Start with a port tool that is short and that you can test with a handful of arguments. The library table in mojo-libs also helps. If your tool imports json, csv, argparse, subprocess or hashlib, there is a Mojo library with the same behavior. You can tell mojo-port where your library checkouts live, and verify then adds them to the build for each import the port uses.
Two more commands help once you have a first version. mojo-port cases grows your list of test cases with a model until every line of the Python original runs, and it keeps a candidate case only if it adds coverage. mojo-port cert runs the verify step, the mutants and the coverage check, and writes the result and the file hashes into one record, so a later check can tell whether anything changed.
What it does not prove
verifycompares standard output and exit status. It does not compare error messages or timing.- Parity covers only the paths your cases take.
- The checker runs on Linux and was tested with Mojo 1.2.0. It works with Python originals that use only the standard library. A tool that needs a third-party Python package is tagged
wrapand is not ported automatically.
Words you may not know
- Port: write the same program again in another language.
- Oracle: the trusted original your port is checked against.
- Exit status: the number a program returns when it ends. Zero means success.
- Mutant: your code with one deliberate mistake, used to test your tests.
- Standard library: the tools that come with a language, with nothing extra to install.
Try it
- Clone mojo-port and run the
wcldemo. - Pick a ten-line Python tool of your own, make a folder like
wcl/, and write five cases. - Browse the mojo-libs table before you write something from scratch. A library may already do the hard part.
Read next
- Oracle tests: checking new code against code you trust explains the idea behind the checker.

Commentaires
Pas encore de commentaires.
Se connecter pour commenter.