github.com/vugu/vugu@v0.3.6-0.20240430171613-3f6f402e014b/vugufmt/goimports_test.go (about)

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