Skip to content
This repository has been archived by the owner on Aug 31, 2023. It is now read-only.

Commit

Permalink
feat(rome_service): add $schema to configuration (#3738)
Browse files Browse the repository at this point in the history
  • Loading branch information
ematipico authored Nov 21, 2022
1 parent f17ace3 commit 3bcf54c
Show file tree
Hide file tree
Showing 19 changed files with 2,968 additions and 55 deletions.
3 changes: 3 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ crates/rome_js_analyze/src/registry.rs linguist-generated=true text=auto eol=lf
crates/rome_service/src/configuration/linter/rules.rs linguist-generated=true text=auto eol=lf
npm/backend-jsonrpc/src/workspace.ts linguist-generated=true text=auto eol=lf
website/src/docs/lint/rules/**/*.md linguist-generated=true text=auto eol=lf
npm/rome/configuration_schema.json linguist-generated=true text=auto eol=lf
editors/vscode/configuration_schema.json linguist-generated=true text=auto eol=lf


crates/rome_js_formatter/tests/**/*.ts.prettier-snap linguist-language=TypeScript
Expand All @@ -20,3 +22,4 @@ crates/rome_js_formatter/tests/**/*.js.snap linguist-language=Markdown
crates/rome_cli/tests/**/*.snap linguist-language=Markdown
crates/rome_js_analyze/tests/specs/**/*.snap linguist-language=Markdown


1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

83 changes: 83 additions & 0 deletions crates/rome_cli/tests/commands/init.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
use crate::configs::{CONFIG_INIT_DEFAULT, CONFIG_INIT_DEFAULT_WHEN_INSTALLED};
use crate::run_cli;
use crate::snap_test::{assert_cli_snapshot, SnapshotPayload};
use pico_args::Arguments;
use rome_console::BufferConsole;
use rome_fs::{FileSystemExt, MemoryFileSystem};
use rome_service::DynRef;
use std::ffi::OsString;
use std::path::Path;

#[test]
fn creates_config_file() {
let mut fs = MemoryFileSystem::default();
let mut console = BufferConsole::default();

let result = run_cli(
DynRef::Borrowed(&mut fs),
DynRef::Borrowed(&mut console),
Arguments::from_vec(vec![OsString::from("init")]),
);
assert!(result.is_ok(), "run_cli returned {result:?}");

let file_path = Path::new("rome.json");

let mut file = fs
.open(file_path)
.expect("configuration file was not written on disk");

let mut content = String::new();
file.read_to_string(&mut content)
.expect("failed to read file from memory FS");
assert_eq!(content, CONFIG_INIT_DEFAULT);

drop(file);

assert_cli_snapshot(SnapshotPayload::new(
module_path!(),
"creates_config_file",
fs,
console,
result,
));
}

#[test]
fn creates_config_file_when_rome_installed_via_package_manager() {
let mut fs = MemoryFileSystem::default();
let mut console = BufferConsole::default();

let file_path = Path::new("./")
.join("node_modules")
.join("rome")
.join("configuration_schema.json");
fs.insert(file_path, *b"{}");

let result = run_cli(
DynRef::Borrowed(&mut fs),
DynRef::Borrowed(&mut console),
Arguments::from_vec(vec![OsString::from("init")]),
);
assert!(result.is_ok(), "run_cli returned {result:?}");

let file_path = Path::new("rome.json");

let mut file = fs
.open(file_path)
.expect("configuration file was not written on disk");

let mut content = String::new();
file.read_to_string(&mut content)
.expect("failed to read file from memory FS");
assert_eq!(content, CONFIG_INIT_DEFAULT_WHEN_INSTALLED);

drop(file);

assert_cli_snapshot(SnapshotPayload::new(
module_path!(),
"creates_config_file_when_rome_installed_via_package_manager",
fs,
console,
result,
));
}
1 change: 1 addition & 0 deletions crates/rome_cli/tests/commands/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
mod check;
mod ci;
mod format;
mod init;
mod rage;
mod version;
10 changes: 10 additions & 0 deletions crates/rome_cli/tests/configs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,16 @@ pub const CONFIG_INIT_DEFAULT: &str = r#"{
}
}"#;

pub const CONFIG_INIT_DEFAULT_WHEN_INSTALLED: &str = r#"{
"$schema": "./node_modules/rome/configuration_schema.json",
"linter": {
"enabled": true,
"rules": {
"recommended": true
}
}
}"#;

pub const CONFIG_DISABLED_FORMATTER: &str = r#"{
"formatter": {
"enabled": false
Expand Down
46 changes: 0 additions & 46 deletions crates/rome_cli/tests/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -217,52 +217,6 @@ mod main {
}
}

mod init {
use super::*;
use crate::configs::CONFIG_INIT_DEFAULT;
use crate::snap_test::SnapshotPayload;
use pico_args::Arguments;
use rome_console::BufferConsole;
use rome_fs::{FileSystemExt, MemoryFileSystem};
use rome_service::DynRef;
use std::ffi::OsString;
use std::path::Path;

#[test]
fn creates_config_file() {
let mut fs = MemoryFileSystem::default();
let mut console = BufferConsole::default();

let result = run_cli(
DynRef::Borrowed(&mut fs),
DynRef::Borrowed(&mut console),
Arguments::from_vec(vec![OsString::from("init")]),
);
assert!(result.is_ok(), "run_cli returned {result:?}");

let file_path = Path::new("rome.json");

let mut file = fs
.open(file_path)
.expect("configuration file was not written on disk");

let mut content = String::new();
file.read_to_string(&mut content)
.expect("failed to read file from memory FS");
assert_eq!(content, CONFIG_INIT_DEFAULT);

drop(file);

assert_cli_snapshot(SnapshotPayload::new(
module_path!(),
"creates_config_file",
fs,
console,
result,
));
}
}

mod configuration {
use super::*;
use crate::configs::{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
---
source: crates/rome_cli/tests/snap_test.rs
expression: content
---
## `rome.json`

```json
{
"$schema": "./node_modules/rome/configuration_schema.json",
"linter": {
"enabled": true,
"rules": {
"recommended": true
}
}
}
```

## `./node_modules/rome/configuration_schema.json`

```json
{}
```

# Emitted Messages

```block
Welcome to Rome! Let's get you started...
Files created ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
- rome.json: Your project configuration. Documentation: https://rome.tools/configuration
Next Steps ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
1. Setup an editor extension
Get live errors as you type and format when you save. Learn more: https://rome.tools/editors
2. Try a command
rome ci checks for lint errors and verifies formatting. Run rome --help for a full list of commands and options.
3. Read the documentation
Our website serves as a comprehensive source of guides and documentation: https://docs.rome.tools
4. Get involved in the community
Ask questions, get support, or contribute by participating on GitHub (https://github.com/rome/tools),
or join our community Discord (https://discord.gg/rome)
```


2 changes: 2 additions & 0 deletions crates/rome_fs/src/fs/memory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,8 @@ impl FileSystem for MemoryFileSystem {
self.open(path)
} else if options.create_new || options.write {
self.create(path)
} else if options.read {
self.read(path)
} else {
unimplemented!("the set of open options provided don't match any case")
}
Expand Down
Loading

0 comments on commit 3bcf54c

Please sign in to comment.