Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 12 additions & 5 deletions config/default.yml
Original file line number Diff line number Diff line change
Expand Up @@ -737,12 +737,19 @@ PreCommit:
install_command: 'brew install swiftlint'
include: '**/*.swift'

TerraformFormat:
enabled: false
description: 'Analyze with Terraform'
required_executable: 'terraform'
flags: ['fmt', '-check=true', '-diff=false']
include: '**/*.tf'

TsLint:
enabled: false
description: 'Analyze with TSLint'
required_executable: 'tslint'
install_command: 'npm install -g tslint typescript'
include: '**/*.ts'
enabled: false
description: 'Analyze with TSLint'
required_executable: 'tslint'
install_command: 'npm install -g tslint typescript'
include: '**/*.ts'

TrailingWhitespace:
enabled: false
Expand Down
19 changes: 19 additions & 0 deletions lib/overcommit/hook/pre_commit/terraform_format.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# frozen_string_literal: true

module Overcommit::Hook::PreCommit
# Runs 'terraform fmt' against any modified *.tf files.
#
# @see https://www.terraform.io/docs/commands/fmt.html
class TerraformFormat < Base
def run
messages = []
applicable_files.each do |f|
result = execute(command, args: [f])
unless result.success?
messages << Overcommit::Hook::Message.new(:error, f, nil, "violation found in #{f}")
end
end
messages
end
end
end
38 changes: 38 additions & 0 deletions spec/overcommit/hook/pre_commit/terraform_format_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# frozen_string_literal: true

require 'spec_helper'

describe Overcommit::Hook::PreCommit::TerraformFormat do
let(:config) { Overcommit::ConfigurationLoader.default_configuration }
let(:context) { double('context') }
subject { described_class.new(config, context) }

before do
subject.stub(:applicable_files).and_return(%w[file1.tf file2.tf])
end

context 'when Terraform exits successfully' do
before do
result = double('result')
result.stub(:success?).and_return(true)
subject.stub(:execute).and_return(result)
end

it { should pass }
end

context 'when Terraform exits unsucessfully' do
let(:result_ok) { double('result') }
let(:result_bad) { double('result') }
let(:cmdline) { %w[terraform fmt -check=true -diff=false] }

before do
result_ok.stub(:success?).and_return(true)
result_bad.stub(:success?).and_return(false)
subject.stub(:execute).with(cmdline, args: ['file1.tf']).and_return(result_ok)
subject.stub(:execute).with(cmdline, args: ['file2.tf']).and_return(result_bad)
end

it { should fail_hook }
end
end