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

Commit

Permalink
fix(rome_cli): Allow to run on read-only file systems
Browse files Browse the repository at this point in the history
  • Loading branch information
realtimetodie committed Nov 29, 2022
1 parent 00f5dfe commit 09d2ed8
Show file tree
Hide file tree
Showing 7 changed files with 257 additions and 3 deletions.
9 changes: 8 additions & 1 deletion crates/rome_cli/src/traversal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -770,7 +770,14 @@ fn process_file(ctx: &TraversalOptions, path: &Path, file_id: FileId) -> FileRes
};
}

let open_options = OpenOptions::default().read(true).write(true);
let write_access = matches!(
ctx.execution.traversal_mode(),
TraversalMode::Check {
fix_file_mode: Some(_),
} | TraversalMode::Format { write: true, .. }
);

let open_options = OpenOptions::default().read(true).write(write_access);
let mut file = ctx
.fs
.open_with_options(path, open_options)
Expand Down
80 changes: 79 additions & 1 deletion crates/rome_cli/tests/commands/check.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use pico_args::Arguments;
use rome_cli::Termination;
use std::env::temp_dir;
use std::ffi::OsString;
use std::fs::{create_dir, create_dir_all, remove_dir_all};
use std::fs::{create_dir, create_dir_all, remove_dir_all, set_permissions, File};
#[cfg(target_family = "unix")]
use std::os::unix::fs::symlink;
#[cfg(target_os = "windows")]
Expand Down Expand Up @@ -85,6 +85,37 @@ fn ok() {
assert!(result.is_ok(), "run_cli returned {result:?}");
}

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

let root_path = temp_dir().join("rome_test_check_ok_read_only");

#[allow(unused_must_use)]
{
remove_dir_all(root_path.clone());
}
create_dir(root_path.clone()).unwrap();

let file_path = root_path.join(Path::new("file.js"));
let file = File::create(file_path.clone()).unwrap();

let metadata = file.metadata().unwrap();
let mut permissions = metadata.permissions();
permissions.set_readonly(true);
set_permissions(file_path.clone(), permissions).unwrap();

let result = run_cli(
DynRef::Owned(Box::new(OsFileSystem)),
DynRef::Borrowed(&mut console),
Arguments::from_vec(vec![OsString::from("check"), file_path.as_os_str().into()]),
);

remove_dir_all(root_path).unwrap();

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

#[test]
fn parse_error() {
let mut fs = MemoryFileSystem::default();
Expand Down Expand Up @@ -793,6 +824,53 @@ fn fs_error_infinite_symlink_exapansion() {
));
}

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

let root_path = temp_dir().join("rome_test_check_error_read_only");

#[allow(unused_must_use)]
{
remove_dir_all(root_path.clone());
}
create_dir(root_path.clone()).unwrap();

let file_path = root_path.join(Path::new("file.js"));
let file = File::create(file_path.clone()).unwrap();

let metadata = file.metadata().unwrap();
let mut permissions = metadata.permissions();
permissions.set_readonly(true);
set_permissions(file_path.clone(), permissions).unwrap();

let result = run_cli(
DynRef::Owned(Box::new(OsFileSystem)),
DynRef::Borrowed(&mut console),
Arguments::from_vec(vec![
OsString::from("check"),
OsString::from("--apply"),
file_path.as_os_str().into(),
]),
);

remove_dir_all(root_path).unwrap();

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

assert_cli_snapshot(SnapshotPayload::new(
module_path!(),
#[cfg(target_family = "unix")]
"fs_error_read_only_unix",
#[cfg(target_os = "windows")]
"fs_error_read_only_windows",
fs,
console,
result,
));
}

#[test]
fn fs_error_unknown() {
let mut fs = MemoryFileSystem::default();
Expand Down
51 changes: 50 additions & 1 deletion crates/rome_cli/tests/commands/format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,11 @@ use crate::{
use pico_args::Arguments;
use rome_cli::Termination;
use rome_console::{markup, BufferConsole, MarkupBuf};
use rome_fs::{FileSystemExt, MemoryFileSystem};
use rome_fs::{FileSystemExt, MemoryFileSystem, OsFileSystem};
use rome_service::DynRef;
use std::env::temp_dir;
use std::ffi::OsString;
use std::fs::{create_dir, remove_dir_all, set_permissions, File};
use std::path::{Path, PathBuf};

// six spaces
Expand Down Expand Up @@ -1158,6 +1160,53 @@ fn does_not_format_ignored_directories() {
));
}

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

let root_path = temp_dir().join("rome_test_format_error_read_only");

#[allow(unused_must_use)]
{
remove_dir_all(root_path.clone());
}
create_dir(root_path.clone()).unwrap();

let file_path = root_path.join(Path::new("file.js"));
let file = File::create(file_path.clone()).unwrap();

let metadata = file.metadata().unwrap();
let mut permissions = metadata.permissions();
permissions.set_readonly(true);
set_permissions(file_path.clone(), permissions).unwrap();

let result = run_cli(
DynRef::Owned(Box::new(OsFileSystem)),
DynRef::Borrowed(&mut console),
Arguments::from_vec(vec![
OsString::from("format"),
OsString::from("--write"),
file_path.as_os_str().into(),
]),
);

remove_dir_all(root_path).unwrap();

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

assert_cli_snapshot(SnapshotPayload::new(
module_path!(),
#[cfg(target_family = "unix")]
"fs_error_read_only_unix",
#[cfg(target_os = "windows")]
"fs_error_read_only_windows",
fs,
console,
result,
));
}

#[test]
fn file_too_large() {
let mut fs = MemoryFileSystem::default();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
---
source: crates/rome_cli/tests/snap_test.rs
assertion_line: 334
expression: content
---
# Termination Message

```block
no files were processed in the specified paths.
```

# Emitted Messages

```block
<TEMP_DIR>/rome_test_check_error_read_only/file.js internalError/io ━━━━━━━━━━━━━━━━━━━━
× Permission denied (os error 13)
```

```block
Fixed 0 file(s) in <TIME>
```

```block
Skipped 1 file(s)
```


Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
---
source: crates/rome_cli/tests/snap_test.rs
assertion_line: 334
expression: content
---
# Termination Message

```block
no files were processed in the specified paths.
```

# Emitted Messages

```block
<TEMP_DIR>/rome_test_check_error_read_only/file.js internalError/io ━━━━━━━━━━━━━━━━━━━━
× Access is denied. (os error 5)
```

```block
Fixed 0 file(s) in <TIME>
```

```block
Skipped 1 file(s)
```


Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
---
source: crates/rome_cli/tests/snap_test.rs
assertion_line: 334
expression: content
---
# Termination Message

```block
no files were processed in the specified paths.
```

# Emitted Messages

```block
<TEMP_DIR>/rome_test_format_error_read_only/file.js internalError/io ━━━━━━━━━━━━━━━━━━━━
× Permission denied (os error 13)
```

```block
Formatted 0 file(s) in <TIME>
```

```block
Skipped 1 file(s)
```


Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
---
source: crates/rome_cli/tests/snap_test.rs
assertion_line: 334
expression: content
---
# Termination Message

```block
no files were processed in the specified paths.
```

# Emitted Messages

```block
<TEMP_DIR>/rome_test_format_error_read_only/file.js internalError/io ━━━━━━━━━━━━━━━━━━━━
× Access is denied. (os error 5)
```

```block
Formatted 0 file(s) in <TIME>
```

```block
Skipped 1 file(s)
```


0 comments on commit 09d2ed8

Please sign in to comment.