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

@ -191,7 +191,7 @@ func testAccCheckAWSDBInstanceSnapshot(s *terraform.State) error {
} else {
if len(resp.DBInstances) != 0 &&
*resp.DBInstances[0].DBInstanceIdentifier == rs.Primary.ID {
*resp.DBInstances[0].DBInstanceIdentifier == rs.Primary.ID {
return fmt.Errorf("DB Instance still exists")
}
}
@ -245,7 +245,7 @@ func testAccCheckAWSDBInstanceNoSnapshot(s *terraform.State) error {
} else {
if len(resp.DBInstances) != 0 &&
*resp.DBInstances[0].DBInstanceIdentifier == rs.Primary.ID {
*resp.DBInstances[0].DBInstanceIdentifier == rs.Primary.ID {
return fmt.Errorf("DB Instance still exists")
}
}

View File

@ -160,12 +160,12 @@ func resourceAwsDynamoDbTable() *schema.Resource {
},
},
"stream_enabled": &schema.Schema{
Type: schema.TypeBool,
Type: schema.TypeBool,
Optional: true,
Computed: true,
},
"stream_view_type": &schema.Schema{
Type: schema.TypeString,
Type: schema.TypeString,
Optional: true,
Computed: true,
StateFunc: func(v interface{}) string {
@ -280,9 +280,9 @@ func resourceAwsDynamoDbTableCreate(d *schema.ResourceData, meta interface{}) er
}
if _, ok := d.GetOk("stream_enabled"); ok {
req.StreamSpecification = &dynamodb.StreamSpecification{
StreamEnabled: aws.Bool(d.Get("stream_enabled").(bool)),
StreamEnabled: aws.Bool(d.Get("stream_enabled").(bool)),
StreamViewType: aws.String(d.Get("stream_view_type").(string)),
}
@ -372,7 +372,7 @@ func resourceAwsDynamoDbTableUpdate(d *schema.ResourceData, meta interface{}) er
}
req.StreamSpecification = &dynamodb.StreamSpecification{
StreamEnabled: aws.Bool(d.Get("stream_enabled").(bool)),
StreamEnabled: aws.Bool(d.Get("stream_enabled").(bool)),
StreamViewType: aws.String(d.Get("stream_view_type").(string)),
}
@ -804,10 +804,10 @@ func waitForTableToBeActive(tableName string, meta interface{}) error {
func validateStreamViewType(v interface{}, k string) (ws []string, errors []error) {
value := v.(string)
viewTypes := map[string]bool {
"KEYS_ONLY": true,
"NEW_IMAGE": true,
"OLD_IMAGE": true,
viewTypes := map[string]bool{
"KEYS_ONLY": true,
"NEW_IMAGE": true,
"OLD_IMAGE": true,
"NEW_AND_OLD_IMAGES": true,
}

View File

@ -24,10 +24,10 @@ func resourceAwsSecurityGroup() *schema.Resource {
Schema: map[string]*schema.Schema{
"name": &schema.Schema{
Type: schema.TypeString,
Optional: true,
Computed: true,
ForceNew: true,
Type: schema.TypeString,
Optional: true,
Computed: true,
ForceNew: true,
ConflictsWith: []string{"name_prefix"},
ValidateFunc: func(v interface{}, k string) (ws []string, errors []error) {
value := v.(string)

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
resource.Test(t, resource.TestCase{
@ -345,7 +345,7 @@ func testAccCheckAWSSecurityGroupDestroy(s *terraform.State) error {
}
func testAccCheckAWSSecurityGroupGeneratedNamePrefix(
resource, prefix string) resource.TestCheckFunc {
resource, prefix string) resource.TestCheckFunc {
return func(s *terraform.State) error {
r, ok := s.RootModule().Resources[resource]
if !ok {

View File

@ -183,7 +183,7 @@ func resourceDigitalOceanRecordDelete(d *schema.ResourceData, meta interface{})
log.Printf("[INFO] Deleting record: %s, %d", domain, id)
resp, delErr := client.Domains.DeleteRecord(domain, id)
resp, delErr := client.Domains.DeleteRecord(domain, id)
if delErr != nil {
// If the record is somehow already destroyed, mark as
// successfully gone

View File

@ -14,9 +14,9 @@ import (
)
type folder struct {
datacenter string
existingPath string
path string
datacenter string
existingPath string
path string
}
func resourceVSphereFolder() *schema.Resource {
@ -47,7 +47,7 @@ func resourceVSphereFolder() *schema.Resource {
}
func resourceVSphereFolderCreate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*govmomi.Client)
f := folder{
@ -67,7 +67,6 @@ func resourceVSphereFolderCreate(d *schema.ResourceData, meta interface{}) error
return resourceVSphereFolderRead(d, meta)
}
func createFolder(client *govmomi.Client, f *folder) error {
finder := find.NewFinder(client.Client, true)
@ -96,41 +95,40 @@ func createFolder(client *govmomi.Client, f *folder) error {
subfolder, err := si.FindByInventoryPath(
context.TODO(), fmt.Sprintf("%v/vm/%v", f.datacenter, workingPath))
if err != nil {
return fmt.Errorf("error %s", err)
} else if subfolder == nil {
log.Printf("[DEBUG] folder not found; creating: %s", workingPath)
folder, err = folder.CreateFolder(context.TODO(), pathPart)
if err != nil {
return fmt.Errorf("error %s", err)
} else if subfolder == nil {
log.Printf("[DEBUG] folder not found; creating: %s", workingPath)
folder, err = folder.CreateFolder(context.TODO(), pathPart)
if err != nil {
return fmt.Errorf("Failed to create folder at %s; %s", workingPath, err)
}
} else {
log.Printf("[DEBUG] folder already exists: %s", workingPath)
f.existingPath = workingPath
folder = subfolder.(*object.Folder)
}
} else {
log.Printf("[DEBUG] folder already exists: %s", workingPath)
f.existingPath = workingPath
folder = subfolder.(*object.Folder)
}
}
return nil
}
func resourceVSphereFolderRead(d *schema.ResourceData, meta interface{}) error {
log.Printf("[DEBUG] reading folder: %#v", d)
client := meta.(*govmomi.Client)
dc, err := getDatacenter(client, d.Get("datacenter").(string))
if err != nil {
return err
}
finder := find.NewFinder(client.Client, true)
finder = finder.SetDatacenter(dc)
folder, err := object.NewSearchIndex(client.Client).FindByInventoryPath(
context.TODO(), fmt.Sprintf("%v/vm/%v", d.Get("datacenter").(string),
d.Get("path").(string)))
context.TODO(), fmt.Sprintf("%v/vm/%v", d.Get("datacenter").(string),
d.Get("path").(string)))
if err != nil {
return err
}
@ -143,9 +141,9 @@ func resourceVSphereFolderRead(d *schema.ResourceData, meta interface{}) error {
}
func resourceVSphereFolderDelete(d *schema.ResourceData, meta interface{}) error {
f := folder{
path: strings.TrimRight(d.Get("path").(string), "/"),
path: strings.TrimRight(d.Get("path").(string), "/"),
existingPath: d.Get("existing_path").(string),
}
@ -156,7 +154,7 @@ func resourceVSphereFolderDelete(d *schema.ResourceData, meta interface{}) error
client := meta.(*govmomi.Client)
deleteFolder(client, &f)
d.SetId("")
return nil
}
@ -175,7 +173,7 @@ func deleteFolder(client *govmomi.Client, f *folder) error {
folderRef, err := si.FindByInventoryPath(
context.TODO(), fmt.Sprintf("%v/vm/%v", f.datacenter, f.path))
if err != nil {
return fmt.Errorf("[ERROR] Could not locate folder %s: %v", f.path, err)
} else {

View File

@ -4,7 +4,6 @@ import (
"fmt"
"os"
"testing"
"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/terraform"
@ -21,7 +20,7 @@ func TestAccVSphereFolder_basic(t *testing.T) {
testMethod := "basic"
resourceName := "vsphere_folder." + testMethod
path := "tf_test_basic"
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
@ -47,7 +46,7 @@ func TestAccVSphereFolder_basic(t *testing.T) {
}
func TestAccVSphereFolder_nested(t *testing.T) {
var f folder
datacenter := os.Getenv("VSPHERE_DATACENTER")
testMethod := "nested"
@ -79,7 +78,7 @@ func TestAccVSphereFolder_nested(t *testing.T) {
}
func TestAccVSphereFolder_dontDeleteExisting(t *testing.T) {
var f folder
datacenter := os.Getenv("VSPHERE_DATACENTER")
testMethod := "dontDeleteExisting"
@ -88,17 +87,17 @@ func TestAccVSphereFolder_dontDeleteExisting(t *testing.T) {
path := existingPath + "/tf_nested/tf_test"
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: resource.ComposeTestCheckFunc(
assertVSphereFolderExists(datacenter, existingPath),
removeVSphereFolder(datacenter, existingPath, ""),
),
Steps: []resource.TestStep{
resource.TestStep{
PreConfig: func() {
PreConfig: func() {
createVSphereFolder(datacenter, existingPath)
},
},
Config: fmt.Sprintf(
testAccCheckVSphereFolderConfig,
testMethod,
@ -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"])
*f = folder{
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"])
*f = folder{
path: rs.Primary.Attributes["path"],
}
@ -217,10 +214,10 @@ func testAccCheckVSphereFolderExistingPathExists(n string, f *folder) resource.T
func assertVSphereFolderExists(datacenter string, folder_name string) resource.TestCheckFunc {
return func(s *terraform.State) error {
return func(s *terraform.State) error {
client := testAccProvider.Meta().(*govmomi.Client)
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))
if err != nil {
return fmt.Errorf("Error: %s", err)
} else if folder == nil {
@ -232,16 +229,16 @@ func assertVSphereFolderExists(datacenter string, folder_name string) resource.T
}
func createVSphereFolder(datacenter string, folder_name string) error {
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(
context.TODO(), fmt.Sprintf("%v/vm/%v", datacenter, folder_name))
context.TODO(), fmt.Sprintf("%v/vm/%v", datacenter, folder_name))
if err != nil {
return fmt.Errorf("error %s", err)
}
}
if folder == nil {
createFolder(client, &f)
@ -253,16 +250,16 @@ func createVSphereFolder(datacenter string, folder_name string) error {
}
func removeVSphereFolder(datacenter string, folder_name string, existing_path string) resource.TestCheckFunc {
f := folder{path: folder_name, datacenter: datacenter, existingPath: existing_path,}
return func(s *terraform.State) error {
f := folder{path: folder_name, datacenter: datacenter, existingPath: existing_path}
return func(s *terraform.State) error {
client := testAccProvider.Meta().(*govmomi.Client)
// finder := find.NewFinder(client.Client, true)
folder, _ := 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))
if folder != nil {
deleteFolder(client, &f)
}
@ -276,4 +273,4 @@ resource "vsphere_folder" "%s" {
path = "%s"
datacenter = "%s"
}
`
`

View File

@ -426,7 +426,7 @@ func resourceVSphereVirtualMachineCreate(d *schema.ResourceData, meta interface{
}
func resourceVSphereVirtualMachineRead(d *schema.ResourceData, meta interface{}) error {
log.Printf("[DEBUG] reading virtual machine: %#v", d)
client := meta.(*govmomi.Client)
dc, err := getDatacenter(client, d.Get("datacenter").(string))
@ -1021,7 +1021,7 @@ func (vm *virtualMachine) deployVirtualMachine(c *govmomi.Client) error {
if err != nil {
return err
}
log.Printf("[DEBUG] folder: %#v", vm.folder)
folder := dcFolders.VmFolder
if len(vm.folder) > 0 {

View File

@ -216,8 +216,8 @@ func TestAccVSphereVirtualMachine_createInExistingFolder(t *testing.T) {
label := os.Getenv("VSPHERE_NETWORK_LABEL_DHCP")
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: resource.ComposeTestCheckFunc(
testAccCheckVSphereVirtualMachineDestroy,
removeVSphereFolder(datacenter, folder, ""),
@ -283,8 +283,8 @@ func TestAccVSphereVirtualMachine_createWithFolder(t *testing.T) {
label := os.Getenv("VSPHERE_NETWORK_LABEL_DHCP")
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: resource.ComposeTestCheckFunc(
testAccCheckVSphereVirtualMachineDestroy,
testAccCheckVSphereFolderDestroy,
@ -344,7 +344,6 @@ func testAccCheckVSphereVirtualMachineDestroy(s *terraform.State) error {
return fmt.Errorf("error %s", err)
}
folder := dcFolders.VmFolder
if len(rs.Primary.Attributes["folder"]) > 0 {
si := object.NewSearchIndex(client.Client)
@ -370,7 +369,6 @@ func testAccCheckVSphereVirtualMachineDestroy(s *terraform.State) error {
func testAccCheckVSphereVirtualMachineExistsHasCustomConfig(n string, vm *virtualMachine) resource.TestCheckFunc {
return func(s *terraform.State) error {
rs, ok := s.RootModule().Resources[n]
if !ok {
return fmt.Errorf("Not found: %s", n)
@ -393,7 +391,6 @@ func testAccCheckVSphereVirtualMachineExistsHasCustomConfig(n string, vm *virtua
return fmt.Errorf("error %s", err)
}
_, err = object.NewSearchIndex(client.Client).FindChild(context.TODO(), dcFolders.VmFolder, rs.Primary.Attributes["name"])
if err != nil {
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"])
*vm = virtualMachine{
name: rs.Primary.ID,
}
@ -598,4 +594,4 @@ resource "vsphere_virtual_machine" "with_folder" {
template = "%s"
}
}
`
`

View File

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