Merge pull request #4373 from hashicorp/f-gofmt-fail

Fail builds if gofmt reports non-compliance
This commit is contained in:
James Nugent 2015-12-17 12:43:13 -05:00
commit 3ff7635b64
12 changed files with 96 additions and 84 deletions

View File

@ -4,12 +4,12 @@ VETARGS?=-asmdecl -atomic -bool -buildtags -copylocks -methods -nilfunc -printf
default: test default: test
# bin generates the releaseable binaries for Terraform # bin generates the releaseable binaries for Terraform
bin: generate bin: fmtcheck generate
@sh -c "'$(CURDIR)/scripts/build.sh'" @sh -c "'$(CURDIR)/scripts/build.sh'"
# dev creates binaries for testing Terraform locally. These are put # dev creates binaries for testing Terraform locally. These are put
# into ./bin/ as well as $GOPATH/bin # into ./bin/ as well as $GOPATH/bin
dev: generate dev: fmtcheck generate
@TF_DEV=1 sh -c "'$(CURDIR)/scripts/build.sh'" @TF_DEV=1 sh -c "'$(CURDIR)/scripts/build.sh'"
quickdev: generate quickdev: generate
@ -18,22 +18,22 @@ quickdev: generate
# Shorthand for quickly building the core of Terraform. Note that some # Shorthand for quickly building the core of Terraform. Note that some
# changes will require a rebuild of everything, in which case the dev # changes will require a rebuild of everything, in which case the dev
# target should be used. # target should be used.
core-dev: generate core-dev: fmtcheck generate
go install github.com/hashicorp/terraform go install github.com/hashicorp/terraform
# Shorthand for building and installing just one plugin for local testing. # Shorthand for building and installing just one plugin for local testing.
# Run as (for example): make plugin-dev PLUGIN=provider-aws # Run as (for example): make plugin-dev PLUGIN=provider-aws
plugin-dev: generate plugin-dev: fmtcheck generate
go install github.com/hashicorp/terraform/builtin/bins/$(PLUGIN) go install github.com/hashicorp/terraform/builtin/bins/$(PLUGIN)
mv $(GOPATH)/bin/$(PLUGIN) $(GOPATH)/bin/terraform-$(PLUGIN) mv $(GOPATH)/bin/$(PLUGIN) $(GOPATH)/bin/terraform-$(PLUGIN)
# test runs the unit tests and vets the code # test runs the unit tests and vets the code
test: generate test: fmtcheck generate
TF_ACC= go test $(TEST) $(TESTARGS) -timeout=30s -parallel=4 TF_ACC= go test $(TEST) $(TESTARGS) -timeout=30s -parallel=4
@$(MAKE) vet @$(MAKE) vet
# testacc runs acceptance tests # testacc runs acceptance tests
testacc: generate testacc: fmtcheck generate
@if [ "$(TEST)" = "./..." ]; then \ @if [ "$(TEST)" = "./..." ]; then \
echo "ERROR: Set TEST to a specific package. For example,"; \ echo "ERROR: Set TEST to a specific package. For example,"; \
echo " make testacc TEST=./builtin/providers/aws"; \ echo " make testacc TEST=./builtin/providers/aws"; \
@ -42,7 +42,7 @@ testacc: generate
TF_ACC=1 go test $(TEST) -v $(TESTARGS) -timeout 90m TF_ACC=1 go test $(TEST) -v $(TESTARGS) -timeout 90m
# testrace runs the race checker # testrace runs the race checker
testrace: generate testrace: fmtcheck generate
TF_ACC= go test -race $(TEST) $(TESTARGS) TF_ACC= go test -race $(TEST) $(TESTARGS)
# updatedeps installs all the dependencies that Terraform needs to run # updatedeps installs all the dependencies that Terraform needs to run
@ -84,4 +84,10 @@ vet:
generate: generate:
go generate ./... go generate ./...
.PHONY: bin default generate test updatedeps vet fmt:
gofmt -w .
fmtcheck:
@sh -c "'$(CURDIR)/scripts/gofmtcheck.sh'"
.PHONY: bin default generate test updatedeps vet fmt fmtcheck

View File

