Repository navigation
How do conditional commands in TOML syntax work? #3874
|
I want to modernise my current I'm running the latest version 4.49.1, and looking at the documentation, there is https://tox.wiki/en/4.49.1/reference/config.html#conditional-value-reference [env_run_base]
commands = [
{ replace = "if", condition = "factor.linux", then = [["pytest", "--numprocesses=auto"]] },
{ replace = "if", condition = "not factor.linux", then = [["pytest"]] },
]Because I wasn't able to make it work, I tried and copy&pasted the above commands list verbatim into an environment (named So it doesn't expect a list of commands, but a single command? Modifying the sample above to instead read commands = [
{ replace = "if", condition = "factor.linux", then = ["pytest", "--numprocesses=auto"] },
{ replace = "if", condition = "not factor.linux", then = ["pytest"] },
]instead gives this error message: Now it got an empty string, but expects a list?! What's going on here? |
Replies: 1 comment 1 reply
|
Hi @luziferius, great question! The missing piece is For your specific use case (conditionally adding commands = [["pytest", { replace = "if", condition = "factor.linux", then = ["--numprocesses=auto"], extend = true }]]On linux this runs If you need entirely different commands per platform, you can also do: commands = [
{ replace = "if", condition = "factor.linux", then = [["pytest", "--numprocesses=auto"]], extend = true },
{ replace = "if", condition = "not factor.linux", then = [["pytest"]], extend = true },
]When the condition is false and no You can find more details and examples in the Conditional value reference section of the docs at https://tox.wiki/en/latest/. Hope that helps! |

Hi @luziferius, great question!
The missing piece is
extend = trueon the conditional replacement. Without it, tox appends thethenvalue as a single element into the commands list, which creates an extra level of nesting that fails validation. Withextend = true, the contents are unpacked into the parent list instead.For your specific use case (conditionally adding
--numprocesses=auto), the simplest approach is to keep a single command and conditionally inject the extra argument:On linux this runs
pytest --numprocesses=auto, on other platforms justpytest.If you need …