diff --git a/Cargo.lock b/Cargo.lock index 7700d6e311..09f404f9c1 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -984,6 +984,15 @@ dependencies = [ "windows-sys", ] +[[package]] +name = "is_executable" +version = "1.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fa9acdc6d67b75e626ad644734e8bc6df893d9cd2a834129065d3dd6158ea9c8" +dependencies = [ + "winapi", +] + [[package]] name = "itertools" version = "0.10.5" @@ -1847,6 +1856,7 @@ dependencies = [ "ignore", "imperative", "insta", + "is_executable", "itertools", "js-sys", "libcst", diff --git a/Cargo.toml b/Cargo.toml index 0a83f5cfd1..9afed6291a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -61,7 +61,7 @@ strum_macros = { version = "0.24.3" } textwrap = { version = "0.16.0" } thiserror = { version = "1.0" } titlecase = { version = "2.2.1" } -toml = { version = "0.6.0", features= ["parse"] } +toml = { version = "0.6.0" } # https://docs.rs/getrandom/0.2.7/getrandom/#webassembly-support # For (future) wasm-pack support @@ -73,6 +73,9 @@ serde-wasm-bindgen = { version = "0.4" } js-sys = { version = "0.3.60" } wasm-bindgen = { version = "0.2.83" } +[target.'cfg(not(target_family = "wasm"))'.dependencies] +is_executable = "1.0.1" + [dev-dependencies] insta = { version = "1.19.1", features = ["yaml"] } test-case = { version = "2.2.2" } diff --git a/flake8_to_ruff/Cargo.toml b/flake8_to_ruff/Cargo.toml index 6509657784..eceaaa033e 100644 --- a/flake8_to_ruff/Cargo.toml +++ b/flake8_to_ruff/Cargo.toml @@ -16,7 +16,7 @@ serde = { version = "1.0.147", features = ["derive"] } serde_json = { version = "1.0.87" } strum = { version = "0.24.1", features = ["strum_macros"] } strum_macros = { version = "0.24.3" } -toml = { version = "0.6.0", features = ["parse"] } +toml = { version = "0.6.0" } [dev-dependencies] diff --git a/src/rules/flake8_executable/rules/shebang_missing.rs b/src/rules/flake8_executable/rules/shebang_missing.rs index 5233085274..4e2f24f578 100644 --- a/src/rules/flake8_executable/rules/shebang_missing.rs +++ b/src/rules/flake8_executable/rules/shebang_missing.rs @@ -1,7 +1,7 @@ -#[cfg(not(target_family = "wasm"))] -use std::os::unix::prelude::MetadataExt; use std::path::Path; +#[cfg(not(target_family = "wasm"))] +use is_executable::IsExecutable; use ruff_macros::derive_message_formats; #[cfg(not(target_family = "wasm"))] @@ -23,14 +23,9 @@ impl Violation for ShebangMissingExecutableFile { /// EXE002 #[cfg(not(target_family = "wasm"))] pub fn shebang_missing(filepath: &Path) -> Option { - if let Ok(metadata) = filepath.metadata() { - // Check if file is executable by anyone - if metadata.mode() & 0o111 == 0 { - None - } else { - let diagnostic = Diagnostic::new(ShebangMissingExecutableFile, Range::default()); - Some(diagnostic) - } + if filepath.is_executable() { + let diagnostic = Diagnostic::new(ShebangMissingExecutableFile, Range::default()); + Some(diagnostic) } else { None } diff --git a/src/rules/flake8_executable/rules/shebang_not_executable.rs b/src/rules/flake8_executable/rules/shebang_not_executable.rs index 85e5976bbb..1381c49ac2 100644 --- a/src/rules/flake8_executable/rules/shebang_not_executable.rs +++ b/src/rules/flake8_executable/rules/shebang_not_executable.rs @@ -1,7 +1,7 @@ -#[cfg(not(target_family = "wasm"))] -use std::os::unix::prelude::MetadataExt; use std::path::Path; +#[cfg(not(target_family = "wasm"))] +use is_executable::IsExecutable; use ruff_macros::derive_message_formats; #[cfg(not(target_family = "wasm"))] use rustpython_ast::Location; @@ -31,22 +31,17 @@ pub fn shebang_not_executable( shebang: &ShebangDirective, ) -> Option { if let ShebangDirective::Match(_, start, end, _) = shebang { - if let Ok(metadata) = filepath.metadata() { - // Check if file is executable by anyone - if metadata.mode() & 0o111 == 0 { - let diagnostic = Diagnostic::new( - ShebangNotExecutable, - Range::new( - Location::new(lineno + 1, *start), - Location::new(lineno + 1, *end), - ), - ); - Some(diagnostic) - } else { - None - } - } else { + if filepath.is_executable() { None + } else { + let diagnostic = Diagnostic::new( + ShebangNotExecutable, + Range::new( + Location::new(lineno + 1, *start), + Location::new(lineno + 1, *end), + ), + ); + Some(diagnostic) } } else { None