monochange 0.8.0

Manage versions and releases for your multiplatform, multilanguage monorepo
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
#![allow(clippy::disallowed_methods)]
use std::fs;
use std::path::PathBuf;

use super::*;
use crate::cli::build_lint_subcommand;

fn lint_settings_root() -> PathBuf {
	monochange_test_helpers::fixture_path!("config/lint-settings")
}

fn clean_lint_workspace() -> tempfile::TempDir {
	monochange_test_helpers::setup_scenario_workspace!("lint-check/clean")
}

fn readonly_fix_workspace() -> tempfile::TempDir {
	monochange_test_helpers::setup_scenario_workspace!("lint-check/read-only-fix")
}

#[test]
fn test_format_check_report_empty() {
	let report = LintReport::new();
	let output = format_check_report(&report, false, false);
	assert!(output.contains("no issues found"));
}

#[test]
fn test_format_check_report_with_issues() {
	let mut report = LintReport::new();
	report.add(monochange_core::lint::LintResult::new(
		"test/rule",
		monochange_core::lint::LintLocation::new("test.toml", 1, 1),
		"Test error",
		LintSeverity::Error,
	));
	report.add(monochange_core::lint::LintResult::new(
		"test/rule",
		monochange_core::lint::LintLocation::new("test.toml", 2, 1),
		"Test warning",
		LintSeverity::Warning,
	));

	let output = format_check_report(&report, false, true);
	assert!(output.contains("1 errors, 1 warnings"));
	assert!(output.contains("✗ **test/rule** at 1:1"));
	assert!(output.contains("Test error"));
	assert!(output.contains("Test warning"));
	assert!(output.contains("severity: error"));
}

#[test]
fn render_lint_catalog_lists_rules_and_presets() {
	let text = render_lint_catalog(OutputFormat::Text).unwrap();
	assert!(text.contains("cargo/internal-dependency-workspace"));
	assert!(text.contains("cargo/baseline"));
	assert!(text.contains("cargo/recommended"));
	assert!(text.contains("dart/required-package-fields"));
	assert!(text.contains("dart/sdk-constraint-present"));
	assert!(text.contains("dart/internal-path-dependency-policy"));
	assert!(text.contains("dart/flutter-package-metadata-consistent"));
	assert!(text.contains("dart/baseline"));
	assert!(text.contains("dart/recommended"));
	assert!(text.contains("dart/strict"));

	let json = render_lint_catalog(OutputFormat::Json).unwrap();
	assert!(json.contains("\"rules\""));
	assert!(json.contains("\"presets\""));
}

#[test]
fn render_lint_explanation_supports_rules_and_presets() {
	let rule =
		render_lint_explanation("cargo/internal-dependency-workspace", OutputFormat::Text).unwrap();
	assert!(rule.contains("Internal dependency workspace"));

	let baseline = render_lint_explanation("cargo/baseline", OutputFormat::Text).unwrap();
	assert!(baseline.contains("Cargo baseline"));
	assert!(baseline.contains("cargo/required-package-fields"));

	let preset = render_lint_explanation("cargo/recommended", OutputFormat::Text).unwrap();
	assert!(preset.contains("Cargo recommended"));
	assert!(preset.contains("cargo/sorted-dependencies"));
}

#[test]
fn generated_config_defaults_to_baseline_lints() {
	let template = include_str!("../monochange.toml.template");
	assert!(template.contains("use = [\"cargo/baseline\", \"npm/baseline\", \"dart/baseline\"]"));
	assert!(template.contains("without turning formatting preferences into blocking errors"));
}

#[test]
fn render_lint_explanation_rejects_unknown_ids() {
	let error = render_lint_explanation("unknown/rule", OutputFormat::Text)
		.expect_err("expected unknown lint explanation error");
	assert!(error.to_string().contains("unknown lint rule or preset"));
}

#[test]
fn render_lint_explanation_supports_json() {
	let rule =
		render_lint_explanation("cargo/internal-dependency-workspace", OutputFormat::Json).unwrap();
	assert!(rule.contains("\"cargo/internal-dependency-workspace\""));

	let preset = render_lint_explanation("cargo/recommended", OutputFormat::Json).unwrap();
	assert!(preset.contains("\"cargo/recommended\""));
}

