1
0
Fork 0
mirror of https://github.com/NixOS/nix synced 2024-09-19 10:50:24 -04:00

Add StoreReference::render

This will be needed for the next step.

Also allows us to write round trip tests.
This commit is contained in:
John Ericson 2024-01-23 15:36:44 -05:00
parent c036d75f9e
commit b59a7a14c4
15 changed files with 233 additions and 3 deletions

View file

@ -15,7 +15,7 @@ SpaceAfterCStyleCast: true
SpaceAfterTemplateKeyword: false
AccessModifierOffset: -4
AlignAfterOpenBracket: AlwaysBreak
AlignEscapedNewlines: DontAlign
AlignEscapedNewlines: Left
ColumnLimit: 120
BreakStringLiterals: false
BitFieldColonSpacing: None

View file

@ -17,7 +17,8 @@
excludes = [
# We don't want to format test data
# ''tests/(?!nixos/).*\.nix''
''^tests/.*''
''^tests/functional/.*$''
''^tests/unit/[^/]*/data/.*$''
# Don't format vendored code
''^src/toml11/.*''
@ -426,6 +427,69 @@
''^src/nix/upgrade-nix\.cc$''
''^src/nix/verify\.cc$''
''^src/nix/why-depends\.cc$''
''^tests/nixos/ca-fd-leak/sender\.c''
''^tests/nixos/ca-fd-leak/smuggler\.c''
''^tests/unit/libexpr-support/tests/libexpr\.hh''
''^tests/unit/libexpr-support/tests/value/context\.cc''
''^tests/unit/libexpr-support/tests/value/context\.hh''
''^tests/unit/libexpr/derived-path\.cc''
''^tests/unit/libexpr/error_traces\.cc''
''^tests/unit/libexpr/eval\.cc''
''^tests/unit/libexpr/flake/flakeref\.cc''
''^tests/unit/libexpr/flake/url-name\.cc''
''^tests/unit/libexpr/json\.cc''
''^tests/unit/libexpr/main\.cc''
''^tests/unit/libexpr/primops\.cc''
''^tests/unit/libexpr/search-path\.cc''
''^tests/unit/libexpr/trivial\.cc''
''^tests/unit/libexpr/value/context\.cc''
''^tests/unit/libexpr/value/print\.cc''
''^tests/unit/libfetchers/public-key\.cc''
''^tests/unit/libstore-support/tests/derived-path\.cc''
''^tests/unit/libstore-support/tests/derived-path\.hh''
''^tests/unit/libstore-support/tests/libstore\.hh''
''^tests/unit/libstore-support/tests/nix_api_store\.hh''
''^tests/unit/libstore-support/tests/outputs-spec\.cc''
''^tests/unit/libstore-support/tests/outputs-spec\.hh''
''^tests/unit/libstore-support/tests/path\.cc''
''^tests/unit/libstore-support/tests/path\.hh''
''^tests/unit/libstore-support/tests/protocol\.hh''
''^tests/unit/libstore/common-protocol\.cc''
''^tests/unit/libstore/content-address\.cc''
''^tests/unit/libstore/derivation\.cc''
''^tests/unit/libstore/derived-path\.cc''
''^tests/unit/libstore/downstream-placeholder\.cc''
''^tests/unit/libstore/machines\.cc''
''^tests/unit/libstore/nar-info-disk-cache\.cc''
''^tests/unit/libstore/nar-info\.cc''
''^tests/unit/libstore/outputs-spec\.cc''
''^tests/unit/libstore/path-info\.cc''
''^tests/unit/libstore/path\.cc''
''^tests/unit/libstore/serve-protocol\.cc''
''^tests/unit/libstore/worker-protocol\.cc''
''^tests/unit/libutil-support/tests/characterization\.hh''
''^tests/unit/libutil-support/tests/hash\.cc''
''^tests/unit/libutil-support/tests/hash\.hh''
''^tests/unit/libutil/args\.cc''
''^tests/unit/libutil/canon-path\.cc''
''^tests/unit/libutil/chunked-vector\.cc''
''^tests/unit/libutil/closure\.cc''
''^tests/unit/libutil/compression\.cc''
''^tests/unit/libutil/config\.cc''
''^tests/unit/libutil/file-content-address\.cc''
''^tests/unit/libutil/git\.cc''
''^tests/unit/libutil/hash\.cc''
''^tests/unit/libutil/hilite\.cc''
''^tests/unit/libutil/json-utils\.cc''
''^tests/unit/libutil/logging\.cc''
''^tests/unit/libutil/lru-cache\.cc''
''^tests/unit/libutil/pool\.cc''
''^tests/unit/libutil/references\.cc''
''^tests/unit/libutil/suggestions\.cc''
''^tests/unit/libutil/tests\.cc''
''^tests/unit/libutil/url\.cc''
''^tests/unit/libutil/xml-writer\.cc''
];
};

View file

