Skip to content

Commit 2774e67

Browse files
authored
Merge pull request #14128 from obsidiansystems/expose-dummy-store-for-tests-somewhat
Expose some core implementation details and write a basic unit test for the dummy store
2 parents d0c017d + 9ac306c commit 2774e67

File tree

6 files changed

+87
-20
lines changed

6 files changed

+87
-20
lines changed

src/libstore-tests/dummy-store.cc

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#include <gtest/gtest.h>
2+
3+
#include "nix/store/dummy-store.hh"
4+
#include "nix/store/globals.hh"
5+
#include "nix/store/realisation.hh"
6+
7+
namespace nix {
8+
9+
TEST(DummyStore, realisation_read)
10+
{
11+
initLibStore(/*loadConfig=*/false);
12+
13+
auto store = [] {
14+
auto cfg = make_ref<DummyStoreConfig>(StoreReference::Params{});
15+
cfg->readOnly = false;
16+
return cfg->openStore();
17+
}();
18+
19+
auto drvHash = Hash::parseExplicitFormatUnprefixed(
20+
"ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad", HashAlgorithm::SHA256, HashFormat::Base16);
21+
22+
auto outputName = "foo";
23+
24+
EXPECT_EQ(store->queryRealisation({drvHash, outputName}), nullptr);
25+
}
26+
27+
} // namespace nix

src/libstore-tests/meson.build

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ sources = files(
6161
'derivation.cc',
6262
'derived-path.cc',
6363
'downstream-placeholder.cc',
64+
'dummy-store.cc',
6465
'http-binary-cache-store.cc',
6566
'legacy-ssh-store.cc',
6667
'local-binary-cache-store.cc',

src/libstore/dummy-store.cc

Lines changed: 11 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
#include "nix/util/archive.hh"
33
#include "nix/util/callback.hh"
44
#include "nix/util/memory-source-accessor.hh"
5-
#include "nix/store/dummy-store.hh"
5+
#include "nix/store/dummy-store-impl.hh"
66

77
#include <boost/unordered/concurrent_flat_map.hpp>
88

@@ -108,23 +108,14 @@ class WholeStoreViewAccessor : public SourceAccessor
108108

109109
} // namespace
110110

111-
struct DummyStore : virtual Store
111+
ref<Store> DummyStoreConfig::openStore() const
112112
{
113-
using Config = DummyStoreConfig;
114-
115-
ref<const Config> config;
116-
117-
struct PathInfoAndContents
118-
{
119-
UnkeyedValidPathInfo info;
120-
ref<MemorySourceAccessor> contents;
121-
};
113+
return openDummyStore();
114+
}
122115

123-
/**
124-
* This is map conceptually owns the file system objects for each
125-
* store object.
126-
*/
127-
boost::concurrent_flat_map<StorePath, PathInfoAndContents> contents;
116+
struct DummyStoreImpl : DummyStore
117+
{
118+
using Config = DummyStoreConfig;
128119

129120
/**
130121
* This view conceptually just borrows the file systems objects of
@@ -135,9 +126,9 @@ struct DummyStore : virtual Store
135126
*/
136127
ref<WholeStoreViewAccessor> wholeStoreView = make_ref<WholeStoreViewAccessor>();
137128

138-
DummyStore(ref<const Config> config)
129+
DummyStoreImpl(ref<const Config> config)
139130
: Store{*config}
140-
, config(config)
131+
, DummyStore{config}
141132
{
142133
wholeStoreView->setPathDisplay(config->storeDir);
143134
}
@@ -294,9 +285,9 @@ struct DummyStore : virtual Store
294285
}
295286
};
296287

297-
ref<Store> DummyStore::Config::openStore() const
288+
ref<DummyStore> DummyStore::Config::openDummyStore() const
298289
{
299-
return make_ref<DummyStore>(ref{shared_from_this()});
290+
return make_ref<DummyStoreImpl>(ref{shared_from_this()});
300291
}
301292

302293
static RegisterStoreImplementation<DummyStore::Config> regDummyStore;
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
#pragma once
2+
///@file
3+
4+
#include "nix/store/dummy-store.hh"
5+
6+
#include <boost/unordered/concurrent_flat_map.hpp>
7+
8+
namespace nix {
9+
10+
struct MemorySourceAccessor;
11+
12+
/**
13+
* Enough of the Dummy Store exposed for sake of writing unit tests
14+
*/
15+
struct DummyStore : virtual Store
16+
{
17+
using Config = DummyStoreConfig;
18+
19+
ref<const Config> config;
20+
21+
struct PathInfoAndContents
22+
{
23+
UnkeyedValidPathInfo info;
24+
ref<MemorySourceAccessor> contents;
25+
};
26+
27+
/**
28+
* This is map conceptually owns the file system objects for each
29+
* store object.
30+
*/
31+
boost::concurrent_flat_map<StorePath, PathInfoAndContents> contents;
32+
33+
DummyStore(ref<const Config> config)
34+
: Store{*config}
35+
, config(config)
36+
{
37+
}
38+
};
39+
40+
} // namespace nix

src/libstore/include/nix/store/dummy-store.hh

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55

66
namespace nix {
77

8+
struct DummyStore;
9+
810
struct DummyStoreConfig : public std::enable_shared_from_this<DummyStoreConfig>, virtual StoreConfig
911
{
1012
DummyStoreConfig(const Params & params)
@@ -42,6 +44,11 @@ struct DummyStoreConfig : public std::enable_shared_from_this<DummyStoreConfig>,
4244
return {"dummy"};
4345
}
4446

47+
/**
48+
* Same as `openStore`, just with a more precise return type.
49+
*/
50+
ref<DummyStore> openDummyStore() const;
51+
4552
ref<Store> openStore() const override;
4653

4754
StoreReference getReference() const override

src/libstore/include/nix/store/meson.build

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ headers = [ config_pub_h ] + files(
3434
'derived-path-map.hh',
3535
'derived-path.hh',
3636
'downstream-placeholder.hh',
37+
'dummy-store-impl.hh',
3738
'dummy-store.hh',
3839
'export-import.hh',
3940
'filetransfer.hh',

0 commit comments

Comments
 (0)