dominikh / go-mode.el

Emacs mode for the Go programming language
BSD 3-Clause "New" or "Revised" License
1.39k stars 211 forks source link

Showing test coverage in current buffer #195

Open seriousben opened 7 years ago

seriousben commented 7 years ago

I would really like to have test coverage show in my current (go-mode) buffer. (like atom)

Could you give me some pointers on what would be needed to implement it?

dominikh commented 7 years ago

M-x go-coverage and select the coverage profile you've created with go test beforehand.

seriousben commented 7 years ago

whenever I do this, I end up with separate buffers a go-mode buffer and a go-cov buffer. I might be doing something wrong though.

seriousben commented 7 years ago

I would really like the coverage info (just if a line is covered or not really) to show in my go-mode buffer either as an annotation like what flycheck does or as background color changes.

dominikh commented 7 years ago

No, that's how it works. It creates a new buffer, that while being in go-mode, has the coverage overlay, and can technically be edited in (though that will eventually break the coverage annotations).

What's the use case for displaying the same information in the same buffer?

seriousben commented 7 years ago

Really just ease of use and screen space saving. When I develop, I have 2 buffers opened: 1 for the unit under test + 1 for the test. It would just be more elegant to provide coverage info directly on the source file buffer.

seriousben commented 7 years ago

I would be willing to look into such a feature given some pointers. If it makes sense for this project.

I would see this as a setting for go-mode.

dominikh commented 7 years ago

I guess we can offer it as a customizable option, though personally I'm not really motivated to work on this. If you want to work on this, you'd start somewhere in the go-coverage function, which gets/creates the coverage buffer and creates the colored overlays. You'd want to skip the creation of a buffer, and write a different implementation of go--coverage-make-overlay, one that works better in an existing Go buffer, with syntax highlighting and all.

seriousben commented 7 years ago

Awesome, thanks. I'll give it a try.

LMMilewski commented 7 years ago

FWIW. I used to have a similar workflow. You could try https://github.com/google/coverage

cov-show displays coverage for the current buffer and starts to watch the compilation buffer for updated versions of the profile.

seriousben commented 7 years ago

Nice! Thanks a lot.

On Fri, Jun 9, 2017, 21:11 LMMilewski notifications@github.com wrote:

FWIW. I used to have a similar workflow. You could try https://raw.githubusercontent.com/google/coverage cov-gen displays coverage for the current buffer and starts to watch the compilation buffer for updated versions of the profile.

— You are receiving this because you modified the open/close state. Reply to this email directly, view it on GitHub https://github.com/dominikh/go-mode.el/issues/195#issuecomment-307532337, or mute the thread https://github.com/notifications/unsubscribe-auth/AF3FjpWrdzVQkqRf2r4gwJsPCmdh85Jcks5sCe0ugaJpZM4NuO-w .

oleorhagen commented 7 years ago

@seriousben I'm sry, but I cannot get google-coverage to work out of the box, after running cov-show, this is what I get:

Compilation finished compilation ok. Parsing coverage profile /tmp/cov22712_G0 compilation ok. error in process sentinel: process-lines: go exited with status 1 error in process sentinel: go exited with status 1

Did you solve this yourself? or did it work out of the box?

seriousben commented 7 years ago

In vacation now, I haven't had the chance to look at this yet. But was going to look at how it is implemented to do the same in go-mode.

On Thu, Jul 6, 2017, 06:44 oleorhagen notifications@github.com wrote:

@seriousben https://github.com/seriousben I'm sry, but I cannot get google-coverage to work out of the box, after running cov-show, this is what I get:

Compilation finished compilation ok. Parsing coverage profile /tmp/cov22712_G0 compilation ok. error in process sentinel: process-lines: go exited with status 1 error in process sentinel: go exited with status 1

Did you solve this yourself? or did it work out of the box?

— You are receiving this because you were mentioned.

Reply to this email directly, view it on GitHub https://github.com/dominikh/go-mode.el/issues/195#issuecomment-313385310, or mute the thread https://github.com/notifications/unsubscribe-auth/AF3Fjt4GfRphLFiLhQ0FkPS00DjP1jN9ks5sLNaWgaJpZM4NuO-w .

lukasz-milewski commented 7 years ago

error in process sentinel: process-lines: go exited with status 1

This messages says that running the go command via process-lines returned status 1. It's probably on this line: https://github.com/google/coverage/blob/master/coverage.el#L541

Does the following command work for you when run manually?

go list -f '{{.Root}}'
oleorhagen commented 7 years ago

Well, I did some debugging myself too, and it looks like that was the line. I figured it had something to do with goroot/gopath? So made a new dir and set it's gopath, then cloned a fresh fork, and ran according to instructions. Now I don't get that same error any longer, so that must've been it. However, it still does not highlight the buffer in go mode ( it does however do so in c), I have looked at it a little bit using edebug, in which case I have had to change all the cl-let's to let's, just to get through a run. Not sure if this has to do with edebug, or the code. The hashmap also seems to be saving the wrong keys or something, cause I do get empty blocks returned after running gen, and setting the source to (go-coverage . "Coverage.out") , where I have generated the coverage file myself and keep it in the source-code directory. Any thoughts?

lukasz-milewski commented 7 years ago

Let's start with a clean coverage, go-mode, and Emacs setup:

export GOPATH=/tmp/go/
mkdir -p $GOPATH/src
cd $GOPATH/src/
git clone https://github.com/google/coverage.git
git clone https://github.com/dominikh/go-mode.el.git
emacs -Q 

In the scratch buffer evaluate

(require 'coverage "/tmp/go/src/coverage/coverage.el")
(require 'go-mode "/tmp/go/src/go-mode.el/go-mode.el")

In Emacs, open the file /tmp/go/src/coverage/example/go/example.go and run cov-show

Let me know if that works.

olepor commented 7 years ago

well. That actually worked, so I guess the problem is with my emacs - which is spacemacs. So appareantly this is on me. Still, would you have any ideas as to what might be causing this ?

lukasz-milewski commented 7 years ago

I think that it's an issue with coverge.el. GOPATH must be set (i.e. it can't be empty), which is unfortunate. Try setting GOPATH with setenv within Emacs (setenv "GOPATH" "/your/gopath/here/").

Also, be sure to use cov-show for files under $GOPATH.

seriousben commented 7 years ago

I am getting no profile source for file whenever I run cov-show. But it seems to compile go files nicely.

tpanum commented 7 years ago

@seriousben I'm experiencing the same.

tpanum commented 7 years ago

I believe I found the issue.

I assume historically go list -f '{{.Root}}' returned the root of golang packages with a trailing slash. However, it does not do that any more, which caused cov-show not to show anything (or register coverage profiles as lookups in the hash table failed).

I've fixed the issue in my fork, which can be found here

LMMilewski commented 7 years ago

I'm sorry for a delayed response, I was a little busy during the work week.

Thank you tpanum@ for triage and the fix. It's much appreciated!

I enabled issues on the google/coverage repo, so if anyone runs into any other problems, let's open issues there. I think that we hijacked "Showing test coverage in current buffer" for long enough.

@seriousben

I haven't had the chance to look at this yet. But was going to look at how it is implemented to do the same in go-mode.

I think that for go-mode it would be enough to make it easier to:

That is rather easy to do and doesn't require much code. I believe that while google/coverage works for certain types of workflows, it was a little over-engineered and a simpler solution would be a better fit for go-mode.

dakyskye commented 3 years ago

I get Wrong type argument: stringp nil

markflarup commented 3 years ago

I get Wrong type argument: stringp nil

Did you select the actual go file instead of the file containing the coverage data e.g., coverage.out?