diff --git a/lib/rails/diff/cli.rb b/lib/rails/diff/cli.rb index 74a50bc..85cb69f 100644 --- a/lib/rails/diff/cli.rb +++ b/lib/rails/diff/cli.rb @@ -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| + 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: [] diff --git a/spec/lib/rails/diff/cli_spec.rb b/spec/lib/rails/diff/cli_spec.rb new file mode 100644 index 0000000..e18857a --- /dev/null +++ b/spec/lib/rails/diff/cli_spec.rb @@ -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 \ No newline at end of file diff --git a/spec/support/git_repo.rb b/spec/support/git_repo.rb index 89770e1..ef987fd 100644 --- a/spec/support/git_repo.rb +++ b/spec/support/git_repo.rb @@ -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 'test@example.com'` - `git config user.name 'Test User'` + run_command("git config user.email 'test@example.com'") + 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