def test_find_package_drake(self):
        cmake_source_dir = install_test_helper.create_temporary_dir("src")

        cc_content_drake = """
            #include <drake/common/symbolic.h>
            int main() {
              drake::symbolic::Environment environment;
              return 0;
            }
        """

        cc_filename_drake = os.path.join(cmake_source_dir, "main_drake.cc")

        with open(cc_filename_drake, "w") as f:
            f.write(textwrap.dedent(cc_content_drake))

        cc_content_drake_common_text_logging_gflags = """
            #include <drake/common/text_logging_gflags.h>
            int main() {
              drake::logging::HandleSpdlogGflags();
              return 0;
            }
        """

        cc_filename_drake_common_text_logging_gflags = os.path.join(
            cmake_source_dir, "main_drake_common_text_logging_gflags.cc")

        with open(cc_filename_drake_common_text_logging_gflags, "w") as f:
            f.write(textwrap.dedent(
                cc_content_drake_common_text_logging_gflags))

        cmake_prefix_path = install_test_helper.get_install_dir()

        cmake_content = """
            cmake_minimum_required(VERSION 3.10)
            project(find_package_drake_install_test)
            set(CMAKE_PREFIX_PATH {cmake_prefix_path})
            find_package(drake CONFIG REQUIRED)
            add_executable(main_drake main_drake.cc)
            target_link_libraries(main_drake drake::drake)
            add_executable(main_drake_common_text_logging_gflags
              main_drake_common_text_logging_gflags.cc)
            target_link_libraries(main_drake_common_text_logging_gflags
              drake::drake-common-text-logging-gflags)
        """.format(cmake_prefix_path=cmake_prefix_path)

        cmake_filename = os.path.join(cmake_source_dir, "CMakeLists.txt")

        with open(cmake_filename, "w") as f:
            f.write(textwrap.dedent(cmake_content))

        cmake_binary_dir = install_test_helper.create_temporary_dir("build")

        subprocess.check_call(["cmake", cmake_source_dir],
                              cwd=cmake_binary_dir)
        subprocess.check_call("make", cwd=cmake_binary_dir)
    def test_install_and_run(self):
        install_dir = install_test_helper.get_install_dir()
        resource_subfolder = "share/drake/"
        installed_sentinel = os.path.join(install_dir, resource_subfolder,
                                          ".drake-find_resource-sentinel")
        # Verifies that the sentinel file exists in the installed directory.
        # If it has been removed, we need to update this test.
        self.assertTrue(os.path.isfile(installed_sentinel))
        # Create a resource in the temporary directory.
        tmp_dir = install_test_helper.create_temporary_dir()

        resource_folder = os.path.join(tmp_dir, resource_subfolder)
        test_folder = os.path.join(resource_folder, "common/test")
        os.makedirs(test_folder)
        # Create sentinel file.
        sentinel = os.path.join(resource_folder,
                                os.path.basename(installed_sentinel))
        os.symlink(installed_sentinel, sentinel)
        # Create resource file.
        resource = os.path.join(test_folder, "tmp_resource")
        resource_data = "tmp_resource"
        with open(resource, "w") as f:
            f.write(resource_data)

        # Cross-check the resource root environment variable name.
        env_name = "DRAKE_RESOURCE_ROOT"
        resource_tool = os.path.join(install_dir,
                                     "share/drake/common/resource_tool")
        output_name = install_test_helper.check_output([
            resource_tool,
            "--print_resource_root_environment_variable_name",
        ], ).strip()
        self.assertEqual(output_name, env_name)

        # Use the installed resource_tool to find a resource.
        tool_env = dict(os.environ)
        tool_env[env_name] = os.path.join(tmp_dir, "share")
        absolute_path = install_test_helper.check_output(
            [
                resource_tool,
                "--print_resource_path",
                "drake/common/test/tmp_resource",
            ],
            env=tool_env,
        ).strip()
        with open(absolute_path, 'r') as data:
            self.assertEqual(data.read(), resource_data)

        # Use --add_resource_search_path instead of environment variable
        # to find resources.
        absolute_path = install_test_helper.check_output([
            resource_tool,
            "--print_resource_path",
            "drake/common/test/tmp_resource",
            "--add_resource_search_path",
            os.path.join(tmp_dir, "share"),
        ], ).strip()
        with open(absolute_path, 'r') as data:
            self.assertEqual(data.read(), resource_data)
