Skip to content

Add dotfiles option #17

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
32 changes: 21 additions & 11 deletions spec/support/git_repo.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,28 +4,38 @@
class GitRepo
attr_reader :remote_repo, :commits

# Executes a shell command, suppressing output if successful, but capturing errors if it fails
def run_command(command)
result = `#{command} 2>&1`
raise "Command failed: #{result}" unless $?.success?
result
end

# Initializes and creates a bare remote repo and a working repo with main branch
def initialize
@remote_dir = Dir.mktmpdir
@remote_repo = File.join(@remote_dir, "origin.git")
Dir.chdir(@remote_dir) { `git init --bare --initial-branch=main origin.git > /dev/null 2>&1` }


run_command("git init --bare #{@remote_repo}")

Dir.chdir(@remote_repo) do
`git symbolic-ref HEAD refs/heads/main`
end

@commits = []
@work_dir = Dir.mktmpdir
Dir.chdir(@work_dir) do
`git clone #{@remote_repo} . > /dev/null 2>&1`
`git checkout -b main > /dev/null 2>&1`
run_command("git clone #{@remote_repo} .")
run_command("git checkout -b main")
FileUtils.mkdir_p("railties")
File.write("railties/README", "keep")
`git add railties > /dev/null 2>&1`
run_command("git add railties")

`git config user.email '[email protected]'`
`git config user.name 'Test User'`
run_command("git config user.email '[email protected]'")
run_command("git config user.name 'Test User'")

commit_result = `git commit -m "add railties dir" 2>&1`
raise "Initial commit failed: #{commit_result}" unless $?.success?
@commits << `git rev-parse HEAD`.strip
`git push -u origin main > /dev/null 2>&1`
commit_result = run_command("git commit -m 'add railties dir'")
@commits << run_command("git rev-parse HEAD").strip
end
end

Expand Down
Loading