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
|
//! ref: composer/tests/Composer/Test/Repository/PathRepositoryTest.php
use crate::test_case::get_package;
use indexmap::IndexMap;
use serial_test::serial;
use shirabe::config::Config;
use shirabe::io::{IOInterface, NullIO};
use shirabe::repository::PathRepository;
use shirabe::util::http_downloader::HttpDownloader;
use shirabe::util::r#loop::Loop;
use shirabe::util::{Platform, ProcessExecutor};
use shirabe_php_shim::{
DIRECTORY_SEPARATOR, PhpMixed, file_get_contents, hash, realpath, serialize,
};
fn fixtures_dir() -> String {
format!(
"{}/../../composer/tests/Composer/Test/Repository/Fixtures",
env!("CARGO_MANIFEST_DIR")
)
}
/// ref: PathRepositoryTest::createPathRepo
fn create_path_repo(options: IndexMap<String, PhpMixed>) -> PathRepository {
let io: std::rc::Rc<std::cell::RefCell<dyn IOInterface>> =
std::rc::Rc::new(std::cell::RefCell::new(NullIO::new()));
let config = std::rc::Rc::new(std::cell::RefCell::new(Config::new(true, None)));
let proc = std::rc::Rc::new(std::cell::RefCell::new(ProcessExecutor::new(None)));
// ref: createPathRepo wires the ProcessExecutor through a Loop so the VersionGuesser's async
// git calls are permitted; constructing the Loop calls enable_async() on the shared executor.
let http_downloader = std::rc::Rc::new(std::cell::RefCell::new(HttpDownloader::new(
io.clone(),
config.clone(),
IndexMap::new(),
false,
)));
let _loop = Loop::new(http_downloader, Some(proc.clone()));
PathRepository::new(options, io, config, None, None, Some(proc)).unwrap()
}
fn coordinates(pairs: Vec<(&str, PhpMixed)>) -> IndexMap<String, PhpMixed> {
let mut map: IndexMap<String, PhpMixed> = IndexMap::new();
for (key, value) in pairs {
map.insert(key.to_string(), value);
}
map
}
#[test]
fn test_load_package_from_file_system_with_incorrect_path() {
let repository_url =
[fixtures_dir(), "path".to_string(), "missing".to_string()].join(DIRECTORY_SEPARATOR);
let mut repository =
create_path_repo(coordinates(vec![("url", PhpMixed::String(repository_url))]));
assert!(repository.__get_packages().is_err());
}
#[test]
fn test_load_package_from_file_system_with_version() {
let repository_url = [
fixtures_dir(),
"path".to_string(),
"with-version".to_string(),
]
.join(DIRECTORY_SEPARATOR);
let mut repository =
create_path_repo(coordinates(vec![("url", PhpMixed::String(repository_url))]));
repository.__get_packages().unwrap();
assert_eq!(1, repository.__count().unwrap());
assert!(
repository
.__has_package(get_package("test/path-versioned", "0.0.2"))
.unwrap()
);
}
#[test]
fn test_load_package_from_file_system_without_version() {
let repository_url = [
fixtures_dir(),
"path".to_string(),
"without-version".to_string(),
]
.join(DIRECTORY_SEPARATOR);
let mut repository =
create_path_repo(coordinates(vec![("url", PhpMixed::String(repository_url))]));
let packages = repository.__get_packages().unwrap();
assert!(repository.__count().unwrap() >= 1);
let package = &packages[0];
assert_eq!("test/path-unversioned", package.get_name());
let package_version = package.get_version();
assert!(!package_version.is_empty());
}
#[test]
fn test_load_package_from_file_system_with_wildcard() {
let repository_url =
[fixtures_dir(), "path".to_string(), "*".to_string()].join(DIRECTORY_SEPARATOR);
let mut repository =
create_path_repo(coordinates(vec![("url", PhpMixed::String(repository_url))]));
let packages = repository.__get_packages().unwrap();
let mut names: Vec<String> = Vec::new();
assert!(repository.__count().unwrap() >= 2);
let package = &packages[0];
names.push(package.get_name());
let package = &packages[1];
names.push(package.get_name());
names.sort();
assert_eq!(
vec![
"test/path-unversioned".to_string(),
"test/path-versioned".to_string()
],
names
);
}
#[test]
fn test_load_package_with_explicit_versions() {
let mut versions: IndexMap<String, PhpMixed> = IndexMap::new();
versions.insert(
"test/path-unversioned".to_string(),
PhpMixed::String("4.3.2.1".to_string()),
);
versions.insert(
"test/path-versioned".to_string(),
PhpMixed::String("3.2.1.0".to_string()),
);
let options = coordinates(vec![("versions", PhpMixed::Array(versions))]);
let repository_url =
[fixtures_dir(), "path".to_string(), "*".to_string()].join(DIRECTORY_SEPARATOR);
let mut repository = create_path_repo(coordinates(vec![
("url", PhpMixed::String(repository_url)),
("options", PhpMixed::Array(options)),
]));
let packages = repository.__get_packages().unwrap();
let mut versions: IndexMap<String, String> = IndexMap::new();
assert_eq!(2, repository.__count().unwrap());
let package = &packages[0];
versions.insert(package.get_name(), package.get_version());
let package = &packages[1];
versions.insert(package.get_name(), package.get_version());
versions.sort_keys();
let expected: IndexMap<String, String> = [
("test/path-unversioned".to_string(), "4.3.2.1".to_string()),
("test/path-versioned".to_string(), "3.2.1.0".to_string()),
]
.into_iter()
.collect();
assert_eq!(expected, versions);
}
/// Restores the previous cwd on drop so a panicking assertion cannot leak the changed cwd into
/// other tests.
struct CwdGuard {
prev_cwd: std::path::PathBuf,
}
impl CwdGuard {
fn new(dir: &str) -> Self {
let prev_cwd = std::env::current_dir().unwrap();
std::env::set_current_dir(dir).unwrap();
Self { prev_cwd }
}
}
impl Drop for CwdGuard {
fn drop(&mut self) {
let _ = std::env::set_current_dir(&self.prev_cwd);
}
}
/// Verify relative repository URLs remain relative, see #4439
#[test]
#[serial]
fn test_url_remains_relative() {
// PHP runs under phpunit with the process cwd inside the composer checkout, an ancestor of
// __DIR__, so stripping the cwd prefix yields a valid relative path; cargo runs tests from the
// crate manifest dir, which is not an ancestor of the fixtures, so replicate the phpunit
// precondition by chdir'ing to the __DIR__ equivalent for the duration of the test. #[serial]
// keeps other cwd-touching tests in this binary from interleaving.
let _cwd_guard = CwdGuard::new(&fixtures_dir().replace("/Fixtures", ""));
// realpath() does not fully expand the paths
// PHP Bug https://bugs.php.net/bug.php?id=72642
let repository_url = [
realpath(&realpath(&fixtures_dir().replace("/Fixtures", "")).unwrap_or_default())
.unwrap_or_default(),
"Fixtures".to_string(),
"path".to_string(),
"with-version".to_string(),
]
.join(DIRECTORY_SEPARATOR);
// getcwd() not necessarily match __DIR__
// PHP Bug https://bugs.php.net/bug.php?id=73797
let cwd = realpath(&realpath(&Platform::get_cwd(false).unwrap()).unwrap_or_default())
.unwrap_or_default();
let relative_url = repository_url[cwd.len().min(repository_url.len())..]
.trim_start_matches(DIRECTORY_SEPARATOR)
.to_string();
let mut repository = create_path_repo(coordinates(vec![(
"url",
PhpMixed::String(relative_url.clone()),
)]));
let packages = repository.__get_packages().unwrap();
assert_eq!(1, repository.__count().unwrap());
let package = &packages[0];
assert_eq!("test/path-versioned", package.get_name());
// Convert platform specific separators back to generic URL slashes
let relative_url = relative_url.replace(DIRECTORY_SEPARATOR, "/");
assert_eq!(Some(relative_url), package.get_dist_url());
}
#[test]
fn test_reference_none() {
let options = coordinates(vec![("reference", PhpMixed::String("none".to_string()))]);
let repository_url =
[fixtures_dir(), "path".to_string(), "*".to_string()].join(DIRECTORY_SEPARATOR);
let mut repository = create_path_repo(coordinates(vec![
("url", PhpMixed::String(repository_url)),
("options", PhpMixed::Array(options)),
]));
let packages = repository.__get_packages().unwrap();
assert!(repository.__count().unwrap() >= 2);
for package in &packages {
assert_eq!(package.get_dist_reference(), None);
}
}
#[test]
fn test_reference_config() {
let options = coordinates(vec![
("reference", PhpMixed::String("config".to_string())),
("relative", PhpMixed::Bool(true)),
]);
let repository_url =
[fixtures_dir(), "path".to_string(), "*".to_string()].join(DIRECTORY_SEPARATOR);
let mut repository = create_path_repo(coordinates(vec![
("url", PhpMixed::String(repository_url)),
("options", PhpMixed::Array(options.clone())),
]));
let packages = repository.__get_packages().unwrap();
assert!(repository.__count().unwrap() >= 2);
for package in &packages {
let dist_url = package.get_dist_url().unwrap_or_default();
assert_eq!(
package.get_dist_reference(),
Some(hash(
"sha1",
&format!(
"{}{}",
file_get_contents(format!("{}/composer.json", dist_url)).unwrap_or_default(),
serialize(&PhpMixed::Array(options.clone()))
)
))
);
}
}
|