cursus 0.9.2

Library crate for the cursus release management CLI
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
//! Shared test helpers for integration tests.

// Each tests/*.rs binary compiles this module independently; helpers used in one
// binary but not another appear as dead code in that binary's compilation unit.
#![allow(dead_code)]

use std::process::Command;

use cursus::model::config::{CargoConfig, Config, GitConfig, NpmConfig, PackageManager};

use tempfile::TempDir;

/// Creates a minimal `Env` with a real command runner, local filesystem, and git for the given dir.
pub(super) fn test_env(dir: &std::path::Path) -> cursus::Env {
	let runner = std::sync::Arc::new(cursus::command::RealCommandRunner)
		as std::sync::Arc<dyn cursus::command::CommandRunner>;
	let path = cursus::path::AbsolutePath::new(dir).unwrap();
	cursus::Env::new(
		std::sync::Arc::clone(&runner),
		std::sync::Arc::new(cursus::filesystem::LocalFilesystem),
		std::sync::Arc::new(cursus::git::GitWorkdir::new(runner, path)),
	)
}

/// Runs cursus with a real command runner and local filesystem.
///
/// This is the standard way to invoke cursus from integration tests.
/// Parses CLI arguments, handles dry-run wrapping, performs git discovery,
/// and delegates to [`cursus::run`].
pub(super) async fn run_cursus(
	args: impl IntoIterator<Item = impl Into<std::ffi::OsString> + Clone>,
	cwd: &std::path::Path,
) -> anyhow::Result<std::process::ExitCode> {
	use std::sync::Arc;

	use clap::Parser as _;
	use cursus::command::{CommandRunner, DryRunCommandRunner, RealCommandRunner};
	use cursus::filesystem::LocalFilesystem;
	use cursus::path::AbsolutePath;

	let runner: Arc<dyn CommandRunner> = Arc::new(RealCommandRunner);
	let filesystem: Arc<dyn cursus::filesystem::Filesystem> = Arc::new(LocalFilesystem);

	let cli = cursus::cli::Cli::try_parse_from(args)?;

	let runner = if cli.global.dry_run {
		Arc::new(DryRunCommandRunner::new(runner)) as Arc<dyn CommandRunner>
	} else {
		runner
	};

	let cwd_abs = AbsolutePath::new(cwd)?;
	let git_workdir = cursus::git::find_workdir(&cwd_abs, &*filesystem)
		.await
		.ok_or_else(|| anyhow::anyhow!("No git repository found"))?;
	let git = Arc::new(cursus::git::GitWorkdir::new(
		Arc::clone(&runner),
		git_workdir,
	));
	let env = cursus::Env::new(runner, filesystem, git);

	let config = cursus::model::config::load(env.fs(), env.git().path()).await?;
	cursus::run(cli, env, config).await
}

/// Runs a git command in the given directory and panics on failure.
///
/// Stdout is suppressed to keep test output clean. Stderr is captured and
/// included in the panic message so failures are diagnosable.
pub(super) fn git_cmd(dir: &std::path::Path, args: &[&str]) {
	let output = Command::new("git")
		.args(args)
		.current_dir(dir)
		.stdout(std::process::Stdio::null())
		.stderr(std::process::Stdio::piped())
		.output()
		.unwrap_or_else(|e| panic!("Failed to spawn git {args:?}: {e}"));
	assert!(
		output.status.success(),
		"git {args:?} failed with status {}:\n{}",
		output.status,
		String::from_utf8_lossy(&output.stderr)
	);
}

/// Creates a real git repository with an initial commit in a temp directory.
///
/// Configures `user.name`, `user.email`, and disables commit/tag signing so that
/// git operations succeed in any environment without a GPG key.
pub(super) fn temp_real_git_repo() -> TempDir {
	let dir = tempfile::tempdir().expect("Failed to create temp dir");
	git_cmd(dir.path(), &["init", "-b", "main"]);
	git_cmd(dir.path(), &["config", "user.name", "Cursus Test"]);
	git_cmd(dir.path(), &["config", "user.email", "test@cursus.local"]);
	git_cmd(dir.path(), &["config", "commit.gpgsign", "false"]);
	git_cmd(dir.path(), &["config", "tag.gpgsign", "false"]);
	// Create an initial empty commit so the repo has a HEAD
	git_cmd(
		dir.path(),
		&["commit", "--allow-empty", "-m", "chore: initial commit"],
	);
	dir
}

