cursus 0.3.0

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
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
//! Integration tests for the `publish` command's git lifecycle integration.

mod common;

use common::{
	git_enabled_config, git_tags, run_cursus, temp_real_git_repo_with_cargo_workspace,
	temp_real_git_repo_with_config,
};

use cursus::model::config::{GitConfig, PackageManager};
use cursus::test_logging::{init_test_logger, take_logs};

/// Helper: write a config TOML directly to `.cursus/config.toml`.
fn write_config(dir: &std::path::Path, toml: &str) {
	let config_dir = dir.join(".cursus");
	std::fs::create_dir_all(&config_dir).unwrap();
	std::fs::write(config_dir.join("config.toml"), toml).unwrap();
}

// --- Flag parsing ---

#[tokio::test]
async fn publish_no_git_flag_parses_without_error() {
	let dir = temp_real_git_repo_with_config(PackageManager::Cargo, git_enabled_config()).await;
	std::fs::write(
		dir.path().join("Cargo.toml"),
		"[package]\nname = \"my-app\"\nversion = \"1.0.0\"\n",
	)
	.unwrap();

	// --no-git just skips git operations; --dry-run avoids hitting a registry.
	// The command should succeed (no changesets → exit success is fine too).
	let result = run_cursus(
		[
			"cursus",
			"publish",
			"--no-interactive",
			"--dry-run",
			"--no-git",
		],
		dir.path(),
	)
	.await;
	assert!(result.is_ok(), "Expected Ok, got: {result:?}");
}

// --- Dry-run does not create actual tags ---

#[tokio::test]
async fn publish_git_enabled_dry_run_does_not_create_tags() {
	let dir =
		temp_real_git_repo_with_cargo_workspace(&[("my-app", "1.0.0")], git_enabled_config()).await;
	// Dry-run should report what would happen but not touch the git repository.
	let result = run_cursus(
		["cursus", "publish", "--no-interactive", "--dry-run"],
		dir.path(),
	)
	.await;
	assert!(result.is_ok(), "Expected Ok, got: {result:?}");
	assert!(
		git_tags(dir.path()).is_empty(),
		"Dry-run should not create any git tags"
	);
}

#[tokio::test]
async fn publish_no_git_dry_run_does_not_create_tags() {
	let dir =
		temp_real_git_repo_with_cargo_workspace(&[("my-app", "1.0.0")], git_enabled_config()).await;
	let result = run_cursus(
		[
			"cursus",
			"publish",
			"--no-interactive",
			"--dry-run",
			"--no-git",
		],
		dir.path(),
	)
	.await;
	assert!(result.is_ok(), "Expected Ok, got: {result:?}");
	assert!(
		git_tags(dir.path()).is_empty(),
		"--no-git should not create any git tags"
	);
}

// --- --no-git skips GitHub Releases token check ---

#[tokio::test]
async fn publish_no_git_skips_github_token_check() {
	// With github enabled but no token, the command should fail.
	// With --no-git, GitHub Releases are skipped entirely, so no token is needed.
	let dir = temp_real_git_repo_with_config(PackageManager::Cargo, git_enabled_config()).await;
	write_config(
		dir.path(),
		"[cargo]\nenabled = true\n[git]\nenabled = true\n[github]\nenabled = true\n",
	);
	std::fs::write(
		dir.path().join("Cargo.toml"),
		"[package]\nname = \"my-app\"\nversion = \"1.0.0\"\n",
	)
	.unwrap();

	// Without --no-git and no token, this would fail due to missing GitHub token.
	// With --no-git, it should succeed (dry-run so no actual publish either).
	let result = run_cursus(
		[
			"cursus",
			"publish",
			"--no-interactive",
			"--dry-run",
			"--no-git",
		],
		dir.path(),
	)
	.await;
	assert!(result.is_ok(), "Expected Ok with --no-git, got: {result:?}");
}

// --- Log-content checks for dry-run tag decisions ---

