-
Notifications
You must be signed in to change notification settings - Fork 100
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
tests: replace tmpnam
use
#3497
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 <http://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
#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_ |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 <http://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
#include <system_error> | ||
#include <unistd.h> | ||
|
||
#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; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,6 +18,8 @@ | |
#include <miral/external_client.h> | ||
#include <miral/x11_support.h> | ||
|
||
#include "mir/test/file_utils.h" | ||
|
||
#include <gtest/gtest.h> | ||
#include <gmock/gmock.h> | ||
#include <fstream> | ||
|
@@ -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(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This could be a |
||
|
||
auto client_env_value(std::string const& key) const -> std::string | ||
{ | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,6 +19,7 @@ | |
#include "mir/logging/logger.h" | ||
|
||
#include <mir/test/doubles/mock_display_configuration.h> | ||
#include "mir/test/file_utils.h" | ||
|
||
#include <gtest/gtest.h> | ||
#include <gmock/gmock.h> | ||
|
@@ -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()); | ||
Comment on lines
+155
to
+156
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This could be |
||
miral::StaticDisplayConfig{filename}; | ||
} | ||
|
||
TEST_F(StaticDisplayConfig, empty_config_input_causes_AbnormalExit) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 <filesystem> | ||
|
@@ -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(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This could be |
||
{ | ||
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(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As above |
||
{ | ||
std::ofstream tmp_file; | ||
tmp_file.open(tmp_file_name); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So, this suffers basically all the problems that
tmpnam
has.But it's only used in test code where the problems of
tmpnam
aren't really problems, so 🤷♀️But it would be reasonably easy to replace the current uses of
tmpname
withmkstemp
directly. So even more 🤷♀️There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah this is just about silencing the linker warning. But you're right, we can just use
mkstemp
directly - thanks for the reality check.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I concur