mock

package
v1.1.3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 28, 2025 License: MIT Imports: 10 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var Command = &cobra.Command{
	Use:   "mock",
	Short: "generate mock goner code for interface",
	RunE: func(cmd *cobra.Command, args []string) error {
		if scanDir == "" && scanFile == nil {
			return errors.New("scan file or scan directory, at least one must be specified")
		}

		if packageName == "" {
			return errors.New("package name is required")
		}

		if destinationDir == "" {
			destinationDir = path.Join(scanDir, packageName)
		}

		err := os.MkdirAll(destinationDir, os.ModePerm)
		if err != nil {
			return err
		}

		var mockedStructs []string

		var genCode = func(theFilepath string) error {
			if !strings.HasSuffix(theFilepath, "gone.go") && strings.HasSuffix(theFilepath, ".go") {
				code, err := GenMockCode(theFilepath, packageName)
				if err != nil {
					return err
				}

				filename := filepath.Base(theFilepath)

				newFilepath := path.Join(destinationDir, fmt.Sprintf("%s.gone.go", strings.TrimSuffix(filename, ".go")))

				code = AddGoneCode(code)
				mocked := GetMockedInterface(code)
				mockedStructs = append(mockedStructs, mocked...)

				if len(mocked) > 0 {
					return os.WriteFile(newFilepath, []byte(code), 0644)
				}
			}
			return nil
		}

		if scanDir != "" {

			err = filepath.Walk(scanDir, func(theFilepath string, info os.FileInfo, err error) error {
				if err != nil {
					return err
				}

				if !info.IsDir() {
					return genCode(theFilepath)
				}
				return nil
			})
			if err != nil {
				return err
			}
		}

		for _, f := range scanFile {
			err := genCode(f)
			if err != nil {
				return err
			}
		}

		priestCode := GenMockPriestCode(mockedStructs, packageName)

		return os.WriteFile(path.Join(destinationDir, "load.gone.go"), []byte(priestCode), 0644)
	},
}

Functions

func AddGoneCode

func AddGoneCode(code string) string

func GenMockCode

func GenMockCode(source string, packageName string) (code string, err error)

func GenMockPriestCode

func GenMockPriestCode(mockedStructs []string, packageName string) (code string)

func GetMockedInterface

func GetMockedInterface(code string) (list []string)

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL