agiledragon / gomonkey

gomonkey is a library to make monkey patching in unit tests easy
MIT License
1.96k stars 179 forks source link

it seems not worked in linux OS when I try to mock functions from os package #9

Closed howieyuen closed 4 years ago

howieyuen commented 4 years ago

When I used gomonkey write test case in win10 platform, mocked os.RemoveAll() and it worked, but in Ubuntu14.04 and Ubuntu16.04, not worked. Please take a look at this case, see if it is a bug.

below is my test code and screenshot of test results.

package test

import (
    "fmt"
    "os"
    "testing"

    "github.com/agiledragon/gomonkey"
)

func Test_RemoveAll(t *testing.T) {
    patch := gomonkey.NewPatches()
    patch.ApplyFunc(os.RemoveAll, func(_ string) error {
        fmt.Println("use mock functions to do rm -rf")
        return nil
    })
    defer patch.Reset()
    doRemove("")
}

func doRemove(path string) {
    if err := os.RemoveAll(path); err != nil {
        fmt.Println(err)
    }
}
agiledragon commented 4 years ago

go test -v -gcflags=all=-l

Please refer to the first of the Notes

howieyuen commented 4 years ago

Many thanks!Sorry,I thought the node only refers to inline writing style function。