#[test]
fn run_check_command_supports_text_json_and_fix_error_paths() {
	let clean_workspace = clean_lint_workspace();
	let json = run_check_command(
		clean_workspace.path(),
		false,
		&[],
		&[],
		OutputFormat::Json,
		false,
	)
	.unwrap();
	assert!(json.contains("\"error_count\": 0"));

	let text = run_check_command(
		clean_workspace.path(),
		false,
		&[],
		&[],
		OutputFormat::Text,
		false,
	)
	.unwrap();
	assert!(text.contains("workspace validation passed"));

	let tempdir = readonly_fix_workspace();
	let cargo_toml = tempdir.path().join("crates/example/Cargo.toml");
	let mut permissions = fs::metadata(&cargo_toml).unwrap().permissions();
	permissions.set_readonly(true);
	fs::set_permissions(&cargo_toml, permissions).unwrap();
	let error = run_check_command(tempdir.path(), true, &[], &[], OutputFormat::Text, false)
		.expect_err("expected fix write to fail for readonly manifest");
	assert!(error.to_string().contains("Failed to write fixed content"));
}

#[test]
fn run_check_command_reports_all_validation_errors_before_failing() {
	let tempdir = tempfile::tempdir()
		.unwrap_or_else(|error| panic!("expected tempdir to be created: {error}"));
	let root = tempdir.path();
	let crate_dir = root.join("crates/core");
	fs::create_dir_all(&crate_dir)
		.unwrap_or_else(|error| panic!("expected crate dir to be created: {error}"));
	fs::create_dir_all(root.join(".changeset"))
		.unwrap_or_else(|error| panic!("expected changeset dir to be created: {error}"));
	fs::write(
		root.join("monochange.toml"),
		"[package.core]\n\
		path = \"crates/core\"\n\
		type = \"cargo\"\n\
		versioned_files = [\n\
		  { path = \"missing.toml\", type = \"cargo\" },\n\
		  { path = \"missing/*.toml\", type = \"cargo\" },\n\
		]\n",
	)
	.unwrap_or_else(|error| panic!("expected config to be written: {error}"));
	fs::write(
		crate_dir.join("Cargo.toml"),
		"[package]\n\
		name = \"core\"\n\
		version = \"1.0.0\"\n\
		edition = \"2021\"\n\
		description = \"Test core package\"\n\
		license = \"MIT\"\n\
		repository = \"https://github.com/monochange/monochange\"\n",
	)
	.unwrap_or_else(|error| panic!("expected manifest to be written: {error}"));
	fs::write(
		root.join(".changeset/broken.md"),
		"---\nmissing: patch\n---\n\n# Broken target\n",
	)
	.unwrap_or_else(|error| panic!("expected changeset to be written: {error}"));

	let (warnings, errors) = collect_workspace_validation_issues(
		root,
		&monochange_config::load_workspace_configuration(root).unwrap(),
	);
	assert!(warnings.is_empty());
	assert!(
		errors
			.iter()
			.any(|error| error.contains("unknown package or group `missing`"))
	);
	assert!(
		errors
			.iter()
			.any(|error| error.contains("versioned file") && error.contains("missing.toml"))
	);

	let warning_tempdir = tempfile::tempdir()
		.unwrap_or_else(|error| panic!("expected warning tempdir to be created: {error}"));
	let warning_root = warning_tempdir.path();
	let warning_crate_dir = warning_root.join("crates/core");
	fs::create_dir_all(&warning_crate_dir)
		.unwrap_or_else(|error| panic!("expected warning crate dir to be created: {error}"));
	fs::write(
		warning_root.join("monochange.toml"),
		"[package.core]\n\
		path = \"crates/core\"\n\
		type = \"cargo\"\n\
		versioned_files = [{ path = \"missing/*.toml\", type = \"cargo\" }]\n",
	)
	.unwrap_or_else(|error| panic!("expected warning config to be written: {error}"));
	fs::write(
		warning_crate_dir.join("Cargo.toml"),
		"[package]\n\
		name = \"core\"\n\
		version = \"1.0.0\"\n\
		edition = \"2021\"\n\
		description = \"Test core package\"\n\
		license = \"MIT\"\n\
		repository = \"https://github.com/monochange/monochange\"\n",
	)
	.unwrap_or_else(|error| panic!("expected warning manifest to be written: {error}"));
	let warning_text = run_check_command(warning_root, false, &[], &[], OutputFormat::Text, false)
		.unwrap_or_else(|error| panic!("expected warning-only check to pass: {error}"));
	assert!(warning_text.contains("warning:"));
	assert!(warning_text.contains("matches no files"));

	let text_error = run_check_command(root, false, &[], &[], OutputFormat::Text, false)
		.expect_err("expected text check to fail validation");
	let text_message = text_error.to_string();
	assert!(text_message.contains("workspace validation failed"));
	assert!(text_message.contains("unknown package or group `missing`"));
	assert!(text_message.contains("missing.toml"));

	let json_error = run_check_command(root, false, &[], &[], OutputFormat::Json, false)
		.expect_err("expected json check to fail validation");
	let json_message = json_error.to_string();
	assert!(json_message.contains("check failed"));
	assert!(json_message.contains("workspace validation failed"));
}

