Skip to content

Commit a225491

Browse files
committed
Fixes
1 parent 6f0b9aa commit a225491

File tree

6 files changed

+52
-51
lines changed

6 files changed

+52
-51
lines changed

test/src/unit-cppapi-profile.cc

Lines changed: 15 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -35,44 +35,33 @@
3535

3636
#include "test/support/src/temporary_local_directory.h"
3737
#include "tiledb/sm/cpp_api/profile_experimental.h"
38+
#include "tiledb/sm/rest/rest_profile.h"
3839

3940
using namespace tiledb;
4041
using namespace tiledb::test;
4142

42-
const char* name_ = tiledb::sm::RestProfile::DEFAULT_NAME.c_str();
43+
const std::string name_ = tiledb::sm::RestProfile::DEFAULT_NAME;
4344
tiledb::sm::TemporaryLocalDirectory tempdir_("unit_cppapi_profile");
4445

45-
/**
46-
* Notes to reviewer:
47-
* This test is segfaulting. I suspect it's something to do with the
48-
* destruction of the underlying object. I had a lot of trouble getting
49-
* the behavior on L91 of tiledb/sm/cpp_api/profile_experimental.h to work,
50-
* which may be a conrtibuting factor.
51-
*
52-
* Please see my other notes to reviewer on L127 of
53-
* tiledb/sm/rest/rest_profile.h for important information regarding the
54-
* get_homedir() method.
55-
*/
56-
5746
TEST_CASE(
5847
"C++ API: Profile get_name validation", "[cppapi][profile][get_name]") {
59-
auto homedir_ = tempdir_.path().c_str();
48+
const std::string homedir_ = tempdir_.path();
6049
SECTION("default, explicitly passed") {
6150
Profile p(name_, homedir_);
6251
REQUIRE(p.get_name() == name_);
6352
}
64-
/*SECTION("default, inherited from nullptr") {
65-
Profile p(nullptr, homedir_);
53+
SECTION("default, inherited from nullptr") {
54+
Profile p(std::nullopt, homedir_);
6655
REQUIRE(p.get_name() == name_);
6756
}
6857
SECTION("non-default") {
6958
const char* name = "non_default";
7059
Profile p(name, homedir_);
7160
REQUIRE(p.get_name() == name);
72-
}*/
61+
}
7362
}
7463

75-
/*TEST_CASE(
64+
TEST_CASE(
7665
"C++ API: Profile get_homedir validation",
7766
"[cppapi][profile][get_homedir]") {
7867
auto homedir_ = tempdir_.path().c_str();
@@ -81,7 +70,12 @@ TEST_CASE(
8170
REQUIRE(p.get_homedir() == homedir_);
8271
}
8372
SECTION("inherited from nullptr") {
84-
Profile p(name_, nullptr);
85-
REQUIRE(p.get_homedir() == homedir_);
73+
Profile p(name_, std::nullopt);
74+
REQUIRE(p.get_homedir() == tiledb::common::filesystem::home_directory());
8675
}
87-
}*/
76+
SECTION("non-default") {
77+
const char* homedir = "non_default";
78+
Profile p(name_, homedir);
79+
REQUIRE(p.get_homedir() == "non_default");
80+
}
81+
}

