Skip to content

Commit b23c691

Browse files
committed
cpp impl: Add Errorcode InvalidArgument
Add InvalidArgument errorcode, if dir argument is invalid
1 parent 426ebac commit b23c691

File tree

4 files changed

+50
-8
lines changed

4 files changed

+50
-8
lines changed

src/cpp/inc/kvs.hpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,10 @@ enum class MyErrorCode : score::result::ErrorCode {
9090
MutexLockFailed,
9191

9292
/* Invalid value type*/
93-
InvalidValueType
93+
InvalidValueType,
94+
95+
/* Invalid argument*/
96+
InvalidArgument
9497
};
9598

9699
class MyErrorDomain final : public score::result::ErrorDomain
@@ -579,6 +582,9 @@ class KvsBuilder final {
579582
/**
580583
* @brief Specify the directory where KVS files are stored.
581584
* @param dir The directory path as a string.
585+
* Important: The directory path must end with a '/' to be valid.
586+
* Use "./" for the current directory.
587+
*
582588
* @return Reference to this builder (for chaining).
583589
*/
584590
KvsBuilder& dir(std::string&& dir_path);

src/cpp/src/kvs.cpp

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -435,12 +435,20 @@ KvsBuilder& KvsBuilder::dir(std::string&& dir_path) {
435435

436436

437437
score::Result<Kvs> KvsBuilder::build() const {
438-
auto result = Kvs::open(
439-
instance_id,
440-
need_defaults ? OpenNeedDefaults::Required : OpenNeedDefaults::Optional,
441-
need_kvs ? OpenNeedKvs::Required : OpenNeedKvs::Optional,
442-
std::move(directory)
443-
);
438+
score::Result<Kvs> result = score::MakeUnexpected(MyErrorCode::UnmappedError);
439+
440+
/* Check if directory argument is valid */
441+
if (directory.empty() || directory.back() != '/') {
442+
result = score::MakeUnexpected(MyErrorCode::InvalidArgument);
443+
} else {
444+
result = Kvs::open(
445+
instance_id,
446+
need_defaults ? OpenNeedDefaults::Required : OpenNeedDefaults::Optional,
447+
need_kvs ? OpenNeedKvs::Required : OpenNeedKvs::Optional,
448+
std::move(directory)
449+
);
450+
}
451+
444452
return result;
445453
}
446454

src/cpp/tests/bm_kvs.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,16 @@
1+
/********************************************************************************
2+
* Copyright (c) 2025 Contributors to the Eclipse Foundation
3+
*
4+
* See the NOTICE file(s) distributed with this work for additional
5+
* information regarding copyright ownership.
6+
*
7+
* This program and the accompanying materials are made available under the
8+
* terms of the Apache License Version 2.0 which is available at
9+
* https://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* SPDX-License-Identifier: Apache-2.0
12+
********************************************************************************/
13+
114
#include <benchmark/benchmark.h>
215
#include <string>
316

src/cpp/tests/test_kvs.cpp

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -666,7 +666,7 @@ TEST(kvs_MessageFor, MessageFor) {
666666

667667
}
668668

669-
TEST(kvs_kvsbuilder, kvsbuilder) {
669+
TEST(kvs_kvsbuilder, kvsbuilder_success) {
670670
/* This test also checks the kvs open function with the KvsBuilder */
671671

672672
/* Test the KvsBuilder constructor */
@@ -699,7 +699,22 @@ TEST(kvs_kvsbuilder, kvsbuilder) {
699699
result_build.value().flush_on_exit = false;
700700
std::string expected_filename_prefix = "./kvsbuilder/kvs_"+std::to_string(instance_id.id);
701701
EXPECT_EQ(result_build.value().filename_prefix, expected_filename_prefix);
702+
}
703+
704+
TEST(kvs_kvsbuilder, kvsbuilder_directory_invalid) {
705+
706+
/* Test the KvsBuilder with an empty directory */
707+
KvsBuilder builder(instance_id);
708+
builder.dir("");
709+
auto result_build = builder.build();
710+
EXPECT_FALSE(result_build);
711+
EXPECT_EQ(static_cast<MyErrorCode>(*result_build.error()), MyErrorCode::InvalidArgument);
702712

713+
/* Test the KvsBuilder with an string which doesnt end with / */
714+
builder.dir("./kvsbuilder");
715+
result_build = builder.build();
716+
EXPECT_FALSE(result_build);
717+
EXPECT_EQ(static_cast<MyErrorCode>(*result_build.error()), MyErrorCode::InvalidArgument);
703718
}
704719

705720
TEST(kvs_constructor, move_constructor) {

0 commit comments

Comments
 (0)