/// Multi-package dry-run logs tags in `pkg@version` format (not `v{version}`).
///
/// This guards against `replace > with <` on `projects.len() > 1` (line ~109),
/// which would treat a multi-package repo as single-package and use the wrong
/// tag format.
#[tokio::test]
async fn publish_multi_package_dry_run_logs_prefixed_tag_format() {
	init_test_logger();
	let _ = take_logs();
	let dir = temp_real_git_repo_with_cargo_workspace(
		&[("pkg-a", "1.0.0"), ("pkg-b", "2.0.0")],
		git_enabled_config(),
	)
	.await;
	std::fs::write(dir.path().join("pkg-a/CHANGELOG.md"), "# Changelog\n").unwrap();
	std::fs::write(dir.path().join("pkg-b/CHANGELOG.md"), "# Changelog\n").unwrap();

	let result = run_cursus(
		["cursus", "publish", "--no-interactive", "--dry-run"],
		dir.path(),
	)
	.await;
	assert!(result.is_ok(), "Expected Ok, got: {result:?}");

	let logs = take_logs();
	// Multi-package repos use "pkg@version" format
	assert!(
		logs.iter()
			.any(|(_, m)| m.contains("Would create tag") && m.contains("pkg-a@1.0.0")),
		"Multi-package dry-run should log 'Would create tag pkg-a@1.0.0', got: {logs:?}"
	);
	// Should not use the single-package "v{version}" format
	assert!(
		!logs.iter().any(|(_, m)| m.contains("Would create tag v")),
		"Multi-package dry-run should not log 'v{{version}}' format, got: {logs:?}"
	);
}

/// Single-package, git enabled, no `--no-git`: dry-run logs "Would create tag".
///
/// Guards against `delete !` on `git_enabled = config.git.enabled() && !args.no_git`
/// (line ~115), which would make git_enabled false when no_git is false.
#[tokio::test]
async fn publish_git_enabled_dry_run_logs_would_create_tag_and_summary_tag_note() {
	init_test_logger();
	let _ = take_logs();
	let dir =
		temp_real_git_repo_with_cargo_workspace(&[("my-app", "1.0.0")], git_enabled_config()).await;
	std::fs::write(dir.path().join("my-app/CHANGELOG.md"), "# Changelog\n").unwrap();

	let result = run_cursus(
		["cursus", "publish", "--no-interactive", "--dry-run"],
		dir.path(),
	)
	.await;
	assert!(result.is_ok(), "Expected Ok, got: {result:?}");

	let logs = take_logs();
	// "Would create tag" must appear when git is enabled
	assert!(
		logs.iter().any(|(_, m)| m.contains("Would create tag")),
		"Should log 'Would create tag' when git is enabled, got: {logs:?}"
	);
	// Summary must include the tag note when git is enabled and packages were published
	assert!(
		logs.iter().any(|(_, m)| m.contains("would be tagged")),
		"Summary should include 'would be tagged' when git is enabled, got: {logs:?}"
	);
}

/// Git disabled, no `--no-git`: dry-run must NOT log "Would create tag".
///
/// Guards against `replace && with ||` on `git_enabled = config.git.enabled() && !args.no_git`
/// (line ~115), which would make git_enabled true even when git is disabled.
/// Also guards against `replace && with ||` on the tag_note guard (line ~171).
#[tokio::test]
async fn publish_git_disabled_dry_run_no_would_create_tag_in_logs_or_summary() {
	init_test_logger();
	let _ = take_logs();
	// Use write_config to set up a Cargo-only config (no git section → git disabled).
	let dir =
		temp_real_git_repo_with_cargo_workspace(&[("my-app", "1.0.0")], git_enabled_config()).await;
	write_config(dir.path(), "[cargo]\nenabled = true\n");

	let result = run_cursus(
		["cursus", "publish", "--no-interactive", "--dry-run"],
		dir.path(),
	)
	.await;
	assert!(result.is_ok(), "Expected Ok, got: {result:?}");

	let logs = take_logs();
	assert!(
		!logs.iter().any(|(_, m)| m.contains("Would create tag")),
		"Should NOT log 'Would create tag' when git is disabled, got: {logs:?}"
	);
	assert!(
		!logs.iter().any(|(_, m)| m.contains("would be tagged")),
		"Summary should NOT include 'would be tagged' when git is disabled, got: {logs:?}"
	);
}

/// GitHub disabled, no `--no-git`: dry-run must NOT log "Would create GitHub Release".
///
/// Guards against `replace && with ||` on `config.github.enabled && !args.no_git`
/// (line ~134), which would log GitHub Release messages even when GitHub is disabled.
#[tokio::test]
async fn publish_github_disabled_dry_run_no_would_create_github_release() {
	init_test_logger();
	let _ = take_logs();
	// git enabled but github not configured → github.enabled = false
	let dir =
		temp_real_git_repo_with_cargo_workspace(&[("my-app", "1.0.0")], git_enabled_config()).await;

	let result = run_cursus(
		["cursus", "publish", "--no-interactive", "--dry-run"],
		dir.path(),
	)
	.await;
	assert!(result.is_ok(), "Expected Ok, got: {result:?}");

	let logs = take_logs();
	assert!(
		!logs
			.iter()
			.any(|(_, m)| m.contains("Would create GitHub Release")),
		"Should NOT log GitHub Release messages when GitHub is disabled, got: {logs:?}"
	);
}

