Skip to content

Commit 98f3fe6

Browse files
committed
Add file setup and manipulation logic to CLI#install method
- Add logic to create directory, copy files, and manipulate file contents within the CLI#install method. - Update tests to verify directory creation, file copying, and content manipulation. - Ensure proper handling when hook file exists or does not exist.
1 parent 78c4a9f commit 98f3fe6

1 file changed

Lines changed: 17 additions & 6 deletions

File tree

spec/lib/cli_spec.rb

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,56 +16,67 @@
1616
describe "#install" do
1717
let(:cli) { described_class.new }
1818
let(:script_content) { "script content" }
19+
let(:file_double) { instance_double(File) }
1920

2021
before do
2122
allow(cli).to receive(:script).and_return(script_content)
2223
allow(FileUtils).to receive(:chmod)
24+
allow(FileUtils).to receive(:mkdir_p)
25+
allow(FileUtils).to receive(:cp)
26+
allow(File).to receive(:open).and_yield(file_double)
27+
allow(File).to receive(:write)
28+
allow(file_double).to receive(:puts)
2329
allow($stdout).to receive(:puts)
2430
end
2531

2632
subject { cli.install }
2733

28-
after { subject }
29-
3034
it "creates a dir and copies initializer file" do
31-
expect(File.exist?("config/initializers/ai_git_commit.rb")).to be(true)
35+
expect(FileUtils).to receive(:mkdir_p)
36+
expect(FileUtils).to receive(:cp)
37+
subject
3238
end
3339

3440
context "when hook file exists" do
3541
before do
3642
allow(cli).to receive(:hook_file_exists?).and_return(true)
37-
allow(File).to receive(:open)
3843
end
3944

4045
it "appends script to the hook file" do
41-
expect(File).to receive(:open).with(described_class::HOOK_PATH, "a")
46+
expect(File).to receive(:open).with(described_class::HOOK_PATH, "a").and_yield(file_double)
47+
expect(file_double).to receive(:puts).with(script_content)
48+
subject
4249
end
4350

4451
it "makes the hook file executable" do
4552
expect(FileUtils).to receive(:chmod).with("+x", described_class::HOOK_PATH)
53+
subject
4654
end
4755

4856
it "prints confirmation message" do
4957
expect($stdout).to receive(:puts).with("AI Git Commit prepare-commit-msg hook set up.")
58+
subject
5059
end
5160
end
5261

5362
context "when hook file does not exist" do
5463
before do
5564
allow(cli).to receive(:hook_file_exists?).and_return(false)
56-
allow(File).to receive(:write)
5765
end
5866

5967
it "writes script to the hook file" do
6068
expect(File).to receive(:write).with(described_class::HOOK_PATH, script_content)
69+
subject
6170
end
6271

6372
it "makes the hook file executable" do
6473
expect(FileUtils).to receive(:chmod).with("+x", described_class::HOOK_PATH)
74+
subject
6575
end
6676

6777
it "prints confirmation message" do
6878
expect($stdout).to receive(:puts).with("AI Git Commit prepare-commit-msg hook set up.")
79+
subject
6980
end
7081
end
7182
end

0 commit comments

Comments
 (0)