1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320
|
require "fileutils"
require "pathname"
require "tmpdir"
require "yaml"
require "support/fakefs"
require "librarian/config/database"
describe Librarian::Config::Database do
include ::Support::FakeFS
def write_yaml!(path, *yamlables)
path = Pathname(path)
path.dirname.mkpath unless path.dirname.directory?
File.open(path, "wb"){|f| yamlables.each{|y| YAML.dump(y, f)}}
end
let(:adapter_name) { "gem" }
let(:env) { { } }
let(:pwd) { Pathname(Dir.tmpdir) }
let(:home) { Pathname("~").expand_path }
let(:project_path) { nil }
let(:specfile_name) { nil }
let(:global) { home.join(".librarian/gem/config") }
let(:local) { pwd.join(".librarian/gem/config") }
let(:specfile) { pwd.join("Gemfile") }
before do
FileUtils.mkpath(pwd)
FileUtils.touch(specfile)
end
let(:database) do
described_class.new(adapter_name,
:env => env,
:pwd => pwd.to_s,
:home => home.to_s,
:project_path => project_path,
:specfile_name => specfile_name
)
end
context "when a key is given globally" do
let(:key) { "jam" }
let(:value) { "jelly" }
let(:raw_key) { "LIBRARIAN_GEM_JAM" }
before do
write_yaml! global, raw_key => value
end
it "should have the key globally" do
expect(database.global[key]).to eq value
end
it "should not have the key in the env" do
expect(database.env[key]).to be_nil
end
it "should not have the key locally" do
expect(database.local[key]).to be_nil
end
it "should have the key generally" do
expect(database[key]).to eq value
end
end
context "when a key is set globally" do
let(:key) { "jam" }
let(:value) { "jelly" }
let(:raw_key) { "LIBRARIAN_GEM_JAM" }
before do
database.global[key] = value
end
it "should have the key globally" do
expect(database.global[key]).to eq value
end
it "should not have the key in the env" do
expect(database.env[key]).to be_nil
end
it "should not have the key locally" do
expect(database.local[key]).to be_nil
end
it "should have the key generally" do
expect(database[key]).to eq value
end
it "should persist the key" do
data = YAML.load_file(global)
expect(data).to eq({raw_key => value})
end
end
context "when the key is set and unset globally" do
let(:key) { "jam" }
let(:value) { "jelly" }
let(:raw_key) { "LIBRARIAN_GEM_JAM" }
before do
database.global[key] = value
database.global[key] = nil
end
it "should not have the key globally" do
expect(database.global[key]).to be_nil
end
it "should not have the key in the env" do
expect(database.env[key]).to be_nil
end
it "should not have the key locally" do
expect(database.local[key]).to be_nil
end
it "should not have the key generally" do
expect(database[key]).to be_nil
end
it "should unpersist the key" do
expect(File).to_not exist global
end
end
context "when a key is given in the env" do
let(:key) { "jam" }
let(:value) { "jelly" }
let(:raw_key) { "LIBRARIAN_GEM_JAM" }
#override
let(:env) { {raw_key => value} }
it "should not have the key globally" do
expect(database.global[key]).to be_nil
end
it "should have the key in the env" do
expect(database.env[key]).to eq value
end
it "should not have the key locally" do
expect(database.local[key]).to be_nil
end
it "should have the key generally" do
expect(database[key]).to eq value
end
end
context "when a key is given locally" do
let(:key) { "jam" }
let(:value) { "jelly" }
let(:raw_key) { "LIBRARIAN_GEM_JAM" }
before do
write_yaml! local, raw_key => value
end
it "should not have the key globally" do
expect(database.global[key]).to be_nil
end
it "should not have the key in the env" do
expect(database.env[key]).to be_nil
end
it "should have the key locally" do
expect(database.local[key]).to eq value
end
it "should have the key generally" do
expect(database[key]).to eq value
end
end
context "when a key is set locally" do
let(:key) { "jam" }
let(:value) { "jelly" }
let(:raw_key) { "LIBRARIAN_GEM_JAM" }
before do
database.local[key] = value
end
it "should not have the key globally" do
expect(database.global[key]).to be_nil
end
it "should not have the key in the env" do
expect(database.env[key]).to be_nil
end
it "should have the key locally" do
expect(database.local[key]).to eq value
end
it "should have the key generally" do
expect(database[key]).to eq value
end
it "should persist the key" do
data = YAML.load_file(local)
expect(data).to eq({raw_key => value})
end
end
context "when the key is set and unset locally" do
let(:key) { "jam" }
let(:value) { "jelly" }
let(:raw_key) { "LIBRARIAN_GEM_JAM" }
before do
database.local[key] = value
database.local[key] = nil
end
it "should not have the key globally" do
expect(database.global[key]).to be_nil
end
it "should not have the key in the env" do
expect(database.env[key]).to be_nil
end
it "should not have the key locally" do
expect(database.local[key]).to be_nil
end
it "should not have the key generally" do
expect(database[key]).to be_nil
end
it "should unpersist the key" do
expect(File).to_not exist local
end
end
context "setting malformatted keys" do
it "should ban caps" do
expect { database.global["JAM"] = "jelly" }.
to raise_error Librarian::Error, %[key not permitted: "JAM"]
end
it "should ban double dots" do
expect { database.global["jam..jam"] = "jelly" }.
to raise_error Librarian::Error, %[key not permitted: "jam..jam"]
end
end
context "setting banned keys" do
it "should ban the specfile key" do
expect { database.global["gemfile"] = "jelly" }.
to raise_error Librarian::Error, %[key not permitted: "gemfile"]
end
it "should ban the global-config key" do
expect { database.global["config"] = "jelly" }.
to raise_error Librarian::Error, %[key not permitted: "config"]
end
end
context "project_path" do
context "by default" do
it "should give the default project path" do
expect(database.project_path).to eq Pathname("/tmp")
end
end
context "when the specfile is set in the env" do
let(:env) { {"LIBRARIAN_GEM_GEMFILE" => "/non/sense/path/to/Sillyfile"} }
it "should give the project path from the env-set specfile" do
expect(database.project_path).to eq Pathname("/non/sense/path/to")
end
end
end
context "specfile_path" do
context "by default" do
it "should give the default specfile path" do
expect(database.specfile_path).to eq specfile
end
end
context "when set in the env" do
let(:env) { {"LIBRARIAN_GEM_GEMFILE" => "/non/sense/path/to/Sillyfile"} }
it "should give the given specfile path" do
expect(database.specfile_path).to eq Pathname("/non/sense/path/to/Sillyfile")
end
end
context "when the project_path is assigned" do
let(:project_path) { "/non/sense/path/to" }
it "should give the assigned specfile path" do
expect(database.specfile_path).to eq Pathname("/non/sense/path/to/Gemfile")
end
end
context "when the specfile_name is assigned" do
let(:specfile_name) { "Sillyfile" }
it "should give the assigned specfile path" do
expect(database.specfile_path).to eq Pathname("/tmp/Sillyfile")
end
end
end
end
|