diff --git a/README.md b/README.md index 42c49b0..0b27037 100644 --- a/README.md +++ b/README.md @@ -31,6 +31,9 @@ comline generation # write generated code for each configured target |---|---| | `comline new [--git]` | Scaffold `/` with `config.idp`, `comline.toml`, `src/main.ids` and a `.gitignore`. `--git` also runs `git init`. | | `comline deps add ` / `comline deps add --git --commit --version ` | Add a dependency to `config.idp`: resolved first (a git pin is fetched), then written with its content `hash` pinned. `--no-hash` skips the pin. | +| `comline deps pin [--commit --version ]` | Re-pin a dependency's `hash` to the content it has now (after it changed on purpose), resolving it first. With `--commit`/`--version`, moves a git dependency to another commit at the same time. | +| `comline deps list [--json]` | List the declared dependencies: source, pin and what's accepted. Reads only `config.idp`; nothing is fetched or written. | +| `comline deps remove [--dry-run]` | Remove a dependency from `config.idp`; refused, listing each use, while a schema still imports it. | | `comline check` | Parse, resolve and validate every schema. No `.comline/` writes (beyond fetching git dependencies), no version bump — safe for editors, hooks and CI. | | `comline build [--release] [--watch]` | Compile, validate, and freeze a new immutable version into `.comline/`. Prints the changelog and the version bump. | | `comline generation [--target ] [--out ] [--layout ] [--mode ] [--watch]` | Validate (no freeze), then write generated code. Output location/layout come from `comline.toml` `[generate]`; targets from there or `config.idp`. `gen` is short for `generation`. | diff --git a/docs/cli.md b/docs/cli.md index 981fc22..6cb8a72 100644 --- a/docs/cli.md +++ b/docs/cli.md @@ -15,6 +15,9 @@ This is the long-form reference. For a two-minute tour see the - [Commands](#commands) - [`new`](#comline-new) - [`deps add`](#comline-deps-add) + - [`deps pin`](#comline-deps-pin) + - [`deps list`](#comline-deps-list) + - [`deps remove`](#comline-deps-remove) - [`check`](#comline-check) - [`inspect`](#comline-inspect) - [`build`](#comline-build) @@ -178,6 +181,110 @@ Exit `0` once added, `1` if the dependency doesn't resolve or can't be added (the name is already declared, isn't an identifier, or is already one of this package's own namespaces), `2` if the directory is not a Comline project. +### `comline deps pin` + +``` +comline deps pin [--commit --version ] [--accept-unverified] +``` + +Pin a dependency's `hash` to the content it has now. For a dependency that +changed on purpose: `check` and `build` fail on the old pin +(`hash mismatch`) until it's updated. The dependency is resolved first, the way +[`deps add`](#comline-deps-add) resolves it (a git pin is fetched into +`.comline/deps-cache/`, then compiled), so a dependency that doesn't resolve is +never pinned. Only its entry in `config.idp` changes; every other line is left +as written. An entry written without a hash (`deps add --no-hash`) gains one. + +The old and new hash are printed. If they're the same, it says so and writes +nothing. + +- `--commit --version ` — move a git dependency to another + commit in the same step (both together, git dependencies only). +- `--accept-unverified` — if the new content gives up more through + `@unverified` than the entry already accepts, what it gives up is shown and, + on a terminal, you are asked to accept it; without a terminal this flag + accepts it. The same steps as `deps add`. Nothing is written unless it's + accepted. + +```bash +comline deps pin shared_types +comline deps pin net --commit 9b1d3e7 --version 1.3.0 +``` + +``` +$ comline --plain deps pin shared_types +Resolving shared_types +Pinned shared_types in config.idp + was: blake3:4b2c2573d7022c99ff9c66922673441b9b4cc36b79e5e6bdfee2251cf3ad10d1 + now: blake3:b9684859cabadf2f66d72f47811581ec4fdbd2821816008d3e85bf550f3fae7a +``` + +Exit `0` once pinned or already pinned, `1` if the dependency isn't declared, +doesn't resolve or can't be written, `2` if it needs accepting and there's no +terminal and no flag, or the directory is not a Comline project. + +### `comline deps list` + +``` +comline deps list [--json] +``` + +One line per dependency in `config.idp`: its name, where it comes from (a path, +or a git URI at a short commit and its version), what its hash is pinned to +(`unpinned` if there is none), and what `accept_unverified` accepts of it, if +anything. + +``` +$ comline --plain deps list +billing path ../billing blake3:4b2c2573d702 accepts Billing (schema agreement and encoding agreement) +net git https://github.com/acme/net@9b1d3e7 (1.3.0) blake3:d476bafdea0f +scratch path ../scratch unpinned +``` + +It only reads `config.idp`: nothing is resolved or fetched, so it never uses +the network, and nothing is written, `.comline/` included. What it shows is what +the file says, not whether the dependency still matches it (that's `check`). + +`--json` prints the same as `{ "dependencies": [...] }` on stdout, one object +per dependency with the full values: `name`, `source` (`{ "kind": "path", +"path" }` or `{ "kind": "git", "uri", "commit", "version" }`), `hash` (`null` if +unpinned), and `accept_unverified` (each accepted protocol with its guarantees, +as `config.idp` names them). With none declared the text report is empty and +the JSON's list is `[]`. + +Exit `0`, or `2` if the directory is not a Comline project. + +### `comline deps remove` + +``` +comline deps remove [--dry-run] +``` + +Take ``'s entry out of `config.idp`'s `dependencies` block. Only that +entry goes; every other line is left as written. + +If a schema still imports it (`use ::...`), it isn't removed: the error +lists each use by file and line, and nothing is written, `--dry-run` or not. + +- `--dry-run` — show the entry that would be removed without writing anything. + +```bash +comline deps remove shared_types +comline deps remove shared_types --dry-run +``` + +``` +$ comline --plain deps remove shared +error: can't remove `shared`: 1 schema statement still imports it: + src/holder.ids:1 use shared::models::Thing + +remove or change those first; config.idp is unchanged +``` + +A name that isn't declared is an error that lists the ones that are. Exit `0` +once removed, `1` if it's still imported, isn't declared or can't be removed, +`2` if the directory is not a Comline project. + ### `comline check` ``` diff --git a/src/cli.rs b/src/cli.rs index 4c92717..01321d1 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -365,12 +365,16 @@ pass `--force`.")] /// Manage the package's dependencies #[command(long_about = "\ -Declare, resolve and accept the other Comline packages this one depends on, \ -recorded in `config.idp`'s `dependencies` block. +Declare, resolve, pin, accept, list and remove the other Comline packages this \ +one depends on, recorded in `config.idp`'s `dependencies` block. The commands that \ +write change only the affected entry; the rest of `config.idp` is left as written. Examples: comline deps add shared_types ../shared-types - comline deps accept billing")] + comline deps accept billing + comline deps pin shared_types + comline deps list + comline deps remove shared_types")] Deps { #[command(subcommand)] command: DepsCommand, @@ -574,6 +578,83 @@ Examples: #[arg(long, short)] yes: bool, }, + + /// Re-pin a dependency's `hash` to the content it has now + #[command(long_about = "\ +Resolve the dependency the way `add` does (a git pin is fetched into \ +`.comline/deps-cache/`, then compiled) and write its `hash` as the content just \ +compiled. Use it when a dependency's content changed on purpose: the build \ +fails on the old pin until it's updated. A dependency that doesn't resolve is \ +never pinned, and an entry written without a hash (`add --no-hash`) gains one. \ +The old and new hash are printed; if they're the same, nothing is written. + +`--commit` and `--version` (both, git dependencies only) move the dependency \ +to another commit in the same step. + +If the new content gives up more through `@unverified` than the entry already \ +accepts, what it gives up is shown and, on a terminal, you are asked to accept \ +it (see `comline deps accept`); otherwise pass `--accept-unverified`. Nothing \ +is written unless it's accepted. + +Examples: + comline deps pin shared_types + comline deps pin net --commit 9b1d3e7 --version 1.3.0")] + Pin { + /// The dependency to pin + name: String, + + /// Move a git dependency to this commit (with `--version`) + #[arg(long, value_name = "SHA", requires = "version")] + commit: Option, + + /// Version that commit stands for (with `--commit`) + #[arg(long, value_name = "VERSION", requires = "commit")] + version: Option, + + /// Accept what its `@unverified` protocols give up without being asked + #[arg(long)] + accept_unverified: bool, + }, + + /// List the declared dependencies + #[command(long_about = "\ +One line per dependency in `config.idp`: its name, where it comes from (a path, \ +or a git URI at a commit and its version), what its hash is pinned to \ +(`unpinned` if it isn't) and what `accept_unverified` accepts of it. + +It only reads `config.idp`: nothing is resolved or fetched, and nothing is \ +written, `.comline/` included. `--json` prints the same as JSON, with every \ +commit and hash in full. + +Examples: + comline deps list + comline deps list --json")] + List { + /// Print the dependencies as JSON + #[arg(long)] + json: bool, + }, + + /// Remove a dependency from `config.idp` + #[command(long_about = "\ +Take NAME's entry out of `config.idp`'s `dependencies` block. The rest of \ +`config.idp` is left as written. + +A dependency a schema still imports (`use NAME::...`) isn't removed: the error \ +lists each use, by file and line, and nothing is written. Remove or change \ +those first. + +Examples: + comline deps remove shared_types + comline deps remove shared_types --dry-run")] + Remove { + /// The dependency to remove + name: String, + + /// Show what would be removed without writing anything + #[arg(long)] + dry_run: bool, + }, } /// `comline generation `. diff --git a/src/commands/accept.rs b/src/commands/accept.rs index 0265186..747b318 100644 --- a/src/commands/accept.rs +++ b/src/commands/accept.rs @@ -217,40 +217,71 @@ pub fn with_acceptance( )); } + Ok(with_line(manifest, entry_close, &line)) +} + +/// `manifest` with `line` added as the last line of the entry whose `}` is at +/// `entry_close`, in the indentation of the fields above it; an entry written +/// on one line stays on one line. +pub(crate) fn with_line(manifest: &str, entry_close: usize, line: &str) -> String { let line_start = manifest[..entry_close].rfind('\n').map_or(0, |i| i + 1); if manifest[line_start..entry_close].trim().is_empty() { // `}` on its own line: a new line before it, indented like the fields. let close_indent = indent_of(manifest, entry_close); - let field_indent = format!("{close_indent}{}", unit(close_indent)); - Ok(format!( + // The line above, if it's a field further in than the `}`, sets it. + let above = manifest[..line_start].trim_end_matches('\n'); + let above = &above[above.rfind('\n').map_or(0, |i| i + 1)..]; + let above_indent = &above[..above.len() - above.trim_start_matches([' ', '\t']).len()]; + let field_indent = match above.trim().is_empty() || above_indent.len() <= close_indent.len() + { + true => format!("{close_indent}{}", unit(close_indent)), + false => above_indent.to_string(), + }; + format!( "{}{field_indent}{line}\n{}", &manifest[..line_start], &manifest[line_start..] - )) + ) } else { // A one-line entry (`a = { path = "../a" }`): keep it on one line. - Ok(format!( + format!( "{} {line} {}", manifest[..entry_close].trim_end(), &manifest[entry_close..] - )) + ) } } /// The `{` and `}` of `name = { ... }` directly inside the block. -fn entry(manifest: &str, open: usize, close: usize, name: &str) -> Option<(usize, usize)> { +pub(crate) fn entry( + manifest: &str, + open: usize, + close: usize, + name: &str, +) -> Option<(usize, usize)> { + entry_at(manifest, open, close, name).map(|(_, open, close)| (open, close)) +} + +/// [`entry`], and where its name starts too. +pub(crate) fn entry_at( + manifest: &str, + open: usize, + close: usize, + name: &str, +) -> Option<(usize, usize, usize)> { let toks: Vec<_> = tokens(manifest) .into_iter() .filter(|&(_, at)| at > open && at < close) .collect(); let mut depth = 0usize; - for (i, &(token, _)) in toks.iter().enumerate() { + for (i, &(token, at)) in toks.iter().enumerate() { match token { Token::Open => depth += 1, Token::Close => depth = depth.saturating_sub(1), Token::Word(word) if depth == 0 && word == name => { if let [(Token::Equals, _), (Token::Open, entry_open), ..] = toks[i + 1..] { - return matching(&toks[i + 2..]).map(|entry_close| (entry_open, entry_close)); + return matching(&toks[i + 2..]) + .map(|entry_close| (at, entry_open, entry_close)); } } _ => {} diff --git a/src/commands/add.rs b/src/commands/add.rs index 2b1811c..405d1f7 100644 --- a/src/commands/add.rs +++ b/src/commands/add.rs @@ -197,7 +197,28 @@ pub(crate) fn declared_dependencies(manifest: &str) -> Result = declared.into_values().collect(); + declared.sort_by(|a, b| a.name.cmp(&b.name)); + Ok(declared) +} + +/// The declared dependency called `name`, or an error that lists the ones +/// that are declared. +pub(crate) fn declared_named<'a>( + declared: &'a [DependencyConfig], + name: &str, +) -> Result<&'a DependencyConfig> { + declared.iter().find(|d| d.name == name).ok_or_else(|| { + let names: Vec<&str> = declared.iter().map(|d| d.name.as_str()).collect(); + miette::miette!( + "`{name}` isn't a dependency in config.idp; {}", + match names.as_slice() { + [] => "it declares none".to_string(), + names => format!("it declares: {}", names.join(", ")), + } + ) + }) } /// The entry's `key = "value"` lines, in the order the docs write them. diff --git a/src/commands/list.rs b/src/commands/list.rs new file mode 100644 index 0000000..ccfb9f7 --- /dev/null +++ b/src/commands/list.rs @@ -0,0 +1,155 @@ +//! `comline deps list` — the dependencies `config.idp` declares, with where +//! each comes from, what it's pinned to, and what's accepted of it. +//! +//! Read-only, and it only reads `config.idp`: nothing is resolved, fetched or +//! written (no `.comline/`), so what it says is what the file says, not what +//! the dependency holds now. `--json` prints the same as JSON. + +use std::collections::BTreeMap; +use std::io::Write as _; +use std::path::Path; + +use comline_core::package::config::dependency::{DependencyConfig, DependencySource}; +use comline_core::schema::ir::frozen::unit::Guarantee; +use comline_core::schema::ir::unverified; +use miette::{IntoDiagnostic, Result, WrapErr}; +use serde_json::json; + +use crate::commands::add::declared_dependencies; +use crate::commands::ensure_project; +use crate::ui; + +/// Characters of a commit shown in the text report. +const SHORT_COMMIT: usize = 7; +/// Characters of a hash shown in the text report, after `blake3:`. +const SHORT_HASH: usize = 12; + +pub fn run(work_dir: &Path, json: bool) -> Result<()> { + ensure_project(work_dir)?; + let manifest = std::fs::read_to_string(work_dir.join("config.idp")) + .into_diagnostic() + .wrap_err("failed to read config.idp")?; + let declared = declared_dependencies(&manifest)?; + + let mut out = anstream::stdout().lock(); + if json { + let report = json!({ "dependencies": declared.iter().map(as_json).collect::>() }); + writeln!( + out, + "{}", + serde_json::to_string_pretty(&report).into_diagnostic()? + ) + .into_diagnostic()?; + return Ok(()); + } + + if declared.is_empty() { + ui::note("No dependencies declared"); + return Ok(()); + } + write!(out, "{}", render(&declared)).into_diagnostic()?; + Ok(()) +} + +/// One line per dependency, in columns: name, source, pin, then what's +/// accepted if anything is. +pub fn render(declared: &[DependencyConfig]) -> String { + let rows: Vec<[String; 4]> = declared + .iter() + .map(|d| { + [ + d.name.clone(), + source_text(&d.source), + d.declared_hash().map_or("unpinned".to_string(), short_hash), + accepted_text(&d.accept_unverified), + ] + }) + .collect(); + let width = |column: usize| { + rows.iter() + .map(|r| r[column].chars().count()) + .max() + .unwrap_or(0) + }; + let (name, source, pin) = (width(0), width(1), width(2)); + + let mut out = String::new(); + for [n, s, p, a] in &rows { + let line = format!("{n: String { + match source { + DependencySource::Path { path, .. } => format!("path {}", slashed(path)), + DependencySource::Git { + uri, + commit, + version, + .. + } => format!( + "git {uri}@{} ({version})", + commit.chars().take(SHORT_COMMIT).collect::() + ), + DependencySource::Registry { uri, version, .. } => format!("registry {uri} ({version})"), + } +} + +fn short_hash(hash: &str) -> String { + match hash.split_once(':') { + Some((algorithm, digest)) => format!( + "{algorithm}:{}", + digest.chars().take(SHORT_HASH).collect::() + ), + None => hash.chars().take(SHORT_HASH).collect(), + } +} + +/// `accepts Billing (schema agreement and encoding agreement)`, or nothing. +fn accepted_text(accepted: &BTreeMap>) -> String { + if accepted.is_empty() { + return String::new(); + } + let protocols: Vec = accepted + .iter() + .map(|(protocol, set)| match set.as_slice() { + [] => format!("{protocol} (nothing)"), + set => format!("{protocol} ({})", unverified::describe(set)), + }) + .collect(); + format!("accepts {}", protocols.join(", ")) +} + +/// The entry as `config.idp` writes it, in full. +fn as_json(dependency: &DependencyConfig) -> serde_json::Value { + let source = match &dependency.source { + DependencySource::Path { path, .. } => json!({ "kind": "path", "path": slashed(path) }), + DependencySource::Git { + uri, + commit, + version, + .. + } => json!({ "kind": "git", "uri": uri, "commit": commit, "version": version }), + DependencySource::Registry { uri, version, .. } => { + json!({ "kind": "registry", "uri": uri, "version": version }) + } + }; + let accepted: BTreeMap<&str, Vec<&str>> = dependency + .accept_unverified + .iter() + .map(|(protocol, set)| (protocol.as_str(), set.iter().map(|g| g.name()).collect())) + .collect(); + json!({ + "name": dependency.name, + "source": source, + "hash": dependency.declared_hash(), + "accept_unverified": accepted, + }) +} + +fn slashed(path: &Path) -> String { + path.to_string_lossy().replace('\\', "/") +} diff --git a/src/commands/mod.rs b/src/commands/mod.rs index 03edb7b..94b489f 100644 --- a/src/commands/mod.rs +++ b/src/commands/mod.rs @@ -13,8 +13,11 @@ pub mod doc; pub mod fix; pub mod generate; pub mod inspect; +pub mod list; pub mod new; +pub mod pin; pub mod release; +pub mod remove; pub mod reset; pub mod targets; diff --git a/src/commands/pin.rs b/src/commands/pin.rs new file mode 100644 index 0000000..68c655d --- /dev/null +++ b/src/commands/pin.rs @@ -0,0 +1,345 @@ +//! `comline deps pin` — pin a dependency's `hash` to the content it has now. +//! +//! For a dependency whose content changed: the build fails on the old pin +//! until it's updated. The dependency is resolved first, the way `comline +//! deps add` resolves it, so a dependency that doesn't resolve is never +//! pinned. Only its entry in `config.idp` changes; every other line is left +//! as written. + +use std::path::Path; + +use comline_core::package::config::dependency::{ + DependencyConfig, DependencySource, DEPS_CACHE_DIR, +}; +use comline_core::package::deps; +use miette::{IntoDiagnostic, Result, WrapErr}; + +use crate::commands::accept::{self, entry, with_acceptance, with_line}; +use crate::commands::add::{declared_dependencies, declared_named, dependencies_block}; +use crate::commands::{core_err, ensure_project}; +use crate::ui; + +/// Another commit of a git dependency, and the version it stands for. +pub struct Move { + pub commit: String, + pub version: String, +} + +pub fn run(work_dir: &Path, name: &str, to: Option, accept_unverified: bool) -> Result<()> { + ensure_project(work_dir)?; + + let manifest_path = work_dir.join("config.idp"); + let manifest = std::fs::read_to_string(&manifest_path) + .into_diagnostic() + .wrap_err("failed to read config.idp")?; + let declared = declared_dependencies(&manifest)?; + let current = declared_named(&declared, name)?; + let old_hash = current.declared_hash().map(str::to_string); + + // What the entry should read once pinned, resolved before anything is written. + let mut dependency = current.clone(); + match (&mut dependency.source, &to) { + ( + DependencySource::Git { + commit, version, .. + }, + Some(to), + ) => { + *commit = to.commit.clone(); + *version = to.version.clone(); + } + (_, Some(_)) => { + return Err(miette::miette!( + "`{name}` isn't a git dependency, so there's no commit to move: --commit and \ + --version are for `--git` dependencies" + )) + } + _ => {} + } + for value in [commit_of(&dependency), version_of(&dependency)] + .into_iter() + .flatten() + { + if value.contains(['"', '\\']) { + return Err(miette::miette!( + "a commit or version can't contain `\"` or `\\`" + )); + } + } + set_hash(&mut dependency, None); + + ui::step(format!("Resolving {name}{}", ui::at_path(work_dir))); + let spinner = ui::spinner(match dependency.source { + DependencySource::Git { .. } => "fetching and compiling", + _ => "compiling", + }); + let cache_dir = work_dir.join(DEPS_CACHE_DIR); + // Resolving without a hash warns that it's trusted, and suggests the very + // pin this command writes: keep that out of a normal run. + let resolve = |dependency: &DependencyConfig| { + accept::quietly(|| deps::resolve(dependency, work_dir, &cache_dir)) + }; + let mut resolved = resolve(&dependency); + ui::finish_spinner(spinner); + + // Content that gives up more than the entry accepts resolves only once + // that's accepted: show what, and ask, as `add` does. + if resolved.is_err() { + let mut wanted = match dependency.package_dir(work_dir) { + Some(dir) if dir.join("config.idp").exists() => { + accept::unverified_in(&dir).unwrap_or_default() + } + _ => vec![], + }; + wanted.retain(|p| dependency.accept_unverified.get(&p.protocol) != Some(&p.set)); + if !wanted.is_empty() { + if !accept::decide(name, &wanted, accept_unverified, "--accept-unverified")? { + ui::note(format!( + "Not accepted: {name} wasn't pinned, config.idp is unchanged" + )); + return Ok(()); + } + for p in wanted { + dependency.accept_unverified.insert(p.protocol, p.set); + } + resolved = resolve(&dependency); + } + } + let resolved = resolved.map_err(|e| core_err("couldn't resolve the dependency", e))?; + let new_hash = format!("blake3:{}", resolved.content_hash.to_hex()); + set_hash(&mut dependency, Some(new_hash.clone())); + + if old_hash.as_deref() == Some(new_hash.as_str()) + && to.is_none() + && dependency.accept_unverified == current.accept_unverified + { + ui::success(format!( + "{name} is already pinned to that content: config.idp is unchanged" + )); + ui::detail(format!("hash = \"{new_hash}\"")); + return Ok(()); + } + + let updated = with_pin(&manifest, current, &dependency) + .and_then(|updated| verify(&updated, &declared, &dependency).map(|()| updated)) + .map_err(|problem| { + miette::miette!( + "couldn't write the pin into config.idp safely ({problem}); set it in `{name}`'s \ + entry by hand:\n hash = \"{new_hash}\"" + ) + })?; + std::fs::write(&manifest_path, updated) + .into_diagnostic() + .wrap_err("failed to write config.idp")?; + + ui::success(format!("Pinned {name} in config.idp")); + ui::detail(format!( + "was: {}", + old_hash.as_deref().unwrap_or("not pinned") + )); + ui::detail(format!("now: {new_hash}")); + if let Some(to) = &to { + ui::detail(format!("moved to {} ({})", to.commit, to.version)); + } + if dependency.accept_unverified != current.accept_unverified { + ui::detail("accept_unverified updated"); + } + Ok(()) +} + +fn commit_of(dependency: &DependencyConfig) -> Option<&str> { + match &dependency.source { + DependencySource::Git { commit, .. } => Some(commit), + _ => None, + } +} + +fn version_of(dependency: &DependencyConfig) -> Option<&str> { + match &dependency.source { + DependencySource::Git { version, .. } => Some(version), + _ => None, + } +} + +fn set_hash(dependency: &mut DependencyConfig, value: Option) { + match &mut dependency.source { + DependencySource::Path { hash, .. } + | DependencySource::Git { hash, .. } + | DependencySource::Registry { hash, .. } => *hash = value, + } +} + +/// `manifest` with `after`'s `hash` (and, if moved, `commit` and `version`; +/// and `accept_unverified`, if it changed) written into `before`'s entry. +fn with_pin( + manifest: &str, + before: &DependencyConfig, + after: &DependencyConfig, +) -> Result { + let mut text = manifest.to_string(); + if let ( + DependencySource::Git { + commit: c0, + version: v0, + .. + }, + DependencySource::Git { + commit: c1, + version: v1, + .. + }, + ) = (&before.source, &after.source) + { + if c0 != c1 { + text = with_field(&text, &after.name, "commit", c1)?; + } + if v0 != v1 { + text = with_field(&text, &after.name, "version", v1)?; + } + } + if let Some(hash) = after.declared_hash() { + text = with_field(&text, &after.name, "hash", hash)?; + } + if after.accept_unverified != before.accept_unverified { + text = with_acceptance(&text, &after.name, &after.accept_unverified)?; + } + Ok(text) +} + +/// `manifest` with `key = "value"` in `name`'s entry: the string replaced +/// where the entry has the key, else a line added last. +fn with_field(manifest: &str, name: &str, key: &str, value: &str) -> Result { + let (block_open, block_close) = + dependencies_block(manifest).ok_or("no `dependencies` block")?; + let (open, close) = entry(manifest, block_open, block_close, name) + .ok_or_else(|| format!("no `{name} = {{ ... }}` entry in it"))?; + + match string_value(manifest, open, close, key)? { + Some((start, end)) => Ok(format!( + "{}\"{value}\"{}", + &manifest[..start], + &manifest[end..] + )), + None => Ok(with_line(manifest, close, &format!("{key} = \"{value}\""))), + } +} + +/// The span, quotes included, of the string in a `key = "..."` directly +/// inside an entry; `None` if it has no such key. +fn string_value( + manifest: &str, + open: usize, + close: usize, + key: &str, +) -> Result, String> { + use crate::commands::add::{tokens, Token}; + + let toks: Vec<_> = tokens(manifest) + .into_iter() + .filter(|&(_, at)| at > open && at < close) + .collect(); + let mut depth = 0usize; + for (i, &(token, _)) in toks.iter().enumerate() { + match token { + Token::Open => depth += 1, + Token::Close => depth = depth.saturating_sub(1), + Token::Word(word) if depth == 0 && word == key => { + // A string's token sits at its closing quote. + if let [(Token::Equals, equals), (Token::Other, end), ..] = toks[i + 1..] { + let rest = &manifest[equals + 1..]; + let start = equals + 1 + rest.len() - rest.trim_start().len(); + if manifest.as_bytes().get(start) == Some(&b'"') + && manifest.as_bytes().get(end) == Some(&b'"') + { + return Ok(Some((start, end + 1))); + } + } + return Err(format!("its `{key}` isn't a plain string")); + } + _ => {} + } + } + Ok(None) +} + +/// Whether `updated` declares what was declared before, with only +/// `dependency`'s entry changed to what it now is. +fn verify( + updated: &str, + before: &[DependencyConfig], + dependency: &DependencyConfig, +) -> Result<(), String> { + let after = declared_dependencies(updated).map_err(|e| e.to_string())?; + if after.len() != before.len() { + return Err("other entries changed".to_string()); + } + for old in before { + let Some(new) = after.iter().find(|d| d.name == old.name) else { + return Err(format!("`{}` isn't read back", old.name)); + }; + let expected = if old.name == dependency.name { + dependency + } else { + old + }; + if new.accept_unverified != expected.accept_unverified + || format!("{:?}", new.source) != format!("{:?}", expected.source) + { + return Err(format!("`{}` reads back differently", old.name)); + } + } + Ok(()) +} + +#[cfg(test)] +mod tests { + use super::*; + + const HEAD: &str = "congregation app\nspecification_version = 1\n"; + + #[test] + fn a_hash_is_replaced_where_it_stands() { + let manifest = format!( + "{HEAD}dependencies = {{\n a = {{\n path = \"../a\"\n hash = \"blake3:old\" // pin\n accept_unverified = {{ A = [\"schema_agreement\"] }}\n }}\n b = {{\n hash = \"blake3:b\"\n path = \"../b\"\n }}\n}}\n" + ); + assert_eq!( + with_field(&manifest, "a", "hash", "blake3:new").unwrap(), + manifest.replace("blake3:old", "blake3:new") + ); + assert_eq!( + with_field(&manifest, "b", "hash", "blake3:new").unwrap(), + manifest.replace("blake3:b", "blake3:new") + ); + } + + #[test] + fn a_missing_hash_is_added_last_in_the_entrys_style() { + let manifest = format!( + "{HEAD}dependencies = {{\n a = {{\n path = \"../a\"\n }}\n b = {{ path = \"../b\" }}\n}}\n" + ); + assert_eq!( + with_field(&manifest, "a", "hash", "blake3:x").unwrap(), + format!( + "{HEAD}dependencies = {{\n a = {{\n path = \"../a\"\n hash = \"blake3:x\"\n }}\n b = {{ path = \"../b\" }}\n}}\n" + ) + ); + assert_eq!( + with_field(&manifest, "b", "hash", "blake3:x").unwrap(), + format!( + "{HEAD}dependencies = {{\n a = {{\n path = \"../a\"\n }}\n b = {{ path = \"../b\" hash = \"blake3:x\" }}\n}}\n" + ) + ); + } + + #[test] + fn a_key_nested_deeper_isnt_the_entrys() { + let manifest = format!( + "{HEAD}dependencies = {{\n a = {{\n path = \"../a\"\n accept_unverified = {{ hash = [\"schema_agreement\"] }}\n }}\n}}\n" + ); + let updated = with_field(&manifest, "a", "hash", "blake3:x").unwrap(); + assert!( + updated.ends_with("] }\n hash = \"blake3:x\"\n }\n}\n"), + "{updated}" + ); + } +} diff --git a/src/commands/remove.rs b/src/commands/remove.rs new file mode 100644 index 0000000..b3728cc --- /dev/null +++ b/src/commands/remove.rs @@ -0,0 +1,405 @@ +//! `comline deps remove` — take a dependency out of `config.idp`. +//! +//! Only the dependency's entry goes: every other line of the file is left as +//! written. A dependency a schema still imports (`use NAME::…`) isn't removed, +//! since that would leave the build broken; the error lists each use. + +use std::path::{Path, PathBuf}; + +use comline_core::package::config::dependency::DependencyConfig; +use comline_core::package::layout::SCHEMAS_DIR; +use comline_core::schema::idl::grammar::{Declaration, UsePath}; +use comline_core::schema::idl::parse::parse_schema; +use miette::{IntoDiagnostic, Result, WrapErr}; + +use crate::commands::accept::entry_at; +use crate::commands::add::{declared_dependencies, declared_named, dependencies_block, indent_of}; +use crate::commands::ensure_project; +use crate::ui; + +/// A schema's `use` of a dependency. +#[derive(Debug, PartialEq)] +pub struct Use { + /// The schema, relative to the package's directory. + pub file: PathBuf, + /// 1-based. + pub line: usize, + /// The statement as written, on its first line. + pub text: String, +} + +pub fn run(work_dir: &Path, name: &str, dry_run: bool) -> Result<()> { + ensure_project(work_dir)?; + + let manifest_path = work_dir.join("config.idp"); + let manifest = std::fs::read_to_string(&manifest_path) + .into_diagnostic() + .wrap_err("failed to read config.idp")?; + let declared = declared_dependencies(&manifest)?; + declared_named(&declared, name)?; + + let uses = uses_of(work_dir, name)?; + if !uses.is_empty() { + let lines: Vec = uses + .iter() + .map(|u| format!(" {}:{} {}", slashed(&u.file), u.line, u.text)) + .collect(); + return Err(miette::miette!( + "can't remove `{name}`: {} still import{} it:\n{}\n\nremove or change those first; \ + config.idp is unchanged", + match uses.len() { + 1 => "1 schema statement".to_string(), + n => format!("{n} schema statements"), + }, + if uses.len() == 1 { "s" } else { "" }, + lines.join("\n") + )); + } + + let (updated, removed) = without_dependency(&manifest, name) + .and_then(|(updated, removed)| { + verify(&updated, &declared, name)?; + Ok((updated, removed)) + }) + .map_err(|problem| { + miette::miette!( + "couldn't remove the entry from config.idp safely ({problem}); remove `{name}`'s \ + entry from the `dependencies` block by hand" + ) + })?; + + if dry_run { + ui::step(format!("Would remove {name} from config.idp")); + for line in removed.trim_end().lines() { + ui::detail(format!("- {}", line.trim_end())); + } + ui::success("Dry run complete — config.idp is unchanged"); + return Ok(()); + } + + std::fs::write(&manifest_path, updated) + .into_diagnostic() + .wrap_err("failed to write config.idp")?; + ui::success(format!("Removed {name} from config.idp")); + Ok(()) +} + +/// Every statement in the package's own schemas that imports from `name` +/// (`use name::…`, with a glob, a list or an alias; the older `import name::…` +/// too), in file order. A schema that doesn't parse can't be told, so it's an +/// error rather than a guess. +pub fn uses_of(work_dir: &Path, name: &str) -> Result> { + let mut files = Vec::new(); + schema_files(&work_dir.join(SCHEMAS_DIR), &mut files)?; + files.sort(); + + let mut uses = Vec::new(); + for file in files { + let source = std::fs::read_to_string(&file) + .into_diagnostic() + .wrap_err_with(|| format!("failed to read {}", file.display()))?; + let shown = file.strip_prefix(work_dir).unwrap_or(&file).to_path_buf(); + let document = parse_schema(&source).map_err(|_| { + miette::miette!( + "can't tell whether {} imports `{name}`: it doesn't parse (`comline check` shows \ + why); fix it first", + slashed(&shown) + ) + })?; + for declaration in &document.0 { + let path = match &declaration.value { + Declaration::Use(u) => match &u.path { + UsePath::Absolute(path) => path.as_str(), + UsePath::Glob(glob) => glob.path.as_str(), + UsePath::Multi(multi) => multi.path.as_str(), + // `self`, `parent` and `package` name this package. + UsePath::Relative(_) | UsePath::RelativeGlob(_) | UsePath::RelativeMulti(_) => { + continue + } + }, + Declaration::Import(import) => import.path.as_str(), + _ => continue, + }; + if path.split("::").next() != Some(name) { + continue; + } + let at = declaration.span.0; + uses.push(Use { + file: shown.clone(), + line: source[..at].matches('\n').count() + 1, + text: source[at..].lines().next().unwrap_or("").trim().to_string(), + }); + } + } + Ok(uses) +} + +/// Every `.ids` file under `dir`, the way the build looks for schemas. +fn schema_files(dir: &Path, found: &mut Vec) -> Result<()> { + let Ok(entries) = std::fs::read_dir(dir) else { + return Ok(()); + }; + for entry in entries { + let path = entry.into_diagnostic()?.path(); + if path.is_dir() { + schema_files(&path, found)?; + } else if path.extension().is_some_and(|e| e == "ids") { + found.push(path); + } + } + Ok(()) +} + +fn slashed(path: &Path) -> String { + path.to_string_lossy().replace('\\', "/") +} + +/// `manifest` without `name`'s entry, and the entry's text as it was. The +/// entry's own lines go (and a trailing `//` comment on its last one), with +/// the blank line that separated it from its neighbour; an entry sharing its +/// lines with others is cut out alone. Every other byte is left as written. +pub fn without_dependency(manifest: &str, name: &str) -> Result<(String, String), String> { + let (block_open, block_close) = + dependencies_block(manifest).ok_or("no `dependencies` block")?; + let (name_at, _, close) = entry_at(manifest, block_open, block_close, name) + .ok_or_else(|| format!("no `{name} = {{ ... }}` entry in it"))?; + + let after = close + 1; + let line_start = manifest[..name_at].rfind('\n').map_or(0, |i| i + 1); + let eol = manifest[after..] + .find('\n') + .map_or(manifest.len(), |i| after + i); + let line_end = (eol + 1).min(manifest.len()); + let own_lines = manifest[line_start..name_at].trim().is_empty() + && (manifest[after..eol].trim().is_empty() + || manifest[after..eol].trim_start().starts_with("//")); + + let (mut start, mut end) = (line_start, line_end); + if own_lines { + let before = &manifest[..line_start]; + let previous = before + .strip_suffix('\n') + .map(|p| &p[p.rfind('\n').map_or(0, |i| i + 1)..]); + let next = manifest[line_end..] + .lines() + .next() + .filter(|_| line_end < manifest.len()); + let previous_blank = previous.is_some_and(|l| l.trim().is_empty()); + let next_blank = next.is_some_and(|l| l.trim().is_empty()); + let next_closes = next.is_some_and(|l| l.trim_start().starts_with('}')); + if previous_blank && (next_blank || next_closes) { + start = before.len() - previous.map_or(0, |l| l.len() + 1); + } else if previous.is_some_and(|l| l.trim_end().ends_with('{')) && next_blank { + end = line_end + next.map_or(0, |l| l.len() + 1); + } + } else { + start = name_at; + end = after; + let is_space = |c: char| c == ' ' || c == '\t'; + let trailing = manifest[end..].len() - manifest[end..].trim_start_matches(is_space).len(); + if trailing > 0 { + end += trailing; + } else { + start -= manifest[..start].len() - manifest[..start].trim_end_matches(is_space).len(); + } + } + + // The entry's text, with its own indentation taken off. + let indent = indent_of(manifest, name_at); + let removed = manifest[name_at..after] + .lines() + .enumerate() + .map(|(i, line)| match i { + 0 => line, + _ => line.strip_prefix(indent).unwrap_or(line), + }) + .collect::>() + .join("\n"); + Ok(( + format!("{}{}", &manifest[..start], &manifest[end..]), + removed, + )) +} + +/// Whether `updated` declares what was declared before, minus `name`. +fn verify(updated: &str, before: &[DependencyConfig], name: &str) -> Result<(), String> { + let after = declared_dependencies(updated).map_err(|e| e.to_string())?; + if after.len() + 1 != before.len() || after.iter().any(|d| d.name == name) { + return Err("it still declares it, or lost another entry".to_string()); + } + for old in before.iter().filter(|d| d.name != name) { + let Some(new) = after.iter().find(|d| d.name == old.name) else { + return Err(format!("`{}` isn't read back", old.name)); + }; + if new.accept_unverified != old.accept_unverified + || format!("{:?}", new.source) != format!("{:?}", old.source) + { + return Err(format!("`{}` reads back differently", old.name)); + } + } + Ok(()) +} + +#[cfg(test)] +mod tests { + use super::*; + + const HEAD: &str = "congregation app\nspecification_version = 1\n"; + + fn entry(name: &str) -> String { + format!(" {name} = {{\n path = \"../{name}\"\n }}\n") + } + + fn without(manifest: &str, name: &str) -> String { + let before = declared_dependencies(manifest).unwrap(); + let (updated, removed) = without_dependency(manifest, name).unwrap(); + assert!(removed.starts_with(&format!("{name} = {{")), "{removed}"); + assert_eq!(verify(&updated, &before, name), Ok(()), "{updated}"); + updated + } + + #[test] + fn an_entrys_lines_go_and_the_rest_stays() { + let manifest = format!( + "{HEAD}\ndependencies = {{\n{}{}{}}}\n\n// after\n", + entry("a"), + entry("b"), + entry("c") + ); + assert_eq!( + without(&manifest, "b"), + format!( + "{HEAD}\ndependencies = {{\n{}{}}}\n\n// after\n", + entry("a"), + entry("c") + ) + ); + assert_eq!( + without(&manifest, "a"), + format!( + "{HEAD}\ndependencies = {{\n{}{}}}\n\n// after\n", + entry("b"), + entry("c") + ) + ); + assert_eq!( + without(&manifest, "c"), + format!( + "{HEAD}\ndependencies = {{\n{}{}}}\n\n// after\n", + entry("a"), + entry("b") + ) + ); + } + + #[test] + fn the_blank_line_that_separated_it_goes_too() { + let manifest = format!( + "{HEAD}dependencies = {{\n{}\n{}\n{}}}\n", + entry("a"), + entry("b"), + entry("c") + ); + assert_eq!( + without(&manifest, "b"), + format!( + "{HEAD}dependencies = {{\n{}\n{}}}\n", + entry("a"), + entry("c") + ) + ); + assert_eq!( + without(&manifest, "a"), + format!( + "{HEAD}dependencies = {{\n{}\n{}}}\n", + entry("b"), + entry("c") + ) + ); + assert_eq!( + without(&manifest, "c"), + format!( + "{HEAD}dependencies = {{\n{}\n{}}}\n", + entry("a"), + entry("b") + ) + ); + } + + #[test] + fn the_last_entry_leaves_an_empty_block() { + let manifest = format!("{HEAD}dependencies = {{\n{}}}\n", entry("a")); + assert_eq!( + without(&manifest, "a"), + format!("{HEAD}dependencies = {{\n}}\n") + ); + } + + #[test] + fn a_trailing_comment_goes_with_its_entry_and_tabs_work() { + let manifest = format!( + "{HEAD}dependencies = {{\n\ta = {{\n\t\tpath = \"../a\"\n\t}} // old\n\tb = {{ path = \"../b\" }}\n}}\n" + ); + assert_eq!( + without(&manifest, "a"), + format!("{HEAD}dependencies = {{\n\tb = {{ path = \"../b\" }}\n}}\n") + ); + } + + #[test] + fn entries_sharing_a_line_are_cut_out_alone() { + let manifest = format!( + "{HEAD}dependencies = {{ a = {{ path = \"../a\" }} b = {{ path = \"../b\" }} }}\n" + ); + assert_eq!( + without(&manifest, "a"), + format!("{HEAD}dependencies = {{ b = {{ path = \"../b\" }} }}\n") + ); + assert_eq!( + without(&manifest, "b"), + format!("{HEAD}dependencies = {{ a = {{ path = \"../a\" }} }}\n") + ); + } + + #[test] + fn a_missing_entry_is_reported() { + let manifest = format!("{HEAD}dependencies = {{\n{}}}\n", entry("a")); + assert_eq!( + without_dependency(&manifest, "b"), + Err("no `b = { ... }` entry in it".to_string()) + ); + assert_eq!( + without_dependency(HEAD, "a"), + Err("no `dependencies` block".to_string()) + ); + } + + #[test] + fn uses_are_found_by_the_parser_not_by_text() { + let temp = tempfile::tempdir().unwrap(); + std::fs::create_dir_all(temp.path().join("src/nested")).unwrap(); + std::fs::write( + temp.path().join("src/a.ids"), + "// use shared::models::Old\nuse shared::models::Thing\nuse shared_types::X\n", + ) + .unwrap(); + std::fs::write( + temp.path().join("src/nested/b.ids"), + "use shared::models::*\nuse shared::models::{A, B} \nuse package::shared::C\n", + ) + .unwrap(); + let found: Vec<(String, usize)> = uses_of(temp.path(), "shared") + .unwrap() + .into_iter() + .map(|u| (slashed(&u.file), u.line)) + .collect(); + assert_eq!( + found, + vec![ + ("src/a.ids".to_string(), 2), + ("src/nested/b.ids".to_string(), 1), + ("src/nested/b.ids".to_string(), 2), + ] + ); + } +} diff --git a/src/main.rs b/src/main.rs index 70c5c5c..ef9e56a 100644 --- a/src/main.rs +++ b/src/main.rs @@ -192,6 +192,22 @@ fn run(cli: Cli) -> Result<()> { commands::add::run(&work_dir, &name, source, !no_hash, accept_unverified) } DepsCommand::Accept { target, yes } => commands::accept::run(&work_dir, &target, yes), + DepsCommand::Pin { + name, + commit, + version, + accept_unverified, + } => { + // clap makes them come together. + let to = commit + .zip(version) + .map(|(commit, version)| commands::pin::Move { commit, version }); + commands::pin::run(&work_dir, &name, to, accept_unverified) + } + DepsCommand::List { json } => commands::list::run(&work_dir, json), + DepsCommand::Remove { name, dry_run } => { + commands::remove::run(&work_dir, &name, dry_run) + } }, Commands::Release { version, diff --git a/tests/cli/add.rs b/tests/cli/add.rs index caa042b..4a1c94c 100644 --- a/tests/cli/add.rs +++ b/tests/cli/add.rs @@ -1,43 +1,13 @@ //! `comline deps add` use std::fs; -use std::path::{Path, PathBuf}; +use std::path::Path; use std::process::Command as Process; use predicates::prelude::*; use crate::util::*; -/// A minimal package at `dir`, declaring `Thing` in `src/models.ids`. -fn write_package(dir: &Path, name: &str) { - fs::create_dir_all(dir.join("src")).unwrap(); - fs::write( - dir.join("config.idp"), - with_bounds(&format!("congregation {name}\nspecification_version = 1\n")), - ) - .unwrap(); - fs::write( - dir.join("src/models.ids"), - "struct Thing {\n id: u64\n}\n", - ) - .unwrap(); -} - -/// The fixture project, with a `shared` package next to it. -fn project_with_sibling(temp: &Path) -> PathBuf { - let project = fixture_project(temp); - write_package(&temp.join("shared"), "shared"); - project -} - -fn uses_thing_from(project: &Path, dependency: &str) { - fs::write( - project.join("src/holder.ids"), - format!("use {dependency}::models::Thing\n\nstruct Holder {{\n thing: Thing\n}}\n"), - ) - .unwrap(); -} - fn check(project: &Path) -> assert_cmd::assert::Assert { comline_cmd().current_dir(project).arg("check").assert() } diff --git a/tests/cli/list.rs b/tests/cli/list.rs new file mode 100644 index 0000000..a0dc529 --- /dev/null +++ b/tests/cli/list.rs @@ -0,0 +1,167 @@ +//! `comline deps list` + +use std::fs; + +use predicates::prelude::*; + +use crate::util::*; + +fn comline(project: &std::path::Path, args: &[&str]) -> assert_cmd::assert::Assert { + comline_cmd().current_dir(project).args(args).assert() +} + +#[test] +fn lists_a_path_dependency_pinned_and_unpinned() { + let temp = tempfile::tempdir().unwrap(); + let project = project_with_sibling(temp.path()); + write_package(&temp.path().join("scratch"), "scratch"); + comline(&project, &["deps", "add", "shared", "../shared"]).success(); + comline( + &project, + &["deps", "add", "scratch", "../scratch", "--no-hash"], + ) + .success(); + let config = fs::read_to_string(project.join("config.idp")).unwrap(); + let hash = config + .split_once("hash = \"blake3:") + .unwrap() + .1 + .split_once('"') + .unwrap() + .0; + + let assert = comline(&project, &["--plain", "deps", "list"]).success(); + let out = String::from_utf8(assert.get_output().stdout.clone()).unwrap(); + // One line each, by name, in columns, with no trailing space. + assert_eq!( + out, + format!( + "scratch path ../scratch unpinned\nshared path ../shared blake3:{}\n", + &hash[..12] + ) + ); +} + +#[test] +fn json_has_every_dependency_in_full() { + let temp = tempfile::tempdir().unwrap(); + let project = project_with_sibling(temp.path()); + write_package(&temp.path().join("scratch"), "scratch"); + comline(&project, &["deps", "add", "shared", "../shared"]).success(); + comline( + &project, + &["deps", "add", "scratch", "../scratch", "--no-hash"], + ) + .success(); + let config = fs::read_to_string(project.join("config.idp")).unwrap(); + let hash = format!( + "blake3:{}", + config + .split_once("hash = \"blake3:") + .unwrap() + .1 + .split_once('"') + .unwrap() + .0 + ); + + let assert = comline(&project, &["deps", "list", "--json"]).success(); + let report: serde_json::Value = serde_json::from_slice(&assert.get_output().stdout).unwrap(); + assert_eq!( + report, + serde_json::json!({ + "dependencies": [ + { + "name": "scratch", + "source": { "kind": "path", "path": "../scratch" }, + "hash": null, + "accept_unverified": {}, + }, + { + "name": "shared", + "source": { "kind": "path", "path": "../shared" }, + "hash": hash, + "accept_unverified": {}, + }, + ] + }) + ); +} + +#[test] +fn shows_what_is_accepted() { + let temp = tempfile::tempdir().unwrap(); + let project = fixture_project(temp.path()); + let billing = temp.path().join("billing"); + fs::create_dir_all(billing.join("src")).unwrap(); + fs::write( + billing.join("config.idp"), + "congregation billing\nspecification_version = 1\n\ + settings = { annotations = { unverified = { allowed = true } } }\n", + ) + .unwrap(); + fs::write( + billing.join("src/api.ids"), + "@handshake = \"none\"\n@unverified = \"the 2009 gateway\"\nprotocol Billing {\n function ping();\n}\n", + ) + .unwrap(); + comline( + &project, + &[ + "deps", + "add", + "billing", + "../billing", + "--accept-unverified", + ], + ) + .success(); + + comline(&project, &["--plain", "deps", "list"]) + .success() + .stdout(predicate::str::contains( + "accepts Billing (schema agreement and encoding agreement)", + )); + let assert = comline(&project, &["deps", "list", "--json"]).success(); + let report: serde_json::Value = serde_json::from_slice(&assert.get_output().stdout).unwrap(); + assert_eq!( + report["dependencies"][0]["accept_unverified"], + serde_json::json!({ "Billing": ["schema_agreement", "encoding_agreement"] }) + ); +} + +#[test] +fn only_reads_config_idp() { + let temp = tempfile::tempdir().unwrap(); + let project = project_with_sibling(temp.path()); + comline(&project, &["deps", "add", "shared", "../shared"]).success(); + // The dependency is gone, and its pin stale: listing says what the file says. + fs::remove_dir_all(temp.path().join("shared")).unwrap(); + let _ = fs::remove_dir_all(project.join(".comline")); + let before = fs::read_to_string(project.join("config.idp")).unwrap(); + + comline(&project, &["--plain", "deps", "list"]) + .success() + .stdout(predicate::str::starts_with( + "shared path ../shared blake3:", + )); + assert_eq!( + fs::read_to_string(project.join("config.idp")).unwrap(), + before + ); + assert!(!project.join(".comline").exists()); +} + +#[test] +fn no_dependencies_is_an_empty_list() { + let temp = tempfile::tempdir().unwrap(); + let project = fixture_project(temp.path()); + + comline(&project, &["--plain", "deps", "list"]) + .success() + .stdout("") + .stderr(predicate::str::contains("No dependencies declared")); + let assert = comline(&project, &["deps", "list", "--json"]).success(); + let report: serde_json::Value = serde_json::from_slice(&assert.get_output().stdout).unwrap(); + assert_eq!(report, serde_json::json!({ "dependencies": [] })); +} diff --git a/tests/cli/main.rs b/tests/cli/main.rs index d84e383..a7f77f9 100644 --- a/tests/cli/main.rs +++ b/tests/cli/main.rs @@ -19,7 +19,10 @@ mod external; mod generate; mod global; mod inspect; +mod list; mod new; +mod pin; mod release; +mod remove; mod reset; mod store_stamp; diff --git a/tests/cli/pin.rs b/tests/cli/pin.rs new file mode 100644 index 0000000..84e050d --- /dev/null +++ b/tests/cli/pin.rs @@ -0,0 +1,329 @@ +//! `comline deps pin` + +use std::fs; +use std::path::Path; + +use predicates::prelude::*; + +use crate::util::*; + +fn comline(project: &Path, args: &[&str]) -> assert_cmd::assert::Assert { + comline_cmd().current_dir(project).args(args).assert() +} + +/// What `shared` holds once its `Thing` has a name. +fn change_shared(temp: &Path) { + fs::write( + temp.join("shared/src/models.ids"), + "struct Thing {\n id: u64\n name: string\n}\n", + ) + .unwrap(); +} + +fn config(project: &Path) -> String { + fs::read_to_string(project.join("config.idp")).unwrap() +} + +/// The `blake3:...` a `hash = "..."` line of `config` holds. +fn pinned(config: &str) -> String { + let rest = config.split_once("hash = \"").expect("a hash").1; + rest.split_once('"').unwrap().0.to_string() +} + +#[test] +fn a_changed_dependency_is_repinned_and_the_build_passes_again() { + let temp = tempfile::tempdir().unwrap(); + let project = project_with_sibling(temp.path()); + comline(&project, &["deps", "add", "shared", "../shared"]).success(); + uses_thing_from(&project, "shared"); + comline(&project, &["check"]).success(); + let before = config(&project); + let old = pinned(&before); + + change_shared(temp.path()); + comline(&project, &["check"]) + .failure() + .stderr(predicate::str::contains("hash mismatch")); + + let assert = comline(&project, &["deps", "pin", "shared"]) + .success() + .stderr(predicate::str::contains("Pinned shared in config.idp")) + .stderr(predicate::str::contains(format!("was: {old}"))); + let after = config(&project); + let new = pinned(&after); + assert_ne!(new, old); + assert!(String::from_utf8_lossy(&assert.get_output().stderr).contains(&format!("now: {new}"))); + // Only the hash changed. + assert_eq!(after, before.replace(&old, &new)); + comline(&project, &["check"]).success(); +} + +#[test] +fn it_is_a_no_op_when_the_content_is_unchanged() { + let temp = tempfile::tempdir().unwrap(); + let project = project_with_sibling(temp.path()); + comline(&project, &["deps", "add", "shared", "../shared"]).success(); + let before = config(&project); + + comline(&project, &["deps", "pin", "shared"]) + .success() + .stderr(predicate::str::contains("already pinned to that content")) + .stderr(predicate::str::contains(pinned(&before))) + .stderr(predicate::str::contains("Pinned shared").not()); + assert_eq!(config(&project), before); +} + +#[test] +fn an_entry_without_a_hash_gains_one() { + let temp = tempfile::tempdir().unwrap(); + let project = project_with_sibling(temp.path()); + comline( + &project, + &["deps", "add", "shared", "../shared", "--no-hash"], + ) + .success(); + assert!(!config(&project).contains("hash =")); + + comline(&project, &["deps", "pin", "shared"]) + .success() + .stderr(predicate::str::contains("was: not pinned")) + .stderr(predicate::str::contains("now: blake3:")); + let after = config(&project); + assert!( + after.contains(" shared = {\n path = \"../shared\"\n hash = \"blake3:"), + "{after}" + ); + + // The pin catches the next change. + change_shared(temp.path()); + uses_thing_from(&project, "shared"); + comline(&project, &["check"]) + .failure() + .stderr(predicate::str::contains("hash mismatch")); +} + +#[test] +fn a_dependency_that_doesnt_resolve_is_never_pinned() { + let temp = tempfile::tempdir().unwrap(); + let project = project_with_sibling(temp.path()); + comline(&project, &["deps", "add", "shared", "../shared"]).success(); + let before = config(&project); + + fs::write(temp.path().join("shared/src/models.ids"), "struct Broken {").unwrap(); + comline(&project, &["deps", "pin", "shared"]) + .failure() + .code(1) + .stderr(predicate::str::contains("couldn't resolve the dependency")); + assert_eq!(config(&project), before); + + fs::remove_dir_all(temp.path().join("shared")).unwrap(); + comline(&project, &["deps", "pin", "shared"]) + .failure() + .stderr(predicate::str::contains("couldn't resolve the dependency")); + assert_eq!(config(&project), before); +} + +#[test] +fn it_refuses_what_it_cant_pin_and_leaves_config_idp_alone() { + let temp = tempfile::tempdir().unwrap(); + let project = project_with_sibling(temp.path()); + comline(&project, &["deps", "add", "shared", "../shared"]).success(); + let before = config(&project); + + for (args, code, message) in [ + ( + vec!["deps", "pin", "nope"], + 1, + "`nope` isn't a dependency in config.idp; it declares: shared", + ), + // A path dependency has no commit to move. + ( + vec![ + "deps", + "pin", + "shared", + "--commit", + "abc", + "--version", + "1.0.0", + ], + 1, + "isn't a git dependency", + ), + // The two come together. + ( + vec!["deps", "pin", "shared", "--commit", "abc"], + 2, + "--version", + ), + ( + vec!["deps", "pin", "shared", "--version", "1.0.0"], + 2, + "--commit", + ), + ] { + comline(&project, &args) + .failure() + .code(code) + .stderr(predicate::str::contains(message)); + assert_eq!(config(&project), before, "{args:?}"); + } +} + +/// A package with `Billing` marked `@unverified`, as `accept.rs`'s. +fn write_unverified_package(dir: &Path, protocols: &[&str]) { + fs::create_dir_all(dir.join("src")).unwrap(); + fs::write( + dir.join("config.idp"), + "congregation billing\nspecification_version = 1\n\ + settings = { annotations = { unverified = { allowed = true } } }\n", + ) + .unwrap(); + let schema: String = protocols + .iter() + .map(|p| { + format!( + "@handshake = \"none\"\n@unverified = \"the 2009 gateway\"\n\ + protocol {p} {{\n function ping();\n}}\n\n" + ) + }) + .collect(); + fs::write(dir.join("src/api.ids"), schema).unwrap(); +} + +#[test] +fn content_that_gives_up_more_needs_it_accepted() { + let temp = tempfile::tempdir().unwrap(); + let project = fixture_project(temp.path()); + let billing = temp.path().join("billing"); + write_unverified_package(&billing, &["Billing"]); + comline( + &project, + &[ + "deps", + "add", + "billing", + "../billing", + "--accept-unverified", + ], + ) + .success(); + + write_unverified_package(&billing, &["Billing", "Ledger"]); + let before = config(&project); + + // No terminal to ask: only what's new is shown, and nothing is written. + comline(&project, &["deps", "pin", "billing"]) + .failure() + .code(2) + .stderr(predicate::str::contains("Ledger gives up")) + .stderr(predicate::str::contains("Billing gives up").not()) + .stderr(predicate::str::contains("--accept-unverified")); + assert_eq!(config(&project), before); + + comline(&project, &["deps", "pin", "billing", "--accept-unverified"]) + .success() + .stderr(predicate::str::contains("Pinned billing in config.idp")) + .stderr(predicate::str::contains("accept_unverified updated")); + let after = config(&project); + assert!( + after.contains( + "accept_unverified = { Billing = [\"schema_agreement\", \"encoding_agreement\"] \ + Ledger = [\"schema_agreement\", \"encoding_agreement\"] }" + ), + "{after}" + ); + comline(&project, &["check"]).success(); +} + +fn git(dir: &Path, args: &[&str]) -> String { + let output = std::process::Command::new("git") + .args([ + "-c", + "user.name=test", + "-c", + "user.email=test@example.test", + "-c", + "commit.gpgsign=false", + ]) + .args(args) + .current_dir(dir) + .output() + .expect("git runs"); + assert!( + output.status.success(), + "git {args:?}: {}", + String::from_utf8_lossy(&output.stderr) + ); + String::from_utf8(output.stdout).unwrap().trim().to_string() +} + +#[test] +fn a_git_dependency_moves_to_another_commit() { + let temp = tempfile::tempdir().unwrap(); + let project = fixture_project(temp.path()); + let repo = temp.path().join("net"); + write_package(&repo, "net"); + git(&repo, &["init", "--quiet"]); + git(&repo, &["add", "."]); + git(&repo, &["commit", "--quiet", "-m", "one"]); + let first = git(&repo, &["rev-parse", "HEAD"]); + fs::write( + repo.join("src/models.ids"), + "struct Thing {\n id: u64\n name: string\n}\n", + ) + .unwrap(); + git(&repo, &["commit", "--quiet", "-am", "two"]); + let second = git(&repo, &["rev-parse", "HEAD"]); + let slashed = repo.display().to_string().replace('\\', "/"); + let uri = if slashed.starts_with('/') { + format!("file://{slashed}") + } else { + format!("file:///{slashed}") + }; + comline( + &project, + &[ + "deps", + "add", + "net", + "--git", + &uri, + "--commit", + &first, + "--version", + "1.0.0", + ], + ) + .success(); + let before = config(&project); + let old = pinned(&before); + + comline( + &project, + &[ + "deps", + "pin", + "net", + "--commit", + &second, + "--version", + "1.1.0", + ], + ) + .success() + .stderr(predicate::str::contains(format!("was: {old}"))) + .stderr(predicate::str::contains(format!( + "moved to {second} (1.1.0)" + ))); + let after = config(&project); + assert!( + after.contains(&format!( + " net = {{\n version = \"1.1.0\"\n uri = \"{uri}\"\n commit = \"{second}\"\n hash = \"blake3:" + )), + "{after}" + ); + assert_ne!(pinned(&after), old); + uses_thing_from(&project, "net"); + comline(&project, &["check"]).success(); +} diff --git a/tests/cli/remove.rs b/tests/cli/remove.rs new file mode 100644 index 0000000..7ef08a7 --- /dev/null +++ b/tests/cli/remove.rs @@ -0,0 +1,158 @@ +//! `comline deps remove` + +use std::fs; + +use predicates::prelude::*; + +use crate::util::*; + +fn comline(project: &std::path::Path, args: &[&str]) -> assert_cmd::assert::Assert { + comline_cmd().current_dir(project).args(args).assert() +} + +#[test] +fn removes_an_unused_dependency() { + let temp = tempfile::tempdir().unwrap(); + let project = project_with_sibling(temp.path()); + comline(&project, &["deps", "add", "shared", "../shared"]).success(); + let with_it = fs::read_to_string(project.join("config.idp")).unwrap(); + assert!(with_it.contains("shared = {"), "{with_it}"); + + comline(&project, &["deps", "remove", "shared"]) + .success() + .stderr(predicate::str::contains("Removed shared from config.idp")); + + // Only its entry went: the block it was in and the rest stay as written. + let after = fs::read_to_string(project.join("config.idp")).unwrap(); + let (before, _) = with_it.split_once("dependencies = {").unwrap(); + assert_eq!(after, format!("{before}dependencies = {{\n}}\n")); + comline(&project, &["check"]).success(); +} + +#[test] +fn only_its_own_entry_goes() { + let temp = tempfile::tempdir().unwrap(); + let project = project_with_sibling(temp.path()); + write_package(&temp.path().join("extra"), "extra"); + comline(&project, &["deps", "add", "shared", "../shared"]).success(); + comline(&project, &["deps", "add", "extra", "../extra"]).success(); + let config = fs::read_to_string(project.join("config.idp")).unwrap(); + let extra_entry = config.split_once(" extra = {").unwrap().1; + + comline(&project, &["deps", "remove", "shared"]).success(); + let after = fs::read_to_string(project.join("config.idp")).unwrap(); + assert!(!after.contains("shared"), "{after}"); + assert!( + after.ends_with(&format!("dependencies = {{\n extra = {{{extra_entry}")), + "{after}" + ); + comline(&project, &["deps", "list"]) + .success() + .stdout(predicate::str::contains("extra")) + .stdout(predicate::str::contains("shared").not()); +} + +#[test] +fn refuses_one_a_schema_still_imports_and_names_the_use() { + let temp = tempfile::tempdir().unwrap(); + let project = project_with_sibling(temp.path()); + comline(&project, &["deps", "add", "shared", "../shared"]).success(); + uses_thing_from(&project, "shared"); + let before = fs::read_to_string(project.join("config.idp")).unwrap(); + + comline(&project, &["deps", "remove", "shared"]) + .failure() + .code(1) + .stderr(predicate::str::contains("can't remove `shared`")) + .stderr(predicate::str::contains( + "src/holder.ids:1 use shared::models::Thing", + )); + // Not even as a dry run. + comline(&project, &["deps", "remove", "shared", "--dry-run"]) + .failure() + .stderr(predicate::str::contains("src/holder.ids:1")); + assert_eq!( + fs::read_to_string(project.join("config.idp")).unwrap(), + before + ); + + // Once nothing imports it, it goes. + fs::remove_file(project.join("src/holder.ids")).unwrap(); + comline(&project, &["deps", "remove", "shared"]).success(); +} + +#[test] +fn every_use_is_listed_with_its_file_and_line() { + let temp = tempfile::tempdir().unwrap(); + let project = project_with_sibling(temp.path()); + comline(&project, &["deps", "add", "shared", "../shared"]).success(); + fs::create_dir_all(project.join("src/nested")).unwrap(); + fs::write( + project.join("src/holder.ids"), + "// use shared::models::Thing\n\nuse shared::models::*\n\nstruct Holder {\n thing: Thing\n}\n", + ) + .unwrap(); + fs::write( + project.join("src/nested/more.ids"), + "use shared::models::{Thing}\n\nstruct More {\n thing: Thing\n}\n", + ) + .unwrap(); + + comline(&project, &["deps", "remove", "shared"]) + .failure() + .stderr(predicate::str::contains( + "2 schema statements still import it", + )) + .stderr(predicate::str::contains( + "src/holder.ids:3 use shared::models::*", + )) + .stderr(predicate::str::contains( + "src/nested/more.ids:1 use shared::models::{Thing}", + )) + .stderr(predicate::str::contains("src/holder.ids:1").not()); +} + +#[test] +fn dry_run_writes_nothing() { + let temp = tempfile::tempdir().unwrap(); + let project = project_with_sibling(temp.path()); + comline(&project, &["deps", "add", "shared", "../shared"]).success(); + let before = fs::read_to_string(project.join("config.idp")).unwrap(); + + comline(&project, &["deps", "remove", "shared", "--dry-run"]) + .success() + .stderr(predicate::str::contains( + "Would remove shared from config.idp", + )) + .stderr(predicate::str::contains("path = \"../shared\"")) + .stderr(predicate::str::contains("Removed").not()); + assert_eq!( + fs::read_to_string(project.join("config.idp")).unwrap(), + before + ); +} + +#[test] +fn an_unknown_name_lists_the_declared_dependencies() { + let temp = tempfile::tempdir().unwrap(); + let project = project_with_sibling(temp.path()); + comline(&project, &["deps", "remove", "shared"]) + .failure() + .code(1) + .stderr(predicate::str::contains( + "`shared` isn't a dependency in config.idp; it declares none", + )); + + comline(&project, &["deps", "add", "shared", "../shared"]).success(); + let before = fs::read_to_string(project.join("config.idp")).unwrap(); + comline(&project, &["deps", "remove", "nope"]) + .failure() + .code(1) + .stderr(predicate::str::contains( + "`nope` isn't a dependency in config.idp; it declares: shared", + )); + assert_eq!( + fs::read_to_string(project.join("config.idp")).unwrap(), + before + ); +} diff --git a/tests/cli/util.rs b/tests/cli/util.rs index ee30dbe..bfa3eca 100644 --- a/tests/cli/util.rs +++ b/tests/cli/util.rs @@ -80,3 +80,33 @@ pub fn with_setting(config: &str, entry: &str) -> String { } } } + +/// A minimal package at `dir`, declaring `Thing` in `src/models.ids`. +pub fn write_package(dir: &Path, name: &str) { + fs::create_dir_all(dir.join("src")).unwrap(); + fs::write( + dir.join("config.idp"), + with_bounds(&format!("congregation {name}\nspecification_version = 1\n")), + ) + .unwrap(); + fs::write( + dir.join("src/models.ids"), + "struct Thing {\n id: u64\n}\n", + ) + .unwrap(); +} + +/// The fixture project, with a `shared` package next to it. +pub fn project_with_sibling(temp: &Path) -> PathBuf { + let project = fixture_project(temp); + write_package(&temp.join("shared"), "shared"); + project +} + +pub fn uses_thing_from(project: &Path, dependency: &str) { + fs::write( + project.join("src/holder.ids"), + format!("use {dependency}::models::Thing\n\nstruct Holder {{\n thing: Thing\n}}\n"), + ) + .unwrap(); +}