/// Creates a real git repository with a Cursus config that has git lifecycle enabled.
pub(super) async fn temp_real_git_repo_with_config(
	pm: PackageManager,
	git_config: GitConfig,
) -> TempDir {
	let dir = temp_real_git_repo();
	let env = test_env(dir.path());
	let config = match pm {
		PackageManager::Npm => Config::new()
			.with_npm(NpmConfig::enabled())
			.with_git(git_config),
		PackageManager::Cargo => Config::new()
			.with_cargo(CargoConfig::enabled())
			.with_git(git_config),
	};
	config.save(env.fs(), env.git().path()).await.unwrap();
	dir
}

/// Creates a real git repository with a Cargo workspace and Cursus config.
///
/// All files are staged and committed in the initial state.
pub(super) async fn temp_real_git_repo_with_cargo_workspace(
	members: &[(&str, &str)],
	git_config: GitConfig,
) -> TempDir {
	let dir = temp_real_git_repo();
	let env = test_env(dir.path());
	let config = Config::new()
		.with_cargo(CargoConfig::enabled())
		.with_git(git_config);
	config.save(env.fs(), env.git().path()).await.unwrap();

	let member_list = members
		.iter()
		.map(|(name, _)| format!("\"{name}\""))
		.collect::<Vec<_>>()
		.join(", ");
	std::fs::write(
		dir.path().join("Cargo.toml"),
		format!("[workspace]\nmembers = [{member_list}]\n"),
	)
	.unwrap();

	for (name, version) in members {
		let pkg_dir = dir.path().join(name);
		std::fs::create_dir_all(pkg_dir.join("src")).unwrap();
		std::fs::write(
			pkg_dir.join("Cargo.toml"),
			format!("[package]\nname = \"{name}\"\nversion = \"{version}\"\nedition = \"2024\"\n"),
		)
		.unwrap();
		std::fs::write(pkg_dir.join("src/lib.rs"), "").unwrap();
	}

	// Commit all setup files
	git_cmd(dir.path(), &["add", "."]);
	git_cmd(dir.path(), &["commit", "-m", "chore: set up workspace"]);

	dir
}

/// Creates a bare "remote" repo and wires it as `origin` of the given working repo.
///
/// Returns the `TempDir` holding the bare repo (must be kept alive for the test duration).
pub(super) fn add_local_remote(working_repo: &std::path::Path) -> TempDir {
	let remote_dir = tempfile::tempdir().expect("Failed to create remote dir");
	git_cmd(remote_dir.path(), &["init", "--bare"]);
	git_cmd(
		working_repo,
		&[
			"remote",
			"add",
			"origin",
			remote_dir.path().to_str().unwrap(),
		],
	);
	remote_dir
}

/// Reads the log of all git commits in the repo (most recent first).
pub(super) fn git_log(dir: &std::path::Path) -> Vec<String> {
	let output = Command::new("git")
		.args(["log", "--format=%s"])
		.current_dir(dir)
		.output()
		.expect("Failed to run git log");
	String::from_utf8(output.stdout)
		.expect("git log output is not UTF-8")
		.lines()
		.map(str::to_string)
		.collect()
}

/// Returns the list of git tags in the repo.
pub(super) fn git_tags(dir: &std::path::Path) -> Vec<String> {
	let output = Command::new("git")
		.args(["tag", "--list"])
		.current_dir(dir)
		.output()
		.expect("Failed to run git tag");
	String::from_utf8(output.stdout)
		.expect("git tag output is not UTF-8")
		.lines()
		.filter(|s| !s.is_empty())
		.map(str::to_string)
		.collect()
}

/// Returns `true` if the given tag exists in the repo.
pub(super) fn git_tag_exists(dir: &std::path::Path, tag: &str) -> bool {
	git_tags(dir).contains(&tag.to_string())
}

/// Returns `true` if the given local branch exists.
pub(super) fn git_local_branch_exists(dir: &std::path::Path, branch: &str) -> bool {
	let output = Command::new("git")
		.args(["branch", "--list", branch])
		.current_dir(dir)
		.output()
		.expect("Failed to run git branch");
	!String::from_utf8(output.stdout)
		.expect("git branch output is not UTF-8")
		.trim()
		.is_empty()
}

