cweill / gotests

Automatically generate Go test boilerplate from your source code.
Apache License 2.0
4.95k stars 345 forks source link

Test document creation error output.Process:imports.Process:... #56

Closed julianfrank closed 6 years ago

julianfrank commented 6 years ago

Everything was working fine till yesterday but today I'm getting this error whenever I try to create a test for the function in vscode 1.19.1 windows x64 output.Process: imports.Process: C:\Users\Johan\AppData\Local\Temp\gotests_174054079:18:1: expected declaration, found ')'

In the temp folder I only see empty 0 bytes gotests_xxx files

No idea what is the problem

cweill commented 6 years ago

Any updates on this issue?

julianfrank commented 6 years ago

I stopped working on windows and now use Ubuntu... same code works without any problem in ubuntu!

Xjs commented 6 years ago

This issue is still happening. I think I have found the cause:

On Windows, lines usually end with \r\n (CR, LF) unlike Unix's \n (LF).

In goparser.go:165, the start point of the code is set to furthestPos + 1, where furthestPos is the End() of the last ast.ImportSpec in f.Imports. The documentation says »position of first character immediately after the node«, which on Unix will be the subsequent \n, but may be a \r on Windows.

So in a Unix-encoded file, b[furthestPos + 1] would be the closing brace after the imports block, as expected, but in a Windows-encoded file, b[furthestPos + 1] will be the \n after the \r after the last import spec, and so the closing brace will be included in the returned slice.

The solution that seems most viable for me is checking for a CR-LF combination at b[furthestPos+1] and if one is encountered, increasing the counter once more. (Since files may have mixed encodings as well, and files on Windows can also be LF-encoded, especially if they are copied from Unix contexts, I wouldn't use any other heuristic.) I will send a pull request in a minute.

kaskavalci commented 4 years ago

For me it was the commented out import statement in test file. Removing it fixed the problem.

import (
    // . "github.com/smartystreets/goconvey/convey"
)