diff --git a/tests/acceptance-tests/server_configuration_options.cpp b/tests/acceptance-tests/server_configuration_options.cpp index dfa01a8f71..6b34d1f7bc 100644 --- a/tests/acceptance-tests/server_configuration_options.cpp +++ b/tests/acceptance-tests/server_configuration_options.cpp @@ -18,6 +18,8 @@ #include "mir/options/option.h" +#include "mir/test/file_utils.h" + #include #include @@ -29,15 +31,6 @@ using namespace testing; namespace { -std::string create_temp_dir() -{ - char temp_dir[] = "temp_dir_XXXXXX"; - if (mkdtemp(temp_dir) == nullptr) - throw std::system_error(errno, std::system_category(), "Failed to create temp dir"); - - return temp_dir; -} - struct ServerConfigurationOptions : mir_test_framework::HeadlessTest { MOCK_METHOD(void, command_line_handler, (std::vector const&)); @@ -102,7 +95,7 @@ struct ServerConfigurationOptions : mir_test_framework::HeadlessTest remove(temp_dir.c_str()); } - std::string const temp_dir = create_temp_dir(); + std::string const temp_dir = mir::test::create_temp_dir(); std::string const fake_xdg_config_home = temp_dir + "/fake_xdg_config_home"; std::string const fake_home = temp_dir + "/fake_home"; std::string const fake_home_config = temp_dir + "/fake_home/.config"; diff --git a/tests/include/mir/test/file_utils.h b/tests/include/mir/test/file_utils.h new file mode 100644 index 0000000000..6d500eaa54 --- /dev/null +++ b/tests/include/mir/test/file_utils.h @@ -0,0 +1,29 @@ +/* + * Copyright © Canonical Ltd. + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2 or 3 as + * published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#ifndef MIR_TEST_FILE_UTILS_H_ +#define MIR_TEST_FILE_UTILS_H_ + +namespace mir +{ +namespace test +{ +std::string create_temp_file(); +std::string create_temp_dir(); +} +} + +#endif // MIR_TEST_FILE_UTILS_H_ diff --git a/tests/mir_test/CMakeLists.txt b/tests/mir_test/CMakeLists.txt index c0c89187f7..04380945ea 100644 --- a/tests/mir_test/CMakeLists.txt +++ b/tests/mir_test/CMakeLists.txt @@ -9,6 +9,7 @@ add_library(mir-public-test OBJECT display_config_matchers.cpp event_factory.cpp event_matchers.cpp + file_utils.cpp pipe.cpp popen.cpp signal.cpp diff --git a/tests/mir_test/file_utils.cpp b/tests/mir_test/file_utils.cpp new file mode 100644 index 0000000000..6327c6c09f --- /dev/null +++ b/tests/mir_test/file_utils.cpp @@ -0,0 +1,44 @@ +/* + * Copyright © Canonical Ltd. + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2 or 3 as + * published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#include +#include + +#include "mir/test/file_utils.h" + +std::string mir::test::create_temp_file() +{ + char temp_file[] = "/tmp/temp_file_XXXXXX"; + if (int fd = mkstemp(temp_file) == -1) + { + throw std::system_error(errno, std::system_category(), "Failed to create temp file"); + } + else + { + close(fd); + } + + return temp_file; +} + +std::string mir::test::create_temp_dir() +{ + char temp_dir[] = "/tmp/temp_dir_XXXXXX"; + if (mkdtemp(temp_dir) == nullptr) + throw std::system_error(errno, std::system_category(), "Failed to create temp dir"); + + return temp_dir; +} diff --git a/tests/miral/CMakeLists.txt b/tests/miral/CMakeLists.txt index e7d52891c6..bf3314dc26 100644 --- a/tests/miral/CMakeLists.txt +++ b/tests/miral/CMakeLists.txt @@ -80,6 +80,9 @@ mir_generate_protocol_wrapper(miral-test "org_kde_kwin_" server-decoration.xml) set_source_files_properties(server_example_decoration.cpp PROPERTIES COMPILE_FLAGS "-I${CMAKE_CURRENT_BINARY_DIR}") +target_include_directories(miral-test + PRIVATE ${PROJECT_SOURCE_DIR}/tests/include) + target_link_libraries(miral-test mir-test-assist PkgConfig::WAYLAND_CLIENT diff --git a/tests/miral/external_client.cpp b/tests/miral/external_client.cpp index c182803b6b..54029bc460 100644 --- a/tests/miral/external_client.cpp +++ b/tests/miral/external_client.cpp @@ -18,6 +18,8 @@ #include #include +#include "mir/test/file_utils.h" + #include #include #include @@ -43,7 +45,7 @@ struct ExternalClient : miral::TestServer miral::X11Support x11_disabled_by_default{}; miral::X11Support x11_enabled_by_default{miral::X11Support{}.default_to_enabled()}; - std::string const output = tmpnam(nullptr); + std::string const output = mir::test::create_temp_file(); auto client_env_value(std::string const& key) const -> std::string { diff --git a/tests/miral/static_display_config.cpp b/tests/miral/static_display_config.cpp index 89830f0853..02dc9dad3f 100644 --- a/tests/miral/static_display_config.cpp +++ b/tests/miral/static_display_config.cpp @@ -19,6 +19,7 @@ #include "mir/logging/logger.h" #include +#include "mir/test/file_utils.h" #include #include @@ -151,7 +152,9 @@ struct StaticDisplayConfig : Test TEST_F(StaticDisplayConfig, nonexistent_config_file_is_no_error) { - miral::StaticDisplayConfig{tmpnam(nullptr)}; + auto filename = mir::test::create_temp_file(); + unlink(filename.c_str()); + miral::StaticDisplayConfig{filename}; } TEST_F(StaticDisplayConfig, empty_config_input_causes_AbnormalExit) diff --git a/tests/unit-tests/frontend_wayland/test_desktop_file_manager.cpp b/tests/unit-tests/frontend_wayland/test_desktop_file_manager.cpp index 8ed4174a2c..e56287b22b 100644 --- a/tests/unit-tests/frontend_wayland/test_desktop_file_manager.cpp +++ b/tests/unit-tests/frontend_wayland/test_desktop_file_manager.cpp @@ -19,6 +19,7 @@ #include "mir/test/doubles/mock_scene_session.h" #include "mir/scene/surface_observer.h" #include "mir/test/doubles/explicit_executor.h" +#include "mir/test/file_utils.h" #include "mir_test_framework/open_wrapper.h" #include @@ -194,7 +195,7 @@ TEST_F(DesktopFileManager, can_resolve_from_valid_flatpak_info) std::stringstream flatpak_info_path; flatpak_info_path << "/proc/" << PID << "/root/.flatpak-info"; auto const flatpak_info = flatpak_info_path.str(); - auto tmp_file_name = std::tmpnam(NULL); + auto tmp_file_name = mir::test::create_temp_file().c_str(); { std::ofstream tmp_file; tmp_file.open(tmp_file_name); @@ -228,7 +229,7 @@ TEST_F(DesktopFileManager, app_id_will_not_resolve_from_flatpak_info_when_name_i std::stringstream flatpak_info_path; flatpak_info_path << "/proc/" << PID << "/root/.flatpak-info"; auto const flatpak_info = flatpak_info_path.str(); - auto tmp_file_name = std::tmpnam(NULL); + auto tmp_file_name = mir::test::create_temp_file().c_str(); { std::ofstream tmp_file; tmp_file.open(tmp_file_name); diff --git a/tests/unit-tests/frontend_wayland/test_g_desktop_file_cache.cpp b/tests/unit-tests/frontend_wayland/test_g_desktop_file_cache.cpp index 8a426a9806..10674a14f8 100644 --- a/tests/unit-tests/frontend_wayland/test_g_desktop_file_cache.cpp +++ b/tests/unit-tests/frontend_wayland/test_g_desktop_file_cache.cpp @@ -18,6 +18,7 @@ #include "src/server/frontend_wayland/foreign_toplevel_manager_v1.h" #include "mir/test/doubles/stub_main_loop.h" +#include "mir/test/file_utils.h" #include #include @@ -43,9 +44,7 @@ struct GDesktopFileCache : Test static void SetUpTestSuite() { // Establish the temporary directory - std::filesystem::path tmp_dir_path {std::filesystem::temp_directory_path() /= std::tmpnam(nullptr)}; - std::filesystem::create_directories(tmp_dir_path); - std::string desktop_file_directory_name = tmp_dir_path; + std::string desktop_file_directory_name = mir::test::create_temp_dir(); setenv("XDG_DATA_DIRS", desktop_file_directory_name.c_str(), 1); applications_dir = desktop_file_directory_name + "/applications"; mkdir(applications_dir.c_str(), S_IRWXU);