Skip to content

Commit d6fe19c

Browse files
committed
feat: add support for explicit file arguments to validate command
Allows users to specify files directly on the command line: bin/codeownership validate file1.rb file2.rb This provides a more flexible alternative to the --diff flag for validating specific files. When explicit files are provided, they take precedence over the --diff flag and a warning is displayed. Changes: - Updated CLI to accept file arguments after options - Added warning when both --diff and explicit files are provided - Added comprehensive specs for file argument behavior - Updated README with usage examples
1 parent 5a77127 commit d6fe19c

File tree

3 files changed

+64
-3
lines changed

3 files changed

+64
-3
lines changed

README.md

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -183,8 +183,15 @@ You can call the validation function with the Ruby API
183183
CodeOwnership.validate!
184184
```
185185
or the CLI
186-
```
186+
```bash
187+
# Validate all files
187188
bin/codeownership validate
189+
190+
# Validate specific files
191+
bin/codeownership validate path/to/file1.rb path/to/file2.rb
192+
193+
# Validate only staged files
194+
bin/codeownership validate --diff
188195
```
189196

190197
## Development

lib/code_ownership/cli.rb

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ def self.validate!(argv)
3737
options = {}
3838

3939
parser = OptionParser.new do |opts|
40-
opts.banner = "Usage: #{EXECUTABLE} validate [options]"
40+
opts.banner = "Usage: #{EXECUTABLE} validate [options] [files...]"
4141

4242
opts.on('--skip-autocorrect', 'Skip automatically correcting any errors, such as the .github/CODEOWNERS file') do
4343
options[:skip_autocorrect] = true
@@ -59,11 +59,22 @@ def self.validate!(argv)
5959
args = parser.order!(argv)
6060
parser.parse!(args)
6161

62-
files = if options[:diff]
62+
# Collect any remaining arguments as file paths
63+
specified_files = argv.reject { |arg| arg.start_with?('--') }
64+
65+
files = if !specified_files.empty?
66+
# Files explicitly provided on command line
67+
if options[:diff]
68+
$stderr.puts "Warning: Ignoring --diff flag because explicit files were provided"
69+
end
70+
specified_files.select { |file| File.exist?(file) }
71+
elsif options[:diff]
72+
# Staged files from git
6373
ENV.fetch('CODEOWNERS_GIT_STAGED_FILES') { `git diff --staged --name-only` }.split("\n").select do |file|
6474
File.exist?(file)
6575
end
6676
else
77+
# No files specified, validate all
6778
nil
6879
end
6980

spec/lib/code_ownership/cli_spec.rb

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,49 @@
4545
end
4646
end
4747

48+
context 'with explicit file arguments' do
49+
let(:argv) { ['validate', 'app/services/my_file.rb', 'frontend/javascripts/my_file.jsx'] }
50+
51+
it 'validates only the specified files' do
52+
expect(CodeOwnership).to receive(:validate!) do |args|
53+
expect(args[:files]).to match_array(['app/services/my_file.rb', 'frontend/javascripts/my_file.jsx'])
54+
expect(args[:autocorrect]).to eq true
55+
expect(args[:stage_changes]).to eq true
56+
end
57+
subject
58+
end
59+
60+
context 'with options' do
61+
let(:argv) { ['validate', '--skip-autocorrect', '--skip-stage', 'app/services/my_file.rb'] }
62+
63+
it 'passes the options correctly' do
64+
expect(CodeOwnership).to receive(:validate!) do |args|
65+
expect(args[:files]).to eq(['app/services/my_file.rb'])
66+
expect(args[:autocorrect]).to eq false
67+
expect(args[:stage_changes]).to eq false
68+
end
69+
subject
70+
end
71+
end
72+
73+
context 'when combined with --diff flag' do
74+
let(:argv) { ['validate', '--diff', 'app/services/my_file.rb'] }
75+
76+
before do
77+
allow(ENV).to receive(:fetch).and_call_original
78+
allow(ENV).to receive(:fetch).with('CODEOWNERS_GIT_STAGED_FILES').and_return('other_file.rb')
79+
end
80+
81+
it 'prioritizes explicit files over git diff and warns the user' do
82+
expect($stderr).to receive(:puts).with("Warning: Ignoring --diff flag because explicit files were provided")
83+
expect(CodeOwnership).to receive(:validate!) do |args|
84+
expect(args[:files]).to eq(['app/services/my_file.rb'])
85+
end
86+
subject
87+
end
88+
end
89+
end
90+
4891
context 'with --diff argument' do
4992
let(:argv) { ['validate', '--diff'] }
5093

0 commit comments

Comments
 (0)