use std::io::{self, Read};
use std::path::{Path, PathBuf};
use cap_std::fs::Dir;
use semver::Version;
use serde_json::Value;
use super::fs::repo_path_display;
use super::CliError;
const NPM_PACKAGE: &str = "@oakoss/oakum";
const UNPARSEABLE_YAML: &str = "unparseable-yaml";
const WITH_NOT_OBJECT: &str = "with-not-object";
pub(super) fn verify(dir: &Dir, expected: &Version) -> Result<(), CliError> {
let pins = collect_pins(dir).map_err(CliError::from_boxed)?;
if pins.is_empty() {
return Err(CliError::unverified(format!(
"unverified: no oakum install pin in `.github/workflows`, `.github/actions`, \
`package.json`, `.mise.toml`, or a Cargo workspace member named `oakum`; pin \
the same version as `tool-version` (`{expected}`), for example \
`cargo binstall --no-confirm oakum@{expected}` or \
`pnpm add -D {NPM_PACKAGE}@{expected}`"
)));
}
let mismatches: Vec<&FoundPin> = pins.iter().filter(|pin| pin.version != *expected).collect();
if mismatches.is_empty() {
return Ok(());
}
let listed = mismatches
.iter()
.map(|pin| format!("{} ({})", pin.version, repo_path_display(&pin.source)))
.collect::<Vec<_>>()
.join(", ");
Err(CliError::unverified(format!(
"unverified: install pin is {listed} but `tool-version` is `{expected}`"
)))
}
#[derive(Debug)]
struct FoundPin {
source: PathBuf,
version: Version,
}
pub(super) fn has_any(dir: &Dir) -> bool {
collect_pins(dir).is_ok_and(|pins| !pins.is_empty())
}
#[cfg(test)]
mod pin_presence {
#[test]
fn an_unreadable_tree_answers_no() {
let dir =
cap_std::fs::Dir::open_ambient_dir(std::env::temp_dir(), cap_std::ambient_authority())
.expect("temp dir");
assert!(!super::has_any(&dir), "no pin sources means no pin");
}
}
fn collect_pins(dir: &Dir) -> Result<Vec<FoundPin>, Box<dyn std::error::Error>> {
let mut pins = Vec::new();
scan_workflows(dir, &mut pins)?;
scan_composite_actions(dir, &mut pins)?;
if let Some(pin) = read_package_json_pin(dir)? {
pins.push(pin);
}
if let Some(pin) = read_mise_pin(dir)? {
pins.push(pin);
}
if let Some(pin) = read_workspace_oakum_pin(dir)? {
pins.push(pin);
}
Ok(pins)
}
fn scan_workflows(dir: &Dir, pins: &mut Vec<FoundPin>) -> Result<(), Box<dyn std::error::Error>> {
let entries = match dir.read_dir(".github/workflows") {
Ok(entries) => entries,
Err(err) if err.kind() == io::ErrorKind::NotFound => return Ok(()),
Err(err) => {
return Err(Box::new(CliError::unverified(format!(
"unverified: failed to read `.github/workflows`: {err}"
))));
}
};
for entry in entries {
let entry = entry.map_err(|err| {
CliError::unverified(format!(
"unverified: failed to read `.github/workflows`: {err}"
))
})?;
let name = entry.file_name();
let path = Path::new(".github/workflows").join(&name);
let Some(name) = name.to_str() else {
return Err(Box::new(CliError::unverified(format!(
"unverified: workflow path `{}` is not valid UTF-8",
repo_path_display(&path)
))));
};
let is_yaml = Path::new(name)
.extension()
.is_some_and(|ext| ext.eq_ignore_ascii_case("yml") || ext.eq_ignore_ascii_case("yaml"));
if !is_yaml {
continue;
}
let file_type = entry.file_type().map_err(|err| {
CliError::unverified(format!(
"unverified: failed to inspect `{}`: {err}",
repo_path_display(&path)
))
})?;
if !file_type.is_file() {
return Err(Box::new(CliError::unverified(format!(
"unverified: `{}` is not a file",
repo_path_display(&path)
))));
}
let text = read_text(dir, &path)?;
push_yaml_pins(&path, &text, pins)?;
}
Ok(())
}
fn scan_composite_actions(
dir: &Dir,
pins: &mut Vec<FoundPin>,
) -> Result<(), Box<dyn std::error::Error>> {
let entries = match dir.read_dir(".github/actions") {
Ok(entries) => entries,
Err(err) if err.kind() == io::ErrorKind::NotFound => return Ok(()),
Err(err) => {
return Err(Box::new(CliError::unverified(format!(
"unverified: failed to read `.github/actions`: {err}"
))));
}
};
for entry in entries {
let entry = entry.map_err(|err| {
CliError::unverified(format!(
"unverified: failed to read `.github/actions`: {err}"
))
})?;
let name = entry.file_name();
let action_dir = Path::new(".github/actions").join(&name);
let file_type = entry.file_type().map_err(|err| {
CliError::unverified(format!(
"unverified: failed to inspect `{}`: {err}",
repo_path_display(&action_dir)
))
})?;
let is_action_dir = if file_type.is_dir() {
true
} else if file_type.is_symlink() {
match dir.metadata(&action_dir) {
Ok(meta) => meta.is_dir(),
Err(err) if err.kind() == io::ErrorKind::NotFound => false,
Err(err) => {
return Err(Box::new(CliError::unverified(format!(
"unverified: failed to inspect `{}`: {err}",
repo_path_display(&action_dir)
))));
}
}
} else {
false
};
if !is_action_dir {
continue;
}
for file_name in ["action.yml", "action.yaml"] {
let path = action_dir.join(file_name);
let Some(text) = read_text_optional(dir, &path)? else {
continue;
};
push_yaml_pins(&path, &text, pins)?;
}
}
Ok(())
}
fn push_yaml_pins(
path: &Path,
text: &str,
pins: &mut Vec<FoundPin>,
) -> Result<(), Box<dyn std::error::Error>> {
for version in versions_in_workflow(text).map_err(|raw| {
if raw == "unversioned" {
CliError::unverified(format!(
"unverified: `{}` installs oakum without a version",
repo_path_display(path)
))
} else if raw == UNPARSEABLE_YAML {
CliError::unverified(format!(
"unverified: `{}` is not valid YAML",
repo_path_display(path)
))
} else if raw == WITH_NOT_OBJECT {
CliError::unverified(format!(
"unverified: `{}` has a `with` value that is not a mapping",
repo_path_display(path)
))
} else {
CliError::unverified(format!(
"unverified: `{}` pins oakum as `{raw}`, which is not an exact version",
repo_path_display(path)
))
}
})? {
pins.push(FoundPin {
source: path.to_path_buf(),
version,
});
}
Ok(())
}
fn read_text_optional(
dir: &Dir,
path: &Path,
) -> Result<Option<String>, Box<dyn std::error::Error>> {
match dir.open(path) {
Ok(mut file) => {
let mut text = String::new();
file.read_to_string(&mut text).map_err(|err| {
CliError::unverified(format!(
"unverified: failed to read `{}`: {err}",
repo_path_display(path)
))
})?;
Ok(Some(text))
}
Err(err) if err.kind() == io::ErrorKind::NotFound => Ok(None),
Err(err) => Err(Box::new(CliError::unverified(format!(
"unverified: failed to read `{}`: {err}",
repo_path_display(path)
)))),
}
}
fn read_text(dir: &Dir, path: &Path) -> Result<String, Box<dyn std::error::Error>> {
let mut file = dir.open(path).map_err(|err| {
CliError::unverified(format!(
"unverified: failed to read `{}`: {err}",
repo_path_display(path)
))
})?;
let mut text = String::new();
file.read_to_string(&mut text).map_err(|err| {
CliError::unverified(format!(
"unverified: failed to read `{}`: {err}",
repo_path_display(path)
))
})?;
Ok(text)
}
fn read_package_json_pin(dir: &Dir) -> Result<Option<FoundPin>, Box<dyn std::error::Error>> {
let text = match dir.open("package.json") {
Ok(mut file) => {
let mut text = String::new();
file.read_to_string(&mut text).map_err(|err| {
CliError::unverified(format!("unverified: failed to read `package.json`: {err}"))
})?;
text
}
Err(err) if err.kind() == io::ErrorKind::NotFound => return Ok(None),
Err(err) => {
return Err(Box::new(CliError::unverified(format!(
"unverified: failed to read `package.json`: {err}"
))));
}
};
let value: serde_json::Value = serde_json::from_str(&text).map_err(|err| {
CliError::unverified(format!(
"unverified: `package.json` is not valid JSON: {err}"
))
})?;
let Some(object) = value.as_object() else {
return Err(Box::new(CliError::unverified(
"unverified: `package.json` is not a JSON object",
)));
};
let mut pin: Option<(&str, Version)> = None;
for section in [
"dependencies",
"devDependencies",
"optionalDependencies",
"peerDependencies",
] {
let Some(section_value) = object.get(section) else {
continue;
};
let Some(deps) = section_value.as_object() else {
return Err(Box::new(CliError::unverified(format!(
"unverified: `package.json` `{section}` is not an object"
))));
};
for name in [NPM_PACKAGE, "oakum"] {
let Some(oakum) = deps.get(name) else {
continue;
};
let Some(raw) = oakum.as_str() else {
return Err(Box::new(CliError::unverified(format!(
"unverified: `package.json` pins `{name}` with a non-string value"
))));
};
let Some(version) = exact_version(raw) else {
return Err(Box::new(CliError::unverified(format!(
"unverified: `package.json` pins `{name}` as `{raw}`, which is not an exact version"
))));
};
match &pin {
None => pin = Some((name, version)),
Some((first, existing)) if *existing != version => {
return Err(Box::new(CliError::unverified(format!(
"unverified: `package.json` pins `{first}` as `{existing}` and `{name}` as `{version}`; keep one exact version"
))));
}
Some(_) => {}
}
}
}
Ok(pin.map(|(_, version)| FoundPin {
source: PathBuf::from("package.json"),
version,
}))
}
fn read_mise_pin(dir: &Dir) -> Result<Option<FoundPin>, Box<dyn std::error::Error>> {
let mut found = None;
for name in [".mise.toml", "mise.toml"] {
let Some(pin) = read_one_mise(dir, name)? else {
continue;
};
match &found {
None => found = Some(pin),
Some(existing) if existing.version != pin.version => {
return Err(Box::new(CliError::unverified(format!(
"unverified: `{}` pins oakum as `{}` but `{}` pins `{pin_version}`",
repo_path_display(&existing.source),
existing.version,
repo_path_display(&pin.source),
pin_version = pin.version,
))));
}
Some(_) => {}
}
}
Ok(found)
}
fn read_one_mise(dir: &Dir, name: &str) -> Result<Option<FoundPin>, Box<dyn std::error::Error>> {
let text = match dir.open(name) {
Ok(mut file) => {
let mut text = String::new();
file.read_to_string(&mut text).map_err(|err| {
CliError::unverified(format!("unverified: failed to read `{name}`: {err}"))
})?;
text
}
Err(err) if err.kind() == io::ErrorKind::NotFound => return Ok(None),
Err(err) => {
return Err(Box::new(CliError::unverified(format!(
"unverified: failed to read `{name}`: {err}"
))));
}
};
let value: toml::Value = toml::from_str(&text).map_err(|err| {
CliError::unverified(format!("unverified: `{name}` is not valid TOML: {err}"))
})?;
let Some(tools_value) = value.get("tools") else {
return Ok(None);
};
let Some(tools) = tools_value.as_table() else {
return Err(Box::new(CliError::unverified(format!(
"unverified: `{name}` has a `tools` value that is not a table"
))));
};
let mut pin = None;
for (key, spec) in tools {
if !is_mise_oakum_key(key) {
continue;
}
let raw = mise_version_spec(spec).ok_or_else(|| {
CliError::unverified(format!(
"unverified: `{name}` pins `{key}` with a value that is not an exact version"
))
})?;
let Some(version) = exact_version(raw) else {
return Err(Box::new(CliError::unverified(format!(
"unverified: `{name}` pins oakum as `{raw}`, which is not an exact version"
))));
};
match &pin {
None => pin = Some(version),
Some(existing) if *existing != version => {
return Err(Box::new(CliError::unverified(format!(
"unverified: `{name}` pins oakum as both `{existing}` and `{version}`"
))));
}
Some(_) => {}
}
}
Ok(pin.map(|version| FoundPin {
source: PathBuf::from(name),
version,
}))
}
fn is_mise_oakum_key(key: &str) -> bool {
key == "oakum" || key == "cargo:oakum" || key == "npm:@oakoss/oakum"
}
fn mise_version_spec(spec: &toml::Value) -> Option<&str> {
spec.as_str()
.or_else(|| spec.get("version").and_then(toml::Value::as_str))
}
fn read_workspace_oakum_pin(dir: &Dir) -> Result<Option<FoundPin>, Box<dyn std::error::Error>> {
let Some(root_text) = read_toml_file(dir, Path::new("Cargo.toml"))? else {
return Ok(None);
};
let root: toml::Value = toml::from_str(&root_text).map_err(|err| {
CliError::unverified(format!("unverified: `Cargo.toml` is not valid TOML: {err}"))
})?;
let mut found: Option<FoundPin> = None;
if let Some(pin) = oakum_pin_from_manifest(&root, Path::new("Cargo.toml"), &root)? {
found = Some(pin);
}
let Some(workspace) = root.get("workspace") else {
return Ok(found);
};
let excluded = excluded_member_dirs(dir, workspace)?;
let members = match workspace.get("members") {
None => return Ok(found),
Some(value) => value.as_array().ok_or_else(|| {
Box::new(CliError::unverified(
"unverified: `Cargo.toml` `[workspace].members` is not an array".to_owned(),
)) as Box<dyn std::error::Error>
})?,
};
for member in members {
let Some(rel) = member_path(member) else {
continue;
};
for path in candidate_member_manifests(dir, rel)? {
let Some(package_dir) = path.parent().map(normalize_rel_path) else {
continue;
};
if excluded.contains(&package_dir) {
continue;
}
let Some(text) = read_toml_file(dir, &path)? else {
continue;
};
let manifest: toml::Value = toml::from_str(&text).map_err(|err| {
CliError::unverified(format!(
"unverified: `{}` is not valid TOML: {err}",
repo_path_display(&path)
))
})?;
let Some(pin) = oakum_pin_from_manifest(&manifest, &path, &root)? else {
continue;
};
match &found {
None => found = Some(pin),
Some(existing) if existing.version != pin.version => {
return Err(Box::new(CliError::unverified(format!(
"unverified: `{}` pins oakum as `{}` but `{}` pins `{pin_version}`",
repo_path_display(&existing.source),
existing.version,
repo_path_display(&pin.source),
pin_version = pin.version,
))));
}
Some(_) => {}
}
}
}
Ok(found)
}
fn member_path(member: &toml::Value) -> Option<&str> {
member
.as_str()
.or_else(|| member.get("path").and_then(toml::Value::as_str))
}
fn is_glob_member(rel: &str) -> bool {
rel.bytes().any(|byte| matches!(byte, b'*' | b'?' | b'['))
}
fn normalize_rel_path(path: &Path) -> PathBuf {
let mut out = PathBuf::new();
for component in path.components() {
match component {
std::path::Component::Normal(part) => out.push(part),
std::path::Component::ParentDir => {
let _ = out.pop();
}
std::path::Component::CurDir
| std::path::Component::RootDir
| std::path::Component::Prefix(_) => {}
}
}
out
}
fn excluded_member_dirs(
dir: &Dir,
workspace: &toml::Value,
) -> Result<Vec<PathBuf>, Box<dyn std::error::Error>> {
let Some(exclude) = workspace.get("exclude") else {
return Ok(Vec::new());
};
let Some(entries) = exclude.as_array() else {
return Err(Box::new(CliError::unverified(
"unverified: `Cargo.toml` `[workspace].exclude` is not an array".to_owned(),
)));
};
let mut dirs = Vec::new();
for entry in entries {
let Some(rel) = member_path(entry) else {
continue;
};
for path in candidate_member_manifests(dir, rel)? {
if let Some(parent) = path.parent() {
dirs.push(normalize_rel_path(parent));
}
}
}
Ok(dirs)
}
fn candidate_member_manifests(
dir: &Dir,
rel: &str,
) -> Result<Vec<PathBuf>, Box<dyn std::error::Error>> {
if !is_glob_member(rel) {
let package = normalize_rel_path(Path::new(rel));
return Ok(vec![package.join("Cargo.toml")]);
}
let Some((parent, pat)) = rel.rsplit_once('/') else {
return Err(Box::new(CliError::unverified(format!(
"unverified: unsupported `workspace.members` glob `{rel}` \
(only a trailing `/*` is expanded)"
))));
};
if pat != "*" {
return Err(Box::new(CliError::unverified(format!(
"unverified: unsupported `workspace.members` glob `{rel}` \
(only a trailing `/*` is expanded)"
))));
}
let parent = normalize_rel_path(Path::new(parent));
let parent_key = parent.to_string_lossy();
let entries = match dir.read_dir(parent.as_path()) {
Ok(entries) => entries,
Err(err) if err.kind() == io::ErrorKind::NotFound => return Ok(Vec::new()),
Err(err) => {
return Err(Box::new(CliError::unverified(format!(
"unverified: failed to read workspace member directory `{parent_key}`: {err}"
))));
}
};
let mut paths = Vec::new();
for entry in entries {
let entry = entry.map_err(|err| {
CliError::unverified(format!(
"unverified: failed to read workspace member directory `{parent_key}`: {err}"
))
})?;
let child = parent.join(entry.file_name());
let file_type = entry.file_type().map_err(|err| {
CliError::unverified(format!(
"unverified: failed to inspect `{}`: {err}",
repo_path_display(&child)
))
})?;
let is_package_dir = if file_type.is_dir() {
true
} else if file_type.is_symlink() {
match dir.metadata(&child) {
Ok(meta) => meta.is_dir(),
Err(err) if err.kind() == io::ErrorKind::NotFound => false,
Err(err) => {
return Err(Box::new(CliError::unverified(format!(
"unverified: failed to inspect `{}`: {err}",
repo_path_display(&child)
))));
}
}
} else {
false
};
if !is_package_dir {
continue;
}
paths.push(child.join("Cargo.toml"));
}
Ok(paths)
}
fn read_toml_file(dir: &Dir, path: &Path) -> Result<Option<String>, Box<dyn std::error::Error>> {
match dir.open(path) {
Ok(mut file) => {
let mut text = String::new();
file.read_to_string(&mut text).map_err(|err| {
CliError::unverified(format!(
"unverified: failed to read `{}`: {err}",
repo_path_display(path)
))
})?;
Ok(Some(text))
}
Err(err) if err.kind() == io::ErrorKind::NotFound => Ok(None),
Err(err) => Err(Box::new(CliError::unverified(format!(
"unverified: failed to read `{}`: {err}",
repo_path_display(path)
)))),
}
}
fn oakum_pin_from_manifest(
manifest: &toml::Value,
source: &Path,
root: &toml::Value,
) -> Result<Option<FoundPin>, Box<dyn std::error::Error>> {
let Some(package) = manifest.get("package") else {
return Ok(None);
};
if package.get("name").and_then(toml::Value::as_str) != Some("oakum") {
return Ok(None);
}
let version = package_version(package, root, source)?;
Ok(Some(FoundPin {
source: source.to_path_buf(),
version,
}))
}
fn package_version(
package: &toml::Value,
root: &toml::Value,
source: &Path,
) -> Result<Version, Box<dyn std::error::Error>> {
if let Some(raw) = package.get("version").and_then(toml::Value::as_str) {
return exact_version(raw).ok_or_else(|| {
Box::new(CliError::unverified(format!(
"unverified: `{}` package version `{raw}` is not an exact version",
repo_path_display(source)
))) as Box<dyn std::error::Error>
});
}
if package
.get("version")
.and_then(|value| value.get("workspace"))
.and_then(toml::Value::as_bool)
== Some(true)
{
let raw = root
.get("workspace")
.and_then(|workspace| workspace.get("package"))
.and_then(|package| package.get("version"))
.and_then(toml::Value::as_str)
.ok_or_else(|| {
CliError::unverified(format!(
"unverified: `{}` inherits `version` but \
`[workspace.package].version` is missing",
repo_path_display(source)
))
})?;
return exact_version(raw).ok_or_else(|| {
Box::new(CliError::unverified(format!(
"unverified: `[workspace.package].version` `{raw}` is not an exact version"
))) as Box<dyn std::error::Error>
});
}
Err(Box::new(CliError::unverified(format!(
"unverified: `{}` package `oakum` has no exact `version`",
repo_path_display(source)
))))
}
fn versions_in_workflow(text: &str) -> Result<Vec<Version>, String> {
if text.trim().is_empty() {
return Ok(Vec::new());
}
let value: Value = serde_saphyr::from_str(text).map_err(|_| UNPARSEABLE_YAML.to_string())?;
let mut versions = Vec::new();
walk_workflow_value(&value, &mut versions)?;
Ok(versions)
}
fn walk_workflow_value(value: &Value, versions: &mut Vec<Version>) -> Result<(), String> {
match value {
Value::Object(map) => {
if let Some(Value::String(run)) = map.get("run") {
append_run_versions(run, versions)?;
}
if let Some(tool) = map.get("tool") {
append_tool_value(tool, versions)?;
}
if let Some(with) = map.get("with") {
walk_with_value(with, versions)?;
}
if let Some(matrix) = map.get("matrix") {
walk_matrix_value(matrix, versions)?;
}
if let Some(Value::String(default)) = map.get("default") {
append_run_versions(default, versions)?;
}
for (key, child) in map {
if matches!(key.as_str(), "with" | "matrix") {
continue;
}
walk_workflow_value(child, versions)?;
}
}
Value::Array(items) => {
for item in items {
walk_workflow_value(item, versions)?;
}
}
_ => {}
}
Ok(())
}
fn walk_with_value(with: &Value, versions: &mut Vec<Version>) -> Result<(), String> {
let Value::Object(map) = with else {
return Err(WITH_NOT_OBJECT.to_owned());
};
for (key, child) in map {
if key == "tool" {
append_tool_value(child, versions)?;
} else {
append_stringish_as_run(child, versions)?;
}
}
Ok(())
}
fn walk_matrix_value(matrix: &Value, versions: &mut Vec<Version>) -> Result<(), String> {
match matrix {
Value::Object(map) => {
for (key, child) in map {
if key == "tool" {
append_tool_value(child, versions)?;
} else {
match child {
Value::String(text) => append_run_versions(text, versions)?,
Value::Array(_) | Value::Object(_) => walk_matrix_value(child, versions)?,
_ => {}
}
}
}
}
Value::Array(items) => {
for item in items {
walk_matrix_value(item, versions)?;
}
}
Value::String(text) => append_run_versions(text, versions)?,
_ => {}
}
Ok(())
}
fn append_tool_value(value: &Value, versions: &mut Vec<Version>) -> Result<(), String> {
match value {
Value::String(tool) => append_tool_versions(tool, versions),
Value::Array(items) => {
for item in items {
if let Value::String(tool) = item {
append_tool_versions(tool, versions)?;
}
}
Ok(())
}
_ => Ok(()),
}
}
fn append_stringish_as_run(value: &Value, versions: &mut Vec<Version>) -> Result<(), String> {
match value {
Value::String(text) => append_run_versions(text, versions),
Value::Array(items) => {
for item in items {
if let Value::String(text) = item {
append_run_versions(text, versions)?;
}
}
Ok(())
}
_ => Ok(()),
}
}
fn append_run_versions(run: &str, versions: &mut Vec<Version>) -> Result<(), String> {
for line in logical_lines(run) {
if line.trim().is_empty() {
continue;
}
append_install_line(&format!("run: {line}"), versions)?;
}
Ok(())
}
fn append_tool_versions(tool: &str, versions: &mut Vec<Version>) -> Result<(), String> {
for line in logical_lines(tool) {
if line.trim().is_empty() {
continue;
}
append_install_line(&format!("tool: {line}"), versions)?;
}
Ok(())
}
fn append_install_line(line: &str, versions: &mut Vec<Version>) -> Result<(), String> {
let code = strip_comment(line);
if !is_install_line(code) {
return Ok(());
}
for unit in install_units(code) {
if unversioned_oakum_install(unit) {
return Err("unversioned".to_owned());
}
for raw in install_version_specs(unit) {
match exact_version(raw) {
Some(version) => versions.push(version),
None => return Err(raw.to_owned()),
}
}
}
Ok(())
}
fn logical_lines(text: &str) -> Vec<String> {
let mut lines = Vec::new();
let mut pending = String::new();
for line in text.lines() {
let trimmed = line.trim_end();
if let Some(prefix) = trimmed.strip_suffix('\\') {
pending.push_str(prefix.trim_end());
pending.push(' ');
continue;
}
pending.push_str(trimmed);
lines.push(std::mem::take(&mut pending));
}
if !pending.is_empty() {
lines.push(pending);
}
lines
}
fn is_install_line(line: &str) -> bool {
let lowered = line.to_ascii_lowercase();
let trimmed = lowered.trim();
if let Some(tool) = trimmed
.strip_prefix("tool:")
.or_else(|| trimmed.strip_prefix("- tool:"))
{
return token_present(tool, "oakum");
}
let Some(command) = run_command(&lowered) else {
return false;
};
shell_segments(command).iter().any(|segment| {
if is_echo_or_printf(segment) {
return false;
}
segment.contains("binstall")
|| segment.contains("cargo install")
|| is_npm_install_unit(segment)
})
}
fn npm_install_specs(segment: &str) -> Option<Vec<&str>> {
let mut argv = segment
.split_whitespace()
.map(|token| token.trim_start_matches('('))
.filter(|token| !token.is_empty())
.skip_while(|token| {
token.eq_ignore_ascii_case("sudo")
|| token.starts_with('-')
|| (token.contains('=') && !token.starts_with('-'))
})
.map(crate_token);
let verb = argv.next()?.to_ascii_lowercase();
let mut argv = argv.peekable();
let single = match verb.as_str() {
"npx" => true,
"npm" => match next_subcommand(&mut argv)?.as_str() {
"i" | "install" | "add" => false,
_ => return None,
},
"pnpm" => match next_subcommand(&mut argv)?.as_str() {
"dlx" => true,
"add" | "install" | "i" => false,
_ => return None,
},
_ => return None,
};
let mut specs = Vec::new();
let mut positional_taken = false;
let mut argv = argv.peekable();
while let Some(token) = argv.next() {
if let Some(spec) = token.strip_prefix("--package=") {
specs.push(spec);
} else if token == "-p" || token == "--package" {
if let Some(spec) = argv.next() {
specs.push(spec);
}
} else if token.starts_with('-') {
} else if single {
if !positional_taken {
specs.push(token);
}
positional_taken = true;
} else {
specs.push(token);
}
}
Some(specs)
}
fn next_subcommand<'a>(argv: &mut impl Iterator<Item = &'a str>) -> Option<String> {
loop {
let token = argv.next()?;
if matches!(token, "--filter" | "--prefix" | "--dir" | "-C" | "-F") {
argv.next();
continue;
}
if token.starts_with('-') {
continue;
}
return Some(token.to_ascii_lowercase());
}
}
fn is_oakum_npm_spec(spec: &str) -> bool {
spec.eq_ignore_ascii_case(NPM_PACKAGE)
|| spec
.get(..NPM_PACKAGE.len() + 1)
.is_some_and(|head| head.eq_ignore_ascii_case("@oakoss/oakum@"))
}
fn is_npm_install_unit(segment: &str) -> bool {
npm_install_specs(segment).is_some_and(|specs| specs.iter().any(|spec| is_oakum_npm_spec(spec)))
}
fn npm_oakum_versions(segment: &str) -> Vec<&str> {
npm_install_specs(segment)
.unwrap_or_default()
.into_iter()
.filter(|spec| is_oakum_npm_spec(spec))
.filter_map(|spec| spec.get(NPM_PACKAGE.len() + 1..))
.map(version_token)
.collect()
}
fn install_units(line: &str) -> Vec<&str> {
let Some(command) = run_command(line) else {
return vec![line];
};
shell_segments(command)
.into_iter()
.filter(|segment| !is_echo_or_printf(segment))
.collect()
}
fn shell_segments(command: &str) -> Vec<&str> {
let mut segments = Vec::new();
let mut rest = command;
loop {
let lower = rest.to_ascii_lowercase();
let split = ["&&", ";"]
.iter()
.filter_map(|sep| lower.find(sep).map(|index| (index, sep.len())))
.min_by_key(|(index, _)| *index);
if let Some((index, sep_len)) = split {
segments.push(rest[..index].trim());
rest = rest[index + sep_len..].trim();
} else {
segments.push(rest.trim());
break;
}
}
segments
}
fn is_echo_or_printf(segment: &str) -> bool {
let trimmed = segment.trim().to_ascii_lowercase();
trimmed.starts_with("echo ")
|| trimmed.starts_with("echo\"")
|| trimmed.starts_with("echo'")
|| trimmed.starts_with("printf ")
}
fn run_command(line: &str) -> Option<&str> {
let trimmed = line.trim();
let lower = trimmed.to_ascii_lowercase();
let prefix_len = if lower.starts_with("- run:") {
"- run:".len()
} else if lower.starts_with("run:") {
"run:".len()
} else {
return None;
};
let command = trimmed[prefix_len..].trim();
(!command.is_empty()).then_some(command)
}
fn strip_comment(line: &str) -> &str {
let mut in_single = false;
let mut in_double = false;
for (index, character) in line.char_indices() {
match character {
'\'' if !in_double => in_single = !in_single,
'"' if !in_single => in_double = !in_double,
'#' if !in_single && !in_double => return line[..index].trim_end(),
_ => {}
}
}
line
}
fn is_tool_install_unit(unit: &str) -> bool {
let trimmed = unit.trim().to_ascii_lowercase();
trimmed.starts_with("tool:") || trimmed.starts_with("- tool:")
}
fn install_version_specs(unit: &str) -> Vec<&str> {
if is_tool_install_unit(unit) {
return oakum_at_specs(unit);
}
let lowered = unit.to_ascii_lowercase();
let mut specs = Vec::new();
if lowered.contains("binstall") {
specs.extend(binstall_specs(unit));
}
if lowered.contains("cargo install") {
specs.extend(cargo_install_specs(unit));
}
if is_npm_install_unit(unit) {
specs.extend(npm_oakum_versions(unit));
}
if specs.is_empty() {
specs = oakum_at_specs(unit);
}
specs
}
fn oakum_at_specs(line: &str) -> Vec<&str> {
let lowered = line.to_ascii_lowercase();
let mut specs = Vec::new();
let mut cursor = 0;
while let Some(relative) = lowered[cursor..].find("oakum@") {
let start = cursor + relative;
if !is_token_start(line, start) {
cursor = start + 6;
continue;
}
specs.push(version_token(&line[start + 6..]));
cursor = start + 6;
}
specs
}
fn unversioned_oakum_install(line: &str) -> bool {
unversioned_tool_line(line)
|| unversioned_binstall(line)
|| unversioned_cargo_install(line)
|| unversioned_npm_install(line)
}
fn unversioned_npm_install(line: &str) -> bool {
npm_install_specs(line).is_some_and(|specs| {
specs
.iter()
.any(|spec| spec.eq_ignore_ascii_case(NPM_PACKAGE))
})
}
fn unversioned_tool_line(line: &str) -> bool {
let lowered = line.to_ascii_lowercase();
let trimmed = lowered.trim();
let Some(tool) = trimmed
.strip_prefix("tool:")
.or_else(|| trimmed.strip_prefix("- tool:"))
else {
return false;
};
tool.split([',', ' ', '\t'])
.map(crate_token)
.any(|token| token == "oakum")
}
fn unversioned_binstall(line: &str) -> bool {
let lowered = line.to_ascii_lowercase();
let mut search = 0;
while let Some(relative) = lowered[search..].find("binstall") {
let start = search + relative;
let rest = &line[start + "binstall".len()..];
if binstall_oakum_unversioned(rest) {
return true;
}
search = start + "binstall".len();
}
false
}
fn binstall_oakum_unversioned(rest: &str) -> bool {
let lower_rest = rest.to_ascii_lowercase();
let end = ["&&", ";", "|"]
.iter()
.filter_map(|sep| lower_rest.find(sep))
.min()
.unwrap_or(rest.len());
argv_oakum_unversioned(rest[..end].trim())
}
fn crate_token(token: &str) -> &str {
token.trim_matches(|character| {
matches!(
character,
'"' | '\'' | '(' | ')' | '[' | ']' | '{' | '}' | '`' | ',' | ';'
)
})
}
fn unversioned_cargo_install(line: &str) -> bool {
let lowered = line.to_ascii_lowercase();
let mut search = 0;
while let Some(relative) = lowered[search..].find("cargo install") {
let start = search + relative;
let rest = &line[start + "cargo install".len()..];
if cargo_install_oakum_unversioned(rest) {
return true;
}
search = start + "cargo install".len();
}
false
}
fn cargo_install_oakum_unversioned(rest: &str) -> bool {
let lower_rest = rest.to_ascii_lowercase();
let end = ["&&", ";", "|"]
.iter()
.filter_map(|sep| lower_rest.find(sep))
.min()
.unwrap_or(rest.len());
argv_oakum_unversioned(rest[..end].trim())
}
fn argv_oakum_unversioned(segment: &str) -> bool {
let mut saw_oakum = false;
let mut version = false;
let mut tokens = segment.split_whitespace().peekable();
while let Some(token) = tokens.next() {
let lower = crate_token(token).to_ascii_lowercase();
if lower.starts_with("oakum@") {
continue;
}
if lower == "oakum" {
saw_oakum = true;
continue;
}
if let Some(spec) = lower
.strip_prefix("--version=")
.or_else(|| lower.strip_prefix("--vers="))
{
version = exact_version(spec).is_some();
continue;
}
if lower == "--version" || lower == "--vers" {
version = tokens
.next()
.and_then(|next| exact_version(crate_token(next)))
.is_some();
}
}
saw_oakum && !version
}
fn binstall_specs(line: &str) -> Vec<&str> {
install_command_specs(line, "binstall")
}
fn cargo_install_specs(line: &str) -> Vec<&str> {
install_command_specs(line, "cargo install")
}
fn install_command_specs<'a>(line: &'a str, verb: &str) -> Vec<&'a str> {
let lowered = line.to_ascii_lowercase();
let mut specs = Vec::new();
let mut search = 0;
while let Some(relative) = lowered[search..].find(verb) {
let start = search + relative;
if let Some(spec) = argv_oakum_version_spec(&line[start + verb.len()..]) {
specs.push(spec);
}
search = start + verb.len();
}
specs
}
fn argv_oakum_version_spec(rest: &str) -> Option<&str> {
let lower_rest = rest.to_ascii_lowercase();
let end = ["&&", ";", "|"]
.iter()
.filter_map(|sep| lower_rest.find(sep))
.min()
.unwrap_or(rest.len());
let segment = rest[..end].trim();
let mut saw_oakum = false;
let mut version = None;
let mut tokens = segment.split_whitespace().peekable();
while let Some(token) = tokens.next() {
let token = crate_token(token);
let lower = token.to_ascii_lowercase();
if let Some(spec) = lower.strip_prefix("oakum@") {
return Some(version_token(&token[token.len() - spec.len()..]));
}
if lower == "oakum" {
saw_oakum = true;
continue;
}
if let Some(spec) = lower
.strip_prefix("--version=")
.or_else(|| lower.strip_prefix("--vers="))
{
version = Some(version_token(&token[token.len() - spec.len()..]));
continue;
}
if lower == "--version" || lower == "--vers" {
if let Some(next) = tokens.next() {
version = Some(version_token(
next.trim_matches(|character| matches!(character, '"' | '\'')),
));
}
}
}
saw_oakum.then_some(())?;
version
}
fn token_present(lowered: &str, token: &str) -> bool {
let mut cursor = 0;
while let Some(relative) = lowered[cursor..].find(token) {
let start = cursor + relative;
if is_token_start(lowered, start) && is_token_end(lowered, start + token.len()) {
return true;
}
cursor = start + 1;
}
false
}
fn is_token_start(text: &str, index: usize) -> bool {
index == 0 || {
let previous = text.as_bytes()[index - 1];
!previous.is_ascii_alphanumeric() && previous != b'_' && previous != b'-'
}
}
fn is_token_end(text: &str, index: usize) -> bool {
index >= text.len() || {
let next = text.as_bytes()[index];
!next.is_ascii_alphanumeric() && next != b'_' && next != b'-'
}
}
fn version_token(rest: &str) -> &str {
let rest = rest.trim_start_matches(['"', '\'']);
let end = rest
.find(|character: char| {
character.is_whitespace()
|| matches!(
character,
'"' | '\'' | ',' | ';' | '#' | '|' | '`' | ')' | ']'
)
})
.unwrap_or(rest.len());
rest[..end].trim_end_matches(['"', '\''])
}
fn exact_version(raw: &str) -> Option<Version> {
let trimmed = raw.trim();
let trimmed = trimmed.strip_prefix('v').unwrap_or(trimmed);
Version::parse(trimmed).ok()
}
#[cfg(test)]
mod tests {
use super::*;
use crate::test_fixture::Fixture;
fn workflow(text: &str) -> Vec<Version> {
versions_in_workflow(text).expect("exact pins")
}
#[test]
fn binstall_line_yields_the_version() {
let versions = workflow("run: cargo binstall --no-confirm oakum@1.2.3\n");
assert_eq!(versions, vec![Version::parse("1.2.3").unwrap()]);
}
#[test]
fn v_prefix_and_comment_are_stripped() {
let versions = workflow("run: cargo binstall oakum@v0.1.0 # pin\n");
assert_eq!(versions, vec![Version::parse("0.1.0").unwrap()]);
}
#[test]
fn double_v_prefix_is_not_an_exact_pin() {
let err = versions_in_workflow("run: cargo binstall oakum@vv0.1.0\n").unwrap_err();
assert_eq!(err, "vv0.1.0");
}
#[test]
fn commented_out_pin_is_ignored() {
assert!(workflow("# cargo binstall oakum@1.2.3\n").is_empty());
}
#[test]
fn cargo_install_version_flag() {
let versions = workflow("run: cargo install oakum --version 2.0.0\n");
assert_eq!(versions, vec![Version::parse("2.0.0").unwrap()]);
let versions = workflow("run: cargo install oakum --vers=2.0.0\n");
assert_eq!(versions, vec![Version::parse("2.0.0").unwrap()]);
}
#[test]
fn cargo_install_oakum_at_is_one_pin_not_two() {
let versions = workflow("run: cargo install oakum@1.2.3\n");
assert_eq!(versions, vec![Version::parse("1.2.3").unwrap()]);
}
#[test]
fn binstall_version_flag() {
let versions = workflow("run: cargo binstall oakum --version 2.0.0\n");
assert_eq!(versions, vec![Version::parse("2.0.0").unwrap()]);
let versions = workflow("run: cargo binstall --no-confirm oakum --vers=2.0.0\n");
assert_eq!(versions, vec![Version::parse("2.0.0").unwrap()]);
}
#[test]
fn install_action_without_tool_is_not_a_pin() {
assert!(workflow("uses: oakoss/install-action@v1\n").is_empty());
assert!(workflow("- uses: oakoss/install-action@v1\n").is_empty());
}
#[test]
fn mixed_binstall_and_cargo_install_collect_both_pins() {
let versions = workflow("run: cargo binstall oakum@1.0.0 || cargo install oakum@2.0.0\n");
assert_eq!(
versions,
vec![
Version::parse("1.0.0").unwrap(),
Version::parse("2.0.0").unwrap()
]
);
}
#[test]
fn cargo_install_continuation_is_one_command() {
let versions = workflow("run: cargo install oakum \\\n --version 0.0.0\n");
assert_eq!(versions, vec![Version::parse("0.0.0").unwrap()]);
}
#[test]
fn run_block_scalar_is_an_install() {
let versions = workflow("run: |\n cargo binstall --no-confirm oakum@1.2.3\n");
assert_eq!(versions, vec![Version::parse("1.2.3").unwrap()]);
}
#[test]
fn inexact_pin_in_run_block_is_unverified_even_when_another_pin_matches() {
let err = versions_in_workflow(
"jobs:\n ci:\n steps:\n - run: cargo binstall --no-confirm oakum@0.0.0\n - run: |\n cargo binstall oakum@latest\n",
)
.unwrap_err();
assert_eq!(err, "latest");
}
#[test]
fn inexact_pin_in_tool_block_is_unverified_even_when_another_pin_matches() {
let err = versions_in_workflow(
"jobs:\n ci:\n steps:\n - run: cargo binstall --no-confirm oakum@0.0.0\n - uses: taiki-e/install-action\n with:\n tool: |\n oakum@latest\n",
)
.unwrap_err();
assert_eq!(err, "latest");
}
#[test]
fn cargo_install_version_must_belong_to_oakum() {
assert!(workflow("run: cargo install ripgrep --version 0.0.0 && oakum check\n").is_empty());
}
#[test]
fn env_or_echo_oakum_at_is_not_a_pin() {
assert!(workflow("env:\n OLD: oakum@0.0.0\n").is_empty());
assert!(workflow("run: echo oakum@0.0.0\n").is_empty());
assert!(workflow("run: echo cargo binstall oakum@0.0.0\n").is_empty());
assert!(workflow("OLD: cargo binstall oakum@0.0.0\n").is_empty());
}
#[test]
fn with_tool_array_is_a_pin() {
let versions = workflow(
"- uses: taiki-e/install-action\n with:\n tool:\n - oakum@0.3.1\n - ripgrep\n",
);
assert_eq!(versions, vec![Version::parse("0.3.1").unwrap()]);
}
#[test]
fn with_install_cmd_string_is_a_pin() {
let versions = workflow(
"- uses: ./local-install\n with:\n install_cmd: cargo binstall --no-confirm oakum@1.4.0\n",
);
assert_eq!(versions, vec![Version::parse("1.4.0").unwrap()]);
}
#[test]
fn with_run_string_is_a_pin() {
let versions = workflow(
"- uses: ./local-install\n with:\n run: cargo binstall --no-confirm oakum@1.5.0\n",
);
assert_eq!(versions, vec![Version::parse("1.5.0").unwrap()]);
}
#[test]
fn with_install_cmd_array_is_a_pin() {
let versions = workflow(
"- uses: ./local-install\n with:\n install_cmd:\n - cargo binstall --no-confirm oakum@1.6.0\n",
);
assert_eq!(versions, vec![Version::parse("1.6.0").unwrap()]);
}
#[test]
fn matrix_install_array_is_a_pin() {
let versions = workflow(
"strategy:\n matrix:\n install:\n - cargo binstall --no-confirm oakum@2.2.0\n",
);
assert_eq!(versions, vec![Version::parse("2.2.0").unwrap()]);
}
#[test]
fn matrix_include_run_key_is_one_pin_not_two() {
let versions = workflow(
"strategy:\n matrix:\n include:\n - run: cargo binstall --no-confirm oakum@2.0.0\n",
);
assert_eq!(versions, vec![Version::parse("2.0.0").unwrap()]);
}
#[test]
fn non_object_with_is_unverified() {
let err = versions_in_workflow(
"- uses: ./local-install\n with: cargo binstall --no-confirm oakum@1.4.0\n",
)
.unwrap_err();
assert_eq!(err, WITH_NOT_OBJECT);
}
#[test]
fn matrix_install_cell_is_a_pin() {
let versions = workflow(
"strategy:\n matrix:\n install: cargo binstall --no-confirm oakum@2.0.0\n",
);
assert_eq!(versions, vec![Version::parse("2.0.0").unwrap()]);
}
#[test]
fn matrix_include_install_cell_is_a_pin() {
let versions = workflow(
"strategy:\n matrix:\n include:\n - install: cargo binstall --no-confirm oakum@2.1.0\n",
);
assert_eq!(versions, vec![Version::parse("2.1.0").unwrap()]);
}
#[test]
fn matrix_tool_key_is_still_a_pin() {
let versions = workflow("strategy:\n matrix:\n tool: oakum@0.3.1\n");
assert_eq!(versions, vec![Version::parse("0.3.1").unwrap()]);
}
#[test]
fn workflow_call_input_default_install_is_a_pin() {
let versions = workflow(
"on:\n workflow_call:\n inputs:\n install:\n type: string\n default: cargo binstall --no-confirm oakum@3.0.0\n",
);
assert_eq!(versions, vec![Version::parse("3.0.0").unwrap()]);
}
#[test]
fn dynamic_matrix_run_expression_alone_is_not_a_pin() {
assert!(workflow("run: ${{ matrix.install }}\n").is_empty());
}
#[test]
fn install_action_tool_line_is_a_pin() {
let versions = workflow(" tool: oakum@0.3.1\n");
assert_eq!(versions, vec![Version::parse("0.3.1").unwrap()]);
}
#[test]
fn tool_block_scalar_is_a_pin() {
let versions = workflow(" tool: |\n oakum@0.3.1\n");
assert_eq!(versions, vec![Version::parse("0.3.1").unwrap()]);
}
#[test]
fn folded_run_block_is_one_command() {
let versions = workflow("run: >\n cargo install oakum\n --version 0.0.0\n");
assert_eq!(versions, vec![Version::parse("0.0.0").unwrap()]);
}
#[test]
fn every_cargo_install_on_a_line_is_scanned() {
let versions = workflow(
"run: cargo install oakum --version 0.0.0 && cargo install oakum --version 1.2.3\n",
);
assert_eq!(
versions,
vec![
Version::parse("0.0.0").unwrap(),
Version::parse("1.2.3").unwrap()
]
);
}
#[test]
fn unversioned_binstall_is_unverified() {
let err = versions_in_workflow("run: cargo binstall oakum\n").unwrap_err();
assert_eq!(err, "unversioned");
let err = versions_in_workflow(
"jobs:\n ci:\n steps:\n - run: cargo binstall oakum@0.0.0\n - run: cargo binstall oakum\n",
)
.unwrap_err();
assert_eq!(err, "unversioned");
}
#[test]
fn unversioned_install_on_the_same_line_is_unverified() {
let err = versions_in_workflow(
"run: cargo binstall --no-confirm oakum@0.0.0 && cargo binstall oakum\n",
)
.unwrap_err();
assert_eq!(err, "unversioned");
}
#[test]
fn unversioned_tool_line_is_unverified_even_when_another_pin_matches() {
let err = versions_in_workflow(
"jobs:\n ci:\n steps:\n - run: cargo binstall --no-confirm oakum@0.0.0\n - uses: taiki-e/install-action\n with:\n tool: oakum\n",
)
.unwrap_err();
assert_eq!(err, "unversioned");
}
#[test]
fn echo_then_binstall_is_a_pin() {
let versions =
workflow("run: echo installing && cargo binstall --no-confirm oakum@0.0.0\n");
assert_eq!(versions, vec![Version::parse("0.0.0").unwrap()]);
}
#[test]
fn echoed_oakum_at_is_not_a_pin() {
assert!(
workflow("run: echo oakum@0.0.0 && cargo binstall --no-confirm ripgrep\n").is_empty()
);
}
#[test]
fn unversioned_binstall_target_after_a_pin_is_unverified() {
let err = versions_in_workflow("run: cargo binstall oakum@0.0.0 oakum\n").unwrap_err();
assert_eq!(err, "unversioned");
let err = versions_in_workflow("run: cargo binstall ripgrep oakum\n").unwrap_err();
assert_eq!(err, "unversioned");
let err = versions_in_workflow("run: (cargo binstall oakum@0.0.0 oakum)\n").unwrap_err();
assert_eq!(err, "unversioned");
}
#[test]
fn unversioned_cargo_install_target_after_a_pin_is_unverified() {
let err = versions_in_workflow(
"run: cargo install oakum --version 0.0.0 && cargo install oakum\n",
)
.unwrap_err();
assert_eq!(err, "unversioned");
let err = versions_in_workflow("run: cargo install oakum@0.0.0 oakum\n").unwrap_err();
assert_eq!(err, "unversioned");
let err = versions_in_workflow("run: cargo install ripgrep oakum\n").unwrap_err();
assert_eq!(err, "unversioned");
let versions = workflow("run: cargo install --version 1.2.3 ripgrep oakum\n");
assert_eq!(versions, vec![Version::parse("1.2.3").unwrap()]);
let versions = workflow("run: cargo install ripgrep oakum --version 1.2.3\n");
assert_eq!(versions, vec![Version::parse("1.2.3").unwrap()]);
let err = versions_in_workflow("run: cargo install oakum --version\n").unwrap_err();
assert_eq!(err, "unversioned");
let err = versions_in_workflow(
"run: cargo binstall --no-confirm oakum@0.0.0 && cargo install oakum --versioned\n",
)
.unwrap_err();
assert_eq!(err, "unversioned");
}
#[test]
fn unversioned_tool_entry_after_a_pin_is_unverified() {
let err = versions_in_workflow(" tool: oakum@0.0.0, oakum\n").unwrap_err();
assert_eq!(err, "unversioned");
}
#[test]
fn bare_oakum_invocation_is_not_a_pin() {
assert!(workflow("run: oakum check\n").is_empty());
}
#[test]
fn not_oakum_at_is_ignored() {
assert!(workflow("run: cargo binstall fake-oakum@1.0.0\n").is_empty());
}
#[test]
fn range_or_latest_is_unverified() {
let err = versions_in_workflow("run: cargo binstall oakum@latest\n").unwrap_err();
assert_eq!(err, "latest");
let err = versions_in_workflow("run: cargo binstall oakum@^1.0.0\n").unwrap_err();
assert_eq!(err, "^1.0.0");
}
fn scratch(label: &str) -> Fixture {
Fixture::new("pin", label)
}
#[test]
fn package_json_later_section_cannot_hide_an_inexact_pin() {
let root = scratch("pkg-conflict");
std::fs::write(
root.join("package.json"),
r#"{"dependencies":{"oakum":"0.0.0"},"devDependencies":{"oakum":"latest"}}"#,
)
.unwrap();
let dir = Dir::open_ambient_dir(&root, cap_std::ambient_authority()).unwrap();
let err = collect_pins(&dir).expect_err("latest in later section");
assert!(err.to_string().contains("latest"), "{err}");
}
#[test]
fn package_json_peer_section_cannot_hide_an_inexact_pin() {
let root = scratch("pkg-peer");
std::fs::write(
root.join("package.json"),
r#"{"dependencies":{"oakum":"0.0.0"},"peerDependencies":{"oakum":"latest"}}"#,
)
.unwrap();
let dir = Dir::open_ambient_dir(&root, cap_std::ambient_authority()).unwrap();
let err = collect_pins(&dir).expect_err("peer latest");
assert!(err.to_string().contains("latest"), "{err}");
}
#[test]
fn package_json_range_is_not_an_exact_pin() {
let root = scratch("pkg-range");
std::fs::write(
root.join("package.json"),
r#"{"devDependencies":{"oakum":"^0.1.0"}}"#,
)
.unwrap();
let dir = Dir::open_ambient_dir(&root, cap_std::ambient_authority()).unwrap();
let err = collect_pins(&dir).expect_err("range");
assert!(err.to_string().contains("not an exact version"), "{err}");
assert!(err.to_string().contains("^0.1.0"), "{err}");
}
#[test]
fn inexact_pin_is_unverified_even_when_another_pin_matches() {
let root = scratch("latest-and-exact");
std::fs::create_dir_all(root.join(".github/workflows")).unwrap();
std::fs::write(
root.join(".github/workflows/release.yml"),
"run: cargo binstall --no-confirm oakum@0.0.0\n",
)
.unwrap();
std::fs::write(
root.join(".github/workflows/ci.yml"),
"run: cargo binstall --no-confirm oakum@latest\n",
)
.unwrap();
let dir = Dir::open_ambient_dir(&root, cap_std::ambient_authority()).unwrap();
let err = collect_pins(&dir).expect_err("latest");
assert!(err.to_string().contains("latest"), "{err}");
assert!(err.to_string().contains("not an exact version"), "{err}");
}
#[test]
fn invalid_workflow_yaml_is_unverified() {
let root = scratch("workflow-invalid-yaml");
std::fs::create_dir_all(root.join(".github/workflows")).unwrap();
std::fs::write(root.join(".github/workflows/ci.yml"), "on: [\n").unwrap();
std::fs::write(
root.join("package.json"),
r#"{"devDependencies":{"oakum":"0.0.0"}}"#,
)
.unwrap();
let dir = Dir::open_ambient_dir(&root, cap_std::ambient_authority()).unwrap();
let err = collect_pins(&dir).expect_err("invalid yaml");
assert!(err.to_string().contains("not valid YAML"), "{err}");
assert!(err.to_string().contains("ci.yml"), "{err}");
}
#[test]
fn unversioned_workflow_message_uses_forward_slashes() {
let root = scratch("unversioned-slash");
std::fs::create_dir_all(root.join(".github/workflows")).unwrap();
std::fs::write(
root.join(".github/workflows/ci.yml"),
"- uses: oakoss/install-action@v1\n with:\n tool: oakum\n",
)
.unwrap();
let dir = Dir::open_ambient_dir(&root, cap_std::ambient_authority()).unwrap();
let err = collect_pins(&dir).expect_err("unversioned");
let text = err.to_string();
assert!(
text.contains(".github/workflows/ci.yml"),
"unversioned pin must name the workflow with /: {text}"
);
assert!(
!text.contains(".github/workflows\\ci.yml"),
"unversioned pin must not use a backslash: {text}"
);
}
#[test]
fn invalid_package_json_is_unverified() {
let root = scratch("pkg-invalid");
std::fs::write(root.join("package.json"), "{").unwrap();
std::fs::create_dir_all(root.join(".github/workflows")).unwrap();
std::fs::write(
root.join(".github/workflows/release.yml"),
"run: cargo binstall --no-confirm oakum@0.0.0\n",
)
.unwrap();
let dir = Dir::open_ambient_dir(&root, cap_std::ambient_authority()).unwrap();
let err = collect_pins(&dir).expect_err("invalid json");
assert!(err.to_string().contains("not valid JSON"), "{err}");
}
#[test]
fn package_json_exact_pin_is_collected() {
let root = scratch("pkg-exact");
std::fs::write(
root.join("package.json"),
r#"{"devDependencies":{"oakum":"0.4.2"}}"#,
)
.unwrap();
let dir = Dir::open_ambient_dir(&root, cap_std::ambient_authority()).unwrap();
let pins = collect_pins(&dir).unwrap();
assert_eq!(pins.len(), 1);
assert_eq!(pins[0].source, PathBuf::from("package.json"));
assert_eq!(pins[0].version, Version::parse("0.4.2").unwrap());
}
#[test]
fn package_json_conflicting_exact_versions_are_unverified() {
let root = scratch("pkg-both-exact");
std::fs::write(
root.join("package.json"),
r#"{"dependencies":{"oakum":"0.0.0"},"devDependencies":{"oakum":"1.0.0"}}"#,
)
.unwrap();
let dir = Dir::open_ambient_dir(&root, cap_std::ambient_authority()).unwrap();
let err = collect_pins(&dir).expect_err("conflicting exacts");
assert!(
err.to_string()
.contains("pins `oakum` as `0.0.0` and `oakum` as `1.0.0`"),
"{err}"
);
}
#[test]
fn package_json_non_string_pin_is_unverified() {
let root = scratch("pkg-object");
std::fs::write(
root.join("package.json"),
r#"{"devDependencies":{"oakum":{"version":"0.0.0"}}}"#,
)
.unwrap();
let dir = Dir::open_ambient_dir(&root, cap_std::ambient_authority()).unwrap();
let err = collect_pins(&dir).expect_err("non-string");
assert!(err.to_string().contains("non-string"), "{err}");
}
#[test]
fn package_json_optional_section_inexact_pin_is_unverified() {
let root = scratch("pkg-optional");
std::fs::write(
root.join("package.json"),
r#"{"optionalDependencies":{"oakum":"latest"}}"#,
)
.unwrap();
let dir = Dir::open_ambient_dir(&root, cap_std::ambient_authority()).unwrap();
let err = collect_pins(&dir).expect_err("optional latest");
assert!(err.to_string().contains("latest"), "{err}");
}
#[test]
fn mise_exact_pin_is_collected() {
let root = scratch("mise-exact");
std::fs::write(root.join(".mise.toml"), "[tools]\noakum = \"0.4.2\"\n").unwrap();
let dir = Dir::open_ambient_dir(&root, cap_std::ambient_authority()).unwrap();
let pins = collect_pins(&dir).unwrap();
assert_eq!(pins.len(), 1);
assert_eq!(pins[0].source, PathBuf::from(".mise.toml"));
assert_eq!(pins[0].version, Version::parse("0.4.2").unwrap());
}
#[test]
fn mise_cargo_backend_and_table_form_are_pins() {
let root = scratch("mise-cargo");
std::fs::write(
root.join("mise.toml"),
"[tools]\n\"cargo:oakum\" = { version = \"0.1.0\" }\n",
)
.unwrap();
let dir = Dir::open_ambient_dir(&root, cap_std::ambient_authority()).unwrap();
let pins = collect_pins(&dir).unwrap();
assert_eq!(pins[0].source, PathBuf::from("mise.toml"));
assert_eq!(pins[0].version, Version::parse("0.1.0").unwrap());
}
#[test]
fn mise_latest_is_unverified() {
let root = scratch("mise-latest");
std::fs::write(root.join(".mise.toml"), "[tools]\noakum = \"latest\"\n").unwrap();
let dir = Dir::open_ambient_dir(&root, cap_std::ambient_authority()).unwrap();
let err = collect_pins(&dir).expect_err("latest");
assert!(err.to_string().contains("latest"), "{err}");
}
#[test]
fn mise_conflicting_files_are_unverified() {
let root = scratch("mise-conflict");
std::fs::write(root.join(".mise.toml"), "[tools]\noakum = \"0.1.0\"\n").unwrap();
std::fs::write(root.join("mise.toml"), "[tools]\noakum = \"0.2.0\"\n").unwrap();
let dir = Dir::open_ambient_dir(&root, cap_std::ambient_authority()).unwrap();
let err = collect_pins(&dir).expect_err("conflict");
assert!(err.to_string().contains("0.1.0"), "{err}");
assert!(err.to_string().contains("0.2.0"), "{err}");
}
#[test]
fn mise_tools_that_is_not_a_table_is_unverified() {
let root = scratch("mise-tools-string");
std::fs::create_dir_all(root.join(".github/workflows")).unwrap();
std::fs::write(
root.join(".github/workflows/release.yml"),
"run: cargo binstall --no-confirm oakum@0.0.0\n",
)
.unwrap();
std::fs::write(root.join(".mise.toml"), "tools = \"oakum@latest\"\n").unwrap();
let dir = Dir::open_ambient_dir(&root, cap_std::ambient_authority()).unwrap();
let err = collect_pins(&dir).expect_err("tools not a table");
assert!(err.to_string().contains("not a table"), "{err}");
}
#[test]
fn workspace_member_named_oakum_is_a_pin() {
let root = scratch("ws-oakum");
std::fs::write(
root.join("Cargo.toml"),
"[workspace]\nmembers = [\"crates/oakum\"]\n",
)
.unwrap();
std::fs::create_dir_all(root.join("crates/oakum")).unwrap();
std::fs::write(
root.join("crates/oakum/Cargo.toml"),
"[package]\nname = \"oakum\"\nversion = \"0.4.2\"\nedition = \"2021\"\n",
)
.unwrap();
let dir = Dir::open_ambient_dir(&root, cap_std::ambient_authority()).unwrap();
let pins = collect_pins(&dir).unwrap();
assert_eq!(pins.len(), 1);
assert_eq!(pins[0].source, PathBuf::from("crates/oakum/Cargo.toml"));
assert_eq!(pins[0].version, Version::parse("0.4.2").unwrap());
}
#[test]
fn workspace_inherited_oakum_version_is_a_pin() {
let root = scratch("ws-inherit");
std::fs::write(
root.join("Cargo.toml"),
"[workspace]\nmembers = [\"crates/oakum\"]\n\n\
[workspace.package]\nversion = \"1.2.3\"\n",
)
.unwrap();
std::fs::create_dir_all(root.join("crates/oakum")).unwrap();
std::fs::write(
root.join("crates/oakum/Cargo.toml"),
"[package]\nname = \"oakum\"\nversion.workspace = true\nedition = \"2021\"\n",
)
.unwrap();
let dir = Dir::open_ambient_dir(&root, cap_std::ambient_authority()).unwrap();
let pins = collect_pins(&dir).unwrap();
assert_eq!(pins[0].version, Version::parse("1.2.3").unwrap());
}
#[test]
fn workspace_without_oakum_package_yields_no_pin() {
let root = scratch("ws-demo");
std::fs::write(
root.join("Cargo.toml"),
"[workspace]\nmembers = [\"crates/demo\"]\n",
)
.unwrap();
std::fs::create_dir_all(root.join("crates/demo")).unwrap();
std::fs::write(
root.join("crates/demo/Cargo.toml"),
"[package]\nname = \"demo\"\nversion = \"0.1.0\"\nedition = \"2021\"\n",
)
.unwrap();
let dir = Dir::open_ambient_dir(&root, cap_std::ambient_authority()).unwrap();
assert!(collect_pins(&dir).unwrap().is_empty());
}
#[test]
fn root_package_named_oakum_is_a_pin() {
let root = scratch("root-oakum");
std::fs::write(
root.join("Cargo.toml"),
"[package]\nname = \"oakum\"\nversion = \"0.9.0\"\nedition = \"2021\"\n\n[workspace]\n",
)
.unwrap();
let dir = Dir::open_ambient_dir(&root, cap_std::ambient_authority()).unwrap();
let pins = collect_pins(&dir).unwrap();
assert_eq!(pins[0].source, PathBuf::from("Cargo.toml"));
assert_eq!(pins[0].version, Version::parse("0.9.0").unwrap());
}
#[test]
fn workspace_path_table_member_is_a_pin() {
let root = scratch("ws-path-table");
std::fs::write(
root.join("Cargo.toml"),
"[workspace]\nmembers = [{ path = \"crates/oakum\" }]\n",
)
.unwrap();
std::fs::create_dir_all(root.join("crates/oakum")).unwrap();
std::fs::write(
root.join("crates/oakum/Cargo.toml"),
"[package]\nname = \"oakum\"\nversion = \"0.3.1\"\nedition = \"2021\"\n",
)
.unwrap();
let dir = Dir::open_ambient_dir(&root, cap_std::ambient_authority()).unwrap();
let pins = collect_pins(&dir).unwrap();
assert_eq!(pins[0].version, Version::parse("0.3.1").unwrap());
assert_eq!(pins[0].source, PathBuf::from("crates/oakum/Cargo.toml"));
}
#[test]
fn workspace_glob_member_finds_oakum() {
let root = scratch("ws-glob");
std::fs::write(
root.join("Cargo.toml"),
"[workspace]\nmembers = [\"crates/*\"]\n",
)
.unwrap();
std::fs::create_dir_all(root.join("crates/oakum")).unwrap();
std::fs::create_dir_all(root.join("crates/other")).unwrap();
std::fs::write(
root.join("crates/oakum/Cargo.toml"),
"[package]\nname = \"oakum\"\nversion = \"0.4.2\"\nedition = \"2021\"\n",
)
.unwrap();
std::fs::write(
root.join("crates/other/Cargo.toml"),
"[package]\nname = \"other\"\nversion = \"0.1.0\"\nedition = \"2021\"\n",
)
.unwrap();
let dir = Dir::open_ambient_dir(&root, cap_std::ambient_authority()).unwrap();
let pins = collect_pins(&dir).unwrap();
assert_eq!(pins.len(), 1);
assert_eq!(pins[0].version, Version::parse("0.4.2").unwrap());
}
#[test]
fn missing_workspace_member_does_not_mask_workflow_pin() {
let root = scratch("ws-missing-mask");
std::fs::write(
root.join("Cargo.toml"),
"[workspace]\nmembers = [\"crates/gone\", \"crates/*\"]\n",
)
.unwrap();
std::fs::create_dir_all(root.join(".github/workflows")).unwrap();
std::fs::write(
root.join(".github/workflows/release.yml"),
"run: cargo binstall --no-confirm oakum@0.1.0\n",
)
.unwrap();
let dir = Dir::open_ambient_dir(&root, cap_std::ambient_authority()).unwrap();
let pins = collect_pins(&dir).unwrap();
assert_eq!(pins.len(), 1);
assert_eq!(pins[0].version, Version::parse("0.1.0").unwrap());
}
#[test]
fn workspace_inherited_version_without_workspace_package_is_unverified() {
let root = scratch("ws-inherit-missing");
std::fs::write(
root.join("Cargo.toml"),
"[workspace]\nmembers = [\"crates/oakum\"]\n",
)
.unwrap();
std::fs::create_dir_all(root.join("crates/oakum")).unwrap();
std::fs::write(
root.join("crates/oakum/Cargo.toml"),
"[package]\nname = \"oakum\"\nversion.workspace = true\nedition = \"2021\"\n",
)
.unwrap();
let dir = Dir::open_ambient_dir(&root, cap_std::ambient_authority()).unwrap();
let err = collect_pins(&dir).expect_err("missing workspace.package.version");
assert!(
err.to_string().contains("[workspace.package].version"),
"{err}"
);
}
#[test]
fn conflicting_workspace_oakum_versions_are_unverified() {
let root = scratch("ws-conflict");
std::fs::write(
root.join("Cargo.toml"),
"[workspace]\nmembers = [\"crates/a\", \"crates/b\"]\n",
)
.unwrap();
for (name, version) in [("a", "0.1.0"), ("b", "0.2.0")] {
let crate_dir = root.join("crates").join(name);
std::fs::create_dir_all(&crate_dir).unwrap();
std::fs::write(
crate_dir.join("Cargo.toml"),
format!(
"[package]\nname = \"oakum\"\nversion = \"{version}\"\nedition = \"2021\"\n"
),
)
.unwrap();
}
let dir = Dir::open_ambient_dir(&root, cap_std::ambient_authority()).unwrap();
let err = collect_pins(&dir).expect_err("conflict");
let message = err.to_string();
assert!(message.contains("0.1.0"), "{message}");
assert!(message.contains("0.2.0"), "{message}");
}
#[test]
fn invalid_member_toml_is_unverified() {
let root = scratch("ws-bad-toml");
std::fs::write(
root.join("Cargo.toml"),
"[workspace]\nmembers = [\"crates/oakum\"]\n",
)
.unwrap();
std::fs::create_dir_all(root.join("crates/oakum")).unwrap();
std::fs::write(root.join("crates/oakum/Cargo.toml"), "[[[not toml").unwrap();
let dir = Dir::open_ambient_dir(&root, cap_std::ambient_authority()).unwrap();
let err = collect_pins(&dir).expect_err("bad toml");
assert!(err.to_string().contains("not valid TOML"), "{err}");
}
#[test]
fn excluded_glob_member_is_not_a_workspace_pin() {
let root = scratch("ws-exclude");
std::fs::write(
root.join("Cargo.toml"),
"[workspace]\nmembers = [\"crates/*\"]\nexclude = [\"crates/oakum\"]\n",
)
.unwrap();
std::fs::create_dir_all(root.join("crates/oakum")).unwrap();
std::fs::create_dir_all(root.join("crates/keep")).unwrap();
std::fs::write(
root.join("crates/oakum/Cargo.toml"),
"[package]\nname = \"oakum\"\nversion = \"9.9.9\"\nedition = \"2021\"\n",
)
.unwrap();
std::fs::write(
root.join("crates/keep/Cargo.toml"),
"[package]\nname = \"keep\"\nversion = \"0.1.0\"\nedition = \"2021\"\n",
)
.unwrap();
matching_workflow(&root);
let dir = Dir::open_ambient_dir(&root, cap_std::ambient_authority()).unwrap();
let pins = collect_pins(&dir).unwrap();
assert_eq!(pins.len(), 1);
assert!(
pins.iter()
.all(|pin| pin.source != *"crates/oakum/Cargo.toml"),
"excluded oakum must not appear as a pin: {pins:?}"
);
assert_eq!(
pins[0].source,
PathBuf::from(".github/workflows/release.yml")
);
assert_eq!(pins[0].version, Version::parse("0.0.0").unwrap());
}
#[test]
fn exclude_dot_slash_spelling_matches_glob_member() {
let root = scratch("ws-exclude-dot");
std::fs::write(
root.join("Cargo.toml"),
"[workspace]\nmembers = [\"crates/*\"]\nexclude = [\"./crates/oakum\"]\n",
)
.unwrap();
std::fs::create_dir_all(root.join("crates/oakum")).unwrap();
std::fs::write(
root.join("crates/oakum/Cargo.toml"),
"[package]\nname = \"oakum\"\nversion = \"9.9.9\"\nedition = \"2021\"\n",
)
.unwrap();
matching_workflow(&root);
let dir = Dir::open_ambient_dir(&root, cap_std::ambient_authority()).unwrap();
let pins = collect_pins(&dir).unwrap();
assert!(
pins.iter()
.all(|pin| pin.source != *"crates/oakum/Cargo.toml"),
"dot-slash exclude must match: {pins:?}"
);
assert_eq!(pins.len(), 1);
}
#[test]
#[cfg(unix)]
fn glob_directory_symlink_member_is_a_pin() {
let root = scratch("ws-symlink");
std::fs::write(
root.join("Cargo.toml"),
"[workspace]\nmembers = [\"crates/*\"]\n",
)
.unwrap();
std::fs::create_dir_all(root.join("real/oakum")).unwrap();
std::fs::write(
root.join("real/oakum/Cargo.toml"),
"[package]\nname = \"oakum\"\nversion = \"0.5.0\"\nedition = \"2021\"\n",
)
.unwrap();
std::fs::create_dir_all(root.join("crates")).unwrap();
std::os::unix::fs::symlink("../real/oakum", root.join("crates/oakum")).unwrap();
let dir = Dir::open_ambient_dir(&root, cap_std::ambient_authority()).unwrap();
let pins = collect_pins(&dir).unwrap();
assert_eq!(pins.len(), 1);
assert_eq!(pins[0].source, PathBuf::from("crates/oakum/Cargo.toml"));
assert_eq!(pins[0].version, Version::parse("0.5.0").unwrap());
}
#[test]
fn unsupported_members_glob_is_unverified() {
let root = scratch("ws-bad-glob");
std::fs::write(
root.join("Cargo.toml"),
"[workspace]\nmembers = [\"crates/oaku?\"]\n",
)
.unwrap();
matching_workflow(&root);
let dir = Dir::open_ambient_dir(&root, cap_std::ambient_authority()).unwrap();
let err = collect_pins(&dir).expect_err("unsupported glob");
assert!(err.to_string().contains("unsupported"), "{err}");
assert!(err.to_string().contains("crates/oaku?"), "{err}");
}
#[test]
fn workspace_members_not_an_array_is_unverified() {
let root = scratch("ws-members-string");
std::fs::write(
root.join("Cargo.toml"),
"[workspace]\nmembers = \"crates/oakum\"\n",
)
.unwrap();
matching_workflow(&root);
let dir = Dir::open_ambient_dir(&root, cap_std::ambient_authority()).unwrap();
let err = collect_pins(&dir).expect_err("members not array");
assert!(err.to_string().contains("not an array"), "{err}");
}
fn matching_workflow(root: &Path) {
std::fs::create_dir_all(root.join(".github/workflows")).unwrap();
std::fs::write(
root.join(".github/workflows/release.yml"),
"run: cargo binstall --no-confirm oakum@0.0.0\n",
)
.unwrap();
}
#[test]
fn composite_action_run_step_is_a_pin() {
let root = scratch("composite-pin");
std::fs::create_dir_all(root.join(".github/actions/setup-oakum")).unwrap();
std::fs::write(
root.join(".github/actions/setup-oakum/action.yml"),
"runs:\n using: composite\n steps:\n - run: cargo binstall --no-confirm oakum@0.7.0\n shell: bash\n",
)
.unwrap();
let dir = Dir::open_ambient_dir(&root, cap_std::ambient_authority()).unwrap();
let pins = collect_pins(&dir).unwrap();
assert_eq!(pins.len(), 1);
assert_eq!(
pins[0].source,
PathBuf::from(".github/actions/setup-oakum/action.yml")
);
assert_eq!(pins[0].version, Version::parse("0.7.0").unwrap());
}
#[test]
fn composite_action_yaml_extension_is_a_pin() {
let root = scratch("composite-yaml-ext");
std::fs::create_dir_all(root.join(".github/actions/setup-oakum")).unwrap();
std::fs::write(
root.join(".github/actions/setup-oakum/action.yaml"),
"runs:\n using: composite\n steps:\n - run: cargo binstall --no-confirm oakum@0.8.0\n shell: bash\n",
)
.unwrap();
let dir = Dir::open_ambient_dir(&root, cap_std::ambient_authority()).unwrap();
let pins = collect_pins(&dir).unwrap();
assert_eq!(pins.len(), 1);
assert_eq!(
pins[0].source,
PathBuf::from(".github/actions/setup-oakum/action.yaml")
);
assert_eq!(pins[0].version, Version::parse("0.8.0").unwrap());
}
#[test]
#[cfg(unix)]
fn composite_action_directory_symlink_is_a_pin() {
let root = scratch("composite-symlink");
std::fs::create_dir_all(root.join("real/setup-oakum")).unwrap();
std::fs::write(
root.join("real/setup-oakum/action.yml"),
"runs:\n using: composite\n steps:\n - run: cargo binstall --no-confirm oakum@0.9.0\n shell: bash\n",
)
.unwrap();
std::fs::create_dir_all(root.join(".github/actions")).unwrap();
std::os::unix::fs::symlink(
"../../real/setup-oakum",
root.join(".github/actions/setup-oakum"),
)
.unwrap();
let dir = Dir::open_ambient_dir(&root, cap_std::ambient_authority()).unwrap();
let pins = collect_pins(&dir).unwrap();
assert_eq!(pins.len(), 1);
assert_eq!(
pins[0].source,
PathBuf::from(".github/actions/setup-oakum/action.yml")
);
assert_eq!(pins[0].version, Version::parse("0.9.0").unwrap());
}
#[test]
fn invalid_composite_action_yaml_is_unverified() {
let root = scratch("composite-bad-yaml");
matching_workflow(&root);
std::fs::create_dir_all(root.join(".github/actions/broken")).unwrap();
std::fs::write(root.join(".github/actions/broken/action.yml"), "runs: [\n").unwrap();
let dir = Dir::open_ambient_dir(&root, cap_std::ambient_authority()).unwrap();
let err = collect_pins(&dir).expect_err("invalid composite yaml");
assert!(err.to_string().contains("not valid YAML"), "{err}");
assert!(
err.to_string()
.contains(".github/actions/broken/action.yml"),
"{err}"
);
}
#[test]
fn yaml_named_directory_is_unverified() {
let root = scratch("yaml-dir");
matching_workflow(&root);
std::fs::create_dir_all(root.join(".github/workflows/ci.yml")).unwrap();
let dir = Dir::open_ambient_dir(&root, cap_std::ambient_authority()).unwrap();
let err = collect_pins(&dir).expect_err("yaml directory");
assert!(err.to_string().contains("not a file"), "{err}");
}
#[test]
fn package_json_non_object_root_is_unverified() {
let root = scratch("pkg-array");
matching_workflow(&root);
std::fs::write(root.join("package.json"), "[]").unwrap();
let dir = Dir::open_ambient_dir(&root, cap_std::ambient_authority()).unwrap();
let err = collect_pins(&dir).expect_err("array root");
assert!(err.to_string().contains("not a JSON object"), "{err}");
}
#[test]
fn package_json_non_object_section_is_unverified() {
let root = scratch("pkg-section-string");
matching_workflow(&root);
std::fs::write(
root.join("package.json"),
r#"{"devDependencies":"oakum@latest"}"#,
)
.unwrap();
let dir = Dir::open_ambient_dir(&root, cap_std::ambient_authority()).unwrap();
let err = collect_pins(&dir).expect_err("section string");
assert!(err.to_string().contains("not an object"), "{err}");
}
#[test]
fn npm_and_pnpm_install_lines_yield_the_version() {
for line in [
"run: pnpm add -D @oakoss/oakum@1.2.3\n",
"run: pnpm add -Dw @oakoss/oakum@1.2.3\n",
"run: npm i -g @oakoss/oakum@1.2.3\n",
"run: npm install --global @oakoss/oakum@1.2.3\n",
"run: npx @oakoss/oakum@1.2.3 check --strict\n",
"run: pnpm dlx @oakoss/oakum@1.2.3 check\n",
"run: pnpm install -g @oakoss/oakum@1.2.3\n",
"run: sudo npm i -g @oakoss/oakum@1.2.3\n",
"run: CI=1 npm i -g @oakoss/oakum@1.2.3 --save-exact\n",
"run: (npm i -g \"@oakoss/oakum@1.2.3\")\n",
"run: npx -y @oakoss/oakum@1.2.3 check\n",
"run: npx --package=@oakoss/oakum@1.2.3 oakum check\n",
"run: npx -p @oakoss/oakum@1.2.3 oakum check\n",
"run: pnpm add -D @oakoss/oakum@1.2.3 @other/oakum@9.9.9\n",
"run: pnpm -w add -D @oakoss/oakum@1.2.3\n",
"run: pnpm --filter demo add -D @oakoss/oakum@1.2.3\n",
"run: npm --prefix . i -g @oakoss/oakum@1.2.3\n",
"run: npm -g i @oakoss/oakum@1.2.3\n",
"run: sudo -E npm i -g @oakoss/oakum@1.2.3\n",
] {
assert_eq!(
versions_in_workflow(line).expect(line),
vec![Version::parse("1.2.3").unwrap()],
"{line}"
);
}
}
#[test]
fn unversioned_npm_install_is_unverified() {
for line in [
"run: pnpm add -D @oakoss/oakum\n",
"run: npm i -g @oakoss/oakum\n",
"run: npx @oakoss/oakum check\n",
"run: pnpm add -D @oakoss/oakum@1.2.3 && npm i -g @oakoss/oakum\n",
"run: pnpm -w add -D @oakoss/oakum\n",
"run: sudo -E npm i -g @oakoss/oakum\n",
] {
assert_eq!(
versions_in_workflow(line).unwrap_err(),
"unversioned",
"{line}"
);
}
}
#[test]
fn npm_dist_tag_is_not_an_exact_pin() {
let err = versions_in_workflow("run: pnpm add -D @oakoss/oakum@latest\n").unwrap_err();
assert_eq!(err, "latest");
}
#[test]
fn npm_invocations_and_other_packages_are_not_pins() {
for line in [
"run: pnpm exec oakum check --strict\n",
"run: pnpm install\n",
"run: pnpm add -D @oakoss/other@1.2.3\n",
"run: pnpm add -D @oakoss/oakum-extra@1.2.3\n",
"run: npm run oakum\n",
"run: npx some-tool @oakoss/oakum@1.2.3\n",
"run: pnpm dlx some-tool @oakoss/oakum\n",
] {
assert_eq!(
versions_in_workflow(line).expect(line),
Vec::new(),
"{line}"
);
}
}
#[test]
fn package_json_scoped_pin_is_collected() {
let root = scratch("npm-scoped");
std::fs::write(
root.join("package.json"),
r#"{"devDependencies":{"@oakoss/oakum":"0.4.2"}}"#,
)
.unwrap();
let dir = Dir::open_ambient_dir(&root, cap_std::ambient_authority()).unwrap();
let pins = collect_pins(&dir).unwrap();
assert_eq!(pins.len(), 1);
assert_eq!(pins[0].version, Version::parse("0.4.2").unwrap());
}
#[test]
fn package_json_npm_alias_spec_is_not_an_exact_pin() {
let root = scratch("npm-alias");
std::fs::write(
root.join("package.json"),
r#"{"devDependencies":{"oakum":"npm:@oakoss/oakum@0.4.2"}}"#,
)
.unwrap();
let dir = Dir::open_ambient_dir(&root, cap_std::ambient_authority()).unwrap();
let err = collect_pins(&dir).unwrap_err();
assert!(
err.to_string().contains(
"pins `oakum` as `npm:@oakoss/oakum@0.4.2`, which is not an exact version"
),
"{err}"
);
}
#[test]
fn package_json_scoped_and_bare_names_must_agree() {
let root = scratch("npm-both");
std::fs::write(
root.join("package.json"),
r#"{"devDependencies":{"@oakoss/oakum":"0.4.2","oakum":"0.4.3"}}"#,
)
.unwrap();
let dir = Dir::open_ambient_dir(&root, cap_std::ambient_authority()).unwrap();
let err = collect_pins(&dir).unwrap_err();
assert!(
err.to_string()
.contains("pins `@oakoss/oakum` as `0.4.2` and `oakum` as `0.4.3`"),
"{err}"
);
}
#[test]
fn mise_npm_backend_is_a_pin() {
let root = scratch("mise-npm");
std::fs::write(
root.join(".mise.toml"),
"[tools]\n\"npm:@oakoss/oakum\" = \"0.4.2\"\n",
)
.unwrap();
let dir = Dir::open_ambient_dir(&root, cap_std::ambient_authority()).unwrap();
let pins = collect_pins(&dir).unwrap();
assert_eq!(pins.len(), 1);
assert_eq!(pins[0].version, Version::parse("0.4.2").unwrap());
}
}