/// Pushes the current branch to origin.
///
/// Panics if the push fails.
pub(super) fn git_push_to_remote(working_repo: &std::path::Path) {
	let branch = git_current_branch(working_repo);
	let output = Command::new("git")
		.args(["push", "origin", &branch])
		.current_dir(working_repo)
		.stderr(std::process::Stdio::piped())
		.output()
		.expect("Failed to run git push");
	assert!(
		output.status.success(),
		"git push to origin failed:\n{}",
		String::from_utf8_lossy(&output.stderr)
	);
}

/// Returns the name of the current git branch.
///
/// Panics if the command fails or the HEAD is detached.
pub(super) fn git_current_branch(dir: &std::path::Path) -> String {
	let output = Command::new("git")
		.args(["rev-parse", "--abbrev-ref", "HEAD"])
		.current_dir(dir)
		.output()
		.expect("Failed to run git rev-parse");
	let branch = String::from_utf8(output.stdout)
		.expect("git rev-parse output is not UTF-8")
		.trim()
		.to_string();
	assert!(
		branch != "HEAD",
		"git_current_branch called in detached HEAD state"
	);
	branch
}

/// Creates a temporary directory with a `.git` folder to simulate a git repository.
pub(super) fn temp_git_repo() -> TempDir {
	let dir = tempfile::tempdir().expect("Failed to create temp dir");
	std::fs::create_dir(dir.path().join(".git")).unwrap();
	dir
}

/// Creates a temporary git repository with a Cursus config file.
pub(super) async fn temp_git_repo_with_config(pm: PackageManager) -> TempDir {
	let dir = temp_git_repo();
	let env = test_env(dir.path());
	let config = match pm {
		PackageManager::Npm => Config::new().with_npm(NpmConfig::enabled()),
		PackageManager::Cargo => Config::new().with_cargo(CargoConfig::enabled()),
	};
	config.save(env.fs(), env.git().path()).await.unwrap();
	dir
}

/// Creates a temporary git repository with a config and matching package manifest.
pub(super) async fn temp_git_repo_with_project(pm: PackageManager) -> TempDir {
	let dir = temp_git_repo();
	let env = test_env(dir.path());
	let config = match pm {
		PackageManager::Npm => Config::new().with_npm(NpmConfig::enabled()),
		PackageManager::Cargo => Config::new().with_cargo(CargoConfig::enabled()),
	};
	config.save(env.fs(), env.git().path()).await.unwrap();
	match pm {
		PackageManager::Npm => {
			std::fs::write(
				dir.path().join("package.json"),
				r#"{"name": "test-project", "version": "0.1.0"}"#,
			)
			.unwrap();
		}
		PackageManager::Cargo => {
			std::fs::write(
				dir.path().join("Cargo.toml"),
				"[package]\nname = \"test-project\"\nversion = \"0.1.0\"\nedition = \"2024\"\n",
			)
			.unwrap();
			// Create src/lib.rs so cargo can generate a valid Cargo.lock
			std::fs::create_dir_all(dir.path().join("src")).unwrap();
			std::fs::write(dir.path().join("src/lib.rs"), "").unwrap();
		}
	}
	dir
}

/// Creates a temporary git repository with a Cargo workspace containing named packages.
///
/// Each entry in `members` is a `(name, version)` pair. The workspace root
/// `Cargo.toml` lists all members, and each gets its own `Cargo.toml` and
/// an empty `src/lib.rs`.
pub(super) async fn temp_git_repo_with_cargo_workspace(members: &[(&str, &str)]) -> TempDir {
	let dir = temp_git_repo();
	let env = test_env(dir.path());
	let config = Config::new().with_cargo(CargoConfig::enabled());
	config.save(env.fs(), env.git().path()).await.unwrap();

	let member_list = members
		.iter()
		.map(|(name, _)| format!("\"{name}\""))
		.collect::<Vec<_>>()
		.join(", ");
	std::fs::write(
		dir.path().join("Cargo.toml"),
		format!("[workspace]\nmembers = [{member_list}]\n"),
	)
	.unwrap();

	for (name, version) in members {
		let pkg_dir = dir.path().join(name);
		std::fs::create_dir_all(pkg_dir.join("src")).unwrap();
		std::fs::write(
			pkg_dir.join("Cargo.toml"),
			format!("[package]\nname = \"{name}\"\nversion = \"{version}\"\nedition = \"2024\"\n"),
		)
		.unwrap();
		std::fs::write(pkg_dir.join("src/lib.rs"), "").unwrap();
	}

	dir
}

