question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

run_local not working on windows

See original GitHub issue

My system is Python 3.7.0 running Windows 10 and I receive an error when I try to run anything with run_local on windows (which affects all backends). The stack trace looks like this:

venv\lib\site-packages\testinfra\host.py:71: in run
    return self.backend.run(command, *args, **kwargs)
venv\lib\site-packages\testinfra\backend\docker.py:35: in run
    "docker exec %s /bin/sh -c %s", self.name, cmd)
venv\lib\site-packages\testinfra\backend\base.py:203: in run_local
    stderr=subprocess.PIPE,
..\..\..\appdata\local\programs\python\python37-32\Lib\subprocess.py:756: in __init__
    restore_signals, start_new_session)
..\..\..\appdata\local\programs\python\python37-32\Lib\subprocess.py:1100: in _execute_child
    args = list2cmdline(args)
..\..\..\appdata\local\programs\python\python37-32\Lib\subprocess.py:511: TypeError
    TypeError: argument of type 'int' is not iterable

Looks like the fact that the command is being encoded before running is to blame. Popen in Windows doesn’t handle byte arrays the same way it does on Linux. Was able to fix by modifying code to this:

def run_local(self, command, *args):
    command = self.quote(command, *args)
    if os.name != 'nt':
        command = self.encode(command)
    p = subprocess.Popen(
        command, shell=True,
        stdin=subprocess.PIPE,
        stdout=subprocess.PIPE,
        stderr=subprocess.PIPE,
    )
    stdout, stderr = p.communicate()
    result = self.result(p.returncode, command, stdout, stderr)
    return result

Basically only encode when not on windows

Issue Analytics

  • State:open
  • Created 5 years ago
  • Reactions:3
  • Comments:6 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
viniciusarturcommented, Feb 20, 2021

How did you patch the run_local(self, command, *args) method and how can I do this with the approach above? For me it looks like as if my patched run_local method is not used.

In my conftest.py:

def patch_testinfra():
    def run_local_new(self, command, *args):
        command = self.quote(command, *args)
        p = subprocess.Popen(
            command, shell=True,
            stdin=subprocess.PIPE,
            stdout=subprocess.PIPE,
            stderr=subprocess.PIPE,
        )
        stdout, stderr = p.communicate()
        result = self.result(p.returncode, command, stdout, stderr)
        return result

    from testinfra.backend.base import BaseBackend
    BaseBackend.run_local = run_local_new

def pytest_generate_tests(metafunc):
    patch_testinfra()
0reactions
thomasleveilcommented, Oct 12, 2021

@viniciusartur solution wasn’t suffisent in my case (I’m using testinfra with docker backend), but combining its solution with https://github.com/pytest-dev/pytest-testinfra/issues/375#issue-360991022 made it work :

# conftest.py
import os
import re
import subprocess
import testinfra

def patch_testinfra():
    if os.name == 'nt':
        """
        Making testinfa work from Windows with Docker backend.
        Inspired from 
        - https://github.com/pytest-dev/pytest-testinfra/issues/411#issuecomment-782729362
        - https://github.com/pytest-dev/pytest-testinfra/issues/375#issue-360991022 
        """
        def quote(command, *args):
            def anon_1(arg):
                if re.match(r'/("|\s|\')', arg) != None:
                    return arg

                arg = re.sub('"', '\"', arg)
                return '"%s"' % (arg)

            if args:
                return command % tuple(anon_1(a) for a in args)
                # return command % tuple(pipes.quote(a) for a in args)
            return command

        def run_local_new(self, command, *args):
            command = quote(command, *args)
            p = subprocess.Popen(
                command,
                shell=True,
                stdin=subprocess.PIPE,
                stdout=subprocess.PIPE,
                stderr=subprocess.PIPE,
            )
            stdout, stderr = p.communicate()
            result = self.result(p.returncode, command, stdout, stderr)
            return result

        from testinfra.backend.base import BaseBackend

        BaseBackend.run_local = run_local_new


def pytest_generate_tests(metafunc):
    patch_testinfra()
Read more comments on GitHub >

github_iconTop Results From Across the Web

npm run local binaries on windows - Stack Overflow
How can you run local npm binaries on Windows(10)?. I'm trying to run a webpack dev-server(using webpack-encore in this instance)
Read more >
Can't run local command on Windows - Julia Discourse
Something in the PATH works (e.g.): run(`git submodule update --init --recursive $smName`) where smName is a string defined previously.
Read more >
Not able to run local dispatcher on Windows 10 Pro
Solved: Hi Guys, Getting error "Required image not found, trying to load from archive... unsupported os linux" when tried setting - 414480.
Read more >
Running locally installed npm executables - 2ality
This blog post explains how to run locally installed executables. ... That directory node_modules/ may or may not exist, already; ...
Read more >
Work with Azure Functions Core Tools | Microsoft Learn
Learn how to code and test Azure Functions from the command prompt or terminal on your local computer before you run them on...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found