Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
add dotfiles option
  • Loading branch information
steve-redka committed Jun 19, 2025
commit efc263d682e62bde4d186e6d9f14045698ab2c12
9 changes: 9 additions & 0 deletions lib/rails/diff/cli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,15 @@ def file(*files)
options[:fail] ? abort(diff) : puts(diff)
end

desc "dotfiles", "Compare dotfiles in your repository with Rails' generated versions"
def dotfiles
dotfiles = Dir.glob("**/.*").reject do |path|
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This isn't exactly what I had in mind. It should be all dotfiles in the toplevel dir. It should also include files inside folders like .github and similar.

I'd like to reject files in .gitignore too.

Maybe we should delegate this to Git?

git ls-files --cached --others --exclude-standard -- '.*'

path.start_with?(".git") || path.include?("/.git/")
end

file(*dotfiles)
end

desc "generated GENERATOR [args]", "Compare files that would be created by a Rails generator"
option :skip, type: :array, desc: "Skip specific files or directories", aliases: ["-s"], default: []
option :only, type: :array, desc: "Only include specific files or directories", aliases: ["-o"], default: []
Expand Down
59 changes: 59 additions & 0 deletions spec/lib/rails/diff/cli_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
require "rspec"
require "rails/diff/cli"

RSpec.describe Rails::Diff::CLI do
let(:cli) { described_class.new }
let(:mock_diff) { "Mocked diff output" }

before do
allow(Rails::Diff).to receive(:file).and_return(mock_diff)
end

describe "#file" do
context "when no files are provided" do
it "aborts with an error message" do
expect { cli.file }.to raise_error(SystemExit, "Please provide at least one file to compare")
end
end

context "when files are provided" do
it "outputs the diff if differences exist" do
expect { cli.file("file1.rb", "file2.rb") }.to output("#{mock_diff}\n").to_stdout
end

it "aborts if differences exist and fail option is set" do
cli.options = { fail: true }
expect { cli.file("file1.rb", "file2.rb") }.to raise_error(SystemExit, mock_diff)
end
end
end

describe "#dotfiles" do
let(:mock_dotfiles) { [".env", ".gitignore"] }

before do
allow(Dir).to receive(:glob).with("**/.*").and_return(mock_dotfiles)
allow(mock_dotfiles).to receive(:reject).and_return(mock_dotfiles)
end

context "when no dotfiles are found" do
before do
allow(Dir).to receive(:glob).with("**/.*").and_return([])
end

it "does not output anything" do
expect { cli.dotfiles }.to_not output.to_stdout
end
end

context "when dotfiles are found and differences exist" do
before do
allow(Rails::Diff).to receive(:file).and_return(mock_diff)
end

it "outputs the diff" do
expect { cli.dotfiles }.to output("#{mock_diff}\n").to_stdout
end
end
end
end
Loading