Skip to content

Commit

Permalink
test_package only if build missing (#15999)
Browse files Browse the repository at this point in the history
* test_package only if build missing

* fix

* review
  • Loading branch information
memsharded authored Apr 3, 2024
1 parent 739bc4d commit 9b2b84e
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 0 deletions.
12 changes: 12 additions & 0 deletions conan/cli/commands/create.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from conan.cli.printers import print_profiles
from conan.cli.printers.graph import print_graph_packages, print_graph_basic
from conan.errors import ConanException
from conans.client.graph.graph import BINARY_BUILD
from conans.util.files import mkdir


Expand All @@ -26,11 +27,17 @@ def create(conan_api, parser, *args):
parser.add_argument("-tf", "--test-folder", action=OnceArgument,
help='Alternative test folder name. By default it is "test_package". '
'Use "" to skip the test stage')
parser.add_argument("-tm", "--test-missing", action='store_true', default=False,
help='Run the test_package checks only if the package is built from source'
' but not if it already existed (using --build=missing)')
parser.add_argument("-bt", "--build-test", action="append",
help="Same as '--build' but only for the test_package requires. By default"
" if not specified it will take the '--build' value if specified")
args = parser.parse_args(*args)

if args.test_missing and args.test_folder == "":
raise ConanException('--test-folder="" is incompatible with --test-missing')

cwd = os.getcwd()
path = conan_api.local.get_conanfile_path(args.path, cwd, py=True)
test_conanfile_path = _get_test_conanfile_path(args.test_folder, path)
Expand Down Expand Up @@ -91,6 +98,11 @@ def create(conan_api, parser, *args):
lockfile = conan_api.lockfile.update_lockfile(lockfile, deps_graph, args.lockfile_packages,
clean=args.lockfile_clean)

# If the user provide --test-missing and the binary was not built from source, skip test_package
if args.test_missing and deps_graph.root.dependencies\
and deps_graph.root.dependencies[0].dst.binary != BINARY_BUILD:
test_conanfile_path = None # disable it

if test_conanfile_path:
# TODO: We need arguments for:
# - decide update policy "--test_package_update"
Expand Down
50 changes: 50 additions & 0 deletions conans/test/integration/command/create_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -811,3 +811,53 @@ def test(self):
c.run("create .")
# Ensure that creating a deps graph does not break the testing
assert "pyreq/1.0 (test package): 42!!!" in c.out


def test_create_test_package_only_build():
c = TestClient()
c.save({"conanfile.py": GenConanfile("pkg", "0.1"),
"test_package/conanfile.py": GenConanfile().with_test("self.output.info('TEST1!!!')"),
"test_package2/conanfile.py": GenConanfile().with_test("self.output.info('TEST2!!!')")})
# As it doesn't exist, it builds and test it
c.run("create . -tm")
assert "Testing the package" in c.out
assert "TEST1!!!" in c.out
# this will not create the binary, so it won't test it
c.run("create . --build=missing --test-missing")
assert "Testing the package" not in c.out
assert "TEST" not in c.out
c.run("create . -tf=test_package2 -tm")
assert "Testing the package" in c.out
assert "TEST2!!!" in c.out
assert "TEST1!!!" not in c.out
c.run("create . -tf=test_package2 --build=missing --test-missing")
assert "Testing the package" not in c.out
assert "TEST2!!!" not in c.out
assert "TEST1!!!" not in c.out

# error
c.run("create . -tm -tf=", assert_error=True)
assert '--test-folder="" is incompatible with --test-missing' in c.out


def test_create_test_package_only_build_python_require():
c = TestClient()
test = textwrap.dedent("""
from conan import ConanFile
class Tool(ConanFile):
python_requires = "tested_reference_str"
def test(self):
self.output.info("TEST!!!!")
""")
c.save({"conanfile.py": GenConanfile("pkg", "0.1").with_package_type("python-require"),
"test_package/conanfile.py": test})
c.run("create .")
assert "Testing the package" in c.out
assert "pkg/0.1 (test package): TEST!!!" in c.out
c.run("create . -tm")
assert "Testing the package" in c.out
assert "pkg/0.1 (test package): TEST!!!" in c.out
c.run("create . -tm --build=missing")
assert "Testing the package" in c.out
assert "pkg/0.1 (test package): TEST!!!" in c.out

0 comments on commit 9b2b84e

Please sign in to comment.