Skip to content

Commit

Permalink
Strip out trailing newlines when reading object type.
Browse files Browse the repository at this point in the history
  • Loading branch information
LTLA committed Nov 20, 2023
1 parent 973d2bd commit 0905bd0
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
5 changes: 5 additions & 0 deletions include/takane/utils_public.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@ inline std::string read_object_type(const std::filesystem::path& path) {
size_t available = reader.available();
output.insert(output.end(), buffer, buffer + available);
}

// Removing trailing newline.
if (output.size() && output.back() == '\n') {
output.pop_back();
}
return output;
}

Expand Down
22 changes: 22 additions & 0 deletions tests/src/dispatch.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,28 @@

#include <filesystem>

TEST(ReadObjectTest, Basic) {
std::filesystem::path dir = "TEST_readObj";
if (std::filesystem::exists(dir)) {
std::filesystem::remove_all(dir);
}
std::filesystem::create_directory(dir);

// Works with a trailing newline.
auto objpath = dir / "OBJECT";
{
std::ofstream output(objpath);
output << "foo_bar 2\n";
}
EXPECT_EQ(takane::read_object_type(dir), "foo_bar 2");

{
std::ofstream output(objpath);
output << "baz-stuff";
}
EXPECT_EQ(takane::read_object_type(dir), "baz-stuff");
}

TEST(GenericDispatch, Validate) {
std::filesystem::path dir = "TEST_dispatcher";
initialize_directory(dir, "foobar");
Expand Down

0 comments on commit 0905bd0

Please sign in to comment.