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

Commit

Permalink
update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
leops committed Dec 9, 2022
1 parent d55c545 commit fc6fa63
Show file tree
Hide file tree
Showing 5 changed files with 104 additions and 49 deletions.
53 changes: 31 additions & 22 deletions crates/rome_cli/tests/commands/format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1104,17 +1104,26 @@ fn does_not_format_if_files_are_listed_in_ignore_option() {
fn does_not_format_ignored_directories() {
let mut console = BufferConsole::default();
let mut fs = MemoryFileSystem::default();

let file_path = Path::new("rome.json");
fs.insert(
file_path.into(),
CONFIG_FORMATTER_IGNORED_DIRECTORIES.as_bytes(),
);

let ignored_file = Path::new("scripts/test.js");
fs.insert(ignored_file.into(), <&str>::clone(&UNFORMATTED).as_bytes());

let file_to_format = Path::new("src/test.js");
fs.insert(file_to_format.into(), UNFORMATTED.as_bytes());
const FILES: [(&str, bool); 6] = [
("test.js", true),
("test1.js", false),
("test2.js", false),
("test3/test.js", false),
("test4/test.js", true),
("test5/test.js", false),
];

for (file_path, _) in FILES {
let file_path = Path::new(file_path);
fs.insert(file_path.into(), UNFORMATTED.as_bytes());
}

let result = run_cli(
DynRef::Borrowed(&mut fs),
Expand All @@ -1128,26 +1137,26 @@ fn does_not_format_ignored_directories() {

assert!(result.is_ok(), "run_cli returned {result:?}");

let mut file = fs
.open(ignored_file)
.expect("formatting target file was removed by the CLI");
for (file_path, expect_formatted) in FILES {
let mut file = fs
.open(Path::new(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");

assert_eq!(content, UNFORMATTED, "we test the file is not formatted");
drop(file);
let mut file = fs
.open(file_to_format)
.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");

let mut content = String::new();
file.read_to_string(&mut content)
.expect("failed to read file from memory FS");
let expected = if expect_formatted {
FORMATTED
} else {
UNFORMATTED
};

assert_eq!(content, FORMATTED, "we test the file is formatted");
drop(file);
assert_eq!(
content, expected,
"content of {file_path} doesn't match the expected content"
);
}

assert_cli_snapshot(SnapshotPayload::new(
module_path!(),
Expand Down
8 changes: 7 additions & 1 deletion crates/rome_cli/tests/configs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,13 @@ pub const CONFIG_FORMATTER_AND_FILES_IGNORE: &str = r#"{

pub const CONFIG_FORMATTER_IGNORED_DIRECTORIES: &str = r#"{
"formatter": {
"ignore": ["scripts/*"]
"ignore": [
"test1.js",
"./test2.js",
"./test3/**/*",
"/test4/**/*",
"test5/**/*"
]
}
}
"#;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,29 +7,60 @@ expression: content
```json
{
"formatter": {
"ignore": ["scripts/*"]
"ignore": [
"test1.js",
"./test2.js",
"./test3/**/*",
"/test4/**/*",
"test5/**/*"
]
}
}

```

## `scripts/test.js`
## `test.js`

```js
statement();

```

## `test1.js`

```js
statement( )
```

## `test2.js`

```js
statement( )
```

## `src/test.js`
## `test3/test.js`

```js
statement( )
```

## `test4/test.js`

```js
statement();

```

## `test5/test.js`

```js
statement( )
```

# Emitted Messages

```block
Formatted 2 file(s) in <TIME>
Formatted 3 file(s) in <TIME>
```


47 changes: 28 additions & 19 deletions crates/rome_lsp/tests/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,21 @@ use tower_lsp::lsp_types::TextDocumentContentChangeEvent;
use tower_lsp::lsp_types::TextDocumentIdentifier;
use tower_lsp::lsp_types::TextDocumentItem;
use tower_lsp::lsp_types::TextEdit;
use tower_lsp::lsp_types::Url;
use tower_lsp::lsp_types::VersionedTextDocumentIdentifier;
use tower_lsp::lsp_types::WorkDoneProgressParams;
use tower_lsp::LspService;
use tower_lsp::{jsonrpc::Request, lsp_types::InitializeParams};

macro_rules! url {
($path:literal) => {
if cfg!(windows) {
lsp::Url::parse(concat!("file:///z%3A/workspace/", $path)).unwrap()
} else {
lsp::Url::parse(concat!("file:///workspace/", $path)).unwrap()
}
};
}

struct Server {
service: Timeout<LspService<LSPServer>>,
}
Expand Down Expand Up @@ -137,7 +146,7 @@ impl Server {
InitializeParams {
process_id: None,
root_path: None,
root_uri: None,
root_uri: Some(url!("")),
initialization_options: None,
capabilities: ClientCapabilities::default(),
trace: None,
Expand Down Expand Up @@ -182,7 +191,7 @@ impl Server {
"textDocument/didOpen",
DidOpenTextDocumentParams {
text_document: TextDocumentItem {
uri: Url::parse("test://workspace/document.js")?,
uri: url!("document.js"),
language_id: String::from("javascript"),
version: 0,
text: text.to_string(),
Expand All @@ -201,7 +210,7 @@ impl Server {
"textDocument/didChange",
DidChangeTextDocumentParams {
text_document: VersionedTextDocumentIdentifier {
uri: Url::parse("test://workspace/document.js")?,
uri: url!("document.js"),
version,
},
content_changes,
Expand All @@ -215,7 +224,7 @@ impl Server {
"textDocument/didClose",
DidCloseTextDocumentParams {
text_document: TextDocumentIdentifier {
uri: Url::parse("test://workspace/document.js")?,
uri: url!("document.js"),
},
},
)
Expand Down Expand Up @@ -378,7 +387,7 @@ async fn document_lifecycle() -> Result<()> {
"rome/get_syntax_tree",
"get_syntax_tree",
GetSyntaxTreeParams {
path: RomePath::new("/document.js", FileId::zero()),
path: RomePath::new("document.js", FileId::zero()),
},
)
.await?
Expand Down Expand Up @@ -455,7 +464,7 @@ async fn document_no_extension() -> Result<()> {
"textDocument/didOpen",
DidOpenTextDocumentParams {
text_document: TextDocumentItem {
uri: Url::parse("test://workspace/document")?,
uri: url!("document"),
language_id: String::from("javascript"),
version: 0,
text: String::from("statement()"),
Expand All @@ -470,7 +479,7 @@ async fn document_no_extension() -> Result<()> {
"formatting",
DocumentFormattingParams {
text_document: TextDocumentIdentifier {
uri: Url::parse("test://workspace/document")?,
uri: url!("document"),
},
options: FormattingOptions {
tab_size: 4,
Expand All @@ -496,7 +505,7 @@ async fn document_no_extension() -> Result<()> {
"textDocument/didClose",
DidCloseTextDocumentParams {
text_document: TextDocumentIdentifier {
uri: Url::parse("test://workspace/document")?,
uri: url!("document"),
},
},
)
Expand Down Expand Up @@ -534,7 +543,7 @@ async fn pull_diagnostics() -> Result<()> {
notification,
Some(ServerNotification::PublishDiagnostics(
PublishDiagnosticsParams {
uri: Url::parse("test://workspace/document.js")?,
uri: url!("document.js"),
version: Some(0),
diagnostics: vec![lsp::Diagnostic {
range: lsp::Range {
Expand All @@ -558,7 +567,7 @@ async fn pull_diagnostics() -> Result<()> {
),
related_information: Some(vec![lsp::DiagnosticRelatedInformation {
location: lsp::Location {
uri: lsp::Url::parse("test://workspace/document.js")?,
uri: url!("document.js"),
range: lsp::Range {
start: lsp::Position {
line: 0,
Expand Down Expand Up @@ -630,7 +639,7 @@ async fn pull_quick_fixes() -> Result<()> {
"pull_code_actions",
lsp::CodeActionParams {
text_document: lsp::TextDocumentIdentifier {
uri: lsp::Url::parse("test://workspace/document.js")?,
uri: url!("document.js"),
},
range: lsp::Range {
start: lsp::Position {
Expand Down Expand Up @@ -659,7 +668,7 @@ async fn pull_quick_fixes() -> Result<()> {

let mut changes = HashMap::default();
changes.insert(
lsp::Url::parse("test://workspace/document.js")?,
url!("document.js"),
vec![lsp::TextEdit {
range: lsp::Range {
start: lsp::Position {
Expand Down Expand Up @@ -694,7 +703,7 @@ async fn pull_quick_fixes() -> Result<()> {

let mut suppression_changes = HashMap::default();
suppression_changes.insert(
lsp::Url::parse("test://workspace/document.js")?,
url!("document.js"),
vec![lsp::TextEdit {
range: lsp::Range {
start: lsp::Position {
Expand Down Expand Up @@ -762,7 +771,7 @@ async fn pull_refactors() -> Result<()> {
"pull_code_actions",
lsp::CodeActionParams {
text_document: lsp::TextDocumentIdentifier {
uri: lsp::Url::parse("test://workspace/document.js")?,
uri: url!("document.js"),
},
range: lsp::Range {
start: lsp::Position {
Expand Down Expand Up @@ -792,7 +801,7 @@ async fn pull_refactors() -> Result<()> {
let mut changes = HashMap::default();

changes.insert(
lsp::Url::parse("test://workspace/document.js")?,
url!("document.js"),
vec![
lsp::TextEdit {
range: lsp::Range {
Expand Down Expand Up @@ -873,7 +882,7 @@ async fn pull_fix_all() -> Result<()> {
"pull_code_actions",
lsp::CodeActionParams {
text_document: lsp::TextDocumentIdentifier {
uri: lsp::Url::parse("test://workspace/document.js")?,
uri: url!("document.js"),
},
range: lsp::Range {
start: lsp::Position {
Expand Down Expand Up @@ -907,7 +916,7 @@ async fn pull_fix_all() -> Result<()> {
let mut changes = HashMap::default();

changes.insert(
lsp::Url::parse("test://workspace/document.js")?,
url!("document.js"),
vec![lsp::TextEdit {
range: lsp::Range {
start: lsp::Position {
Expand Down Expand Up @@ -973,7 +982,7 @@ async fn format_with_syntax_errors() -> Result<()> {
"formatting",
DocumentFormattingParams {
text_document: TextDocumentIdentifier {
uri: Url::parse("test://workspace/document.js")?,
uri: url!("document.js"),
},
options: FormattingOptions {
tab_size: 4,
Expand Down
6 changes: 3 additions & 3 deletions crates/rome_service/src/matcher/pattern.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,8 +125,9 @@ impl Pattern {
// A pattern is relative if it starts with "." followed by a separator
let is_relative = matches!(chars.get(..2), Some(['.', sep]) if path::is_separator(*sep));
if is_relative {
// If a pattern starts with a relative prefix, strip it from the pattern
// If a pattern starts with a relative prefix, strip it from the pattern and replace it with "**"
i += 2;
tokens.push(AnyRecursiveSequence);
} else {
// A pattern is absolute if it starts with a path separator
let mut is_absolute = chars.first().map_or(false, |c| path::is_separator(*c));
Expand All @@ -136,10 +137,9 @@ impl Pattern {
is_absolute = matches!(chars.get(..3), Some(['a'..='z' | 'A'..='Z', ':', sep]) if path::is_separator(*sep));
}

// If a pattern is not absolute, insert a "**/" sequence in front
// If a pattern is not absolute, insert a "**" sequence in front
if !is_absolute {
tokens.push(AnyRecursiveSequence);
tokens.push(Char('/'));
}
}

Expand Down

0 comments on commit fc6fa63

Please sign in to comment.