def main():
    install_dir = install_test_helper.get_install_dir()

    # In scratch, mock up a drake_bazel_installed workspace.
    scratch_dir = install_test_helper.create_temporary_dir("scratch")

    # The commit (version) here should be identical to the commit listed in
    # drake/tools/workspace/rules_python/repository.bzl.
    rules_python_commit = "0.10.2"
    rules_python_url = f"https://github.com/bazelbuild/rules_python/archive/{rules_python_commit}.tar.gz"  # noqa
    rules_python_sha256 = "a3a6e99f497be089f81ec082882e40246bfd435f52f4e82f37e89449b04573f6"  # noqa

    with open(join(scratch_dir, "WORKSPACE"), "w") as f:
        f.write(f"""
workspace(name = "scratch")

load(
    "@bazel_tools//tools/build_defs/repo:http.bzl",
    "http_archive",
)
http_archive(
    name = "rules_python",
    sha256 = "{rules_python_sha256}",
    strip_prefix = "rules_python-{rules_python_commit}",
    url = "{rules_python_url}",
)

new_local_repository(
    name = "drake_binary",
    path = "{install_dir}",
    build_file_content = "#",
)
load(
    "@drake_binary//:share/drake/repo.bzl",
    "drake_repository",
)
drake_repository(name = "drake")
""")

    with open(join(scratch_dir, "BUILD.bazel"), "w") as f:
        f.write(f"""
load("@rules_python//python:defs.bzl", "py_test")
load("@drake//:.os.bzl", OS_NAME = "NAME")

cc_test(
    name = "text_logging_test",
    srcs = ["text_logging_test.cc"],
    # TODO(jwnimmer-tri) On macOS, we need to pkg-config fmt for this to pass.
    # For the moment, we'll say that :drake_shared_library is Ubuntu-only.
    tags = ["manual"] if OS_NAME == "mac os x" else [],
    deps = ["@drake//:drake_shared_library"],
)

py_test(
    name = "find_resource_test",
    srcs = ["find_resource_test.py"],
    size = "small",
    deps = ["@drake//bindings/pydrake"],
)

py_test(
    name = "import_all_test",
    srcs = ["import_all_test.py"],
    size = "small",
    deps = ["@drake//bindings/pydrake"],
)

# A stub for unit testing; not required for end users.
filegroup(name = "dummy_filegroup")
""")

    with open(join(scratch_dir, "text_logging_test.cc"), "w") as f:
        f.write("""
#include <drake/common/text_logging.h>
int main(int argc, char** argv) {
  drake::log()->info("Hello {}", "world");
  return 0;
}
""")

    with open(join(scratch_dir, "find_resource_test.py"), "w") as f:
        f.write("""
from pydrake.common import FindResourceOrThrow, set_log_level
set_log_level("trace")
FindResourceOrThrow("drake/examples/pendulum/Pendulum.urdf")
""")

    with open(join(scratch_dir, "import_all_test.py"), "w") as f:
        f.write("""
import pydrake.all
""")

    # Check that a full `bazel test` passes.
    command = "test"
    if sys.platform.startswith("darwin"):
        # TODO(jwnimmer-tri) A `test //...` doesn't pass yet on macOS.
        command = "build"
    subprocess.check_call(
        cwd=scratch_dir,
        args=[
            "bazel",
            # Use "release engineering" options for hermeticity.
            # https://docs.bazel.build/versions/master/user-manual.html#bazel-releng
            "--bazelrc=/dev/null",
            # Encourage the server to exit after the test completes.
            "--max_idle_secs=1",
            # Run all of the tests from the BUILD.bazel generated above.
            command,
            "//...",
            "--jobs=1",
            # Enable verbosity.
            "--announce_rc",
            "--test_output=streamed",
            # Use "release engineering" options for hermeticity.
            "--nokeep_state_after_build",
            # Nerf the coverage reporter to avoid downloading the entire JDK afresh
            # from the internet every time we run this test.
            "--coverage_report_generator=//:dummy_filegroup",
        ])
    def test_install_and_run(self):
        install_dir = install_test_helper.get_install_dir()
        resource_subfolder = "share/drake/"
        installed_sentinel = os.path.join(install_dir,
                                          resource_subfolder,
                                          ".drake-find_resource-sentinel")
        # Verifies that the sentinel file exists in the installed directory.
        # If it has been removed, we need to update this test.
        self.assertTrue(os.path.isfile(installed_sentinel))
        # Create a resource in the temporary directory.
        tmp_dir = install_test_helper.create_temporary_dir()

        resource_folder = os.path.join(tmp_dir, resource_subfolder)
        test_folder = os.path.join(resource_folder, "common/test")
        os.makedirs(test_folder)
        # Create sentinel file.
        sentinel = os.path.join(resource_folder,
                                os.path.basename(installed_sentinel))
        os.symlink(installed_sentinel, sentinel)
        # Create resource file.
        resource = os.path.join(test_folder, "tmp_resource")
        resource_data = "tmp_resource"
        with open(resource, "w") as f:
            f.write(resource_data)

        # Cross-check the resource root environment variable name.
        env_name = "DRAKE_RESOURCE_ROOT"
        resource_tool = os.path.join(
            install_dir, "share/drake/common/resource_tool")
        output_name = install_test_helper.check_output(
            [resource_tool,
             "--print_resource_root_environment_variable_name",
             ],
            ).strip()
        self.assertEqual(output_name, env_name)

        # Use the installed resource_tool to find a resource.
        tool_env = dict(os.environ)
        tool_env[env_name] = os.path.join(tmp_dir, "share")
        absolute_path = install_test_helper.check_output(
            [resource_tool,
             "--print_resource_path",
             "drake/common/test/tmp_resource",
             ],
            env=tool_env,
            ).strip()
        with open(absolute_path, 'r') as data:
            self.assertEqual(data.read(), resource_data)

        # Use the installed resource_tool to find a directory.
        # (This feature is deprecated and will eventually be removed.)
        absolute_path = install_test_helper.check_output(
            [resource_tool,
             "--print_resource_path",
             "drake/common/test",
             ],
            env=tool_env,
            ).strip()
        with open(absolute_path + '/tmp_resource', 'r') as data:
            self.assertEqual(data.read(), resource_data)

        # Use the installed resource_tool to find a resource, but with a bogus
        # DRAKE_RESOURCE_ROOT that should be ignored.
        tool_env[env_name] = os.path.join(tmp_dir, "share", "drake")
        full_text = install_test_helper.check_output(
            [resource_tool,
             "--print_resource_path",
             "drake/examples/pendulum/Pendulum.urdf",
             ],
            env=tool_env,
            stderr=subprocess.STDOUT,
            ).strip()
        warning = full_text.splitlines()[0]
        absolute_path = full_text.splitlines()[-1]
        self.assertIn("FindResource ignoring DRAKE_RESOURCE_ROOT", warning)
        self.assertTrue(os.path.exists(absolute_path),
                        absolute_path + " does not exist")

        # Use --add_resource_search_path instead of environment variable
        # to find resources.
        # (This feature is deprecated and will eventually be removed.)
        absolute_path = install_test_helper.check_output(
            [resource_tool,
             "--print_resource_path",
             "drake/common/test/tmp_resource",
             "--add_resource_search_path",
             os.path.join(tmp_dir, "share"),
             ],
            ).strip()
        with open(absolute_path, 'r') as data:
            self.assertEqual(data.read(), resource_data)
