From 92dc561a545c0d8a37bfe500fd61caaf7fd80c57 Mon Sep 17 00:00:00 2001 From: David Chambers Date: Sun, 2 Dec 2012 17:06:50 -0800 Subject: [PATCH] use `require` rather than `eval` Doctests no longer share scope with `fetch` when run headless. --- lib/doctest.js | 8 ++++++-- src/doctest.coffee | 6 +++++- test/test.coffee | 9 ++++----- test/test.html | 2 +- test/test.js | 9 ++++----- 5 files changed, 20 insertions(+), 14 deletions(-) diff --git a/lib/doctest.js b/lib/doctest.js index c4007c3a..3c9b5dd6 100644 --- a/lib/doctest.js +++ b/lib/doctest.js @@ -124,14 +124,18 @@ } else { fs = require('fs'); return fs.readFile(path, 'utf8', function(err, text) { - var name, source, type, _ref; + var file, name, source, type, _ref; _ref = /[^/]+[.](coffee|js)$/.exec(path), name = _ref[0], type = _ref[1]; console.log("running doctests in " + name + "..."); source = rewrite(text, type); if (type === 'coffee') { source = CoffeeScript.compile(source); } - eval(source); + name += "-" + (+(new Date)); + file = "" + __dirname + "/" + name + ".js"; + fs.writeFileSync(file, source, 'utf8'); + require("./" + name); + fs.unlink(file); return doctest.run(); }); } diff --git a/src/doctest.coffee b/src/doctest.coffee index 6c63fce2..f5ea7688 100644 --- a/src/doctest.coffee +++ b/src/doctest.coffee @@ -77,7 +77,11 @@ fetch = (path) -> console.log "running doctests in #{name}..." source = rewrite text, type source = CoffeeScript.compile source if type is 'coffee' - eval source + name += "-#{+new Date}" + file = "#{__dirname}/#{name}.js" + fs.writeFileSync file, source, 'utf8' + require "./#{name}" + fs.unlink file doctest.run() diff --git a/test/test.coffee b/test/test.coffee index bce76ee9..22b8b66d 100644 --- a/test/test.coffee +++ b/test/test.coffee @@ -64,10 +64,10 @@ do -> # . 7,8,9] # [1,2,3,4,5,6,7,8,9] 14: 'multiline assignment' - # > @text = "input " + + # > @string = "input " + # . "may span many " + # . "lines" - # > text + # > string # "input may span many lines" 15: 'spaces following "//" and ">" are optional' @@ -88,9 +88,8 @@ do -> # 25 20: 'variable in creation context is not accessible' - # > @text = "The quick brown fox jumps over the lazy dog" - # > window.text - # "The quick brown fox jumps over the lazy dog" + # > typeof text + # "undefined" 21: 'the rewriter should not rely on automatic semicolon insertion' # > "the rewriter should not rely" diff --git a/test/test.html b/test/test.html index 9fc0a0a7..5d6abe8a 100644 --- a/test/test.html +++ b/test/test.html @@ -77,7 +77,7 @@ [true, 25, , 88]) t('variable in creation context is not accessible', - [true, '"The quick brown fox jumps over the lazy dog"', , 93]) + [true, '"undefined"', , 92]) start() } diff --git a/test/test.js b/test/test.js index 480b123d..7b621566 100644 --- a/test/test.js +++ b/test/test.js @@ -64,10 +64,10 @@ global = 'global' // . 7,8,9] // [1,2,3,4,5,6,7,8,9] 14, 'multiline assignment' - // > text = "input " + + // > string = "input " + // . "may span many " + // . "lines" - // > text + // > string // "input may span many lines" 15, 'spaces following "//" and ">" are optional' @@ -88,9 +88,8 @@ global = 'global' // 25 // comment 20, 'variable in creation context is not accessible' - // > text = "The quick brown fox jumps over the lazy dog" - // > window.text - // "The quick brown fox jumps over the lazy dog" + // > typeof text + // "undefined" 21, 'the rewriter should not rely on automatic semicolon insertion' // > "the rewriter should not rely"