tiledb/api/c_api/profile/profile_api.cc

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -43,22 +43,20 @@ capi_return_t tiledb_profile_alloc(
4343
if (!name) {
4444
// Passing nullptr resolves to the default case.
4545
name = tiledb::sm::RestProfile::DEFAULT_NAME.c_str();
46-
}
47-
if (name[0] == '\0') {
46+
} else if (name[0] == '\0') {
4847
throw CAPIException("[tiledb_profile_alloc] Name cannot be empty.");
4948
}
5049

51-
if (homedir) {
52-
if (homedir[0] == '\0') {
53-
throw CAPIException(
54-
"[tiledb_profile_alloc] $HOME directory cannot be empty.");
55-
}
56-
*profile =
57-
tiledb_profile_t::make_handle(std::string(name), std::string(homedir));
58-
} else {
59-
*profile = tiledb_profile_t::make_handle(std::string(name));
50+
if (!homedir) {
51+
// Passing nullptr resolves to the default case.
52+
homedir = tiledb::common::filesystem::home_directory().c_str();
53+
} else if (homedir[0] == '\0') {
54+
throw CAPIException(
55+
"[tiledb_profile_alloc] $HOME directory cannot be empty.");
6056
}
6157

58+
*profile = tiledb_profile_t::make_handle(name, homedir);
59+
6260
return TILEDB_OK;
6361
}
6462

tiledb/api/c_api/profile/test/unit_capi_profile.cc

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,12 @@ TEST_CASE(
8686
REQUIRE(tiledb_status(rc) == TILEDB_OK);
8787
// Validate homedir is non-empty. The path may not be resolved when using
8888
// the default homedir (per the RestProfile::homedir() invariant).
89-
// #TODO REQUIRE(homedir[0] != '\0');
89+
const char* out_ptr;
90+
size_t out_length;
91+
rc = tiledb_string_view(homedir, &out_ptr, &out_length);
92+
REQUIRE(rc == TILEDB_OK);
93+
std::string out_str(out_ptr, out_length);
94+
CHECK(out_str == tiledb::common::filesystem::home_directory());
9095
REQUIRE_NOTHROW(tiledb_profile_free(&profile));
9196
CHECK(profile == nullptr);
9297
}

tiledb/api/c_api/string/string_api_internal.h

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -64,14 +64,6 @@ struct tiledb_string_handle_t
6464
* Ordinary constructor.
6565
* @param s A string
6666
*/
67-
explicit tiledb_string_handle_t(const std::string& s)
68-
: value_{s} {
69-
}
70-
71-
/**
72-
* Ordinary constructor.
73-
* @param s A string_view
74-
*/
7567
explicit tiledb_string_handle_t(const std::string_view s)
7668
: value_{s} {
7769
}

tiledb/sm/cpp_api/profile_experimental.h

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -70,13 +70,15 @@ class Profile {
7070
* preserve local files from in-test changes. Users may pass their own
7171
* `homedir`, but are encouraged to use `nullptr`, the default case.
7272
*
73-
* @param name The profile name, or `nullptr` for default.
74-
* @param homedir The local $HOME directory path, or `nullptr` for default.
73+
* @param name The profile name if you want to override the default.
74+
* @param homedir The local $HOME directory path if you want to override the
75+
* default.
7576
*/
76-
explicit Profile(const std::string& name, const std::string& homedir) {
77-
const char *n = nullptr, *h = nullptr;
78-
name.c_str();
79-
homedir.c_str();
77+
explicit Profile(
78+
std::optional<std::string> name = std::nullopt,
79+
std::optional<std::string> homedir = std::nullopt) {
80+
const char* n = name.has_value() ? name->c_str() : nullptr;
81+
const char* h = homedir.has_value() ? homedir->c_str() : nullptr;
8082

8183
tiledb_profile_t* capi_profile;
8284
tiledb_error_t* capi_error = nullptr;
@@ -117,7 +119,11 @@ class Profile {
117119
throw ProfileException(
118120
"Failed to retrieve profile name due to an internal error.");
119121

120-
return impl::CAPIString(&name).str();
122+
const char* out_ptr;
123+
size_t out_length;
124+
tiledb_string_view(name, &out_ptr, &out_length);
125+
std::string out_str(out_ptr, out_length);
126+
return out_str;
121127
}
122128

123129
/** Retrieves the homedir of the profile. */
@@ -128,7 +134,12 @@ class Profile {
128134
if (rc != TILEDB_OK)
129135
throw ProfileException(
130136
"Failed to retrieve profile homedir due to an internal error.");
131-
return impl::CAPIString(&homedir).str();
137+
138+
const char* out_ptr;
139+
size_t out_length;
140+
tiledb_string_view(homedir, &out_ptr, &out_length);
141+
std::string out_str(out_ptr, out_length);
142+
return out_str;
132143
}
133144

134145
private:

tiledb/sm/rest/test/unit_rest_profile.cc

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,13 +102,14 @@ struct RestProfileFx {
102102
* Returns the RestProfile at the given name from the local file,
103103
* as a json object.
104104
*/
105-
json profile_from_file_to_json(std::string filepath, std::string name) {
105+
json profile_from_file_to_json(
106+
std::string_view filepath, std::string_view name) {
106107
json data;
107108
if (std::filesystem::exists(filepath)) {
108109
std::ifstream file(filepath);
109110
file >> data;
110111
file.close();
111-
return data[name];
112+
return data[std::string(name)];
112113
} else {
113114
return data;
114115
}

0 commit comments

Comments
 (0)