mirror of
https://github.com/denoland/deno.git
synced 2024-12-22 07:14:47 -05:00
Use wildcard to check stack trace outputs (#3)
This commit is contained in:
parent
1156467c93
commit
2242c6c792
10 changed files with 141 additions and 27 deletions
4
Makefile
4
Makefile
|
@ -30,7 +30,9 @@ GO_FILES = \
|
|||
os.go \
|
||||
os_test.go \
|
||||
timers.go \
|
||||
util.go
|
||||
util.go \
|
||||
util_test.go \
|
||||
integration_test.go
|
||||
|
||||
|
||||
deno: msg.pb.go $(GO_FILES)
|
||||
|
|
4
TODO.txt
4
TODO.txt
|
@ -1,9 +1,5 @@
|
|||
- Fix v8_source_maps.ts so that we don't get random segfaults.
|
||||
|
||||
- Add wildcard support to testdata/*.out tests so that we can check
|
||||
the stack trace output in testdata/007_stack_trace.ts and
|
||||
testdata/013_async_throw.ts.
|
||||
|
||||
- Remove text-encoding.d.ts because TS2.8 includes the declarations.
|
||||
https://github.com/DefinitelyTyped/DefinitelyTyped/issues/24695
|
||||
|
||||
|
|
|
@ -47,7 +47,7 @@ func listTestFiles() []string {
|
|||
return out
|
||||
}
|
||||
|
||||
func checkOutput(t *testing.T, outFile string) {
|
||||
func checkOutput(t *testing.T, outFile string, shouldSucceed bool) {
|
||||
outFile = path.Join("testdata", outFile)
|
||||
jsFile := strings.TrimSuffix(outFile, ".out")
|
||||
|
||||
|
@ -57,10 +57,12 @@ func checkOutput(t *testing.T, outFile string) {
|
|||
}
|
||||
|
||||
actual, _, err := deno(jsFile)
|
||||
if err != nil {
|
||||
t.Fatal(err.Error())
|
||||
if shouldSucceed && err != nil {
|
||||
t.Fatalf("Expected success %s", err.Error())
|
||||
} else if !shouldSucceed && err == nil {
|
||||
t.Fatalf("Expected failure but got success")
|
||||
}
|
||||
if bytes.Compare(actual, expected) != 0 {
|
||||
if !patternMatch(string(expected), string(actual)) {
|
||||
t.Fatalf(`Actual output does not match expected.
|
||||
-----Actual-------------------
|
||||
%s-----Expected-----------------
|
||||
|
@ -77,10 +79,9 @@ func deno(inputFn string) (actual []byte, cachedir string, err error) {
|
|||
cmd := exec.Command(denoFn, "--cachedir="+cachedir, inputFn)
|
||||
var out bytes.Buffer
|
||||
cmd.Stdout = &out
|
||||
cmd.Stderr = &out
|
||||
err = cmd.Run()
|
||||
if err == nil {
|
||||
actual = out.Bytes()
|
||||
}
|
||||
actual = out.Bytes()
|
||||
return
|
||||
}
|
||||
|
||||
|
@ -100,7 +101,8 @@ func TestIntegrationFiles(t *testing.T) {
|
|||
outFiles := listTestFiles()
|
||||
for _, outFile := range outFiles {
|
||||
t.Run(outFile, func(t *testing.T) {
|
||||
checkOutput(t, outFile)
|
||||
shouldSucceed := strings.Index(outFile, "error") < 0
|
||||
checkOutput(t, outFile, shouldSucceed)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
@ -133,20 +135,6 @@ func TestIntegrationUrlArgs(t *testing.T) {
|
|||
}
|
||||
}
|
||||
|
||||
func TestErrors(t *testing.T) {
|
||||
integrationTestSetup()
|
||||
|
||||
_, _, err := deno("testdata/013_async_throw.ts")
|
||||
if err == nil {
|
||||
t.Fatalf("Expected error.")
|
||||
}
|
||||
|
||||
_, _, err = deno("testdata/007_stack_trace.ts")
|
||||
if err == nil {
|
||||
t.Fatalf("Expected error.")
|
||||
}
|
||||
}
|
||||
|
||||
func TestTestsTs(t *testing.T) {
|
||||
integrationTestSetup()
|
||||
// TODO Need unit test for each of the permissions.
|
||||
|
|
1
testdata/006_url_imports.ts.out
vendored
1
testdata/006_url_imports.ts.out
vendored
|
@ -1,2 +1,3 @@
|
|||
Downloading http://localhost:4545/testdata/subdir/print_hello.ts
|
||||
Hello
|
||||
success
|
||||
|
|
10
testdata/async_error.ts.out
vendored
Normal file
10
testdata/async_error.ts.out
vendored
Normal file
|
@ -0,0 +1,10 @@
|
|||
hello
|
||||
before error
|
||||
error Error: error
|
||||
at foo ([WILDCARD]testdata/async_error.ts:4:11)
|
||||
at eval ([WILDCARD]testdata/async_error.ts:6:1)
|
||||
at Object.eval [as globalEval] (<anonymous>)
|
||||
at execute (/main.js:[WILDCARD])
|
||||
at FileModule.compileAndRun (/main.js:[WILDCARD])
|
||||
at /main.js:[WILDCARD]
|
||||
at /main.js:[WILDCARD]
|
10
testdata/error.ts.out
vendored
Normal file
10
testdata/error.ts.out
vendored
Normal file
|
@ -0,0 +1,10 @@
|
|||
/main.js:[WILDCARD]
|
||||
throw _iteratorError;
|
||||
^
|
||||
Error: bad
|
||||
at foo ([WILDCARD]testdata/error.ts:2:9)
|
||||
at bar ([WILDCARD]testdata/error.ts:6:3)
|
||||
at eval ([WILDCARD]testdata/error.ts:9:1)
|
||||
at Object.eval [as globalEval] (<anonymous>)
|
||||
at execute (../runtime.ts:[WILDCARD])
|
||||
at FileModule.compileAndRun (../runtime.ts:[WILDCARD]
|
45
util.go
45
util.go
|
@ -6,6 +6,7 @@ import (
|
|||
"fmt"
|
||||
"net/url"
|
||||
"os"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func logDebug(format string, v ...interface{}) {
|
||||
|
@ -59,3 +60,47 @@ func async(cb func()) {
|
|||
wg.Done()
|
||||
}()
|
||||
}
|
||||
|
||||
const wildcard = "[WILDCARD]"
|
||||
|
||||
// Matches the pattern string against the text string. The pattern can
|
||||
// contain "[WILDCARD]" substrings which will match one or more characters.
|
||||
// Returns true if matched.
|
||||
func patternMatch(pattern string, text string) bool {
|
||||
// Empty pattern only match empty text.
|
||||
if len(pattern) == 0 {
|
||||
return len(text) == 0
|
||||
}
|
||||
|
||||
if pattern == wildcard {
|
||||
return true
|
||||
}
|
||||
|
||||
parts := strings.Split(pattern, wildcard)
|
||||
|
||||
if len(parts) == 1 {
|
||||
return pattern == text
|
||||
}
|
||||
|
||||
if strings.HasPrefix(text, parts[0]) {
|
||||
text = text[len(parts[0]):]
|
||||
} else {
|
||||
return false
|
||||
}
|
||||
|
||||
for i := 1; i < len(parts); i++ {
|
||||
// If the last part is empty, we match.
|
||||
if i == len(parts)-1 {
|
||||
if parts[i] == "" || parts[i] == "\n" {
|
||||
return true
|
||||
}
|
||||
}
|
||||
index := strings.Index(text, parts[i])
|
||||
if index < 0 {
|
||||
return false
|
||||
}
|
||||
text = text[index+len(parts[i]):]
|
||||
}
|
||||
|
||||
return len(text) == 0
|
||||
}
|
||||
|
|
62
util_test.go
Normal file
62
util_test.go
Normal file
|
@ -0,0 +1,62 @@
|
|||
package deno
|
||||
|
||||
import (
|
||||
"testing"
|
||||
)
|
||||
|
||||
const exStackTrace = `hello
|
||||
before error
|
||||
error Error: error
|
||||
at foo (/Users/rld/go/src/github.com/ry/deno/testdata/013_async_throw.ts:4:11)
|
||||
at eval (/Users/rld/go/src/github.com/ry/deno/testdata/013_async_throw.ts:6:1)
|
||||
at Object.eval [as globalEval] (<anonymous>)
|
||||
at execute (/main.js:144781:15)
|
||||
at FileModule.compileAndRun (/main.js:144678:13)
|
||||
at /main.js:145161:13
|
||||
at /main.js:15733:13`
|
||||
const exStackTracePattern = `hello
|
||||
before error
|
||||
error Error: error
|
||||
at foo ([WILDCARD]testdata/013_async_throw.ts:4:11)
|
||||
at eval ([WILDCARD]testdata/013_async_throw.ts:6:1)
|
||||
at Object.eval [as globalEval] (<anonymous>)
|
||||
at execute (/main.js:[WILDCARD]`
|
||||
|
||||
func TestPatternMatch(t *testing.T) {
|
||||
if patternMatch("aa", "a") != false {
|
||||
t.Fatalf("Wrong resullt (1).")
|
||||
}
|
||||
if patternMatch("aaa[WILDCARD]b", "aaaxsdfdb") != true {
|
||||
t.Fatalf("Wrong resullt (2).")
|
||||
}
|
||||
if patternMatch("aab[WILDCARD]", "xsd") != false {
|
||||
t.Fatalf("Wrong resullt (3).")
|
||||
}
|
||||
if patternMatch("a[WILDCARD]b[WILDCARD]c", "abc") != true {
|
||||
t.Fatalf("Wrong resullt (4).")
|
||||
}
|
||||
if patternMatch("a[WILDCARD]b[WILDCARD]c", "axbc") != true {
|
||||
t.Fatalf("Wrong resullt (5).")
|
||||
}
|
||||
if patternMatch("a[WILDCARD]b[WILDCARD]c", "abxc") != true {
|
||||
t.Fatalf("Wrong resullt (6).")
|
||||
}
|
||||
if patternMatch("a[WILDCARD]b[WILDCARD]c", "axbxc") != true {
|
||||
t.Fatalf("Wrong resullt (7).")
|
||||
}
|
||||
if patternMatch("a[WILDCARD]b[WILDCARD]c", "abcx") != false {
|
||||
t.Fatalf("Wrong resullt (8).")
|
||||
}
|
||||
if patternMatch("a[WILDCARD][WILDCARD]c", "abc") != true {
|
||||
t.Fatalf("Wrong resullt (9).")
|
||||
}
|
||||
if patternMatch("a[WILDCARD][WILDCARD]c", "ac") != true {
|
||||
t.Fatalf("Wrong resullt (10).")
|
||||
}
|
||||
}
|
||||
|
||||
func TestPatternMatchStackTrace(t *testing.T) {
|
||||
if patternMatch(exStackTracePattern, exStackTrace) != true {
|
||||
t.Fatalf("Wrong resullt (11).")
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue