diff --git a/Cargo.lock b/Cargo.lock index 0f5c43d..1d681e3 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -398,7 +398,7 @@ dependencies = [ [[package]] name = "comline-codegen" version = "0.1.0" -source = "git+https://github.com/ComlineProject/generation?rev=46ff2b0f98af9c3b6cf92a15f4cf311ef4c19d64#46ff2b0f98af9c3b6cf92a15f4cf311ef4c19d64" +source = "git+https://github.com/ComlineProject/generation?rev=64136010b9e9ae7ccc2f6c1d8400bccb5da42e5e#64136010b9e9ae7ccc2f6c1d8400bccb5da42e5e" dependencies = [ "comline-core", "eyre", @@ -408,7 +408,7 @@ dependencies = [ [[package]] name = "comline-codegen-rust" version = "0.1.0" -source = "git+https://github.com/ComlineProject/comline-rust?rev=7a2c04c871be6ee13ed7e9a64f45b72babda7790#7a2c04c871be6ee13ed7e9a64f45b72babda7790" +source = "git+https://github.com/ComlineProject/comline-rust?rev=edef1c870f99eae43c4444f7b3875a5145ee35a0#edef1c870f99eae43c4444f7b3875a5145ee35a0" dependencies = [ "comline-codegen", "comline-core", @@ -418,7 +418,7 @@ dependencies = [ [[package]] name = "comline-codegen-typescript" version = "0.1.0" -source = "git+https://github.com/ComlineProject/comline-typescript?rev=0309d82a8871fc459143ddde653fc8f395571588#0309d82a8871fc459143ddde653fc8f395571588" +source = "git+https://github.com/ComlineProject/comline-typescript?rev=d30ae2919180262ee4495faa83cd3f61b971b81c#d30ae2919180262ee4495faa83cd3f61b971b81c" dependencies = [ "comline-codegen", "comline-core", @@ -428,7 +428,7 @@ dependencies = [ [[package]] name = "comline-core" version = "0.1.0" -source = "git+https://github.com/ComlineProject/core?rev=bd9d9f4557e56db1dbcece1b6fae54c1fc5394fd#bd9d9f4557e56db1dbcece1b6fae54c1fc5394fd" +source = "git+https://github.com/ComlineProject/core?rev=af0371f84994da906805cc8c9b6693236adb86f5#af0371f84994da906805cc8c9b6693236adb86f5" dependencies = [ "ariadne", "blake3", @@ -455,7 +455,7 @@ dependencies = [ [[package]] name = "comline-core-stdlib" version = "0.1.0" -source = "git+https://github.com/ComlineProject/core?rev=bd9d9f4557e56db1dbcece1b6fae54c1fc5394fd#bd9d9f4557e56db1dbcece1b6fae54c1fc5394fd" +source = "git+https://github.com/ComlineProject/core?rev=af0371f84994da906805cc8c9b6693236adb86f5#af0371f84994da906805cc8c9b6693236adb86f5" [[package]] name = "concolor" diff --git a/Cargo.toml b/Cargo.toml index 9d8ee81..e58556d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -30,15 +30,15 @@ toml = "0.8" # needs real I/O (git fetch, filesystem), so it's off by default for # `comline-core`'s wasm32 consumers (the playground editor, the language # server); the CLI is the one consumer that actually runs `comline build`. -comline-core = { git = "https://github.com/ComlineProject/core", rev = "bd9d9f4557e56db1dbcece1b6fae54c1fc5394fd", features = ["deps"] } +comline-core = { git = "https://github.com/ComlineProject/core", rev = "af0371f84994da906805cc8c9b6693236adb86f5", features = ["deps"] } # All three pinned to revs that agree on one `comline-codegen`, and bumped # together: comline-rust and comline-typescript's codegen crates consume # `comline-codegen`'s types, so the rev below must be byte-identical to the # one *they* pin - Cargo only unifies a git dependency across identical rev # strings, and two copies of `GenRequest` are two incompatible types. -comline-codegen = { git = "https://github.com/ComlineProject/generation", rev = "46ff2b0f98af9c3b6cf92a15f4cf311ef4c19d64" } -comline-codegen-rust = { git = "https://github.com/ComlineProject/comline-rust", rev = "7a2c04c871be6ee13ed7e9a64f45b72babda7790", optional = true } -comline-codegen-typescript = { git = "https://github.com/ComlineProject/comline-typescript", rev = "0309d82a8871fc459143ddde653fc8f395571588", optional = true } +comline-codegen = { git = "https://github.com/ComlineProject/generation", rev = "64136010b9e9ae7ccc2f6c1d8400bccb5da42e5e" } +comline-codegen-rust = { git = "https://github.com/ComlineProject/comline-rust", rev = "edef1c870f99eae43c4444f7b3875a5145ee35a0", optional = true } +comline-codegen-typescript = { git = "https://github.com/ComlineProject/comline-typescript", rev = "d30ae2919180262ee4495faa83cd3f61b971b81c", optional = true } [features] # Which language generators are compiled in. Drop one to shed its whole diff --git a/src/cli.rs b/src/cli.rs index a69d01b..c7f7cb4 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -43,6 +43,14 @@ pub struct Cli { pub command: Commands, } +/// How far `comline release --raise` goes. +#[derive(Debug, Clone, Copy, clap::ValueEnum)] +pub enum ReleaseLevel { + Patch, + Minor, + Major, +} + #[derive(Subcommand, Debug)] pub enum Commands { /// Compile the project and freeze a new immutable version @@ -170,6 +178,46 @@ history.")] dry_run: bool, }, + /// Release the builds since the last release as a version + #[command(long_about = "\ +Builds record history under pre-release versions (`0.3.0-dev.2`: the release \ +they lead to, and a count). A release is the version consumers see. This builds \ +first, so what's released is what's in `src/` now, then creates the version from \ +the highest bump the changes since the last release call for: a breaking change \ +is a major (before 1.0, the minor), a new feature a minor, anything else a patch. \ +The first release is 0.1.0. + +`--raise` releases a higher bump than the changes show (a behaviour change the \ +schema can't express), with a `--reason` recorded with the release. It never \ +lowers the bump. + +`comline release 1.0` makes a 0.x package stable: it says what 1.0 commits to \ +and what the package exposes, then releases 1.0.0. + +Raising to major and releasing 1.0 ask for confirmation on a terminal; \ +otherwise pass `--yes`. + +Examples: + comline release + comline release --raise minor --reason \"`ping` now retries on timeout\" + comline release 1.0")] + Release { + /// `1.0`, to make a 0.x package stable; otherwise omitted + version: Option, + + /// Release a higher bump than the changes call for + #[arg(long, value_enum, value_name = "LEVEL")] + raise: Option, + + /// Why: required with `--raise`, recorded with the release + #[arg(long, value_name = "TEXT")] + reason: Option, + + /// Don't ask for confirmation + #[arg(long, short)] + yes: bool, + }, + /// Discard the version history (delete `.comline/`) #[command(long_about = "\ Delete the project's `.comline/` content-addressable store — every frozen \ diff --git a/src/commands/build.rs b/src/commands/build.rs index 13ecf15..b5d19fe 100644 --- a/src/commands/build.rs +++ b/src/commands/build.rs @@ -38,7 +38,8 @@ fn build_once(work_dir: &Path, release: bool) -> Result<()> { ui::success("Project built"); if result.is_initial_build() { - ui::detail(pkg(format!("initial version {}", result.current_version))); + ui::detail(pkg(format!("initial build {}", result.current_version))); + next_release_line(&result); return Ok(()); } @@ -54,6 +55,7 @@ fn build_once(work_dir: &Path, release: bool) -> Result<()> { "version {} (no changes)", result.current_version ))); + next_release_line(&result); return Ok(()); } @@ -68,10 +70,36 @@ fn build_once(work_dir: &Path, release: bool) -> Result<()> { changes::render(&dependency.changes); } ui::detail(bump_line(result.version_bump)); + next_release_line(&result); Ok(()) } +/// What `comline release` would make now: the release this build leads to. +/// Nothing when the latest commit is itself a release. +fn next_release_line(result: &build::BuildResult) { + // A build's version is the release it leads to plus `-dev.N`. + let Some((next, _)) = result.current_version.split_once('-') else { + return; + }; + let since = match &result.last_release { + Some(last) => format!(" ({} since {last})", bump_word(result.release_bump)), + None => " (the first release)".to_string(), + }; + ui::detail(format!( + "next release: {next}{since} — run `comline release`" + )); +} + +fn bump_word(bump: VersionBump) -> &'static str { + match bump { + VersionBump::Major => "breaking changes", + VersionBump::Minor => "new features", + VersionBump::Patch => "modifications", + VersionBump::None => "nothing that calls for a bump", + } +} + /// A `📦`-prefixed package line, or plain text (with an ASCII arrow) under `--plain`. fn pkg(body: String) -> String { if ui::plain() { diff --git a/src/commands/generate.rs b/src/commands/generate.rs index 4793a70..0ae14cd 100644 --- a/src/commands/generate.rs +++ b/src/commands/generate.rs @@ -350,9 +350,18 @@ fn expand_versions( }]) } VersionSpec::All => { + // Every release: what consumers see. The builds between them + // are named one by one when wanted. let chain = history::load(work_dir)?; let store = ObjectStore::new(work_dir); - chain + let releases: Vec<_> = chain.iter().filter(|e| e.commit.is_release()).collect(); + if releases.is_empty() { + return Err(miette!( + "`package_versions = \"all\"` generates every release, and there's none yet; \ + run `comline release`, or name the builds to generate" + )); + } + releases .iter() .map(|e| gen_version_from_commit(&store, &e.commit.version, &e.commit)) .collect() diff --git a/src/commands/mod.rs b/src/commands/mod.rs index ebb0e0a..f4f4d62 100644 --- a/src/commands/mod.rs +++ b/src/commands/mod.rs @@ -10,6 +10,7 @@ pub mod completions; pub mod diff; pub mod generate; pub mod new; +pub mod release; pub mod reset; pub mod targets; diff --git a/src/commands/release.rs b/src/commands/release.rs new file mode 100644 index 0000000..9de75df --- /dev/null +++ b/src/commands/release.rs @@ -0,0 +1,218 @@ +//! `comline release` — turn the builds since the last release into a +//! version. +//! +//! Builds record history under pre-release versions (`0.3.0-dev.2`); a +//! release is the version consumers see. It builds first, so what's released +//! is what's in `src/` now, then takes the highest bump the changes since the +//! last release call for. `--raise` goes higher, with a reason. `release 1.0` +//! moves a `0.x` package to `1.0.0` after saying what that commits to. + +use std::io::{self, IsTerminal, Write}; +use std::path::Path; + +use comline_core::package::build::{self, ReleaseRequest, VersionBump}; +use comline_core::package::config::ir::context::ProjectContext; +use comline_core::schema::ir::frozen::unit::FrozenUnit; +use comline_core::schema::ir::unverified; +use miette::{IntoDiagnostic, Result}; + +use crate::changes; +use crate::cli::ReleaseLevel as Level; +use crate::commands::{core_err, ensure_project}; +use crate::error::CliError; +use crate::ui; + +fn bump(level: Level) -> VersionBump { + match level { + Level::Patch => VersionBump::Patch, + Level::Minor => VersionBump::Minor, + Level::Major => VersionBump::Major, + } +} + +pub fn run( + work_dir: &Path, + version: Option<&str>, + raise: Option, + reason: Option, + yes: bool, +) -> Result<()> { + ensure_project(work_dir)?; + let stable = match version { + None => false, + Some("1.0" | "1.0.0") => true, + Some(other) => { + return Err(miette::miette!( + "`comline release {other}`: the version comes from the changes; the only one \ + you name is `1.0`, to make the package stable" + )) + } + }; + if raise.is_some() && reason.as_deref().is_none_or(|r| r.trim().is_empty()) { + return Err(miette::miette!( + "--raise needs --reason: it's recorded with the release and shown in its history" + )); + } + + ui::step(format!("Building project{}", ui::at_path(work_dir))); + let spinner = ui::spinner("compiling schemas"); + let built = build::build(work_dir); + ui::finish_spinner(spinner); + let built = built.map_err(|e| core_err("build failed", e))?; + for warning in built.context.rendered_warnings() { + ui::warn(warning); + } + + if stable { + explain_stable(&built.context, built.last_release.as_deref()); + if !confirm("Release 1.0.0", yes, "make the package stable")? { + ui::note("Not released"); + return Ok(()); + } + } else if matches!(raise, Some(Level::Major)) + && !confirm( + "Raise to a major release", + yes, + "raise the release to major", + )? + { + ui::note("Not released"); + return Ok(()); + } + + let request = ReleaseRequest { + raise: raise.map(bump), + reason, + stable, + }; + let released = + build::release(work_dir, &request).map_err(|e| core_err("couldn't release", e))?; + + ui::success(format!("Released {}", released.version)); + match &released.previous_release { + Some(previous) => ui::detail(format!("{previous} -> {}", released.version)), + None => ui::detail("the first release"), + } + if let Some(changes) = &released.changes { + changes::render(&changes.schema_changes); + for dependency in &changes.dependency_changes { + ui::group(format!("In dependency `{}`:", dependency.name)); + changes::render(&dependency.changes); + } + } + let record = &released.record; + if record.stable { + ui::detail("stable: from now on a breaking change is a major release"); + } else if released.previous_release.is_some() { + ui::detail(format!( + "{} bump{}", + describe(record.applied), + match (record.computed == record.applied, record.covered) { + (true, true) => " (the raise was already covered by the changes)".to_string(), + (true, false) => String::new(), + (false, _) => format!(", raised from {}", describe(record.computed)), + } + )); + } + if let Some(reason) = &record.reason { + ui::detail(format!("reason: {reason}")); + } + Ok(()) +} + +fn describe(bump: VersionBump) -> &'static str { + match bump { + VersionBump::Major => "major", + VersionBump::Minor => "minor", + VersionBump::Patch => "patch", + VersionBump::None => "no", + } +} + +/// What 1.0 commits to, and what the package exposes. The things worth +/// settling first are listed for information, not required. +fn explain_stable(context: &ProjectContext, last_release: Option<&str>) { + ui::warn(format!( + "Releasing 1.0.0{}: from then on a breaking change is a major release, and peers and \ + consumers rely on that", + last_release + .map(|v| format!(" (from {v})")) + .unwrap_or_default() + )); + + let mut protocols = Vec::new(); + let mut given_up = Vec::new(); + let mut deprecated = Vec::new(); + for schema in &context.schema_contexts { + let schema = schema.borrow(); + let frozen = schema.frozen_schema.borrow(); + let Some(units) = frozen.as_ref() else { + continue; + }; + for unit in units { + let (name, parameters) = match unit { + FrozenUnit::Protocol { + name, parameters, .. + } => { + protocols.push(format!( + "{name} (handshake `{}`)", + unverified::handshake(parameters) + )); + let set = unverified::frozen_set(parameters); + if !set.is_empty() { + given_up.push(format!("{name} gives up {}", unverified::describe(set))); + } + (name, parameters) + } + FrozenUnit::Struct { + name, parameters, .. + } + | FrozenUnit::Error { + name, parameters, .. + } + | FrozenUnit::TypeAlias { + name, parameters, .. + } => (name, parameters), + _ => continue, + }; + if parameters.iter().any(|p| matches!(p, FrozenUnit::Deprecated(d) if !matches!(d, comline_core::schema::ir::frozen::unit::DeprecatedValue::False))) { + deprecated.push(name.clone()); + } + } + } + if protocols.is_empty() { + ui::detail("protocols: none"); + } else { + ui::detail(format!("protocols: {}", protocols.join(", "))); + } + if !given_up.is_empty() || !deprecated.is_empty() { + ui::note("worth settling first (not required):"); + for line in given_up { + ui::detail(line); + } + if !deprecated.is_empty() { + ui::detail(format!( + "deprecated, and kept until 2.0: {}", + deprecated.join(", ") + )); + } + } +} + +/// Ask on a terminal (default no); without one, `yes` is the only way on. +fn confirm(question: &str, yes: bool, what: &'static str) -> Result { + if yes { + return Ok(true); + } + if !io::stdin().is_terminal() { + return Err(CliError::ReleaseConfirmationRequired { what }.into()); + } + print!("{question}? [y/N] "); + io::stdout().flush().into_diagnostic()?; + let mut line = String::new(); + io::stdin().read_line(&mut line).into_diagnostic()?; + Ok(matches!( + line.trim().to_ascii_lowercase().as_str(), + "y" | "yes" + )) +} diff --git a/src/error.rs b/src/error.rs index 8215c1d..1aaf28c 100644 --- a/src/error.rs +++ b/src/error.rs @@ -41,6 +41,12 @@ pub enum CliError { to be asked, or pass `{flag}` to accept them" )] AcceptanceRequired { flag: &'static str }, + + #[error( + "refusing to {what} without confirmation: re-run in a terminal to be asked, or pass \ + `--yes`" + )] + ReleaseConfirmationRequired { what: &'static str }, } /// Map a finished-command error to its process exit code. @@ -50,6 +56,7 @@ pub fn exit_code_for(report: &miette::Report) -> i32 { Some(CliError::NothingBuilt(_)) => EXIT_PRECONDITION, Some(CliError::ConfirmationRequired) => EXIT_PRECONDITION, Some(CliError::AcceptanceRequired { .. }) => EXIT_PRECONDITION, + Some(CliError::ReleaseConfirmationRequired { .. }) => EXIT_PRECONDITION, None => EXIT_FAILURE, } } diff --git a/src/gen_config.rs b/src/gen_config.rs index 0ae7e7a..65cda3a 100644 --- a/src/gen_config.rs +++ b/src/gen_config.rs @@ -45,7 +45,8 @@ fn default_external_std(mode: &str) -> bool { /// /// TOML: `package_versions = "latest"` (default) | `"all"` | `"0.3.0"` | /// `["0.3.0", "0.4.0"]`. `latest` is the working tree; anything else reads -/// committed versions from the CAS chain. +/// committed versions from the CAS chain: `all` every release, a list any +/// version named, a build (`0.3.0-dev.2`) included. #[derive(Debug, Clone, PartialEq, Eq, Default)] pub enum VersionSpec { /// The working tree (no CAS read). diff --git a/src/main.rs b/src/main.rs index d61ab30..10128fa 100644 --- a/src/main.rs +++ b/src/main.rs @@ -160,6 +160,12 @@ fn run(cli: Cli) -> Result<()> { commands::add::run(&work_dir, &name, source, !no_hash, accept_unverified) } Commands::Accept { target, yes } => commands::accept::run(&work_dir, &target, yes), + Commands::Release { + version, + raise, + reason, + yes, + } => commands::release::run(&work_dir, version.as_deref(), raise, reason, yes), Commands::Completions { shell } => commands::completions::run(shell), Commands::Targets { features, json } => commands::targets::run(features, json), } diff --git a/tests/cli/build.rs b/tests/cli/build.rs index 8726843..25ee46b 100644 --- a/tests/cli/build.rs +++ b/tests/cli/build.rs @@ -15,7 +15,10 @@ fn freezes_an_initial_version() { .assert() .success() .stderr(predicate::str::contains("Project built")) - .stderr(predicate::str::contains("initial version 0.0.1")); + .stderr(predicate::str::contains("initial build 0.1.0-dev.1")) + .stderr(predicate::str::contains( + "next release: 0.1.0 (the first release)", + )); assert!(project.join(".comline").is_dir()); } diff --git a/tests/cli/diff.rs b/tests/cli/diff.rs index 6d95968..ad1d53d 100644 --- a/tests/cli/diff.rs +++ b/tests/cli/diff.rs @@ -31,7 +31,7 @@ fn reports_changes_between_two_versions() { comline_cmd() .current_dir(&project) - .args(["diff", "0.0.1", "HEAD"]) + .args(["diff", "0.1.0-dev.1", "HEAD"]) .assert() .success() .stderr(predicate::str::contains("New features")) @@ -45,7 +45,7 @@ fn without_a_build_fails_with_precondition_code() { comline_cmd() .current_dir(&project) - .args(["diff", "0.0.1", "HEAD"]) + .args(["diff", "0.1.0-dev.1", "HEAD"]) .assert() .failure() .code(2) @@ -98,7 +98,7 @@ fn a_new_schema_file_sorting_first_is_an_addition_not_a_break() { comline_cmd() .current_dir(&project) - .args(["diff", "0.0.1", "HEAD"]) + .args(["diff", "0.1.0-dev.1", "HEAD"]) .assert() .success() .stderr(predicate::str::contains("Added struct `First`")) diff --git a/tests/cli/generate.rs b/tests/cli/generate.rs index 44305e1..011f9a8 100644 --- a/tests/cli/generate.rs +++ b/tests/cli/generate.rs @@ -321,11 +321,12 @@ fn dylib_mode_is_rejected() { // -------- multi-version (package_versions) -------- -/// Build twice with a schema change, so the CAS chain has 0.0.1 and 0.1.0. +/// Release twice with a schema change between, so the CAS chain has the +/// releases 0.1.0 and 0.1.1 (a new struct is a minor: before 1.0, the patch). fn built_twice(project: &std::path::Path) { comline_cmd() .current_dir(project) - .arg("build") + .arg("release") .assert() .success(); let schema = project.join("src/api.ids"); @@ -334,7 +335,7 @@ fn built_twice(project: &std::path::Path) { fs::write(&schema, text).unwrap(); comline_cmd() .current_dir(project) - .arg("build") + .arg("release") .assert() .success(); } @@ -360,7 +361,7 @@ fn latest_stamps_the_last_committed_version() { .assert() .success(); - assert!(project.join("generated/rust/0.0.1/api.rs").exists()); + assert!(project.join("generated/rust/0.1.0-dev.1/api.rs").exists()); } /// The tree shows where files land; only `--list` shows which schema each @@ -407,7 +408,7 @@ fn dry_run_and_list_combine() { } #[test] -fn package_versions_all_generates_every_committed_version() { +fn package_versions_all_generates_every_release() { let temp = tempfile::tempdir().unwrap(); let project = fixture_project(temp.path()); built_twice(&project); @@ -425,12 +426,14 @@ fn package_versions_all_generates_every_committed_version() { .success() // One tree per version. `layout` has to carry `{{package_version}}` // for multi-version output, so each tree's root names its version - - // no `[0.0.1]` tag needed to tell them apart. - .stderr(predicate::str::contains("generated/rust/0.0.1/")) - .stderr(predicate::str::contains("generated/rust/0.1.0/")); + // no `[0.1.0]` tag needed to tell them apart. + .stderr(predicate::str::contains("generated/rust/0.1.0/")) + .stderr(predicate::str::contains("generated/rust/0.1.1/")); - assert!(project.join("generated/rust/0.0.1/api.rs").exists()); assert!(project.join("generated/rust/0.1.0/api.rs").exists()); + assert!(project.join("generated/rust/0.1.1/api.rs").exists()); + // Releases only: not the builds that led to them. + assert!(!project.join("generated/rust/0.1.0-dev.1").exists()); // `--list` tags each line with the version it belongs to. comline_cmd() @@ -438,8 +441,8 @@ fn package_versions_all_generates_every_committed_version() { .args(["generate", "--target", "rust", "--list"]) .assert() .success() - .stderr(predicate::str::contains("[0.0.1]")) - .stderr(predicate::str::contains("[0.1.0]")); + .stderr(predicate::str::contains("[0.1.0]")) + .stderr(predicate::str::contains("[0.1.1]")); } #[test] @@ -449,7 +452,7 @@ fn package_versions_list_generates_only_the_named_versions() { built_twice(&project); fs::write( project.join("comline.toml"), - "[generate]\npackage_versions = [\"0.0.1\"]\n\ + "[generate]\npackage_versions = [\"0.1.0\"]\n\ layout = \"{{language}}/{{package_version}}/{{namespace}}.{{ext}}\"\n", ) .unwrap(); @@ -460,8 +463,8 @@ fn package_versions_list_generates_only_the_named_versions() { .assert() .success(); - assert!(project.join("generated/rust/0.0.1/api.rs").exists()); - assert!(!project.join("generated/rust/0.1.0").exists()); + assert!(project.join("generated/rust/0.1.0/api.rs").exists()); + assert!(!project.join("generated/rust/0.1.1").exists()); } #[test] diff --git a/tests/cli/main.rs b/tests/cli/main.rs index d530fc7..a599648 100644 --- a/tests/cli/main.rs +++ b/tests/cli/main.rs @@ -16,4 +16,5 @@ mod end_to_end; mod generate; mod global; mod new; +mod release; mod reset; diff --git a/tests/cli/release.rs b/tests/cli/release.rs new file mode 100644 index 0000000..0907738 --- /dev/null +++ b/tests/cli/release.rs @@ -0,0 +1,112 @@ +//! `comline release` + +use std::fs; +use std::path::Path; + +use predicates::prelude::*; + +use crate::util::*; + +const PRECONDITION: i32 = 2; + +fn comline(project: &Path, args: &[&str]) -> assert_cmd::assert::Assert { + comline_cmd().current_dir(project).args(args).assert() +} + +fn add_struct(project: &Path, name: &str) { + let schema = project.join("src/api.ids"); + let mut text = fs::read_to_string(&schema).unwrap(); + text.push_str(&format!("\nstruct {name} {{\n x: string\n}}\n")); + fs::write(&schema, text).unwrap(); +} + +#[test] +fn releases_what_was_built_and_then_has_nothing_to_release() { + let temp = tempfile::tempdir().unwrap(); + let project = fixture_project(temp.path()); + + // It builds first: no `comline build` needed. + comline(&project, &["release"]) + .success() + .stderr(predicate::str::contains("Released 0.1.0")) + .stderr(predicate::str::contains("the first release")); + comline(&project, &["release"]) + .failure() + .stderr(predicate::str::contains("0.1.0 is already released")); + + add_struct(&project, "Added"); + comline(&project, &["build"]) + .success() + .stderr( + predicate::str::contains("0.1.0 → 0.1.1-dev.1") + .or(predicate::str::contains("0.1.0 -> 0.1.1-dev.1")), + ) + .stderr(predicate::str::contains( + "next release: 0.1.1 (new features since 0.1.0)", + )); + comline(&project, &["release"]) + .success() + .stderr(predicate::str::contains("Released 0.1.1")) + .stderr(predicate::str::contains("0.1.0 -> 0.1.1")) + .stderr(predicate::str::contains("Added struct `Added`")) + .stderr(predicate::str::contains("minor bump")); +} + +#[test] +fn raising_needs_a_reason_and_a_major_raise_is_confirmed() { + let temp = tempfile::tempdir().unwrap(); + let project = fixture_project(temp.path()); + comline(&project, &["release"]).success(); + add_struct(&project, "Added"); + + comline(&project, &["release", "--raise", "major"]) + .failure() + .stderr(predicate::str::contains("--raise needs --reason")); + comline( + &project, + &["release", "--raise", "major", "--reason", "retries changed"], + ) + .failure() + .code(PRECONDITION) + .stderr(predicate::str::contains("pass `--yes`")); + comline( + &project, + &[ + "release", + "--raise", + "major", + "--reason", + "retries changed", + "--yes", + ], + ) + .success() + .stderr(predicate::str::contains("Released 0.2.0")) + .stderr(predicate::str::contains("major bump, raised from minor")) + .stderr(predicate::str::contains("reason: retries changed")); +} + +#[test] +fn release_1_0_says_what_it_commits_to() { + let temp = tempfile::tempdir().unwrap(); + let project = fixture_project(temp.path()); + comline(&project, &["release"]).success(); + + comline(&project, &["release", "2.0"]) + .failure() + .stderr(predicate::str::contains("the only one you name is `1.0`")); + comline(&project, &["release", "1.0"]) + .failure() + .code(PRECONDITION) + .stderr(predicate::str::contains( + "a breaking change is a major release", + )) + .stderr(predicate::str::contains("pass `--yes`")); + comline(&project, &["release", "1.0", "--yes"]) + .success() + .stderr(predicate::str::contains("Released 1.0.0")) + .stderr(predicate::str::contains("stable")); + comline(&project, &["release", "1.0", "--yes"]) + .failure() + .stderr(predicate::str::contains("already stable")); +} diff --git a/tests/cli/reset.rs b/tests/cli/reset.rs index 38c403b..ddccfcb 100644 --- a/tests/cli/reset.rs +++ b/tests/cli/reset.rs @@ -75,7 +75,7 @@ fn dry_run_keeps_everything() { .assert() .success() .stderr(predicate::str::contains("would remove .comline")) - .stderr(predicate::str::contains("currently 0.0.1")); + .stderr(predicate::str::contains("currently 0.1.0-dev.1")); assert!(project.join(".comline").is_dir()); }