Skip to content

Commit b747dd4

Browse files
authored
Merge pull request #16 from steve-redka/main
Add --only option
2 parents ca8637f + 1c48040 commit b747dd4

File tree

5 files changed

+150
-24
lines changed

5 files changed

+150
-24
lines changed

lib/rails/diff.rb

Lines changed: 13 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# frozen_string_literal: true
22

33
require_relative "diff/version"
4+
require_relative "diff/file_tracker"
45
require "rails"
56
require "thor"
67
require "diffy"
@@ -23,12 +24,12 @@ def file(*files, no_cache: false, commit: nil, new_app_options: nil)
2324
files.filter_map { |it| diff_with_header(it) }.join("\n")
2425
end
2526

26-
def generated(generator_name, *args, no_cache: false, skip: [], commit: nil, new_app_options: nil)
27+
def generated(generator_name, *args, no_cache: false, skip: [], only: [], commit: nil, new_app_options: nil)
2728
clear_cache if no_cache
2829
ensure_template_app_exists(commit, new_app_options)
2930
install_app_dependencies
3031

31-
generated_files(generator_name, *args, skip)
32+
generated_files(generator_name, *args, skip, only)
3233
.map { |it| diff_with_header(it) }
3334
.join("\n\n")
3435
end
@@ -97,29 +98,11 @@ def railsrc_options
9798

9899
def app_name = @app_name ||= File.basename(Dir.pwd)
99100

100-
def list_files(dir, skip = [])
101-
Dir.glob("#{dir}/**/*", File::FNM_DOTMATCH).reject do |it|
102-
File.directory?(it) ||
103-
it.start_with?("#{dir}/.git") ||
104-
it.start_with?("#{dir}/tmp") ||
105-
it.start_with?("#{dir}/log") ||
106-
it.start_with?("#{dir}/test") ||
107-
skip.any? { |s| it.start_with?("#{dir}/#{s}") }
108-
end
109-
end
110-
111-
def track_new_files(skip)
112-
files_before = list_files(template_app_path)
113-
yield
114-
files_after = list_files(template_app_path, skip)
115-
files_after - files_before
116-
end
117-
118-
def generated_files(generator_name, *args, skip)
101+
def generated_files(generator_name, *args, skip, only)
119102
Dir.chdir(template_app_path) do
120103
system!("bin/rails", "destroy", generator_name, *args)
121104
info "Running generator: rails generate #{generator_name} #{args.join(' ')}"
122-
track_new_files(skip) { system!("bin/rails", "generate", generator_name, *args) }
105+
FileTracker.new.track_new_files(template_app_path, skip, only) { system!("bin/rails", "generate", generator_name, *args) }
123106
.map { |it| it.delete_prefix("#{template_app_path}/") }
124107
end
125108
end
@@ -255,9 +238,16 @@ def file(*files)
255238

256239
desc "generated GENERATOR [args]", "Compare files that would be created by a Rails generator"
257240
option :skip, type: :array, desc: "Skip specific files or directories", aliases: ["-s"], default: []
241+
option :only, type: :array, desc: "Only include specific files or directories", aliases: ["-o"], default: []
258242
def generated(generator_name, *args)
259243
ENV["DEBUG"] = "true" if options[:debug]
260-
diff = Rails::Diff.generated(generator_name, *args, no_cache: options[:no_cache], skip: options[:skip], commit: options[:commit], new_app_options: options[:new_app_options])
244+
diff = Rails::Diff.generated(generator_name,
245+
*args,
246+
no_cache: options[:no_cache],
247+
skip: options[:skip],
248+
only: options[:only],
249+
commit: options[:commit],
250+
new_app_options: options[:new_app_options])
261251
return if diff.empty?
262252

263253
options[:fail] ? abort(diff) : puts(diff)

