swaggo / swag

Automatically generate RESTful API documentation with Swagger 2.0 for Go.
MIT License
10.45k stars 1.19k forks source link

`gomonkey` breaks tests on macOS #1178

Closed akojo closed 2 years ago

akojo commented 2 years ago

Describe the bug Since macOS doesn't allow writing to text section of programs, trying to use gomonkey will panic.

To Reproduce Steps to reproduce the behavior:

  1. Clone the repo
  2. Run make test
  3. Test will fail with following error
--- FAIL: TestFormater_FormatAPI (0.01s)
    --- PASS: TestFormater_FormatAPI/Format_Test (0.01s)
    --- FAIL: TestFormater_FormatAPI/TestWithMonkeyFilepathAbs (0.00s)
panic: permission denied [recovered]
    panic: permission denied

goroutine 37 [running]:
testing.tRunner.func1.2({0x102df5e60, 0x103110928})
    ~/sdk/go1.17.6/src/testing/testing.go:1209 +0x258
testing.tRunner.func1(0x1400023a680)
    ~/sdk/go1.17.6/src/testing/testing.go:1212 +0x284
panic({0x102df5e60, 0x103110928})
    ~/sdk/go1.17.6/src/runtime/panic.go:1038 +0x21c
github.com/agiledragon/gomonkey/v2.modifyBinary(0x1029c7880, {0x14000218978, 0x18, 0x18})
    ~/go/pkg/mod/github.com/agiledragon/gomonkey/v2@v2.3.1/modify_binary_darwin.go:11 +0x134
github.com/agiledragon/gomonkey/v2.replace(0x1029c7880, 0x1400022dd70)
    ~/go/pkg/mod/github.com/agiledragon/gomonkey/v2@v2.3.1/patch.go:196 +0xb8
github.com/agiledragon/gomonkey/v2.(*Patches).ApplyCore(0x14000313a58, {0x102dd2580, 0x102e53198, 0x13}, {0x102dd2580, 0x1400022dd70, 0x13})
    ~/go/pkg/mod/github.com/agiledragon/gomonkey/v2@v2.3.1/patch.go:158 +0x120
github.com/agiledragon/gomonkey/v2.(*Patches).ApplyFunc(0x140001d2a58, {0x102dd2580, 0x102e53198}, {0x102dd2580, 0x1400022dd70})
    ~/go/pkg/mod/github.com/agiledragon/gomonkey/v2@v2.3.1/patch.go:66 +0x1cc
github.com/agiledragon/gomonkey/v2.ApplyFunc(...)
    ~/go/pkg/mod/github.com/agiledragon/gomonkey/v2@v2.3.1/patch.go:24
github.com/swaggo/swag.TestFormater_FormatAPI.func3(0x1400023a680)
    ~/src/swag/formater_test.go:63 +0x254
testing.tRunner(0x1400023a680, 0x102e52558)
    ~/sdk/go1.17.6/src/testing/testing.go:1259 +0xfc
created by testing.(*T).Run
    ~/sdk/go1.17.6/src/testing/testing.go:1306 +0x328
FAIL    github.com/swaggo/swag  0.212s
FAIL
make: *** [test] Error 1

Expected behavior Tests succeed

Your swag version master

Your go version 1.17.6

Desktop (please complete the following information):

Additional context See for example agiledragon/gomonkey#70

ubogdan commented 2 years ago

@akojo Would you mind taking a look at this issue? I don't own a MacOS.

akojo commented 2 years ago

Yep, I'll take a stab at rewriting the offending tests so that they don't use gomonkey. Though that means I have to also rewrite part of the feature to make it testable without monkey patching.

ubogdan commented 2 years ago

Thanks.

ubogdan commented 2 years ago

Unfortunately, I can't reproduce it on macOS Monterey 12.3.1.

$ go version go version go1.18.1 darwin/amd64

$ go version go version go1.17.6 darwin/amd64

akojo commented 2 years ago

I'm guessing it only affects arm64 macs. I'm working on a PR that fixes the issue. Took me a while because I got a bit carried away while refactoring the code 😄

ubogdan commented 2 years ago

Do you have an eta for this? I'm working on some rewrite for parser.go and It may be nice to not slow you down due to conflicts.