From ee3224a75aab1a14df3a9710aeb2b5bb3245e2bf Mon Sep 17 00:00:00 2001 From: Dmitry Sinelnikov Date: Sun, 13 Oct 2019 21:06:01 +0300 Subject: [PATCH 1/2] Add support for go test #685 --- config/default.yml | 6 ++ lib/overcommit/hook/pre_push/go_test.rb | 14 ++++ spec/overcommit/hook/pre_push/go_test_spec.rb | 66 +++++++++++++++++++ 3 files changed, 86 insertions(+) create mode 100644 lib/overcommit/hook/pre_push/go_test.rb create mode 100644 spec/overcommit/hook/pre_push/go_test_spec.rb diff --git a/config/default.yml b/config/default.yml index 02777c7e..1cc69da2 100644 --- a/config/default.yml +++ b/config/default.yml @@ -1253,6 +1253,12 @@ PrePush: required_executable: 'git-lfs' install_command: 'brew install git-lfs' + GoTest: + enabled: false + description: 'Run go test suite' + required_executable: 'go' + command: ['go', 'test', './...'] + Minitest: enabled: false description: 'Run Minitest test suite' diff --git a/lib/overcommit/hook/pre_push/go_test.rb b/lib/overcommit/hook/pre_push/go_test.rb new file mode 100644 index 00000000..b6b732a6 --- /dev/null +++ b/lib/overcommit/hook/pre_push/go_test.rb @@ -0,0 +1,14 @@ +# frozen_string_literal: true + +module Overcommit::Hook::PrePush + # Runs `go test ./...` command on prepush + class GoTest < Base + def run + result = execute(command) + return :pass if result.success? + + output = result.stdout + result.stderr + [:fail, output] + end + end +end diff --git a/spec/overcommit/hook/pre_push/go_test_spec.rb b/spec/overcommit/hook/pre_push/go_test_spec.rb new file mode 100644 index 00000000..ebb9687a --- /dev/null +++ b/spec/overcommit/hook/pre_push/go_test_spec.rb @@ -0,0 +1,66 @@ +# frozen_string_literal: true + +require 'spec_helper' + +describe Overcommit::Hook::PrePush::GoTest do + let(:config) { Overcommit::ConfigurationLoader.default_configuration } + let(:context) { double('context') } + subject { described_class.new(config, context) } + + context 'when go test exits successfully' do + let(:result) { double('result') } + + before do + result.stub(success?: true, stderr: '', stdout: '') + subject.stub(:execute).and_return(result) + end + + it 'passes' do + expect(subject).to pass + end + end + + context 'when go test exits unsucessfully' do + let(:result) { double('result') } + + before do + result.stub(:success?).and_return(false) + subject.stub(:execute).and_return(result) + end + + context 'when golangci-lint returns an error' do + let(:error_message) { "--- FAIL: Test1 (0.00s)\nFAIL" } + + before do + result.stub(:stdout).and_return(error_message) + result.stub(:stderr).and_return('') + end + + it 'fails' do + expect(subject).to fail_hook + end + + it 'returns valid message' do + message = subject.run.last + expect(message).to eq error_message + end + end + + context 'when a generic error message is written to stderr' do + let(:error_message) { 'go: command not found' } + before do + result.stub(:stdout).and_return('') + result.stub(:stderr).and_return(error_message) + end + + it 'fails' do + expect(subject).to fail_hook + end + + it 'returns valid message' do + message = subject.run.last + expect(message).to eq error_message + end + end + end +end From d161f77ea3eb5cc28d02c196c6ee839ea3ef6c70 Mon Sep 17 00:00:00 2001 From: Dmitry Sinelnikov Date: Sun, 13 Oct 2019 22:32:56 +0300 Subject: [PATCH 2/2] Add support for go test #685 --- spec/overcommit/hook/pre_push/go_test_spec.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spec/overcommit/hook/pre_push/go_test_spec.rb b/spec/overcommit/hook/pre_push/go_test_spec.rb index ebb9687a..19bc4d0d 100644 --- a/spec/overcommit/hook/pre_push/go_test_spec.rb +++ b/spec/overcommit/hook/pre_push/go_test_spec.rb @@ -28,7 +28,7 @@ subject.stub(:execute).and_return(result) end - context 'when golangci-lint returns an error' do + context 'when go test returns an error' do let(:error_message) { "--- FAIL: Test1 (0.00s)\nFAIL" } before do