|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +require 'spec_helper' |
| 4 | + |
| 5 | +describe Overcommit::Hook::PreCommit::GoFmt do |
| 6 | + let(:config) { Overcommit::ConfigurationLoader.default_configuration } |
| 7 | + let(:context) { double('context') } |
| 8 | + subject { described_class.new(config, context) } |
| 9 | + |
| 10 | + let(:files) do |
| 11 | + %w[ |
| 12 | + pkg1/file1.go |
| 13 | + pkg1/file2.go |
| 14 | + pkg2/file1.go |
| 15 | + file1.go |
| 16 | + ] |
| 17 | + end |
| 18 | + before do |
| 19 | + subject.stub(:applicable_files).and_return(files) |
| 20 | + end |
| 21 | + |
| 22 | + context 'when go fmt exits successfully' do |
| 23 | + let(:result) { double('result') } |
| 24 | + |
| 25 | + before do |
| 26 | + result.stub(success?: true, stderr: '', stdout: '') |
| 27 | + subject.stub(:execute).and_return(result) |
| 28 | + end |
| 29 | + |
| 30 | + it 'executes go fmt for each file' do |
| 31 | + files.each do |file| |
| 32 | + expect(subject).to receive(:execute).with(subject.command, args: [file]).once |
| 33 | + end |
| 34 | + subject.run |
| 35 | + end |
| 36 | + |
| 37 | + it 'passes' do |
| 38 | + expect(subject).to pass |
| 39 | + end |
| 40 | + end |
| 41 | + |
| 42 | + context 'when go fmt exits unsucessfully' do |
| 43 | + let(:result) { double('result') } |
| 44 | + |
| 45 | + before do |
| 46 | + result.stub(:success?).and_return(false) |
| 47 | + subject.stub(:execute).and_return(result) |
| 48 | + end |
| 49 | + |
| 50 | + context 'when go fmt returns an error to stdout' do |
| 51 | + let(:error_message) { 'some go fmt error' } |
| 52 | + |
| 53 | + before do |
| 54 | + result.stub(:stdout).and_return(error_message) |
| 55 | + result.stub(:stderr).and_return('') |
| 56 | + end |
| 57 | + |
| 58 | + it 'executes go fmt for each file' do |
| 59 | + files.each do |file| |
| 60 | + expect(subject).to receive(:execute).with(subject.command, args: [file]).once |
| 61 | + end |
| 62 | + subject.run |
| 63 | + end |
| 64 | + |
| 65 | + it 'fails' do |
| 66 | + expect(subject).to fail_hook |
| 67 | + end |
| 68 | + |
| 69 | + it 'returns errors' do |
| 70 | + message = subject.run.last |
| 71 | + expect(message).to eq Array.new(files.count, error_message).join("\n") |
| 72 | + end |
| 73 | + end |
| 74 | + |
| 75 | + context 'when fo fmt returns an error to stderr' do |
| 76 | + let(:error_message) { 'go: command not found' } |
| 77 | + before do |
| 78 | + result.stub(:stdout).and_return('') |
| 79 | + result.stub(:stderr).and_return(error_message) |
| 80 | + end |
| 81 | + |
| 82 | + it 'executes go fmt for each file' do |
| 83 | + files.each do |file| |
| 84 | + expect(subject).to receive(:execute).with(subject.command, args: [file]).once |
| 85 | + end |
| 86 | + subject.run |
| 87 | + end |
| 88 | + |
| 89 | + it 'fails' do |
| 90 | + expect(subject).to fail_hook |
| 91 | + end |
| 92 | + |
| 93 | + it 'returns valid message' do |
| 94 | + message = subject.run.last |
| 95 | + expect(message).to eq Array.new(files.count, error_message).join("\n") |
| 96 | + end |
| 97 | + end |
| 98 | + end |
| 99 | +end |
0 commit comments