Skip to content

Commit 49878de

Browse files
committed
spiking on validate
1 parent 3c979e3 commit 49878de

File tree

6 files changed

+52
-64
lines changed

6 files changed

+52
-64
lines changed

Rakefile

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,5 @@ GEMSPEC = Gem::Specification.load('code_ownership.gemspec')
44

55
task build: 'pkg:ruby'
66

7-
task default: %w[env:dev compile spec]
7+
# task default: %w[env:dev compile spec]
8+
task default: %w[env:dev compile]

ext/code_ownership/src/lib.rs

Lines changed: 12 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,20 @@ fn for_file(file_path: String) -> Result<Option<Value>, Error> {
3131
}
3232
}
3333

34-
// Needed for tests
34+
fn validate() -> Result<Value, Error> {
35+
let run_config = build_run_config();
36+
let run_result = runner::validate(&run_config, vec![]);
37+
validate_result(&run_result)
38+
39+
}
40+
3541
fn generate_and_validate() -> Result<Value, Error> {
3642
let run_config = build_run_config();
3743
let run_result = runner::generate_and_validate(&run_config, vec![]);
38-
dbg!(&run_result);
44+
validate_result(&run_result)
45+
}
46+
47+
fn validate_result(run_result: &runner::RunResult) -> Result<Value, Error> {
3948
if !run_result.validation_errors.is_empty() {
4049
Err(Error::new(
4150
magnus::exception::runtime_error(),
@@ -73,41 +82,7 @@ fn init(ruby: &Ruby) -> Result<(), Error> {
7382
let module = ruby.define_module("RustCodeOwners")?;
7483
module.define_singleton_method("for_file", function!(for_file, 1))?;
7584
module.define_singleton_method("generate_and_validate", function!(generate_and_validate, 0))?;
85+
module.define_singleton_method("validate", function!(validate, 0))?;
7686

7787
Ok(())
7888
}
79-
80-
#[cfg(test)]
81-
mod tests {
82-
use std::env::set_current_dir;
83-
84-
use super::*;
85-
86-
#[test]
87-
#[should_panic]
88-
fn test_for_file_with_invalid_path() {
89-
// panics because not called from a ruby thread
90-
let _result = for_file("invalid/path".to_string());
91-
}
92-
93-
#[test]
94-
fn test_for_file() {
95-
let argv: [*mut c_char; 0] = [];
96-
let argv = argv.as_ptr();
97-
let mut argc = 1;
98-
99-
let manifest_dir = env!("CARGO_MANIFEST_DIR");
100-
let fixture_path = PathBuf::from(manifest_dir).join("tests/fixtures/valid_project");
101-
let _ = set_current_dir(&fixture_path).unwrap();
102-
103-
unsafe {
104-
rb_sys::ruby_sysinit(&mut argc, argv as _);
105-
rb_sys::ruby_init();
106-
107-
init();
108-
let result = generate_and_validate();
109-
assert!(result.is_ok());
110-
rb_sys::ruby_cleanup(0);
111-
}
112-
}
113-
}

lib/code_ownership.rb

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,13 @@
1414
require 'code_ownership/cli'
1515
require 'code_ownership/configuration'
1616

17+
begin
18+
RUBY_VERSION =~ /(\d+\.\d+)/
19+
require "code_ownership/#{Regexp.last_match(1)}/code_ownership"
20+
rescue LoadError
21+
require 'code_ownership/code_ownership'
22+
end
23+
1724
if defined?(Packwerk)
1825
require 'code_ownership/private/permit_pack_owner_top_level_key'
1926
end
@@ -95,15 +102,7 @@ def validate!(
95102
stage_changes: true,
96103
files: nil
97104
)
98-
Private.load_configuration!
99-
100-
tracked_file_subset = if files
101-
files.select { |f| Private.file_tracked?(f) }
102-
else
103-
Private.tracked_files
104-
end
105-
106-
Private.validate!(files: tracked_file_subset, autocorrect: autocorrect, stage_changes: stage_changes)
105+
Private.validate!(autocorrect: autocorrect, stage_changes: stage_changes)
107106
end
108107

109108
# Given a backtrace from either `Exception#backtrace` or `caller`, find the
-160 Bytes
Binary file not shown.

lib/code_ownership/private.rb

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -44,22 +44,22 @@ def self.bust_caches!
4444
@glob_cache = nil
4545
end
4646

47-
sig { params(files: T::Array[String], autocorrect: T::Boolean, stage_changes: T::Boolean).void }
48-
def self.validate!(files:, autocorrect: true, stage_changes: true)
49-
CodeownersFile.update_cache!(files) if CodeownersFile.use_codeowners_cache?
50-
51-
errors = Validator.all.flat_map do |validator|
52-
validator.validation_errors(
53-
files: files,
54-
autocorrect: autocorrect,
55-
stage_changes: stage_changes
56-
)
57-
end
58-
59-
if errors.any?
60-
errors << 'See https://github.com/rubyatscale/code_ownership#README.md for more details'
61-
raise InvalidCodeOwnershipConfigurationError.new(errors.join("\n")) # rubocop:disable Style/RaiseArgs
62-
end
47+
sig { params(autocorrect: T::Boolean, stage_changes: T::Boolean).void }
48+
def self.validate!(autocorrect: true, stage_changes: true)
49+
puts 'begin....'
50+
result = if autocorrect
51+
# T.let(::RustCodeOwners.generate_and_validate, T.nilable(T::Hash[Symbol, String]))
52+
::RustCodeOwners.generate_and_validate
53+
else
54+
# T.let(::RustCodeOwners.validate, T.nilable(T::Hash[Symbol, String]))
55+
::RustCodeOwners.validate
56+
end
57+
58+
puts result
59+
# if errors.any?
60+
# errors << 'See https://github.com/rubyatscale/code_ownership#README.md for more details'
61+
# raise InvalidCodeOwnershipConfigurationError.new(errors.join("\n"))
62+
# end
6363
end
6464

6565
# Returns a string version of the relative path to a Rails constant,

sorbet/rbi/manual.rbi

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,16 @@
11
class Hash
2-
def to_json; end
2+
def to_json(*_args); end
3+
end
4+
5+
module RustCodeOwners
6+
class << self
7+
def for_file(file)
8+
end
9+
10+
def generate_and_validate
11+
end
12+
13+
def validate
14+
end
15+
end
316
end

0 commit comments

Comments
 (0)