-
Notifications
You must be signed in to change notification settings - Fork 171
/
issue_vm.go
66 lines (55 loc) · 1.57 KB
/
issue_vm.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package vm
import (
"bufio"
"fmt"
"os"
"runtime"
)
// InitIssueReportVM initializes a vm in test mode for issue reporting
func InitIssueReportVM(dir string, args []string) (*VM, error) {
v, err := New(dir, args)
v.mode = TestMode
return v, err
}
// PrintError prints an error report string given a vm which evaluated to and Error object
func PrintError(v *VM) {
if v.mainThread.Stack.top() == nil {
fmt.Println("No error detected: stack empty")
return
}
eval := v.mainThread.Stack.top().Target
err, ok := eval.(*Error)
if !ok {
fmt.Println("No error detected")
}
fmt.Printf("# %s\n", err.Type)
fmt.Println(err.Message())
fmt.Printf("### Goby version\n%s\n", Version)
fmt.Printf("### GOBY_ROOT\n%s\n", os.Getenv("GOBY_ROOT"))
fmt.Printf("### Go version\n%s\n", runtime.Version())
fmt.Printf("### GOROOT\n%s\n", os.Getenv("GOROOT"))
fmt.Printf("### GOPATH\n%s\n", os.Getenv("GOPATH"))
fmt.Printf("### Operating system\n%s\n", runtime.GOOS)
t := v.mainThread
cf := t.callFrameStack.top()
file := cf.FileName()
line := cf.SourceLine()
// Print lines in file surrounding error in markdown code block
f, osErr := os.Open(string(file))
if osErr != nil {
fmt.Println("Could not open problem file")
}
scanner := bufio.NewScanner(f)
scanner.Split(bufio.ScanLines)
currLine := 0
// Skip lines until at least 20 lines from error
for ; currLine < line-20; currLine++ {
scanner.Scan()
}
fmt.Println("``` ruby")
// Print until 20 lines past error
for ; currLine < line+20 && scanner.Scan(); currLine++ {
fmt.Printf("%s\n", scanner.Text())
}
fmt.Println("```")
}