def main():
    install_dir = install_test_helper.get_install_dir()

    # In scratch, mock up a drake_bazel_installed workspace.
    scratch_dir = install_test_helper.create_temporary_dir("scratch")
    with open(join(scratch_dir, "WORKSPACE"), "w") as f:
        f.write(f"""
workspace(name = "scratch")
new_local_repository(
    name = "drake_binary",
    path = "{install_dir}",
    build_file_content = "#",
)
load(
    "@drake_binary//:share/drake/repo.bzl",
    "drake_repository",
)
drake_repository(name = "drake")
""")

    with open(join(scratch_dir, "BUILD.bazel"), "w") as f:
        f.write(f"""
py_test(
    name = "find_resource_test",
    srcs = ["find_resource_test.py"],
    size = "small",
    deps = ["@drake//bindings/pydrake"],
)

py_test(
    name = "import_all_test",
    srcs = ["import_all_test.py"],
    size = "small",
    deps = ["@drake//bindings/pydrake"],
)

# A stub for unit testing; not required for end users.
filegroup(name = "dummy_filegroup")
""")

    with open(join(scratch_dir, "find_resource_test.py"), "w") as f:
        f.write(f"""
from pydrake.common import FindResourceOrThrow, set_log_level
set_log_level("trace")
FindResourceOrThrow("drake/examples/pendulum/Pendulum.urdf")
""")

    with open(join(scratch_dir, "import_all_test.py"), "w") as f:
        f.write(f"""
import pydrake.all
""")

    # Check that a full `bazel test` passes.
    command = "test"
    if sys.platform.startswith("darwin"):
        # TODO(jwnimmer-tri) A `test //...` doesn't pass yet on macOS.
        command = "build"
    subprocess.check_call(
        cwd=scratch_dir,
        args=[
            "bazel",
            # Use "release engineering" options for hermeticity.
            # https://docs.bazel.build/versions/master/user-manual.html#bazel-releng
            "--bazelrc=/dev/null",
            # Encourage the server to exit after the test completes.
            "--max_idle_secs=1",
            # Run all of the tests from the BUILD.bazel generated above.
            command,
            "//...",
            "--jobs=1",
            # Enable verbosity.
            "--announce_rc",
            # Use "release engineering" options for hermeticity.
            "--nokeep_state_after_build",
            # Nerf the coverage reporter to avoid downloading the entire JDK afresh
            # from the internet every time we run this test.
            "--coverage_report_generator=//:dummy_filegroup",
        ])
    def test_install_and_run(self):
        install_dir = install_test_helper.get_install_dir()
        resource_subfolder = "share/drake/"
        installed_sentinel = os.path.join(install_dir,
                                          resource_subfolder,
                                          ".drake-find_resource-sentinel")
        # Verifies that the sentinel file exists in the installed directory.
        # If it has been removed, we need to update this test.
        self.assertTrue(os.path.isfile(installed_sentinel))
        # Create a resource in the temporary directory.
        tmp_dir = install_test_helper.create_temporary_dir()

        resource_folder = os.path.join(tmp_dir, resource_subfolder)
        test_folder = os.path.join(resource_folder, "common/test")
        os.makedirs(test_folder)
        # Create sentinel file.
        sentinel = os.path.join(resource_folder,
                                os.path.basename(installed_sentinel))
        os.symlink(installed_sentinel, sentinel)
        # Create resource file.
        resource = os.path.join(test_folder, "tmp_resource")
        resource_data = "tmp_resource"
        with open(resource, "w") as f:
            f.write(resource_data)

        # Cross-check the resource root environment variable name.
        env_name = "DRAKE_RESOURCE_ROOT"
        resource_tool = os.path.join(
            install_dir, "share/drake/common/resource_tool")
        output_name = install_test_helper.check_output(
            [resource_tool,
             "--print_resource_root_environment_variable_name",
             ],
            ).strip()
        self.assertEqual(output_name, env_name)

        # Use the installed resource_tool to find a resource.
        tool_env = dict(os.environ)
        tool_env[env_name] = os.path.join(tmp_dir, "share")
        absolute_path = install_test_helper.check_output(
            [resource_tool,
             "--print_resource_path",
             "drake/common/test/tmp_resource",
             ],
            env=tool_env,
            ).strip()
        with open(absolute_path, 'r') as data:
            self.assertEqual(data.read(), resource_data)

        # Use --add_resource_search_path instead of environment variable
        # to find resources.
        absolute_path = install_test_helper.check_output(
            [resource_tool,
             "--print_resource_path",
             "drake/common/test/tmp_resource",
             "--add_resource_search_path",
             os.path.join(tmp_dir, "share"),
             ],
            ).strip()
        with open(absolute_path, 'r') as data:
            self.assertEqual(data.read(), resource_data)
 def test_create_temporary_dir(self):
     subdirectory_name = "tmp"
     tmp_dir = install_test_helper.create_temporary_dir(subdirectory_name)
     self.assertIn(subdirectory_name, tmp_dir)
     self.assertTrue(os.path.isdir(tmp_dir))
