github.com/vugu/vugu@v0.3.5/vugufmt/gofmt_test.go (about)

     1  package vugufmt
     2  
     3  import (
     4  	"io/ioutil"
     5  	"os"
     6  	"path/filepath"
     7  	"testing"
     8  
     9  	"github.com/stretchr/testify/assert"
    10  )
    11  
    12  // TestGoFmtNoError makes sure that the runGoFmt function
    13  // returns expected output when it deals with go code that
    14  // is perfectly formatted. It uses all the .go files in this
    15  // package to test against.
    16  func TestGoFmtNoError(t *testing.T) {
    17  
    18  	fmt := func(f string) {
    19  		// Need to un-relativize the paths
    20  		absPath, err := filepath.Abs(f)
    21  
    22  		if filepath.Ext(absPath) != ".go" {
    23  			return
    24  		}
    25  
    26  		assert.Nil(t, err, f)
    27  		// get a handle on the file
    28  		testFile, err := ioutil.ReadFile(absPath)
    29  		testFileString := string(testFile)
    30  		assert.Nil(t, err, f)
    31  		// run gofmt on it
    32  		out, err := runGoFmt([]byte(testFileString), false)
    33  		assert.Nil(t, err, f)
    34  		// make sure nothing changed!
    35  		assert.NotNil(t, string(out), f)
    36  		assert.Equal(t, testFileString, string(out), f)
    37  	}
    38  
    39  	err := filepath.Walk("./", func(path string, info os.FileInfo, err error) error {
    40  		fmt(path)
    41  		return nil
    42  	})
    43  
    44  	assert.NoError(t, err)
    45  }
    46  
    47  // TestGoFmtError confirms that gofmt is successfully detecting
    48  // an error, and is reporting it in the expected format.
    49  func TestGoFmtError(t *testing.T) {
    50  	testCode := "package yeah\n\nvar hey := woo\n"
    51  	// run gofmt on it
    52  	_, err := runGoFmt([]byte(testCode), false)
    53  	assert.NotNil(t, err)
    54  	assert.Equal(t, 3, err.Line)
    55  	assert.Equal(t, 9, err.Column)
    56  }