#[test]
fn run_check_command_applies_fixes_and_reports_them() {
	let tempdir = readonly_fix_workspace();
	let output = run_check_command(tempdir.path(), true, &[], &[], OutputFormat::Text, false)
		.unwrap_or_else(|error| panic!("expected fixable lint workspace to succeed: {error}"));
	assert!(output.contains("Fixed all auto-fixable issues."));

	let manifest = fs::read_to_string(tempdir.path().join("crates/example/Cargo.toml"))
		.unwrap_or_else(|error| panic!("expected fixed manifest to be readable: {error}"));
	assert!(manifest.contains("publish = false"));
}

#[test]
fn run_check_command_applies_fixes_without_progress_reporter() {
	let tempdir = readonly_fix_workspace();
	let result = run_check_command(tempdir.path(), true, &[], &[], OutputFormat::Json, false);
	assert!(
		result.is_ok(),
		"expected fixable lint workspace to succeed without a reporter: {result:?}"
	);

	let manifest = fs::read_to_string(tempdir.path().join("crates/example/Cargo.toml"))
		.unwrap_or_else(|error| panic!("expected fixed manifest to be readable: {error}"));
	assert!(manifest.contains("publish = false"));
}

#[test]
fn run_lint_step_reports_successful_check_output() {
	let clean_workspace = clean_lint_workspace();
	let (output, has_errors) = run_lint_step(clean_workspace.path(), false)
		.unwrap_or_else(|error| panic!("expected lint step to succeed: {error}"));
	assert!(!has_errors);
	assert!(output.contains("no issues found"));
}

#[test]
fn run_lint_step_supports_fix_write_failures() {
	let tempdir = readonly_fix_workspace();
	let cargo_toml = tempdir.path().join("crates/example/Cargo.toml");
	let mut permissions = fs::metadata(&cargo_toml).unwrap().permissions();
	permissions.set_readonly(true);
	fs::set_permissions(&cargo_toml, permissions).unwrap();
	let error =
		run_lint_step(tempdir.path(), true).expect_err("expected lint-step fix write failure");
	assert!(error.to_string().contains("Failed to write fixed content"));
}

#[test]
fn handle_lint_subcommand_dispatches_supported_commands() {
	let root = lint_settings_root();
	let list_matches = build_lint_subcommand()
		.try_get_matches_from(["lint", "list"])
		.unwrap();
	let list_output = handle_lint_subcommand(&root, &list_matches).unwrap();
	assert!(list_output.contains("Rules:"));

	let explain_matches = build_lint_subcommand()
		.try_get_matches_from(["lint", "explain", "cargo/recommended", "--format=json"])
		.unwrap();
	let explain_output = handle_lint_subcommand(&root, &explain_matches).unwrap();
	assert!(explain_output.contains("\"cargo/recommended\""));
}

