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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
|
//! ref: composer/tests/Composer/Test/Repository/FilesystemRepositoryTest.php
use crate::test_case::{get_alias_package, get_package};
use indexmap::IndexMap;
use shirabe::dependency_resolver::operation::OperationInterface;
use shirabe::installed_versions::InstalledVersions;
use shirabe::installer::{InstallationManagerInterface, InstallerInterface};
use shirabe::io::IOInterface;
use shirabe::json::json_file::JsonFile;
use shirabe::package::loader::ArrayLoader;
use shirabe::package::{Link, PackageInterfaceHandle, RootAliasPackageHandle, RootPackageHandle};
use shirabe::repository::InstalledRepositoryInterface;
use shirabe::repository::RepositoryInterface;
use shirabe::repository::filesystem_repository::FilesystemRepository;
use shirabe::util::filesystem::Filesystem;
use shirabe_php_shim::PhpMixed;
use shirabe_semver::VersionParser;
use std::cell::RefCell;
use std::rc::Rc;
/// PHP mocks JsonFile::read()/exists(); without a mocking framework the canned read value is
/// materialized as a real temp file whose decoded JSON reproduces the mock return value exactly.
fn create_temp_json_file(contents: &str) -> String {
let mut path = std::env::temp_dir();
let unique = format!(
"shirabe_filesystemrepositorytest_{}_{}",
std::process::id(),
std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.unwrap()
.as_nanos()
);
path.push(unique);
std::fs::write(&path, contents.as_bytes()).unwrap();
path.to_str().unwrap().to_string()
}
#[test]
fn test_repository_read() {
let path = create_temp_json_file(
r#"[{"name": "package1", "version": "1.0.0-beta", "type": "vendor"}]"#,
);
let json = JsonFile::new(path, None, None).unwrap();
let mut repository = FilesystemRepository::new(json, false, None, None).unwrap();
let packages = repository.get_packages().unwrap();
assert_eq!(packages.len(), 1);
assert_eq!(packages[0].get_name(), "package1");
assert_eq!(packages[0].get_version(), "1.0.0.0-beta");
assert_eq!(packages[0].get_type(), "vendor");
}
#[ignore]
#[test]
fn test_corrupted_repository_file() {
// PHP mocks read() to return the scalar string 'foo'; a real file containing the JSON string
// "foo" decodes to the same value, which the repository rejects as a non-array package list.
let path = create_temp_json_file(r#""foo""#);
let json = JsonFile::new(path, None, None).unwrap();
let mut repository = FilesystemRepository::new(json, false, None, None).unwrap();
let result = repository.get_packages();
let err = result.unwrap_err();
assert!(
err.is::<shirabe::repository::InvalidRepositoryException>(),
"expected InvalidRepositoryException, got: {err}"
);
}
#[test]
fn test_unexistent_repository_file() {
let mut path = std::env::temp_dir();
path.push(format!(
"shirabe_filesystemrepositorytest_missing_{}_{}",
std::process::id(),
std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.unwrap()
.as_nanos()
));
let json = JsonFile::new(path.to_str().unwrap().to_string(), None, None).unwrap();
let mut repository = FilesystemRepository::new(json, false, None, None).unwrap();
let packages = repository.get_packages().unwrap();
assert_eq!(packages.len(), 0);
}
mockall::mock! {
/// Mocks the PHPUnit `InstallationManager` mock built with `disableOriginalConstructor()`. Only
/// `get_install_path` is reached by `FilesystemRepository::write`.
#[derive(Debug)]
pub InstallationManager {}
impl InstallationManagerInterface for InstallationManager {
fn add_installer(&mut self, installer: Box<dyn InstallerInterface>);
fn remove_installer(&mut self, installer: &dyn InstallerInterface);
fn disable_plugins(&mut self);
fn is_package_installed(
&mut self,
repo: &dyn InstalledRepositoryInterface,
package: PackageInterfaceHandle,
) -> anyhow::Result<bool>;
fn ensure_binaries_presence(&mut self, package: PackageInterfaceHandle);
fn execute(
&mut self,
repo: &mut dyn InstalledRepositoryInterface,
operations: Vec<Rc<dyn OperationInterface>>,
dev_mode: bool,
run_scripts: bool,
download_only: bool,
) -> anyhow::Result<()>;
fn get_install_path(&mut self, package: PackageInterfaceHandle) -> Option<String>;
fn set_output_progress(&mut self, output_progress: bool);
fn notify_installs(&mut self, io: Rc<RefCell<dyn IOInterface>>);
}
}
#[test]
fn test_repository_write() {
// PHP mocks JsonFile::write/read/getPath; here a real JsonFile under a temp repo dir is written
// and read back. write() never reads the file (it dumps the in-memory packages), so the mocked
// read()/exists() return values are irrelevant to the result.
let base = std::fs::canonicalize(std::env::temp_dir()).unwrap();
let repo_dir = format!(
"{}/shirabe_repo_write_test_{}_{}",
base.display(),
std::process::id(),
std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.unwrap()
.as_nanos()
);
let mut fs = Filesystem::new(None);
fs.remove_directory(&repo_dir).ok();
let json_path = format!("{}/vendor/composer/installed.json", repo_dir);
let json = JsonFile::new(json_path.clone(), None, None).unwrap();
let mut repository = FilesystemRepository::new(json, false, None, None).unwrap();
let mut im = MockInstallationManager::new();
let fixed_path = format!("{}/vendor/woop/woop", repo_dir);
im.expect_get_install_path()
.times(2)
.returning(move |_| Some(fixed_path.clone()));
repository.set_dev_package_names(vec!["mypkg2".to_string()]);
repository
.add_package(get_package("mypkg2", "1.2.3"))
.unwrap();
repository
.add_package(get_package("mypkg", "0.1.10"))
.unwrap();
repository.write(true, &mut im).unwrap();
let written = std::fs::read_to_string(&json_path).unwrap();
let actual: serde_json::Value = serde_json::from_str(&written).unwrap();
let expected = serde_json::json!({
"packages": [
{"name": "mypkg", "type": "library", "version": "0.1.10", "version_normalized": "0.1.10.0", "install-path": "../woop/woop"},
{"name": "mypkg2", "type": "library", "version": "1.2.3", "version_normalized": "1.2.3.0", "install-path": "../woop/woop"},
],
"dev": true,
"dev-package-names": ["mypkg2"],
});
assert_eq!(actual, expected);
fs.remove_directory(&repo_dir).ok();
}
/// chdir()s into a fresh temp dir on construction and restores the previous cwd on drop, mirroring
/// PHP's `getUniqueTmpDirectory()` + `chdir($dir)`. The cwd is restored before the temp tree is
/// removed so the directory (the cwd itself) can be deleted cleanly.
struct CwdGuard {
temp: tempfile::TempDir,
prev_cwd: std::path::PathBuf,
}
impl CwdGuard {
fn new() -> Self {
let temp = tempfile::TempDir::new().unwrap();
let prev_cwd = std::env::current_dir().unwrap();
std::env::set_current_dir(temp.path()).unwrap();
Self { temp, prev_cwd }
}
fn path(&self) -> std::path::PathBuf {
self.temp.path().to_path_buf()
}
}
impl Drop for CwdGuard {
fn drop(&mut self) {
let _ = std::env::set_current_dir(&self.prev_cwd);
}
}
/// ref: TestCase::configureLinks, inlined for the single package being configured.
fn configure_links(
name: &str,
pretty_version: &str,
description: &str,
links: &[(&str, &str)],
) -> IndexMap<String, Link> {
let mut map: IndexMap<String, PhpMixed> = IndexMap::new();
for (target, constraint) in links {
map.insert(
(*target).to_string(),
PhpMixed::String((*constraint).to_string()),
);
}
ArrayLoader::new(None, false)
.parse_links(name, pretty_version, description, map)
.unwrap()
}
#[test]
fn test_repository_writes_installed_php() {
let guard = CwdGuard::new();
let dir = guard.path();
let dir = dir.to_str().unwrap();
let json = JsonFile::new(format!("{}/installed.json", dir), None, None).unwrap();
let root_package = RootPackageHandle::new(
"__root__".to_string(),
VersionParser.normalize("dev-master", None).unwrap(),
"dev-master".to_string(),
);
root_package.set_source_reference(Some("sourceref-by-default".to_string()));
root_package.set_dist_reference(Some("distref".to_string()));
root_package.set_provides(configure_links(
"__root__",
"dev-master",
Link::TYPE_PROVIDE,
&[("foo/impl", "2.0")],
));
let root_package = RootAliasPackageHandle::new(
root_package,
VersionParser.normalize("1.10.x-dev", None).unwrap(),
"1.10.x-dev".to_string(),
);
let mut repository =
FilesystemRepository::new(json, true, Some(root_package.into()), None).unwrap();
repository.set_dev_package_names(vec!["c/c".to_string()]);
let pkg = get_package("a/provider", "1.1");
pkg.__set_provides(configure_links(
"a/provider",
"1.1",
Link::TYPE_PROVIDE,
&[("foo/impl", "^1.1"), ("foo/impl2", "2.0")],
));
pkg.set_dist_reference(Some("distref-as-no-source".to_string()));
repository.add_package(pkg).unwrap();
let pkg = get_package("a/provider2", "1.2");
pkg.__set_provides(configure_links(
"a/provider2",
"1.2",
Link::TYPE_PROVIDE,
&[("foo/impl", "self.version"), ("foo/impl2", "2.0")],
));
pkg.set_source_reference(Some("sourceref".to_string()));
pkg.set_dist_reference(Some("distref-as-installed-from-dist".to_string()));
pkg.set_installation_source(Some("dist".to_string()));
repository.add_package(pkg.clone()).unwrap();
repository
.add_package(get_alias_package(&pkg, "1.4"))
.unwrap();
let pkg = get_package("b/replacer", "2.2");
pkg.__set_replaces(configure_links(
"b/replacer",
"2.2",
Link::TYPE_REPLACE,
&[("foo/impl2", "self.version"), ("foo/replaced", "^3.0")],
));
repository.add_package(pkg).unwrap();
let pkg = get_package("c/c", "3.0");
pkg.set_dist_reference(Some(
"{${passthru('bash -i')}} Foo\\Bar\n\ttab\u{0b}verticaltab\0".to_string(),
));
repository.add_package(pkg).unwrap();
let pkg = get_package("meta/package", "3.0");
pkg.__set_type("metapackage".to_string());
repository.add_package(pkg).unwrap();
let mut im = MockInstallationManager::new();
let cwd = dir.to_string();
im.expect_get_install_path().returning(move |package| {
// check for empty paths handling
if package.get_type() == "metapackage" {
return Some(String::new());
}
if package.get_name() == "c/c" {
// check for absolute paths
return Some("/foo/bar/ven\\do{}r/c/c${}".to_string());
}
if package.get_name() == "a/provider" {
return Some("vendor/{${passthru('bash -i')}}".to_string());
}
// check for cwd
if package.as_root().is_some() {
return Some(cwd.clone());
}
// check for relative paths
Some(format!("vendor/{}", package.get_name()))
});
repository.write(true, &mut im).unwrap();
let expected = std::fs::read_to_string(format!(
"{}/../../composer/tests/Composer/Test/Repository/Fixtures/installed.php",
env!("CARGO_MANIFEST_DIR")
))
.unwrap();
let actual = std::fs::read_to_string(format!("{}/installed.php", dir)).unwrap();
assert_eq!(expected, actual);
}
#[ignore]
#[test]
fn test_safely_load_installed_versions() {
let fixtures_dir = format!(
"{}/../../composer/tests/Composer/Test/Repository/Fixtures",
env!("CARGO_MANIFEST_DIR")
);
let path = format!("{}/installed_complex.php", fixtures_dir);
let result = FilesystemRepository::safely_load_installed_versions(&path);
assert!(result, "The file should be considered valid");
let raw_data = InstalledVersions::get_all_raw_data();
let raw_data = raw_data.last().cloned().unwrap();
let mut root: IndexMap<String, PhpMixed> = IndexMap::new();
root.insert(
"install_path".to_string(),
PhpMixed::String(format!("{}/./", fixtures_dir)),
);
root.insert(
"aliases".to_string(),
PhpMixed::List(vec![
PhpMixed::String("1.10.x-dev".to_string()),
PhpMixed::String("2.10.x-dev".to_string()),
]),
);
root.insert("name".to_string(), PhpMixed::String("__root__".to_string()));
root.insert("true".to_string(), PhpMixed::Bool(true));
root.insert("false".to_string(), PhpMixed::Bool(false));
root.insert("null".to_string(), PhpMixed::Null);
let mut a_provider: IndexMap<String, PhpMixed> = IndexMap::new();
a_provider.insert(
"foo".to_string(),
PhpMixed::String("simple string/no backslash".to_string()),
);
a_provider.insert(
"install_path".to_string(),
PhpMixed::String(format!(
"{}/vendor/{{${{passthru('bash -i')}}}}",
fixtures_dir
)),
);
a_provider.insert("empty array".to_string(), PhpMixed::List(vec![]));
let mut c_c: IndexMap<String, PhpMixed> = IndexMap::new();
c_c.insert(
"install_path".to_string(),
PhpMixed::String("/foo/bar/ven/do{}r/c/c${}".to_string()),
);
c_c.insert("aliases".to_string(), PhpMixed::List(vec![]));
c_c.insert(
"reference".to_string(),
PhpMixed::String("{${passthru('bash -i')}} Foo\\Bar\n\ttab\u{0b}verticaltab\0".to_string()),
);
let mut versions: IndexMap<String, PhpMixed> = IndexMap::new();
versions.insert("a/provider".to_string(), PhpMixed::Array(a_provider));
versions.insert("c/c".to_string(), PhpMixed::Array(c_c));
let mut expected: IndexMap<String, PhpMixed> = IndexMap::new();
expected.insert("root".to_string(), PhpMixed::Array(root));
expected.insert("versions".to_string(), PhpMixed::Array(versions));
assert_eq!(raw_data, expected);
}
|