/// Creates a temporary git repository with a config and package manifest in a subfolder.
pub(super) async fn temp_git_repo_with_project_in_subfolder(
	pm: PackageManager,
	subfolder: &str,
) -> TempDir {
	let dir = temp_git_repo();
	let env = test_env(dir.path());
	let mut config = match pm {
		PackageManager::Npm => Config::new().with_npm(NpmConfig::enabled()),
		PackageManager::Cargo => Config::new().with_cargo(CargoConfig::enabled()),
	};
	match pm {
		PackageManager::Npm => config.npm.path = Some(subfolder.to_string()),
		PackageManager::Cargo => config.cargo.path = Some(subfolder.to_string()),
	}
	config.save(env.fs(), env.git().path()).await.unwrap();
	let sub_path = dir.path().join(subfolder);
	std::fs::create_dir_all(&sub_path).unwrap();
	match pm {
		PackageManager::Npm => {
			std::fs::write(
				sub_path.join("package.json"),
				r#"{"name": "test-project", "version": "0.1.0"}"#,
			)
			.unwrap();
		}
		PackageManager::Cargo => {
			std::fs::write(
				sub_path.join("Cargo.toml"),
				"[package]\nname = \"test-project\"\nversion = \"0.1.0\"\nedition = \"2024\"\n",
			)
			.unwrap();
		}
	}
	dir
}

/// Creates a changeset file in the `.cursus` directory.
///
/// The `content` should be a valid changeset with TOML frontmatter, e.g.:
/// `"+++\npkg-name = \"minor\"\n+++\n\nDescription\n"`.
pub(super) fn write_changeset(dir: &std::path::Path, filename: &str, content: &str) {
	let cursus_dir = dir.join(".cursus");
	std::fs::create_dir_all(&cursus_dir).unwrap();
	std::fs::write(cursus_dir.join(filename), content).unwrap();
}

/// Returns a [`GitConfig`] with git lifecycle enabled and all other fields at their defaults.
pub(super) fn git_enabled_config() -> GitConfig {
	GitConfig::enabled_config()
}

/// Sets `origin/HEAD` to point at the given branch for the remote `origin`.
///
/// Runs `git remote set-head origin <branch>`. Must be called after the remote
/// has been added and the initial push has been made.
pub(super) fn git_set_remote_head(working_repo: &std::path::Path, branch: &str) {
	git_cmd(working_repo, &["remote", "set-head", "origin", branch]);
}

/// Creates a real git repository with a Cursus config and committed project files.
///
/// Unlike [`temp_git_repo_with_project`] (which uses a fake `.git` folder),
/// this creates a proper git repo with commits so that git operations like
/// `rev-list` and `diff-tree` work correctly.
pub(super) async fn temp_real_git_repo_with_project(pm: PackageManager) -> TempDir {
	let dir = temp_real_git_repo();
	let env = test_env(dir.path());
	let config = match pm {
		PackageManager::Npm => Config::new().with_npm(NpmConfig::enabled()),
		PackageManager::Cargo => Config::new().with_cargo(CargoConfig::enabled()),
	};
	config.save(env.fs(), env.git().path()).await.unwrap();
	match pm {
		PackageManager::Npm => {
			std::fs::write(
				dir.path().join("package.json"),
				r#"{"name": "test-project", "version": "0.1.0"}"#,
			)
			.unwrap();
		}
		PackageManager::Cargo => {
			std::fs::write(
				dir.path().join("Cargo.toml"),
				"[package]\nname = \"test-project\"\nversion = \"0.1.0\"\nedition = \"2024\"\n",
			)
			.unwrap();
			std::fs::create_dir_all(dir.path().join("src")).unwrap();
			std::fs::write(dir.path().join("src/lib.rs"), "").unwrap();
		}
	}
	git_cmd(dir.path(), &["add", "."]);
	git_cmd(dir.path(), &["commit", "-m", "chore: add project files"]);
	dir
}