github.com/loafoe/cli@v7.1.0+incompatible/cf/commandsloader/commands_loader_test.go (about)

     1  package commandsloader_test
     2  
     3  import (
     4  	"fmt"
     5  	"os"
     6  	"path/filepath"
     7  	"strings"
     8  
     9  	"code.cloudfoundry.org/cli/cf/commandregistry"
    10  	"code.cloudfoundry.org/cli/cf/commandsloader"
    11  	. "github.com/onsi/ginkgo"
    12  	. "github.com/onsi/gomega"
    13  )
    14  
    15  var _ = Describe("CommandsLoader", func() {
    16  	It("references all command packages so all commands can be registered in commandregistry", func() {
    17  		commandsloader.Load()
    18  
    19  		count := walkDirAndCountCommand("../commands")
    20  		Expect(commandregistry.Commands.TotalCommands()).To(Equal(count))
    21  	})
    22  })
    23  
    24  func walkDirAndCountCommand(path string) int {
    25  	cmdCount := 0
    26  
    27  	filepath.Walk(path, func(p string, info os.FileInfo, err error) error {
    28  		if err != nil {
    29  			fmt.Println("Error walking commands directories:", err)
    30  			return err
    31  		}
    32  
    33  		dir := filepath.Dir(p)
    34  
    35  		if !info.IsDir() && !strings.HasSuffix(dir, "fakes") {
    36  			if strings.HasSuffix(info.Name(), ".go") && !strings.HasSuffix(info.Name(), "_test.go") {
    37  				cmdCount += 1
    38  			}
    39  		}
    40  		return nil
    41  	})
    42  
    43  	return cmdCount
    44  }