@@ -21,6 +21,7 @@ var fs = require('fs');
2121var gcloud = require ( '../' ) ;
2222var glob = require ( 'glob' ) ;
2323var mitm = require ( 'mitm' ) ;
24+ var prop = require ( 'propprop' ) ;
2425var vm = require ( 'vm' ) ;
2526
2627var util = require ( '../lib/common/util.js' ) ;
@@ -34,7 +35,21 @@ function runCodeInSandbox(code, sandbox) {
3435 } ) ;
3536 } catch ( err ) {
3637 // rethrow the error with code for context and resolving issues faster.
37- throw new Error ( '\n' + code + '\n\n' + err . message ) ;
38+ var lineCol = err . stack . match ( 'assert-code\.vm:(.+):(.+)' ) ;
39+ lineCol . line = lineCol [ 1 ] ;
40+ lineCol . col = lineCol [ 2 ] ;
41+
42+ var lines = code . split ( '\n' )
43+ . filter ( function ( line , index ) {
44+ if ( index < lineCol . line ) {
45+ return line ;
46+ }
47+ } )
48+ . join ( '\n' ) ;
49+
50+ err . message = '\n' + lines + '\n\n' + err . message ;
51+
52+ throw err ;
3853 }
3954}
4055
@@ -96,20 +111,20 @@ describe('documentation', function() {
96111 global : global
97112 } ;
98113
99- var examples = fileDocBlocks . methods . map ( function ( method ) {
100- return method . examples . map ( function ( example ) {
101- return example . code ;
102- } ) . join ( '\n' ) ;
103- } ) ;
114+ fileDocBlocks . methods . forEach ( function ( method ) {
115+ var code = method . examples . map ( prop ( 'code' ) ) . join ( '\n' )
116+ . replace ( / r e q u i r e \( \' g c l o u d \' \) / g, 'require(\'..\/\')' )
117+ . replace ( / r e q u i r e \( \' g c l o u d / g, 'require(\'..' ) ;
118+
119+ var displayName = filename
120+ . replace ( 'docs/json/master/' , '' )
121+ . replace ( '.json' , '.js' ) ;
104122
105- var code = examples
106- . join ( '\n' )
107- . replace ( / r e q u i r e \( \' g c l o u d \' \) / g, 'require(\'..\/\')' )
108- . replace ( / r e q u i r e \( \' g c l o u d / g, 'require(\'..' ) ;
123+ displayName += '#' + method . id ;
109124
110- it ( 'should run tests for ' + filename + ' without errors' , function ( ) {
111- this . timeout ( 5000 ) ;
112- assert . doesNotThrow ( runCodeInSandbox . bind ( null , code , sandbox ) ) ;
125+ it ( 'should run ' + displayName + ' example without errors' , function ( ) {
126+ assert . doesNotThrow ( runCodeInSandbox . bind ( null , code , sandbox ) ) ;
127+ } ) ;
113128 } ) ;
114129 } ) ;
115130} ) ;
0 commit comments