wntrblm / nox

Flexible test automation for Python
https://nox.thea.codes
Apache License 2.0
1.3k stars 148 forks source link

special character < is escaped #836

Open gpongelli opened 1 month ago

gpongelli commented 1 month ago

Current Behavior

Hi, I'm trying to run hadolint tool within nox session in following way, as per their docs:

    session.run(
            "podman",
            "run",
            "--rm",
            "-i",
            "ghcr.io/hadolint/hadolint",
            "<",
            "Dockerfile",
            external=True,
        )

when the session is executed, on terminal log I get exit code 127 due to < charater escaped:

nox > podman run --rm ghcr.io/hadolint/hadolint '<' Dockerfile
Error: preparing container eb84b95424e737867600007dcc40c53d6e653b36dd8b54b5b36461f99da22c5c for attach: crun: executable file `<` not found in $PATH: No such file or directory: OCI runtime attempted to invoke a command that was not found
nox > Command podman run --rm ghcr.io/hadolint/hadolint '<' Dockerfile failed with exit code 127
nox > Session container failed.

Expected Behavior

I would be able to execute that command passing the Dockerfile to docker container as it expects.

Steps To Reproduce

No response

Environment

- OS: windows 11
- Python: 3.12
- Nox: 2024.4.15

Anything else?

on activated venv, I can manually launch the same command and it works due to < not escaped:

(....-py3.12) PS C:\...\PyCharmProjects\...> cat .\Dockerfile | podman run --rm -i hadolint/hadolint      
-:10 DL4000 error: MAINTAINER is deprecated
-:13 DL3042 warning: Avoid use of cache directory with pip. Use `pip install --no-cache-dir <package>`
-:13 DL3013 warning: Pin versions in pip. Instead of `pip install <package>` use `pip install <package>==<version>` or `pip install --requirement <requirements file>`
-:16 DL3027 warning: Do not use apt as it is meant to be a end-user tool, use apt-get or apt-cache instead
-:31 DL3042 warning: Avoid use of cache directory with pip. Use `pip install --no-cache-dir <package>`
-:31 DL3013 warning: Pin versions in pip. Instead of `pip install <package>` use `pip install <package>==<version>` or `pip install --requirement <requirements file>`
-:37 DL3042 warning: Avoid use of cache directory with pip. Use `pip install --no-cache-dir <package>`
gpongelli commented 1 month ago

I found a workaround passing the Dockerfile as volume, so removing the need to use < character:

    session.run(
        "podman",
        "run",
        "--rm",
        "-it",
        "-v",
        "Dockerfile:/Dockerfile",
        "-v",
        "hadolint.yaml:/hadolint.yaml",
        "ghcr.io/hadolint/hadolint",
        "hadolint",
        "--config",
        "/hadolint.yaml",
        "/Dockerfile",
        external=True,
    )

hope this could help other people too.

cjolowicz commented 1 month ago

Alternatively, you could wrap the command in a shell. See https://nox.thea.codes/en/stable/config.html#nox.sessions.Session.run . The < character is shell syntax for redirecting standard input. (The session.run function does not wrap commands in a shell, and it does not currently offer a way to redirect standard input.)

gpongelli commented 1 month ago

Hi @cjolowicz , what do you mean with "wrap the command in a shell" ? something like bash -c podman ... (I'm not aware of windows eqivalent) ? I'm already doing session.run command to invoke podman.

Thanks

ember91 commented 1 month ago

In short, session.run doesn't run in a shell and input redirection, i.e. <, is a feature of the shell.

I don't think this is a bug with nox.

A common way to fix that is to run it in a shell. For example with bash as the shell:

session.run("bash", "-c", "podman run ...")