@ -804,7 +804,7 @@ func waitForTableToBeActive(tableName string, meta interface{}) error {
func validateStreamViewType(v interface{}, k string) (ws []string, errors []error) { func validateStreamViewType(v interface{}, k string) (ws []string, errors []error) {
value := v.(string) value := v.(string)
viewTypes := map[string]bool { viewTypes := map[string]bool{
"KEYS_ONLY": true, "KEYS_ONLY": true,
"NEW_IMAGE": true, "NEW_IMAGE": true,
"OLD_IMAGE": true, "OLD_IMAGE": true,

View File

@ -46,7 +46,7 @@ func TestAccAWSSecurityGroup_basic(t *testing.T) {
}) })
} }
func TestAccAWSSecurityGroup_namePrefix( t *testing.T) { func TestAccAWSSecurityGroup_namePrefix(t *testing.T) {
var group ec2.SecurityGroup var group ec2.SecurityGroup
resource.Test(t, resource.TestCase{ resource.Test(t, resource.TestCase{
@ -345,7 +345,7 @@ func testAccCheckAWSSecurityGroupDestroy(s *terraform.State) error {
} }
func testAccCheckAWSSecurityGroupGeneratedNamePrefix( func testAccCheckAWSSecurityGroupGeneratedNamePrefix(
resource, prefix string) resource.TestCheckFunc { resource, prefix string) resource.TestCheckFunc {
return func(s *terraform.State) error { return func(s *terraform.State) error {
r, ok := s.RootModule().Resources[resource] r, ok := s.RootModule().Resources[resource]
if !ok { if !ok {

View File

@ -67,7 +67,6 @@ func resourceVSphereFolderCreate(d *schema.ResourceData, meta interface{}) error
return resourceVSphereFolderRead(d, meta) return resourceVSphereFolderRead(d, meta)
} }
func createFolder(client *govmomi.Client, f *folder) error { func createFolder(client *govmomi.Client, f *folder) error {
finder := find.NewFinder(client.Client, true) finder := find.NewFinder(client.Client, true)
@ -113,7 +112,6 @@ func createFolder(client *govmomi.Client, f *folder) error {
return nil return nil
} }
func resourceVSphereFolderRead(d *schema.ResourceData, meta interface{}) error { func resourceVSphereFolderRead(d *schema.ResourceData, meta interface{}) error {
log.Printf("[DEBUG] reading folder: %#v", d) log.Printf("[DEBUG] reading folder: %#v", d)

View File

@ -5,7 +5,6 @@ import (
"os" "os"
"testing" "testing"
"github.com/hashicorp/terraform/helper/resource" "github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/terraform" "github.com/hashicorp/terraform/terraform"
"github.com/vmware/govmomi" "github.com/vmware/govmomi"
@ -171,7 +170,6 @@ func testAccCheckVSphereFolderExists(n string, f *folder) resource.TestCheckFunc
_, err = object.NewSearchIndex(client.Client).FindChild(context.TODO(), dcFolders.VmFolder, rs.Primary.Attributes["path"]) _, err = object.NewSearchIndex(client.Client).FindChild(context.TODO(), dcFolders.VmFolder, rs.Primary.Attributes["path"])
*f = folder{ *f = folder{
path: rs.Primary.Attributes["path"], path: rs.Primary.Attributes["path"],
} }
@ -206,7 +204,6 @@ func testAccCheckVSphereFolderExistingPathExists(n string, f *folder) resource.T
_, err = object.NewSearchIndex(client.Client).FindChild(context.TODO(), dcFolders.VmFolder, rs.Primary.Attributes["existing_path"]) _, err = object.NewSearchIndex(client.Client).FindChild(context.TODO(), dcFolders.VmFolder, rs.Primary.Attributes["existing_path"])
*f = folder{ *f = folder{
path: rs.Primary.Attributes["path"], path: rs.Primary.Attributes["path"],
} }
@ -235,7 +232,7 @@ func createVSphereFolder(datacenter string, folder_name string) error {
client := testAccProvider.Meta().(*govmomi.Client) client := testAccProvider.Meta().(*govmomi.Client)
f := folder{path: folder_name, datacenter: datacenter,} f := folder{path: folder_name, datacenter: datacenter}
folder, err := object.NewSearchIndex(client.Client).FindByInventoryPath( folder, err := object.NewSearchIndex(client.Client).FindByInventoryPath(
context.TODO(), fmt.Sprintf("%v/vm/%v", datacenter, folder_name)) context.TODO(), fmt.Sprintf("%v/vm/%v", datacenter, folder_name))
@ -254,7 +251,7 @@ func createVSphereFolder(datacenter string, folder_name string) error {
func removeVSphereFolder(datacenter string, folder_name string, existing_path string) resource.TestCheckFunc { func removeVSphereFolder(datacenter string, folder_name string, existing_path string) resource.TestCheckFunc {
f := folder{path: folder_name, datacenter: datacenter, existingPath: existing_path,} f := folder{path: folder_name, datacenter: datacenter, existingPath: existing_path}
return func(s *terraform.State) error { return func(s *terraform.State) error {

View File

@ -344,7 +344,6 @@ func testAccCheckVSphereVirtualMachineDestroy(s *terraform.State) error {
return fmt.Errorf("error %s", err) return fmt.Errorf("error %s", err)
} }
folder := dcFolders.VmFolder folder := dcFolders.VmFolder
if len(rs.Primary.Attributes["folder"]) > 0 { if len(rs.Primary.Attributes["folder"]) > 0 {
si := object.NewSearchIndex(client.Client) si := object.NewSearchIndex(client.Client)
@ -370,7 +369,6 @@ func testAccCheckVSphereVirtualMachineDestroy(s *terraform.State) error {
func testAccCheckVSphereVirtualMachineExistsHasCustomConfig(n string, vm *virtualMachine) resource.TestCheckFunc { func testAccCheckVSphereVirtualMachineExistsHasCustomConfig(n string, vm *virtualMachine) resource.TestCheckFunc {
return func(s *terraform.State) error { return func(s *terraform.State) error {
rs, ok := s.RootModule().Resources[n] rs, ok := s.RootModule().Resources[n]
if !ok { if !ok {
return fmt.Errorf("Not found: %s", n) return fmt.Errorf("Not found: %s", n)
@ -393,7 +391,6 @@ func testAccCheckVSphereVirtualMachineExistsHasCustomConfig(n string, vm *virtua
return fmt.Errorf("error %s", err) return fmt.Errorf("error %s", err)
} }
_, err = object.NewSearchIndex(client.Client).FindChild(context.TODO(), dcFolders.VmFolder, rs.Primary.Attributes["name"]) _, err = object.NewSearchIndex(client.Client).FindChild(context.TODO(), dcFolders.VmFolder, rs.Primary.Attributes["name"])
if err != nil { if err != nil {
return fmt.Errorf("error %s", err) return fmt.Errorf("error %s", err)
@ -493,7 +490,6 @@ func testAccCheckVSphereVirtualMachineExists(n string, vm *virtualMachine) resou
_, err = object.NewSearchIndex(client.Client).FindChild(context.TODO(), folder, rs.Primary.Attributes["name"]) _, err = object.NewSearchIndex(client.Client).FindChild(context.TODO(), folder, rs.Primary.Attributes["name"])
*vm = virtualMachine{ *vm = virtualMachine{
name: rs.Primary.ID, name: rs.Primary.ID,
} }

View File

@ -18,6 +18,7 @@ GIT_DIRTY=$(test -n "`git status --porcelain`" && echo "+CHANGES" || true)
XC_ARCH=${XC_ARCH:-"386 amd64 arm"} XC_ARCH=${XC_ARCH:-"386 amd64 arm"}
XC_OS=${XC_OS:-linux darwin windows freebsd openbsd} XC_OS=${XC_OS:-linux darwin windows freebsd openbsd}
# Get dependencies unless running in quick mode # Get dependencies unless running in quick mode
if [ "${TF_QUICKDEV}x" == "x" ]; then if [ "${TF_QUICKDEV}x" == "x" ]; then
echo "==> Getting dependencies..." echo "==> Getting dependencies..."
@ -30,6 +31,7 @@ rm -f bin/*
rm -rf pkg/* rm -rf pkg/*
mkdir -p bin/ mkdir -p bin/
# If its dev mode, only build for ourself # If its dev mode, only build for ourself
if [ "${TF_DEV}x" != "x" ]; then if [ "${TF_DEV}x" != "x" ]; then
XC_OS=$(go env GOOS) XC_OS=$(go env GOOS)

13
scripts/gofmtcheck.sh Executable file
View File

@ -0,0 +1,13 @@
#!/usr/bin/env bash
# Check gofmt
echo "==> Checking that code complies with gofmt requirements..."
gofmt_files=$(gofmt -l .)
if [[ -n ${gofmt_files} ]]; then
echo 'gofmt needs running on the following files:'
echo "${gofmt_files}"
echo "You can use the command: \`make fmt\` to reformat code."
exit 1
fi
exit 0