@ -4,6 +4,7 @@
#include "url.hh"
#include "store-reference.hh"
#include "file-system.hh"
#include "util.hh"
namespace nix {
@ -17,6 +18,29 @@ static bool isNonUriPath(const std::string & spec)
&& spec.find("/") != std::string::npos;
}
std::string StoreReference::render() const
{
std::string res;
std::visit(
overloaded{
[&](const StoreReference::Auto &) { res = "auto"; },
[&](const StoreReference::Specified & g) {
res = g.scheme;
res += "://";
res += g.authority;
},
},
variant);
if (!params.empty()) {
res += "?";
res += encodeQuery(params);
}
return res;
}
StoreReference StoreReference::parse(const std::string & uri, const StoreReference::Params & extraParams)
{
auto params = extraParams;

View file

@ -58,7 +58,7 @@ struct StoreReference
struct Specified
{
std::string scheme;
std::string authority;
std::string authority = "";
bool operator==(const Specified & rhs) const = default;
auto operator<=>(const Specified & rhs) const = default;
@ -73,6 +73,14 @@ struct StoreReference
bool operator==(const StoreReference & rhs) const = default;
auto operator<=>(const StoreReference & rhs) const = default;
/**
* Render the whole store reference as a URI, including parameters.
*/
std::string render() const;
/**
* Parse a URI into a store reference.
*/
static StoreReference parse(const std::string & uri, const Params & extraParams = Params{});
};

View file

@ -33,6 +33,8 @@ std::string percentEncode(std::string_view s, std::string_view keep="");
std::map<std::string, std::string> decodeQuery(const std::string & query);
std::string encodeQuery(const std::map<std::string, std::string> & query);
ParsedURL parseURL(const std::string & url);
/**

View file

@ -0,0 +1 @@
auto

View file

@ -0,0 +1 @@
auto?root=/foo/bar/baz

View file

@ -0,0 +1 @@
local://?root=/foo/bar/baz

View file

@ -0,0 +1 @@
local:///foo/bar/baz?trusted=true

View file

@ -0,0 +1 @@
local?root=/foo/bar/baz

View file

@ -0,0 +1 @@
/foo/bar/baz?trusted=true

View file

@ -0,0 +1 @@
ssh://localhost

View file

@ -0,0 +1 @@
unix://?max-connections=7&trusted=true

View file

@ -0,0 +1 @@
daemon?max-connections=7&trusted=true

View file

@ -0,0 +1,123 @@
#include <nlohmann/json.hpp>
#include <gtest/gtest.h>
#include "file-system.hh"
#include "store-reference.hh"
#include "tests/characterization.hh"
#include "tests/libstore.hh"
namespace nix {
using nlohmann::json;
class StoreReferenceTest : public CharacterizationTest, public LibStoreTest
{
Path unitTestData = getUnitTestData() + "/store-reference";
Path goldenMaster(PathView testStem) const override
{
return unitTestData + "/" + testStem + ".txt";
}
};
#define URI_TEST_READ(STEM, OBJ) \
TEST_F(StoreReferenceTest, PathInfo_##STEM##_from_uri) \
{ \
readTest(#STEM, ([&](const auto & encoded) { \
StoreReference expected = OBJ; \
auto got = StoreReference::parse(encoded); \
ASSERT_EQ(got, expected); \
})); \
}
#define URI_TEST_WRITE(STEM, OBJ) \
TEST_F(StoreReferenceTest, PathInfo_##STEM##_to_uri) \
{ \
writeTest( \
#STEM, \
[&]() -> StoreReference { return OBJ; }, \
[](const auto & file) { return StoreReference::parse(readFile(file)); }, \
[](const auto & file, const auto & got) { return writeFile(file, got.render()); }); \
}
#define URI_TEST(STEM, OBJ) \
URI_TEST_READ(STEM, OBJ) \
URI_TEST_WRITE(STEM, OBJ)
URI_TEST(
auto,
(StoreReference{
.variant = StoreReference::Auto{},
.params = {},
}))
URI_TEST(
auto_param,
(StoreReference{
.variant = StoreReference::Auto{},
.params =
{
{"root", "/foo/bar/baz"},
},
}))
static StoreReference localExample_1{
.variant =
StoreReference::Specified{
.scheme = "local",
},
.params =
{
{"root", "/foo/bar/baz"},
},
};
static StoreReference localExample_2{
.variant =
StoreReference::Specified{
.scheme = "local",
.authority = "/foo/bar/baz",
},
.params =
{
{"trusted", "true"},
},
};
URI_TEST(local_1, localExample_1)
URI_TEST(local_2, localExample_2)
URI_TEST_READ(local_shorthand_1, localExample_1)
URI_TEST_READ(local_shorthand_2, localExample_2)
static StoreReference unixExample{
.variant =
StoreReference::Specified{
.scheme = "unix",
},
.params =
{
{"max-connections", "7"},
{"trusted", "true"},
},
};
URI_TEST(unix, unixExample)
URI_TEST_READ(unix_shorthand, unixExample)
URI_TEST(
ssh,
(StoreReference{
.variant =
StoreReference::Specified{
.scheme = "ssh",
.authority = "localhost",
},
.params = {},
}))
}