|
16 | 16 | describe "#install" do |
17 | 17 | let(:cli) { described_class.new } |
18 | 18 | let(:script_content) { "script content" } |
| 19 | + let(:file_double) { instance_double(File) } |
19 | 20 |
|
20 | 21 | before do |
21 | 22 | allow(cli).to receive(:script).and_return(script_content) |
22 | 23 | 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) |
23 | 29 | allow($stdout).to receive(:puts) |
24 | 30 | end |
25 | 31 |
|
26 | 32 | subject { cli.install } |
27 | 33 |
|
28 | | - after { subject } |
29 | | - |
30 | 34 | 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 |
32 | 38 | end |
33 | 39 |
|
34 | 40 | context "when hook file exists" do |
35 | 41 | before do |
36 | 42 | allow(cli).to receive(:hook_file_exists?).and_return(true) |
37 | | - allow(File).to receive(:open) |
38 | 43 | end |
39 | 44 |
|
40 | 45 | 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 |
42 | 49 | end |
43 | 50 |
|
44 | 51 | it "makes the hook file executable" do |
45 | 52 | expect(FileUtils).to receive(:chmod).with("+x", described_class::HOOK_PATH) |
| 53 | + subject |
46 | 54 | end |
47 | 55 |
|
48 | 56 | it "prints confirmation message" do |
49 | 57 | expect($stdout).to receive(:puts).with("AI Git Commit prepare-commit-msg hook set up.") |
| 58 | + subject |
50 | 59 | end |
51 | 60 | end |
52 | 61 |
|
53 | 62 | context "when hook file does not exist" do |
54 | 63 | before do |
55 | 64 | allow(cli).to receive(:hook_file_exists?).and_return(false) |
56 | | - allow(File).to receive(:write) |
57 | 65 | end |
58 | 66 |
|
59 | 67 | it "writes script to the hook file" do |
60 | 68 | expect(File).to receive(:write).with(described_class::HOOK_PATH, script_content) |
| 69 | + subject |
61 | 70 | end |
62 | 71 |
|
63 | 72 | it "makes the hook file executable" do |
64 | 73 | expect(FileUtils).to receive(:chmod).with("+x", described_class::HOOK_PATH) |
| 74 | + subject |
65 | 75 | end |
66 | 76 |
|
67 | 77 | it "prints confirmation message" do |
68 | 78 | expect($stdout).to receive(:puts).with("AI Git Commit prepare-commit-msg hook set up.") |
| 79 | + subject |
69 | 80 | end |
70 | 81 | end |
71 | 82 | end |
|
0 commit comments