#![expect(
missing_docs,
reason = "the test binary's tool fixtures are public by the derive's rule"
)]
mod common;
use std::collections::{BTreeMap, BTreeSet};
use std::path::PathBuf;
use flow_abi_testing::crossing::{bytes, crossed, read};
use flow_toolkit::{Flow, Shaped, tool};
#[derive(Flow, Clone, Debug, PartialEq)]
pub struct Place {
pub root: PathBuf,
pub names: Vec<PathBuf>,
}
#[tool]
pub fn under(root: PathBuf, name: String) -> PathBuf {
root.join(name)
}
#[tool]
pub fn places(place: Place, seen: BTreeSet<PathBuf>) -> Option<PathBuf> {
seen.into_iter().next().or(Some(place.root))
}
fn contract() -> String {
let placed = [__flow_tool_under(), __flow_tool_places()]
.map(|function| flow_toolkit::__bridge::at("", "tests/std_paths.rs", 1, function));
common::extract("std_paths", None, &placed)
.expect("the contract is written")
.into_values()
.collect::<Vec<_>>()
.join("\n")
}
#[test]
fn a_path_buf_is_path_dot_path_with_std_path_imported() {
let source = contract();
for line in [
"use \"std@v1/path\"\n",
"pub flow under(root: path.Path, name: Text) -> path.Path\n",
"pub flow places(place: Place, seen: Set<path.Path>) -> Option<path.Path>\n",
" pub root: path.Path,\n",
" pub names: List<path.Path>,\n",
] {
assert!(source.contains(line), "{line:?} in {source}");
}
}
#[test]
fn the_generated_contract_is_one_the_checker_accepts() {
let placed = [__flow_tool_under(), __flow_tool_places()]
.map(|function| flow_toolkit::__bridge::at("", "tests/std_paths.rs", 1, function));
let contracts = common::extract("std_paths", None, &placed).expect("the contract is written");
let listed = common::listed(&contracts, &common::referring(&["lib.under", "lib.places"]));
assert_eq!(common::tool_names(&listed), ["places", "under"]);
assert!(common::names_std(&listed, "path", "Path"));
}
fn projected<T: Shaped>() -> (flow_abi::Catalog, flow_types::Type) {
flow_abi::wire::project(T::FORM, &flow_abi_testing::catalog::std_catalog())
.expect("a Flow type")
}
fn refused<T: Shaped>(bytes: &[u8]) -> bool {
let (catalog, ty) = projected::<T>();
flow_abi::decode_bytes(
&catalog,
&ty,
bytes,
flow_abi::Mode::Rebuild,
&flow_abi::Limits::default(),
)
.is_err()
}
#[test]
fn a_path_is_written_as_its_normal_text() {
for (native, written) in [
("a/./b//c/", "a/b/c"),
("/srv/../x", "/srv/../x"),
("", "."),
("/", "/"),
] {
let path = PathBuf::from(native);
assert_eq!(bytes(&path), bytes(&written.to_owned()), "{native:?}");
}
}
#[test]
fn binary_reading_accepts_only_the_normal_text() {
assert_eq!(
read::<PathBuf>(&bytes(&"src/lib.flow".to_owned())),
PathBuf::from("src/lib.flow")
);
assert!(refused::<PathBuf>(&bytes(&"src//lib.flow/".to_owned())));
}
#[test]
fn sets_and_dictionaries_of_paths_are_in_flow_order_the_paths_text() {
let set: BTreeSet<PathBuf> = ["a/b", "a.b"].map(PathBuf::from).into();
let flow_order = vec!["a.b".to_owned(), "a/b".to_owned()];
assert_eq!(crossed(&set), (set.clone(), bytes(&flow_order)));
let backwards = bytes(&vec!["a/b".to_owned(), "a.b".to_owned()]);
assert!(refused::<BTreeSet<PathBuf>>(&backwards));
let dict: BTreeMap<PathBuf, bool> =
[(PathBuf::from("a/b"), true), (PathBuf::from("a.b"), false)].into();
let pairs: BTreeMap<String, bool> =
[("a.b".to_owned(), false), ("a/b".to_owned(), true)].into();
assert_eq!(crossed(&dict), (dict.clone(), bytes(&pairs)));
}
#[test]
fn a_record_of_paths_crosses_as_its_fields_normal_text() {
let place = Place {
root: PathBuf::from("/srv/./app"),
names: vec![PathBuf::from("a//b")],
};
assert_eq!(
crossed(&place).0,
Place {
root: PathBuf::from("/srv/app"),
names: vec![PathBuf::from("a/b")],
}
);
}
#[cfg(unix)]
#[test]
fn a_path_that_is_not_utf8_is_no_value_of_path_path() {
use std::ffi::OsStr;
use std::os::unix::ffi::OsStrExt;
let path = PathBuf::from(OsStr::from_bytes(b"caf\xe9"));
let (catalog, ty) = projected::<PathBuf>();
assert!(flow_abi::shaped_value(&path, &catalog, &ty).is_err());
}