lib/rails/diff/file_tracker.rb

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# frozen_string_literal: true
2+
3+
class FileTracker
4+
def track_new_files(template_app_path, skip, only = [])
5+
files_before = list_files(template_app_path)
6+
yield
7+
files_after = list_files(template_app_path, skip, only)
8+
files_after - files_before
9+
end
10+
11+
private
12+
13+
def list_files(dir, skip = [], only = [])
14+
files = Dir.glob("#{dir}/**/*", File::FNM_DOTMATCH).reject do |it|
15+
File.directory?(it) ||
16+
it.start_with?("#{dir}/.git") ||
17+
it.start_with?("#{dir}/tmp") ||
18+
it.start_with?("#{dir}/log") ||
19+
it.start_with?("#{dir}/test") ||
20+
skip.any? { |s| it.start_with?("#{dir}/#{s}") }
21+
end
22+
23+
if only.any?
24+
files.select { |it| only.any? { |o| it.start_with?("#{dir}/#{o}") } }
25+
else
26+
files
27+
end
28+
end
29+
end
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
# frozen_string_literal: true
2+
3+
require 'rails/diff/file_tracker'
4+
5+
RSpec.describe FileTracker do
6+
context 'integration tests' do
7+
let(:temp_dir) { Dir.mktmpdir }
8+
9+
after do
10+
FileUtils.remove_entry(temp_dir)
11+
end
12+
13+
it 'tracks newly created files' do
14+
FileUtils.touch("#{temp_dir}/file1.rb")
15+
file_tracker = FileTracker.new
16+
17+
new_files = file_tracker.track_new_files(temp_dir, []) do
18+
FileUtils.touch("#{temp_dir}/file2.rb")
19+
FileUtils.touch("#{temp_dir}/file3.rb")
20+
end
21+
22+
expect(new_files).to contain_exactly("#{temp_dir}/file2.rb", "#{temp_dir}/file3.rb")
23+
end
24+
25+
it 'excludes skipped files' do
26+
FileUtils.touch("#{temp_dir}/file1.rb")
27+
file_tracker = FileTracker.new
28+
29+
new_files = file_tracker.track_new_files(temp_dir, ['file2.rb']) do
30+
FileUtils.touch("#{temp_dir}/file2.rb")
31+
FileUtils.touch("#{temp_dir}/file3.rb")
32+
end
33+
34+
expect(new_files).to contain_exactly("#{temp_dir}/file3.rb")
35+
end
36+
37+
it 'handles files with --only option' do
38+
FileUtils.touch("#{temp_dir}/file1.rb")
39+
file_tracker = FileTracker.new
40+
new_files = file_tracker.track_new_files(temp_dir, [], ['file2.rb']) do
41+
FileUtils.touch("#{temp_dir}/file2.rb")
42+
FileUtils.touch("#{temp_dir}/file3.rb")
43+
end
44+
expect(new_files).to contain_exactly("#{temp_dir}/file2.rb")
45+
end
46+
47+
it 'ignores files in special directories' do
48+
FileUtils.mkdir_p("#{temp_dir}/.git")
49+
FileUtils.mkdir_p("#{temp_dir}/tmp")
50+
FileUtils.mkdir_p("#{temp_dir}/log")
51+
FileUtils.touch("#{temp_dir}/file1.rb")
52+
file_tracker = FileTracker.new
53+
54+
new_files = file_tracker.track_new_files(temp_dir, []) do
55+
FileUtils.touch("#{temp_dir}/.git/config")
56+
FileUtils.touch("#{temp_dir}/tmp/cache")
57+
FileUtils.touch("#{temp_dir}/log/development.log")
58+
FileUtils.touch("#{temp_dir}/file2.rb")
59+
end
60+
61+
expect(new_files).to contain_exactly("#{temp_dir}/file2.rb")
62+
end
63+
64+
it 'handles nested directories' do
65+
FileUtils.touch("#{temp_dir}/file1.rb")
66+
file_tracker = FileTracker.new
67+
68+
new_files = file_tracker.track_new_files(temp_dir, []) do
69+
FileUtils.mkdir_p("#{temp_dir}/nested/dir")
70+
FileUtils.touch("#{temp_dir}/nested/file2.rb")
71+
FileUtils.touch("#{temp_dir}/nested/dir/file3.rb")
72+
end
73+
74+
expect(new_files).to contain_exactly("#{temp_dir}/nested/file2.rb", "#{temp_dir}/nested/dir/file3.rb")
75+
end
76+
end
77+
end

spec/rails/diff_spec.rb

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,33 @@
44
it "has a version number" do
55
expect(Rails::Diff::VERSION).not_to be nil
66
end
7-
end
7+
8+
describe ".generated" do
9+
before do
10+
allow(Rails::Diff).to receive(:system!).and_return(true)
11+
allow(Rails::Diff).to receive(:ensure_template_app_exists)
12+
allow(Rails::Diff).to receive(:install_app_dependencies)
13+
allow(Rails::Diff).to receive(:generated_files).and_return(["file1.rb", "file2.rb"])
14+
allow(Rails::Diff).to receive(:diff_with_header).and_return("file1.rb diff:\n===\nDiff content")
15+
end
16+
17+
it "returns the diff for generated files" do
18+
result = Rails::Diff.generated("model", "User", no_cache: true)
19+
expect(result).to include("file1.rb diff:")
20+
end
21+
end
22+
23+
describe Rails::Diff::CLI do
24+
describe "#generated" do
25+
before do
26+
allow(Rails::Diff).to receive(:system!).and_return(true)
27+
allow(Rails::Diff).to receive(:generated).and_return("file1.rb diff:\n===\nDiff content")
28+
end
29+
30+
it "runs without error" do
31+
cli = Rails::Diff::CLI.new
32+
expect { cli.generated("model", "User") }.not_to raise_error
33+
end
34+
end
35+
end
36+
end

spec/spec_helper.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# frozen_string_literal: true
22

33
require "rails/diff"
4+
$LOAD_PATH.unshift(File.expand_path('../../lib', __dir__))
45

56
RSpec.configure do |config|
67
# Enable flags like --only-failures and --next-failure

0 commit comments

Comments
 (0)