Skip to content

Commit

Permalink
Skip EXE001 and EXE002 rules on Windows
Browse files Browse the repository at this point in the history
  • Loading branch information
charliermarsh committed Feb 22, 2023
1 parent 9645790 commit 0433345
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 28 deletions.
10 changes: 0 additions & 10 deletions Cargo.lock

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

4 changes: 0 additions & 4 deletions crates/ruff/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ titlecase = { version = "2.2.1" }
toml = { workspace = true }

# https://docs.rs/getrandom/0.2.7/getrandom/#webassembly-support
# For (future) wasm-pack support
[target.'cfg(all(target_family = "wasm", target_os = "unknown"))'.dependencies]
getrandom = { version = "0.2.7", features = ["js"] }
console_error_panic_hook = { version = "0.1.7" }
Expand All @@ -66,9 +65,6 @@ 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.0", features = ["yaml", "redactions"] }
test-case = { version = "2.2.2" }
Expand Down
16 changes: 16 additions & 0 deletions crates/ruff/src/rules/flake8_executable/helpers.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
#[cfg(target_family = "unix")]
use std::os::unix::fs::PermissionsExt;
#[cfg(target_family = "unix")]
use std::path::Path;

use once_cell::sync::Lazy;
use regex::Regex;

Expand Down Expand Up @@ -33,6 +38,17 @@ pub fn extract_shebang(line: &str) -> ShebangDirective {
}
}

#[cfg(target_family = "unix")]
pub fn is_executable(filepath: &Path) -> bool {
{
let Ok(metadata) = filepath.metadata() else {
return false;
};
let permissions = metadata.permissions();
permissions.mode() & 0o111 != 0
}
}

#[cfg(test)]
mod tests {
use crate::rules::flake8_executable::helpers::{
Expand Down
13 changes: 7 additions & 6 deletions crates/ruff/src/rules/flake8_executable/rules/shebang_missing.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
#![allow(unused_imports)]

use std::path::Path;

#[cfg(not(target_family = "wasm"))]
use is_executable::IsExecutable;
use ruff_macros::{define_violation, derive_message_formats};

#[cfg(not(target_family = "wasm"))]
use crate::ast::types::Range;
use crate::registry::Diagnostic;
#[cfg(target_family = "unix")]
use crate::rules::flake8_executable::helpers::is_executable;
use crate::violation::Violation;

define_violation!(
Expand All @@ -20,17 +21,17 @@ impl Violation for ShebangMissingExecutableFile {
}

/// EXE002
#[cfg(not(target_family = "wasm"))]
#[cfg(target_family = "unix")]
pub fn shebang_missing(filepath: &Path) -> Option<Diagnostic> {
if filepath.is_executable() {
if is_executable(filepath) {
let diagnostic = Diagnostic::new(ShebangMissingExecutableFile, Range::default());
Some(diagnostic)
} else {
None
}
}

#[cfg(target_family = "wasm")]
#[cfg(not(target_family = "unix"))]
pub fn shebang_missing(_filepath: &Path) -> Option<Diagnostic> {
None
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
#![allow(unused_imports)]

use std::path::Path;

#[cfg(not(target_family = "wasm"))]
use is_executable::IsExecutable;
use ruff_macros::{define_violation, derive_message_formats};
#[cfg(not(target_family = "wasm"))]
use rustpython_parser::ast::Location;

#[cfg(not(target_family = "wasm"))]
use ruff_macros::{define_violation, derive_message_formats};

use crate::ast::types::Range;
use crate::registry::Diagnostic;
#[cfg(target_family = "unix")]
use crate::rules::flake8_executable::helpers::is_executable;
use crate::rules::flake8_executable::helpers::ShebangDirective;
use crate::violation::Violation;

Expand All @@ -23,14 +24,14 @@ impl Violation for ShebangNotExecutable {
}

/// EXE001
#[cfg(not(target_family = "wasm"))]
#[cfg(target_family = "unix")]
pub fn shebang_not_executable(
filepath: &Path,
lineno: usize,
shebang: &ShebangDirective,
) -> Option<Diagnostic> {
if let ShebangDirective::Match(_, start, end, _) = shebang {
if filepath.is_executable() {
if is_executable(filepath) {
None
} else {
let diagnostic = Diagnostic::new(
Expand All @@ -47,7 +48,7 @@ pub fn shebang_not_executable(
}
}

#[cfg(target_family = "wasm")]
#[cfg(not(target_family = "unix"))]
pub fn shebang_not_executable(
_filepath: &Path,
_lineno: usize,
Expand Down

0 comments on commit 0433345

Please sign in to comment.