/// Git disabled + CHANGELOG.md present (package is prepared): dry-run must NOT log "would be tagged".
///
/// Guards `&&`→`||` on the tag_note condition: with git disabled but published packages,
/// a `||` mutation would produce a non-empty tag_note even though git is off.
#[tokio::test]
async fn publish_git_disabled_with_changelog_dry_run_no_tag_note_in_summary() {
	init_test_logger();
	let _ = take_logs();
	// Set up workspace with git-disabled config.
	let dir =
		temp_real_git_repo_with_cargo_workspace(&[("my-app", "1.0.0")], git_enabled_config()).await;
	write_config(dir.path(), "[cargo]\nenabled = true\n");
	// Write CHANGELOG.md so the package is "prepared" and would appear in published list.
	std::fs::write(dir.path().join("my-app/CHANGELOG.md"), "# Changelog\n").unwrap();

	let result = run_cursus(
		["cursus", "publish", "--no-interactive", "--dry-run"],
		dir.path(),
	)
	.await;
	assert!(result.is_ok(), "Expected Ok, got: {result:?}");

	let logs = take_logs();
	assert!(
		!logs.iter().any(|(_, m)| m.contains("would be tagged")),
		"Summary should NOT include 'would be tagged' when git is disabled, got: {logs:?}"
	);
}

// ── publish_private_packages (ADR-043) ────────────────────────────────────────

/// A private npm package listed in `publish_private_packages` shows "Would create tag"
/// in dry-run (using the configured tag format) and does NOT show "Would publish ... to"
/// (no registry publish).
#[tokio::test]
async fn publish_private_package_in_list_dry_run_logs_would_create_tag() {
	init_test_logger();
	let _ = take_logs();
	let dir = temp_real_git_repo_with_config(
		PackageManager::Npm,
		GitConfig::enabled_config().with_publish_private_packages(vec!["my-action".to_string()]),
	)
	.await;
	std::fs::write(
		dir.path().join("package.json"),
		r#"{"name": "my-action", "version": "1.2.0", "private": true}"#,
	)
	.unwrap();
	std::fs::write(dir.path().join("CHANGELOG.md"), "# Changelog\n").unwrap();

	let result = run_cursus(
		["cursus", "publish", "--no-interactive", "--dry-run"],
		dir.path(),
	)
	.await;
	assert!(result.is_ok(), "Expected Ok, got: {result:?}");

	let logs = take_logs();
	// Tag is logged by maybe_create_tags using the configured format.
	// Single-package with TagFormat::Auto → "v{version}".
	assert!(
		logs.iter()
			.any(|(_, m)| m.contains("Would create tag") && m.contains("v1.2.0")),
		"Expected 'Would create tag v1.2.0' in logs: {logs:?}"
	);
	// Summary distinguishes private packages from registry-published ones.
	assert!(
		logs.iter().any(|(_, m)| m.contains("private (tag only)")),
		"Expected 'private (tag only)' in summary: {logs:?}"
	);
	assert!(
		!logs
			.iter()
			.any(|(_, m)| m.contains("Would publish my-action")),
		"Private package should NOT show 'Would publish' to a registry: {logs:?}"
	);
}

/// A private package NOT listed in `publish_private_packages` is silently skipped —
/// no per-package mention in logs (preserves ADR-007 behavior).
#[tokio::test]
async fn publish_private_package_not_in_list_silently_skipped() {
	init_test_logger();
	let _ = take_logs();
	let dir =
		temp_real_git_repo_with_config(PackageManager::Npm, GitConfig::enabled_config()).await;
	std::fs::write(
		dir.path().join("package.json"),
		r#"{"name": "my-action", "version": "1.2.0", "private": true}"#,
	)
	.unwrap();
	std::fs::write(dir.path().join("CHANGELOG.md"), "# Changelog\n").unwrap();

	let result = run_cursus(
		["cursus", "publish", "--no-interactive", "--dry-run"],
		dir.path(),
	)
	.await;
	assert!(result.is_ok(), "Expected Ok, got: {result:?}");

	let logs = take_logs();
	assert!(
		!logs
			.iter()
			.any(|(_, m)| m.contains("my-action") && !m.contains("Summary")),
		"Private package not in list should produce no per-package log lines: {logs:?}"
	);
}

