|
| 1 | +import textwrap |
| 2 | + |
| 3 | +import pytest |
| 4 | +import platform |
| 5 | + |
| 6 | +from conan.test.assets.genconanfile import GenConanfile |
| 7 | +from conan.test.utils.tools import TestClient |
| 8 | + |
| 9 | + |
| 10 | +executable = "myapp.bat" if platform.system() == "Windows" else "myapp.sh" |
| 11 | + |
| 12 | + |
| 13 | +@pytest.fixture(scope="module") |
| 14 | +def client(): |
| 15 | + tc = TestClient(default_server_user=True) |
| 16 | + conanfile = textwrap.dedent(""" |
| 17 | + from conan import ConanFile |
| 18 | + from conan.tools.files import save |
| 19 | + import os |
| 20 | +
|
| 21 | + class Pkg(ConanFile): |
| 22 | + name = "pkg" |
| 23 | + version = "0.1" |
| 24 | + # So that the requirement is run=True even for --requires |
| 25 | + package_type = "application" |
| 26 | + options = {"foo": [True, False, "bar"]} |
| 27 | + default_options = {"foo": True} |
| 28 | + settings = "os" |
| 29 | +
|
| 30 | + def package(self): |
| 31 | + executable = os.path.join(self.package_folder, "bin", '""" + executable + """') |
| 32 | + save(self, executable, f"echo Hello World! foo={self.options.foo}") |
| 33 | + # Make it executable |
| 34 | + os.chmod(executable, 0o755) |
| 35 | + """) |
| 36 | + tc.save({"pkg/conanfile.py": conanfile}) |
| 37 | + tc.run("create pkg") |
| 38 | + return tc |
| 39 | + |
| 40 | + |
| 41 | +@pytest.mark.parametrize("context_flag", ["host", "build", None]) |
| 42 | +@pytest.mark.parametrize("requires_context", ["host", "build",]) |
| 43 | +@pytest.mark.parametrize("use_conanfile", [True, False]) |
| 44 | +def test_run(client, context_flag, requires_context, use_conanfile): |
| 45 | + context_arg = { |
| 46 | + "host": "--context=host", |
| 47 | + "build": "--context=build", |
| 48 | + None: "", |
| 49 | + }.get(context_flag) |
| 50 | + should_find_binary = (context_flag == requires_context) or (context_flag is None) |
| 51 | + if use_conanfile: |
| 52 | + conanfile_consumer = GenConanfile("consumer", "1.0").with_settings("os") |
| 53 | + if requires_context == "host": |
| 54 | + conanfile_consumer.with_requires("pkg/0.1") |
| 55 | + else: |
| 56 | + conanfile_consumer.with_tool_requires("pkg/0.1") |
| 57 | + |
| 58 | + client.save({"conanfile.py": conanfile_consumer}) |
| 59 | + client.run(f"run {executable} {context_arg}", assert_error=not should_find_binary) |
| 60 | + else: |
| 61 | + requires = "requires" if requires_context == "host" else "tool-requires" |
| 62 | + client.run(f"run {executable} --{requires}=pkg/0.1 {context_arg}", |
| 63 | + assert_error=not should_find_binary) |
| 64 | + if should_find_binary: |
| 65 | + assert "Hello World!" in client.out |
| 66 | + else: |
| 67 | + if platform.system() == "Windows": |
| 68 | + assert "not recognized as an internal or external command" in client.out |
| 69 | + else: |
| 70 | + assert "Error 127 while executing" in client.out |
| 71 | + |
| 72 | + |
| 73 | +def test_run_context_priority(client): |
| 74 | + client.run("create pkg -o=pkg/*:foo=False") |
| 75 | + |
| 76 | + client.run(f"run {executable} --requires=pkg/0.1 --tool-requires=pkg/0.1 -o:b=pkg/*:foo=False") |
| 77 | + # True is host, False is build, run gives priority to host |
| 78 | + assert "Hello World! foo=True" in client.out |
| 79 | + |
| 80 | + |
| 81 | +def test_run_missing_executable(client): |
| 82 | + client.run(f"run a-binary-name-that-does-not-exist --requires=pkg/0.1", assert_error=True) |
| 83 | + if platform.system() == "Windows": |
| 84 | + assert "not recognized as an internal or external command" in client.out |
| 85 | + else: |
| 86 | + assert "Error 127 while executing" in client.out |
| 87 | + |
| 88 | + |
| 89 | +def test_run_missing_binary(client): |
| 90 | + client.run("run foo --requires=pkg/0.1 -o=pkg/*:foo=bar", assert_error=True) |
| 91 | + assert "Error installing the dependencies" in client.out |
| 92 | + assert "Missing prebuilt package for 'pkg/0.1'" in client.out |
| 93 | + |
| 94 | + |
| 95 | +def test_run_missing_package(client): |
| 96 | + client.run("run foo --requires=pkg/2.1", assert_error=True) |
| 97 | + assert "Error installing the dependencies" in client.out |
| 98 | + assert "Package 'pkg/2.1' not resolved" in client.out |
| 99 | + |
| 100 | + |
| 101 | +@pytest.mark.skipif(platform.system() == "Windows", reason="Unix only") |
| 102 | +def test_run_status_is_propagated(client): |
| 103 | + client.run("run false --requires=pkg/0.1", assert_error=True) |
| 104 | + assert "Error installing the dependencies" not in client.out |
| 105 | + assert "ERROR: Error 1 while executing" in client.out |
0 commit comments