Skip to content

Commit

Permalink
Don't rely on byteme::temp_file_path to generate test file paths.
Browse files Browse the repository at this point in the history
It's simple enough to write ourselves so no need to incur a dependency
on byteme's semi-internal testing functions.
  • Loading branch information
LTLA committed Nov 19, 2024
1 parent 5b70794 commit a2d3b95
Show file tree
Hide file tree
Showing 8 changed files with 38 additions and 28 deletions.
5 changes: 2 additions & 3 deletions tests/src/check_collection_details.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,11 @@
#include <gmock/gmock.h>

#include "gesel/check_collection_details.hpp"
#include "byteme/temp_file_path.hpp"

#include "utils.h"

TEST(CheckCollectionDetails, Success) {
auto path = byteme::temp_file_path("check_collections");
auto path = temp_file_path("check_collections");

std::string payload1 = "aaron's collection\tthis is aaron's collection\t12345\tAaron Lun\thttps://aaron.net";
std::string payload2 = "yet another collection\tsomeone else's collection\t9999\tSomeone else\thttps://someone.else.com";
Expand All @@ -24,7 +23,7 @@ TEST(CheckCollectionDetails, Success) {
}

TEST(CheckCollectionDetails, Failure) {
auto path = byteme::temp_file_path("check_collections");
auto path = temp_file_path("check_collections");

std::string payload1 = "aaron's collection\tthis is aaron's collection\t12345\tAaron Lun\thttps://aaron.net";
std::string payload2 = "yet another collection\tsomeone else's collection\t9999\tSomeone else\thttps://someone.else.com";
Expand Down
5 changes: 2 additions & 3 deletions tests/src/check_genes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,11 @@
#include <gmock/gmock.h>

#include "gesel/check_genes.hpp"
#include "byteme/temp_file_path.hpp"

#include "utils.h"

TEST(CheckGenes, Success) {
auto path = byteme::temp_file_path("check_genes") + ".gz";
auto path = temp_file_path("check_genes") + ".gz";

{
byteme::GzipFileWriter gwriter(path);
Expand All @@ -31,7 +30,7 @@ TEST(CheckGenes, Success) {
}

TEST(CheckGenes, Failure) {
auto path = byteme::temp_file_path("check_genes") + ".gz";
auto path = temp_file_path("check_genes") + ".gz";

{
byteme::GzipFileWriter gwriter(path);
Expand Down
7 changes: 3 additions & 4 deletions tests/src/check_indices.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
#include <gmock/gmock.h>

#include "gesel/check_indices.hpp"
#include "byteme/temp_file_path.hpp"

#include "utils.h"

Expand All @@ -20,7 +19,7 @@ class TestCheckIndices : public ::testing::Test {
};

TEST_F(TestCheckIndices, Success) {
auto path = byteme::temp_file_path("check_indices");
auto path = temp_file_path("check_indices");

std::string payload = "0\t123\t45\n6\n780\t1\t234\t45\n\n67\t890\n";
{
Expand Down Expand Up @@ -61,7 +60,7 @@ TEST_F(TestCheckIndices, Success) {
}

TEST_F(TestCheckIndices, RawFailure) {
auto path = byteme::temp_file_path("check_indices");
auto path = temp_file_path("check_indices");

// Basic checks for correct integer parsing.
{
Expand Down Expand Up @@ -137,7 +136,7 @@ TEST_F(TestCheckIndices, RawFailure) {
}

TEST_F(TestCheckIndices, GzipFailure) {
auto path = byteme::temp_file_path("check_indices");
auto path = temp_file_path("check_indices");

{
byteme::RawFileWriter rwriter(path);
Expand Down
5 changes: 2 additions & 3 deletions tests/src/check_set_details.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
#include <gmock/gmock.h>

#include "gesel/check_set_details.hpp"
#include "byteme/temp_file_path.hpp"

#include "utils.h"

Expand All @@ -14,7 +13,7 @@ class TestCheckSetDetails : public ::testing::Test {
};

TEST_F(TestCheckSetDetails, Success) {
auto path = byteme::temp_file_path("check_sets");
auto path = temp_file_path("check_sets");

std::string payload1 = "aaron's set\tthis is aaron's set";
std::string payload2 = "another set\tyet another set";
Expand All @@ -31,7 +30,7 @@ TEST_F(TestCheckSetDetails, Success) {
}

TEST_F(TestCheckSetDetails, Failure) {
auto path = byteme::temp_file_path("check_sets");
auto path = temp_file_path("check_sets");

std::string payload1 = "aaron's set\tthis is aaron's set";
std::string payload2 = "another set\tyet another set";
Expand Down
13 changes: 6 additions & 7 deletions tests/src/load_ranges.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,11 @@
#include <gmock/gmock.h>

#include "gesel/load_ranges.hpp"
#include "byteme/temp_file_path.hpp"

#include "utils.h"

TEST(LoadRanges, Success) {
auto path = byteme::temp_file_path("load_ranges");
auto path = temp_file_path("load_ranges");

{
{
Expand All @@ -33,7 +32,7 @@ TEST(LoadRanges, Success) {
}

TEST(LoadRanges, Failure) {
auto path = byteme::temp_file_path("load_ranges");
auto path = temp_file_path("load_ranges");

{
byteme::GzipFileWriter writer(path);
Expand Down Expand Up @@ -79,7 +78,7 @@ TEST(LoadRanges, Failure) {
}

TEST(LoadRangesWithSizes, Success) {
auto path = byteme::temp_file_path("load_ranges_with_sizes");
auto path = temp_file_path("load_ranges_with_sizes");

{
byteme::GzipFileWriter writer(path);
Expand All @@ -94,7 +93,7 @@ TEST(LoadRangesWithSizes, Success) {
}

TEST(LoadRangesWithSizes, Failure) {
auto path = byteme::temp_file_path("load_ranges_with_sizes");
auto path = temp_file_path("load_ranges_with_sizes");

{
byteme::GzipFileWriter writer(path);
Expand All @@ -118,7 +117,7 @@ TEST(LoadRangesWithSizes, Failure) {
}

TEST(LoadNamedRanges, Success) {
auto path = byteme::temp_file_path("load_named_ranges");
auto path = temp_file_path("load_named_ranges");

{
byteme::GzipFileWriter writer(path);
Expand All @@ -133,7 +132,7 @@ TEST(LoadNamedRanges, Success) {
}

TEST(LoadNamedRanges, Failure) {
auto path = byteme::temp_file_path("load_named_ranges");
auto path = temp_file_path("load_named_ranges");

{
byteme::GzipFileWriter writer(path);
Expand Down
17 changes: 17 additions & 0 deletions tests/src/utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

#include <stdexcept>
#include <string>
#include <filesystem>
#include <random>

template<typename Function_>
void expect_error(Function_ fun, std::string match) {
Expand All @@ -20,4 +22,19 @@ void expect_error(Function_ fun, std::string match) {
ASSERT_TRUE(msg.find(match) != std::string::npos) << "function did not throw an exception with message '" << match << "' (got '" << msg << "' instead)";
}

inline std::string temp_file_path(const std::string& base) {
auto tmpdir = std::filesystem::temp_directory_path();
auto prefix = tmpdir / base;
std::filesystem::path full;

std::random_device rd;
std::mt19937_64 rng(rd());
do {
full = prefix;
full += std::to_string(rng());
} while (std::filesystem::exists(full));

return full;
}

#endif
9 changes: 4 additions & 5 deletions tests/src/validate_database.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
#include <unordered_map>
#include <algorithm>

#include "byteme/temp_file_path.hpp"
#include "gesel/validate_database.hpp"
#include "utils.h"

Expand Down Expand Up @@ -181,13 +180,13 @@ class TestValidateDatabase : public ::testing::Test {
};

TEST_F(TestValidateDatabase, Basic) {
auto path = byteme::temp_file_path("validation");
auto path = temp_file_path("validation");
mock_database(path, "9606_");
gesel::validate_database(path + "/9606_", max_genes);
}

TEST_F(TestValidateDatabase, CollectionFailures) {
auto path = byteme::temp_file_path("validation");
auto path = temp_file_path("validation");
mock_database(path, "9606_");

// Verify that the collections are indeed checked.
Expand All @@ -214,7 +213,7 @@ TEST_F(TestValidateDatabase, CollectionFailures) {
}

TEST_F(TestValidateDatabase, SetFailures) {
auto path = byteme::temp_file_path("validation");
auto path = temp_file_path("validation");
mock_database(path, "9606_");

{
Expand Down Expand Up @@ -299,7 +298,7 @@ TEST_F(TestValidateDatabase, SetFailures) {
}

TEST_F(TestValidateDatabase, SetToGeneFailures) {
auto path = byteme::temp_file_path("validation");
auto path = temp_file_path("validation");
mock_database(path, "9606_");

{
Expand Down
5 changes: 2 additions & 3 deletions tests/src/validate_genes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,11 @@
#include <string>
#include <filesystem>

#include "byteme/temp_file_path.hpp"
#include "gesel/validate_genes.hpp"
#include "utils.h"

TEST(ValidateGenes, Basic) {
auto path = byteme::temp_file_path("validation");
auto path = temp_file_path("validation");
if (std::filesystem::exists(path)) {
std::filesystem::remove_all(path);
}
Expand All @@ -26,7 +25,7 @@ TEST(ValidateGenes, Basic) {
}

TEST(ValidateGenes, Failure) {
auto path = byteme::temp_file_path("validation");
auto path = temp_file_path("validation");
if (std::filesystem::exists(path)) {
std::filesystem::remove_all(path);
}
Expand Down

0 comments on commit a2d3b95

Please sign in to comment.