/// A private package in the list but missing `CHANGELOG.md` is warned about and
/// skipped — it was never prepared, so there are no release notes to publish.
#[tokio::test]
async fn publish_private_package_in_list_no_changelog_skipped() {
	init_test_logger();
	let _ = take_logs();
	let dir = temp_real_git_repo_with_config(
		PackageManager::Npm,
		GitConfig::enabled_config().with_publish_private_packages(vec!["my-action".to_string()]),
	)
	.await;
	std::fs::write(
		dir.path().join("package.json"),
		r#"{"name": "my-action", "version": "1.2.0", "private": true}"#,
	)
	.unwrap();
	// No CHANGELOG.md — package was never prepared.

	let result = run_cursus(
		["cursus", "publish", "--no-interactive", "--dry-run"],
		dir.path(),
	)
	.await;
	assert!(result.is_ok(), "Expected Ok, got: {result:?}");

	let logs = take_logs();
	assert!(
		logs.iter()
			.any(|(level, m)| *level == log::Level::Warn && m.contains("no CHANGELOG.md found")),
		"Expected CHANGELOG.md warning for unprepared private package: {logs:?}"
	);
	assert!(
		!logs.iter().any(|(_, m)| m.contains("Would create tag")),
		"Should NOT log 'Would create tag' for a package without CHANGELOG.md: {logs:?}"
	);
}

/// A public package named in `publish_private_packages` follows the normal registry
/// publish path — the list only affects packages that are actually private.
#[tokio::test]
async fn publish_public_package_in_private_list_follows_normal_path() {
	init_test_logger();
	let _ = take_logs();
	let dir = temp_real_git_repo_with_config(
		PackageManager::Npm,
		GitConfig::enabled_config().with_publish_private_packages(vec!["my-pkg".to_string()]),
	)
	.await;
	// Public package (no "private": true)
	std::fs::write(
		dir.path().join("package.json"),
		r#"{"name": "my-pkg", "version": "1.0.0"}"#,
	)
	.unwrap();
	std::fs::write(dir.path().join("CHANGELOG.md"), "# Changelog\n").unwrap();

	let result = run_cursus(
		["cursus", "publish", "--no-interactive", "--dry-run"],
		dir.path(),
	)
	.await;
	assert!(result.is_ok(), "Expected Ok, got: {result:?}");

	let logs = take_logs();
	// Public package should show normal "Would publish ... to" message
	assert!(
		logs.iter()
			.any(|(_, m)| m.contains("Would publish") && m.contains("my-pkg")),
		"Public package in list should still show 'Would publish' (registry path): {logs:?}"
	);
	assert!(
		!logs.iter().any(|(_, m)| m.contains("private (tag only)")),
		"Public package should NOT show private (tag only) in summary: {logs:?}"
	);
}

/// The summary line correctly shows private-tagged packages separately from
/// registry-published packages.
#[tokio::test]
async fn publish_private_package_in_list_dry_run_summary_shows_private_note() {
	init_test_logger();
	let _ = take_logs();
	// Workspace: one public, one private-listed.
	// Use temp_real_git_repo_with_config for the git setup, then write npm workspace manually.
	let dir = temp_real_git_repo_with_config(
		PackageManager::Npm,
		GitConfig::enabled_config().with_publish_private_packages(vec!["my-action".to_string()]),
	)
	.await;
	std::fs::write(
		dir.path().join("package.json"),
		r#"{"name": "root", "version": "1.0.0", "private": true, "workspaces": ["packages/*"]}"#,
	)
	.unwrap();
	std::fs::create_dir_all(dir.path().join("packages/my-pkg")).unwrap();
	std::fs::write(
		dir.path().join("packages/my-pkg/package.json"),
		r#"{"name": "my-pkg", "version": "1.0.0"}"#,
	)
	.unwrap();
	std::fs::write(
		dir.path().join("packages/my-pkg/CHANGELOG.md"),
		"# Changelog\n",
	)
	.unwrap();
	std::fs::create_dir_all(dir.path().join("packages/my-action")).unwrap();
	std::fs::write(
		dir.path().join("packages/my-action/package.json"),
		r#"{"name": "my-action", "version": "1.0.0", "private": true}"#,
	)
	.unwrap();
	std::fs::write(
		dir.path().join("packages/my-action/CHANGELOG.md"),
		"# Changelog\n",
	)
	.unwrap();

	let result = run_cursus(
		["cursus", "publish", "--no-interactive", "--dry-run"],
		dir.path(),
	)
	.await;
	assert!(result.is_ok(), "Expected Ok, got: {result:?}");

	let logs = take_logs();
	// Summary: "1 would be published, 1 private (tag only), 0 would be skipped"
	assert!(
		logs.iter()
			.any(|(_, m)| m.contains("1 would be published") && m.contains("1 private (tag only)")),
		"Expected summary with both registry and private counts: {logs:?}"
	);
}