Skip to content

Commit ce67344

Browse files
committed
much faster
1 parent 831de00 commit ce67344

File tree

3 files changed

+34
-48
lines changed

3 files changed

+34
-48
lines changed

src/ownership.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,11 @@ use std::{
99
};
1010
use tracing::{info, instrument};
1111

12+
pub(crate) mod codeowners_file_parser;
1213
mod file_generator;
1314
mod file_owner_finder;
1415
pub mod for_file_fast;
1516
pub(crate) mod mapper;
16-
pub(crate) mod codeowners_file_parser;
1717
mod validator;
1818

1919
use crate::{
@@ -24,9 +24,9 @@ use crate::{
2424
pub use validator::Errors as ValidatorErrors;
2525

2626
use self::{
27+
codeowners_file_parser::parse_for_team,
2728
file_generator::FileGenerator,
2829
mapper::{JavascriptPackageMapper, Mapper, RubyPackageMapper, TeamFileMapper, TeamGemMapper, TeamGlobMapper, TeamYmlMapper},
29-
codeowners_file_parser::parse_for_team,
3030
validator::Validator,
3131
};
3232

src/ownership/codeowners_file_parser.rs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ use crate::{
44
};
55
use fast_glob::glob_match;
66
use memoize::memoize;
7-
use regex::Regex;
87
use rayon::prelude::*;
8+
use regex::Regex;
99
use std::{
1010
collections::HashMap,
1111
error::Error,
@@ -42,15 +42,12 @@ impl Parser {
4242
return Ok(HashMap::new());
4343
}
4444

45-
let codeowners_lines_in_priority = build_codeowners_lines_in_priority(
46-
self.codeowners_file_path.to_string_lossy().into_owned(),
47-
);
45+
let codeowners_lines_in_priority = build_codeowners_lines_in_priority(self.codeowners_file_path.to_string_lossy().into_owned());
4846
// Pre-parse lines once to avoid repeated split and to handle malformed lines early
4947
let codeowners_entries: Vec<(String, String)> = codeowners_lines_in_priority
5048
.iter()
5149
.map(|line| {
52-
line
53-
.split_once(' ')
50+
line.split_once(' ')
5451
.map(|(glob, team_name)| (glob.to_string(), team_name.to_string()))
5552
.ok_or_else(|| IoError::new(std::io::ErrorKind::InvalidInput, "Invalid line"))
5653
})

src/runner.rs

Lines changed: 29 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -362,10 +362,8 @@ fn for_file_optimized(run_config: &RunConfig, file_path: &str) -> RunResult {
362362
mod tests {
363363
use tempfile::tempdir;
364364

365-
use crate::{common_test, ownership::mapper::Source};
366-
use ignore::{DirEntry, WalkBuilder, WalkParallel, WalkState};
367-
368365
use super::*;
366+
use crate::{common_test, ownership::mapper::Source};
369367

370368
#[test]
371369
fn test_version() {
@@ -421,42 +419,33 @@ mod tests {
421419

422420
#[test]
423421
fn test_teams_for_files_from_codeowners() {
424-
let project_root = Path::new("/Users/perryhertler/workspace/zenpayroll");
425-
let codeowners_file_path = project_root.join(".github/CODEOWNERS");
426-
let config_path = project_root.join("config/code_ownership.yml");
427-
let run_config = RunConfig {
428-
project_root: project_root.to_path_buf(),
429-
codeowners_file_path: codeowners_file_path.to_path_buf(),
430-
config_path: config_path.to_path_buf(),
431-
no_cache: false,
432-
};
433-
434-
// Collect all files in packs and frontend directories recursively
435-
let mut file_paths = Vec::new();
436-
for dir in ["packs", "frontend"] {
437-
let dir_path = project_root.join(dir);
438-
if dir_path.exists() && dir_path.is_dir() {
439-
for entry in WalkBuilder::new(&dir_path)
440-
.filter_entry(|e| {
441-
let name = e.file_name().to_str().unwrap_or("");
442-
!(name == "node_modules" || name == "dist" || name == ".git")
443-
})
444-
.build()
445-
.filter_map(|e| e.ok())
446-
.filter(|e| e.file_type().map(|t| t.is_file()).unwrap_or(false))
447-
.filter_map(|e| e.path().strip_prefix(project_root).ok().map(|p| p.to_string_lossy().to_string()))
448-
{
449-
file_paths.push(entry);
450-
}
451-
}
452-
}
453-
454-
let start_time = std::time::Instant::now();
455-
let teams = teams_for_files_from_codeowners(&run_config, &file_paths).unwrap();
456-
let end_time = std::time::Instant::now();
457-
println!("Time taken: {:?}", end_time.duration_since(start_time));
458-
println!("Teams: {:?}", teams);
459-
assert_eq!(teams.len(), 1);
460-
422+
let project_root = Path::new("tests/fixtures/valid_project");
423+
let file_paths = [
424+
"javascript/packages/items/item.ts",
425+
"config/teams/payroll.yml",
426+
"ruby/app/models/bank_account.rb",
427+
"made/up/file.rb",
428+
"ruby/ignored_files/git_ignored.rb",
429+
];
430+
let run_config = RunConfig {
431+
project_root: project_root.to_path_buf(),
432+
codeowners_file_path: project_root.join(".github/CODEOWNERS").to_path_buf(),
433+
config_path: project_root.join("config/code_ownership.yml").to_path_buf(),
434+
no_cache: false,
435+
};
436+
let teams =
437+
teams_for_files_from_codeowners(&run_config, &file_paths.iter().map(|s| s.to_string()).collect::<Vec<String>>()).unwrap();
438+
assert_eq!(teams.len(), 3);
439+
assert_eq!(
440+
teams.get("javascript/packages/items/item.ts").map(|t| t.name.as_str()),
441+
Some("Payroll")
442+
);
443+
assert_eq!(teams.get("config/teams/payroll.yml").map(|t| t.name.as_str()), Some("Payroll"));
444+
assert_eq!(
445+
teams.get("ruby/app/models/bank_account.rb").map(|t| t.name.as_str()),
446+
Some("Payments")
447+
);
448+
assert_eq!(teams.get("made/up/file.rb").map(|t| t.name.as_str()), None);
449+
assert_eq!(teams.get("ruby/ignored_files/git_ignored.rb").map(|t| t.name.as_str()), None);
461450
}
462451
}

0 commit comments

Comments
 (0)