From 2242c6c7921fcb681d3be7683fba862b4954b04a Mon Sep 17 00:00:00 2001 From: Parsa Ghadimi Date: Wed, 30 May 2018 17:03:55 +0430 Subject: [PATCH] Use wildcard to check stack trace outputs (#3) --- Makefile | 4 +- TODO.txt | 4 -- integration_test.go | 32 +++------- testdata/006_url_imports.ts.out | 1 + .../{013_async_throw.ts => async_error.ts} | 0 testdata/async_error.ts.out | 10 +++ testdata/{007_stack_trace.ts => error.ts} | 0 testdata/error.ts.out | 10 +++ util.go | 45 ++++++++++++++ util_test.go | 62 +++++++++++++++++++ 10 files changed, 141 insertions(+), 27 deletions(-) rename testdata/{013_async_throw.ts => async_error.ts} (100%) create mode 100644 testdata/async_error.ts.out rename testdata/{007_stack_trace.ts => error.ts} (100%) create mode 100644 testdata/error.ts.out create mode 100644 util_test.go diff --git a/Makefile b/Makefile index 437e8882f5..9bcc37f3cf 100644 --- a/Makefile +++ b/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) diff --git a/TODO.txt b/TODO.txt index fc87dd09c3..362897fd29 100644 --- a/TODO.txt +++ b/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 diff --git a/integration_test.go b/integration_test.go index d98856f378..9f780f0fd4 100644 --- a/integration_test.go +++ b/integration_test.go @@ -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. diff --git a/testdata/006_url_imports.ts.out b/testdata/006_url_imports.ts.out index 989ce33e93..f745fe3cf5 100644 --- a/testdata/006_url_imports.ts.out +++ b/testdata/006_url_imports.ts.out @@ -1,2 +1,3 @@ +Downloading http://localhost:4545/testdata/subdir/print_hello.ts Hello success diff --git a/testdata/013_async_throw.ts b/testdata/async_error.ts similarity index 100% rename from testdata/013_async_throw.ts rename to testdata/async_error.ts diff --git a/testdata/async_error.ts.out b/testdata/async_error.ts.out new file mode 100644 index 0000000000..7b7dc9d199 --- /dev/null +++ b/testdata/async_error.ts.out @@ -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] () + at execute (/main.js:[WILDCARD]) + at FileModule.compileAndRun (/main.js:[WILDCARD]) + at /main.js:[WILDCARD] + at /main.js:[WILDCARD] diff --git a/testdata/007_stack_trace.ts b/testdata/error.ts similarity index 100% rename from testdata/007_stack_trace.ts rename to testdata/error.ts diff --git a/testdata/error.ts.out b/testdata/error.ts.out new file mode 100644 index 0000000000..952758df91 --- /dev/null +++ b/testdata/error.ts.out @@ -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] () + at execute (../runtime.ts:[WILDCARD]) + at FileModule.compileAndRun (../runtime.ts:[WILDCARD] diff --git a/util.go b/util.go index 2662a256bc..d5b83c3937 100644 --- a/util.go +++ b/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 +} diff --git a/util_test.go b/util_test.go new file mode 100644 index 0000000000..9f44454dff --- /dev/null +++ b/util_test.go @@ -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] () + 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] () + 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).") + } +}