test(rome_cli): trailing comma cli tests
MODIFY
crates/rome_cli/tests/commands/format.rs
crates/rome_cli/tests/commands/format.rs
@@ -29,6 +29,21 @@ let b = {
const APPLY_QUOTE_STYLE_AFTER: &str = "let a = 'something';
let b = {\n\t'hey': 'hello',\n};\n";
+const APPLY_TRAILING_COMMA_BEFORE: &str = r#"
+const a = [
+ longlonglonglongItem1longlonglonglongItem1,
+ longlonglonglongItem1longlonglonglongItem2,
+ longlonglonglongItem1longlonglonglongItem3,
+];
+"#;
+
+const APPLY_TRAILING_COMMA_AFTER: &str = r#"const a = [
+ longlonglonglongItem1longlonglonglongItem1,
+ longlonglonglongItem1longlonglonglongItem2,
+ longlonglonglongItem1longlonglonglongItem3
+];
+"#;
+
const CUSTOM_CONFIGURATION_BEFORE: &str = r#"function f() {
return { a, b }
}"#;
@@ -398,6 +413,79 @@ fn applies_custom_quote_style() {
));
}
+#[test]
+fn applies_custom_trailing_comma() {
+ let mut fs = MemoryFileSystem::default();
+ let mut console = BufferConsole::default();
+
+ let file_path = Path::new("file.js");
+ fs.insert(file_path.into(), APPLY_TRAILING_COMMA_BEFORE.as_bytes());
+
+ let result = run_cli(
+ DynRef::Borrowed(&mut fs),
+ DynRef::Borrowed(&mut console),
+ Arguments::from_vec(vec![
+ OsString::from("format"),
+ OsString::from("--trailing-comma"),
+ OsString::from("none"),
+ OsString::from("--write"),
+ file_path.as_os_str().into(),
+ ]),
+ );
+
+ assert!(result.is_ok(), "run_cli returned {result:?}");
+
+ let mut file = fs
+ .open(file_path)
+ .expect("formatting target file was removed by the CLI");
+
+ let mut content = String::new();
+ file.read_to_string(&mut content)
+ .expect("failed to read file from memory FS");
+
+ dbg!(&content);
+ assert_eq!(content, APPLY_TRAILING_COMMA_AFTER);
+
+ drop(file);
+ assert_cli_snapshot(SnapshotPayload::new(
+ module_path!(),
+ "applies_custom_trailing_comma",
+ fs,
+ console,
+ result,
+ ));
+}
+
+#[test]
+fn trailing_comma_parse_errors() {
+ let mut console = BufferConsole::default();
+ let mut fs = MemoryFileSystem::default();
+
+ let result = run_cli(
+ DynRef::Borrowed(&mut fs),
+ DynRef::Borrowed(&mut console),
+ Arguments::from_vec(vec![
+ OsString::from("format"),
+ OsString::from("--trailing-comma"),
+ OsString::from("NONE"),
+ OsString::from("file.js"),
+ ]),
+ );
+
+ match result {
+ Err(Termination::ParseError { argument, .. }) => assert_eq!(argument, "--trailing-comma"),
+ _ => panic!("run_cli returned {result:?} for an invalid argument value, expected an error"),
+ }
+
+ assert_cli_snapshot(SnapshotPayload::new(
+ module_path!(),
+ "trailing_comma_parse_errors",
+ fs,
+ console,
+ result,
+ ));
+}
+
#[test]
fn indent_style_parse_errors() {
let mut console = BufferConsole::default();

ADD
crates/rome_cli/tests/snapshots/main_commands_format/applies_custom_trailing_comma.snap
+---
+source: crates/rome_cli/tests/snap_test.rs
+expression: content
+---
+## `file.js`
+
+```js
+const a = [
+ longlonglonglongItem1longlonglonglongItem1,
+ longlonglonglongItem1longlonglonglongItem2,
+ longlonglonglongItem1longlonglonglongItem3
+];
+
+```
+
+# Emitted Messages
+
+

ADD
crates/rome_cli/tests/snapshots/main_commands_format/trailing_comma_parse_errors.snap
+---
+source: crates/rome_cli/tests/snap_test.rs
+expression: content
+---
+# Termination Message
+
+```block
+failed to parse argument '--trailing-comma': failed to parse 'NONE': Value not supported for TrailingComma
+```
+
+

test(rome_cli): trailing comma cli tests (#3479)