Skip to content

Commit

Permalink
Use wildcard to check stack trace outputs (denoland#3)
Browse files Browse the repository at this point in the history
  • Loading branch information
qti3e authored and ry committed May 30, 2018
1 parent 1156467 commit 2242c6c
Show file tree
Hide file tree
Showing 10 changed files with 141 additions and 27 deletions.
4 changes: 3 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 0 additions & 4 deletions TODO.txt
Original file line number Diff line number Diff line change
@@ -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

Expand Down
32 changes: 10 additions & 22 deletions integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")

Expand All @@ -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-----------------
Expand All @@ -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
}

Expand All @@ -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)
})
}
}
Expand Down Expand Up @@ -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.
Expand Down
1 change: 1 addition & 0 deletions testdata/006_url_imports.ts.out
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
Downloading http:https://localhost:4545/testdata/subdir/print_hello.ts
Hello
success
File renamed without changes.
10 changes: 10 additions & 0 deletions testdata/async_error.ts.out
Original file line number Diff line number Diff line change
@@ -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]
File renamed without changes.
10 changes: 10 additions & 0 deletions testdata/error.ts.out
Original file line number Diff line number Diff line change
@@ -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 changes: 45 additions & 0 deletions util.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"fmt"
"net/url"
"os"
"strings"
)

func logDebug(format string, v ...interface{}) {
Expand Down Expand Up @@ -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 changes: 62 additions & 0 deletions util_test.go
Original file line number Diff line number Diff line change
@@ -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).")
}
}

0 comments on commit 2242c6c

Please sign in to comment.