Skip to content

Commit 1b801e6

Browse files
authored
Extend finding components to plugins and executables distributed by forc (#666)
Extend finding components to plugins and executables distributed by forc (#654) When checking if a component is distributed by forc, we currently only look at forc's direct executables. This needs to be extended to resolve components, plugins, and executables.
1 parent 3a60dec commit 1b801e6

File tree

1 file changed

+106
-9
lines changed

1 file changed

+106
-9
lines changed

component/src/lib.rs

+106-9
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,34 @@ impl Component {
5353
.cloned()
5454
}
5555

56+
/// Returns a `Component` from the supplied `Component` name, plugin, or executable
57+
///
58+
/// # Arguments
59+
///
60+
/// * `name` - The name of the component, plugin, or executable
61+
///
62+
/// # Examples
63+
///
64+
/// ```rust
65+
/// use component::Component;
66+
///
67+
/// let forc = Component::resolve_from_name("forc").unwrap();
68+
/// let publishable = Component::resolve_from_name("fuel-core").unwrap();
69+
/// let plugin = Component::resolve_from_name("forc-fmt").unwrap();
70+
/// let executable = Component::resolve_from_name("forc-run").unwrap();
71+
/// ```
72+
pub fn resolve_from_name(name: &str) -> Option<Component> {
73+
Components::collect().ok().and_then(|components| {
74+
components.component.get(name).cloned().or_else(|| {
75+
components
76+
.component
77+
.values()
78+
.find(|comp| comp.executables.contains(&name.to_string()))
79+
.cloned()
80+
})
81+
})
82+
}
83+
5684
pub fn is_default_forc_plugin(name: &str) -> bool {
5785
(Self::from_name(FORC)
5886
.expect("there must always be a `forc` component")
@@ -213,12 +241,34 @@ impl Components {
213241
Ok(executables)
214242
}
215243

216-
pub fn is_distributed_by_forc(plugin_name: &str) -> bool {
217-
let components = Self::from_toml(COMPONENTS_TOML).expect("Failed to parse components toml");
218-
if let Some(forc) = components.component.get(FORC) {
219-
return forc.executables.contains(&plugin_name.to_string());
220-
};
221-
false
244+
/// Tests if the supplied `Component` name, plugin, or executable is
245+
/// distributed by forc
246+
///
247+
/// # Arguments
248+
///
249+
/// * `name` - The name of the `Component`, plugin, or executable
250+
///
251+
/// # Examples
252+
///
253+
/// ```rust
254+
/// use component::Components;
255+
///
256+
/// assert!(Components::is_distributed_by_forc("forc"));
257+
/// assert!(!Components::is_distributed_by_forc("fuel-core"));
258+
/// assert!(Components::is_distributed_by_forc("forc-fmt"));
259+
/// assert!(Components::is_distributed_by_forc("forc-run"));
260+
/// ```
261+
pub fn is_distributed_by_forc(name: &str) -> bool {
262+
match name {
263+
FORC => true,
264+
_ => Component::from_name(FORC)
265+
.ok()
266+
.and_then(|forc| {
267+
Component::resolve_from_name(name)
268+
.map(|component| Component::is_in_same_distribution(&forc, &component))
269+
})
270+
.unwrap_or(false),
271+
}
222272
}
223273
}
224274

@@ -311,7 +361,7 @@ mod tests {
311361
}
312362

313363
#[test]
314-
#[should_panic] // TODO: #654 will fix this
364+
#[should_panic] // This will fail as long as some executables are not plugins
315365
fn test_from_name_executables() {
316366
for executable in &Components::collect_plugin_executables().unwrap() {
317367
let component = Component::from_name(executable).unwrap();
@@ -324,6 +374,55 @@ mod tests {
324374
}
325375
}
326376

377+
#[test]
378+
fn test_resolve_from_name_forc() {
379+
let component = Component::resolve_from_name(FORC).unwrap();
380+
assert_eq!(component.name, FORC, "forc is a publishable component");
381+
}
382+
383+
#[test]
384+
fn test_resolve_from_name_publishable() {
385+
for publishable in Components::collect_publishables().unwrap() {
386+
let component = Component::resolve_from_name(&publishable.name).unwrap();
387+
assert_eq!(component.name, publishable.name);
388+
}
389+
}
390+
391+
#[test]
392+
fn test_resolve_from_name_plugin() {
393+
for plugin in Components::collect_plugins().unwrap() {
394+
let component = Component::resolve_from_name(&plugin.name).unwrap();
395+
assert_eq!(component.name, plugin.name);
396+
}
397+
}
398+
399+
#[test]
400+
fn test_resolve_from_name_from_executable() {
401+
let executables = Components::collect_plugin_executables().unwrap();
402+
403+
for executable in &executables {
404+
let component = Component::resolve_from_name(executable).unwrap();
405+
406+
if component.executables.len() == 1 {
407+
assert_eq!(component.name, *executable);
408+
} else {
409+
assert!(component.executables.contains(executable));
410+
}
411+
}
412+
}
413+
414+
#[test]
415+
fn test_resolve_from_name_nonexistent() {
416+
assert!(Component::resolve_from_name("nonexistent-component").is_none());
417+
}
418+
419+
#[test]
420+
fn test_resolve_from_name_case_sensitivity() {
421+
let original = Component::resolve_from_name("forc");
422+
let uppercase = Component::resolve_from_name("FORC");
423+
assert_ne!(original, uppercase);
424+
}
425+
327426
#[test]
328427
fn test_is_distributed_by_forc_forc() {
329428
assert!(
@@ -341,7 +440,6 @@ mod tests {
341440
}
342441

343442
#[test]
344-
#[should_panic] // TODO: #654 will fix this
345443
fn test_is_distributed_by_forc_plugins() {
346444
for plugin in Components::collect_plugins().unwrap() {
347445
let component = Component::from_name(&plugin.name).unwrap();
@@ -350,7 +448,6 @@ mod tests {
350448
}
351449

352450
#[test]
353-
#[should_panic] // TODO: #654 will fix this
354451
fn test_is_distributed_by_forc_executables() {
355452
for executable in Components::collect_plugin_executables().unwrap() {
356453
let components = Components::collect().unwrap();

0 commit comments

Comments
 (0)