generated from codecrafters-io/tester-template
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathutils.go
66 lines (56 loc) · 2.02 KB
/
utils.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 testcases
import (
"fmt"
"os"
"regexp"
"strings"
"github.com/codecrafters-io/tester-utils/logger"
"github.com/fatih/color"
)
func createTempFileWithContents(contents string) (string, error) {
tmpFileName := "test.lox"
if _, err := os.Stat(tmpFileName); err == nil {
err := os.Remove(tmpFileName)
if err != nil {
return "", fmt.Errorf("CodeCrafters internal error. Error removing tmp file: %v", err)
}
}
tmpFile, err := os.Create(tmpFileName)
if err != nil {
return "", fmt.Errorf("CodeCrafters internal error. Error creating tmp file: %v", err)
}
_, err = tmpFile.WriteString(contents)
if err != nil {
return "", fmt.Errorf("CodeCrafters internal error. Error writing to tmp file: %v", err)
}
err = tmpFile.Close()
if err != nil {
return "", fmt.Errorf("CodeCrafters internal error. Error closing tmp file: %v", err)
}
return tmpFile.Name(), nil
}
func logReadableFileContents(logger *logger.Logger, fileContents string) {
logger.Infof("Writing contents to ./test.lox:")
// If the file contents contain a single %, it will be decoded as a format specifier
// And it will add a `(MISSING)` to the log line
printableFileContents := strings.ReplaceAll(fileContents, "%", "%%")
printableFileContents = strings.ReplaceAll(printableFileContents, "\t", "<|TAB|>")
regex1 := regexp.MustCompile("[ ]+\n")
regex2 := regexp.MustCompile("[ ]+$")
printableFileContents = regex1.ReplaceAllString(printableFileContents, "<|SPACE|>")
printableFileContents = regex2.ReplaceAllString(printableFileContents, "<|SPACE|>")
for _, line := range strings.Split(printableFileContents, "\n") {
if strings.Contains(line, "//") {
code := strings.Split(line, "//")[0]
comment := "//" + strings.Split(line, "//")[1]
logger.Plainf(color.YellowString("[test.lox]") + " " + color.YellowString(code) + " " + color.YellowString(comment))
} else {
logger.Plainf(color.YellowString("[test.lox]") + " " + line)
}
}
}
var exitCodeToErrorTypeMapping = map[int]string{
0: "no error",
65: "compile error",
70: "runtime error",
}