Beispiel #8
0
def main():
    install_dir = install_test_helper.get_install_dir()

    # In scratch, mock up a drake_bazel_installed workspace.
    scratch_dir = install_test_helper.create_temporary_dir("scratch")

    # TODO(jamiesnape): Automatically keep this synchronized with the version
    # used by @drake (or the nearest stable version).
    rules_python_commit = "0.2.0"
    rules_python_url = f"https://github.com/bazelbuild/rules_python/archive/{rules_python_commit}.tar.gz"  # noqa
    rules_python_sha256 = "0d25ab1c7b18b3f48d1bff97bfa70c1625438b40c5f661946fb43eca4ba9d9dd"  # noqa

    with open(join(scratch_dir, "WORKSPACE"), "w") as f:
        f.write(f"""
workspace(name = "scratch")

load(
    "@bazel_tools//tools/build_defs/repo:http.bzl",
    "http_archive",
)
http_archive(
    name = "rules_python",
    sha256 = "{rules_python_sha256}",
    strip_prefix = "rules_python-{rules_python_commit}",
    url = "{rules_python_url}",
)
load(
    "@rules_python//python:repositories.bzl",
    "py_repositories",
)
py_repositories()

new_local_repository(
    name = "drake_binary",
    path = "{install_dir}",
    build_file_content = "#",
)
load(
    "@drake_binary//:share/drake/repo.bzl",
    "drake_repository",
)
drake_repository(name = "drake")
""")

    with open(join(scratch_dir, "BUILD.bazel"), "w") as f:
        f.write(f"""
load("@rules_python//python:defs.bzl", "py_test")

py_test(
    name = "find_resource_test",
    srcs = ["find_resource_test.py"],
    size = "small",
    deps = ["@drake//bindings/pydrake"],
)

py_test(
    name = "import_all_test",
    srcs = ["import_all_test.py"],
    size = "small",
    deps = ["@drake//bindings/pydrake"],
)

# A stub for unit testing; not required for end users.
filegroup(name = "dummy_filegroup")
""")

    with open(join(scratch_dir, "find_resource_test.py"), "w") as f:
        f.write(f"""
from pydrake.common import FindResourceOrThrow, set_log_level
set_log_level("trace")
FindResourceOrThrow("drake/examples/pendulum/Pendulum.urdf")
""")

    with open(join(scratch_dir, "import_all_test.py"), "w") as f:
        f.write(f"""
import pydrake.all
""")

    # Check that a full `bazel test` passes.
    command = "test"
    if sys.platform.startswith("darwin"):
        # TODO(jwnimmer-tri) A `test //...` doesn't pass yet on macOS.
        command = "build"
    subprocess.check_call(
        cwd=scratch_dir,
        args=[
            "bazel",
            # Use "release engineering" options for hermeticity.
            # https://docs.bazel.build/versions/master/user-manual.html#bazel-releng
            "--bazelrc=/dev/null",
            # Encourage the server to exit after the test completes.
            "--max_idle_secs=1",
            # Run all of the tests from the BUILD.bazel generated above.
            command,
            "//...",
            "--jobs=1",
            # Enable verbosity.
            "--announce_rc",
            # Use "release engineering" options for hermeticity.
            "--nokeep_state_after_build",
            # Nerf the coverage reporter to avoid downloading the entire JDK afresh
            # from the internet every time we run this test.
            "--coverage_report_generator=//:dummy_filegroup",
        ])
 def test_create_temporary_dir(self):
     subdirectory_name = "tmp"
     tmp_dir = install_test_helper.create_temporary_dir(subdirectory_name)
     self.assertIn(subdirectory_name, tmp_dir)
     self.assertTrue(os.path.isdir(tmp_dir))