#[test]
fn scaffold_lint_rule_validates_ids_and_creates_expected_files() {
	let error = scaffold_lint_rule(Path::new("."), "missing-dash")
		.expect_err("expected invalid lint id to fail");
	assert!(error.to_string().contains("<ecosystem>/<rule-name>"));

	let tempdir =
		monochange_test_helpers::setup_scenario_workspace!("test-support/scenario-workspace");
	let message = scaffold_lint_rule(tempdir.path(), "cargo/no-path-dependencies").unwrap();
	assert!(message.contains("no_path_dependencies.rs"));
	assert!(
		tempdir
			.path()
			.join("crates/monochange_cargo/src/lints/no_path_dependencies.rs")
			.exists()
	);
	assert!(
		tempdir
			.path()
			.join("fixtures/tests/lints/cargo/no-path-dependencies/workspace/README.md")
			.exists()
	);

	let odd_message = scaffold_lint_rule(tempdir.path(), "cargo/-foo").unwrap();
	assert!(odd_message.contains("_foo.rs"));
	let odd_file = tempdir
		.path()
		.join("crates/monochange_cargo/src/lints/_foo.rs");
	let odd_contents = fs::read_to_string(odd_file).unwrap();
	assert!(odd_contents.contains("pub FooRule"));
	assert!(odd_contents.contains("Keep `declare_lint_rule!`"));

	let dart_message = scaffold_lint_rule(tempdir.path(), "dart/sdk-constraint-present").unwrap();
	assert!(dart_message.contains("sdk_constraint_present.rs"));
	assert!(
		tempdir
			.path()
			.join("crates/monochange_dart/src/lints/sdk_constraint_present.rs")
			.exists()
	);
}

#[test]
fn scaffold_lint_rule_reports_directory_and_file_conflicts() {
	let tempdir = tempfile::tempdir().unwrap();
	let lint_dir = tempdir.path().join("crates/monochange_cargo/src/lints");
	fs::create_dir_all(lint_dir.parent().unwrap()).unwrap();
	fs::write(&lint_dir, "not a directory").unwrap();
	let error = scaffold_lint_rule(tempdir.path(), "cargo/no-path-dependencies")
		.expect_err("expected lint-dir creation to fail");
	assert!(
		error
			.to_string()
			.contains("failed to create lint directory")
	);

	let tempdir = tempfile::tempdir().unwrap();
	fs::create_dir_all(tempdir.path().join("crates/monochange_cargo/src/lints")).unwrap();
	fs::create_dir_all(
		tempdir
			.path()
			.join("fixtures/tests/lints/cargo/no-path-dependencies/workspace"),
	)
	.unwrap();
	fs::write(
		tempdir
			.path()
			.join("crates/monochange_cargo/src/lints/no_path_dependencies.rs"),
		"existing",
	)
	.unwrap();
	let error = scaffold_lint_rule(tempdir.path(), "cargo/no-path-dependencies")
		.expect_err("expected existing lint file to fail");
	assert!(error.to_string().contains("already exists"));

	let tempdir = tempfile::tempdir().unwrap();
	fs::create_dir_all(tempdir.path().join("crates/monochange_cargo/src/lints")).unwrap();
	fs::create_dir_all(
		tempdir
			.path()
			.join("fixtures/tests/lints/cargo/no-path-dependencies"),
	)
	.unwrap();
	fs::write(
		tempdir
			.path()
			.join("fixtures/tests/lints/cargo/no-path-dependencies/workspace"),
		"not a directory",
	)
	.unwrap();
	let error = scaffold_lint_rule(tempdir.path(), "cargo/no-path-dependencies")
		.expect_err("expected fixture-dir creation to fail");
	assert!(
		error
			.to_string()
			.contains("failed to create lint fixture directory")
	);
}

#[test]
fn scaffold_lint_rule_reports_write_failures() {
	let tempdir = tempfile::tempdir().unwrap();
	let lint_dir = tempdir.path().join("crates/monochange_cargo/src/lints");
	fs::create_dir_all(&lint_dir).unwrap();
	let mut permissions = fs::metadata(&lint_dir).unwrap().permissions();
	permissions.set_readonly(true);
	fs::set_permissions(&lint_dir, permissions).unwrap();
	let error = scaffold_lint_rule(tempdir.path(), "cargo/no-path-dependencies")
		.expect_err("expected lint file write to fail");
	assert!(!error.to_string().is_empty());

	let tempdir = tempfile::tempdir().unwrap();
	let lint_dir = tempdir.path().join("crates/monochange_cargo/src/lints");
	let fixture_dir = tempdir
		.path()
		.join("fixtures/tests/lints/cargo/no-path-dependencies/workspace");
	fs::create_dir_all(&lint_dir).unwrap();
	fs::create_dir_all(&fixture_dir).unwrap();
	let mut permissions = fs::metadata(&fixture_dir).unwrap().permissions();
	permissions.set_readonly(true);
	fs::set_permissions(&fixture_dir, permissions).unwrap();
	let error = scaffold_lint_rule(tempdir.path(), "cargo/no-path-dependencies")
		.expect_err("expected fixture note write to fail");
	assert!(!error.to_string().is_empty());
}