monkey

package
v0.16.3 Latest Latest
Warning

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

Go to latest
Published: Mar 7, 2022 License: MIT Imports: 6 Imported by: 0

README

This package is a fork of https://github.com/bouk/monkey.

As you may expect, this package is unsafe and fragile and probably crash you program, it is only recommended for testing usage.

Notes

  1. Monkey sometimes fails to patch a function if inlining is enabled. Try running your tests with inlining disabled, for example: -gcflags=-l (below go1.10) or -gcflags=all=-l (go1.10 and above). The same command line argument can also be used for build.
  2. Monkey won't work on some security-oriented operating system that don't allow memory pages to be both write and execute at the same time. With the current approach there's not really a reliable fix for this.
  3. Monkey is super unsafe, be sure you know what you are doing.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Unpatch

func Unpatch(target interface{}) bool

Unpatch removes any monkey patch applied to the target. It returns whether the target was patched in the first place.

func UnpatchAll

func UnpatchAll()

UnpatchAll removes all applied monkey patches.

func UnpatchByName

func UnpatchByName(targetName string) bool

UnpatchByName removes any monkey patch applied to the target by it's name. TargetName should be the fully-qualified name of the target function or method.

func UnpatchMethod

func UnpatchMethod(target interface{}, methodName string) bool

UnpatchMethod removes any monkey patch applied to the methodName method of the target. It returns whether the method was patched in the first place.

Types

type PatchGuard

type PatchGuard struct {
	// contains filtered or unexported fields
}

PatchGuard holds the patch status of a patch target and it's replacement.

func Patch

func Patch(target, replacement interface{}) *PatchGuard

Patch replaces a function with replacement. If target or replacement is not a function their types do not match, it panics.

func PatchByName

func PatchByName(targetName string, replacement interface{}) *PatchGuard

PatchByName replaces a function with replacement by it's name. TargetName should be the fully-qualified name of the target function or method. If the target cannot be found or the replacement type does not match, it panics.

func PatchMethod

func PatchMethod(target interface{}, methodName string, replacement interface{}) *PatchGuard

PatchMethod replaces an target's methodName method with replacement. Replacement should expect the receiver (of type target) as the first argument. If the method cannot be found or the replacement type does not match, it panics.

func (*PatchGuard) Restore

func (p *PatchGuard) Restore()

Restore re-apply the monkey patch to the target if removed. If the patch has already been applied, it's a no-op.

func (*PatchGuard) Unpatch

func (p *PatchGuard) Unpatch()

Unpatch removes the monkey patch of the target.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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