Skip to content

Commit

Permalink
Avoid usage of tmpnam() for creating random filename (#246)
Browse files Browse the repository at this point in the history
### Public-Facing Changes

Avoid usage of tmpnam() for creating random filename

### Description
Follow up of #232. ROS build farm complained about usage of `tmpnam`.
This PR replaces it by using the epoch time in milliseconds to create a
somewhat random filename (used for tests only)

https://build.ros2.org/job/Idev__foxglove_bridge__ubuntu_jammy_amd64/9/
  • Loading branch information
achim-k committed Jul 11, 2023
1 parent 0d712f9 commit 3933554
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 4 deletions.
8 changes: 6 additions & 2 deletions ros1_foxglove_bridge/tests/smoke_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include <future>
#include <thread>

#include <boost/filesystem.hpp>
#include <gtest/gtest.h>
#include <ros/ros.h>
#include <std_msgs/builtin_string.h>
Expand Down Expand Up @@ -347,13 +348,16 @@ TEST(FetchAssetTest, fetchExistingAsset) {
auto wsClient = std::make_shared<foxglove::Client<websocketpp::config::asio_client>>();
EXPECT_EQ(std::future_status::ready, wsClient->connect(URI).wait_for(DEFAULT_TIMEOUT));

const auto tmpFilePath = std::tmpnam(nullptr) + std::string(".txt");
const auto millisSinceEpoch = std::chrono::duration_cast<std::chrono::milliseconds>(
std::chrono::system_clock::now().time_since_epoch());
const auto tmpFilePath =
boost::filesystem::temp_directory_path() / std::to_string(millisSinceEpoch.count());
constexpr char content[] = "Hello, world";
FILE* tmpAssetFile = std::fopen(tmpFilePath.c_str(), "w");
std::fputs(content, tmpAssetFile);
std::fclose(tmpAssetFile);

const std::string uri = std::string("file://") + tmpFilePath;
const std::string uri = std::string("file://") + tmpFilePath.string();
const uint32_t requestId = 123;

auto future = foxglove::waitForFetchAssetResponse(wsClient);
Expand Down
8 changes: 6 additions & 2 deletions ros2_foxglove_bridge/tests/smoke_test.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include <chrono>
#include <filesystem>
#include <future>
#include <thread>

Expand Down Expand Up @@ -545,13 +546,16 @@ TEST(FetchAssetTest, fetchExistingAsset) {
auto wsClient = std::make_shared<foxglove::Client<websocketpp::config::asio_client>>();
EXPECT_EQ(std::future_status::ready, wsClient->connect(URI).wait_for(DEFAULT_TIMEOUT));

const auto tmpFilePath = std::tmpnam(nullptr) + std::string(".txt");
const auto millisSinceEpoch = std::chrono::duration_cast<std::chrono::milliseconds>(
std::chrono::system_clock::now().time_since_epoch());
const auto tmpFilePath =
std::filesystem::temp_directory_path() / std::to_string(millisSinceEpoch.count());
constexpr char content[] = "Hello, world";
FILE* tmpAssetFile = std::fopen(tmpFilePath.c_str(), "w");
std::fputs(content, tmpAssetFile);
std::fclose(tmpAssetFile);

const std::string uri = std::string("file://") + tmpFilePath;
const std::string uri = std::string("file://") + tmpFilePath.string();
const uint32_t requestId = 123;

auto future = foxglove::waitForFetchAssetResponse(wsClient);
Expand Down

0 comments on commit 3933554

Please sign in to comment.