Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Check added stack trace parts for filename match #387

Merged
merged 7 commits into from
Sep 13, 2017
Merged

Check added stack trace parts for filename match #387

merged 7 commits into from
Sep 13, 2017

Conversation

fongandrew
Copy link
Contributor

Fixes #386. Also refactors regex checking code a little.

One thing I'm not sure about is how to test this. The issue is actually this line here:
https://github.com/fongandrew/tape/blob/2c242ca9715b6b0bfded23fccb522dd414d96612/lib/test.js#L219

Tape ignores stack trace lines that originate from files starting with that dir variable. Currently, it means ignoring all stack trace lines from within tape's own package directory. The problem is that tape's own tests also live within its package directory, so the at variable is never set on the result object.

We could try changing dir to point to just the lib directory inside the package. This would result in the at being set and we could check it. This should be fine to do, since the stack trace for an assertion error shouldn't run through any file outside of the lib directory, but I'm not sure if that could change in the future.

Also, this would result in 25 or so existing tests failing (since their test outputs don't include the at variable). It wouldn't be a big deal to update those tests (correct behavior should be including the at variable in those test results), but feedback welcome on that.

@ljharb
Copy link
Collaborator

ljharb commented Sep 9, 2017

I think that tests kind of need to not ignore stack trace lines that originate within the tests directory, so I'd think updating those tests is the right path here (also totally fine to do that in a separate PR, and this can be rebased after that's merged)

@fongandrew fongandrew mentioned this pull request Sep 10, 2017
@fongandrew
Copy link
Contributor Author

Updated tests are in #388. This PR is rebased off of that one.

@ljharb
Copy link
Collaborator

ljharb commented Sep 11, 2017

Please rebase this on latest master when you can :-D

@fongandrew
Copy link
Contributor Author

Rebased!

lib/test.js Outdated
var filemRe = /((?:\/|[A-Z]:\\)[^:\s]+:(\d+)(?::(\d+))?)/;
var filem;
var sIndex;
for (sIndex in s.slice(0, 4)) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

for..in loops should be avoided; this would be better done as a for loop, or as Array.prototype.some.call or something

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Array.prototype.forEach would be the cleanest way to do this, but it's not supported in older versions of Node (or older browsers) without polyfill support. Not sure what Tape's polyfill policy is.

I can switch it to some variation of for (sIndex = 0; sIndex < s.slice(0, 4).length; s++) { ... } though.

(I'm not sure what expectations Tape makes

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

True; we have the for-each package to handle forEach but that doesn't handle some.

lib/test.js Outdated
res.file = filem[1];
res.line = Number(filem[2]);
if (filem[3]) res.column = filem[3];

res.at = m[1];
res.at = s.length > 1 ? m[1] : '<anonymous> (' + m[1] + ')';
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why s.length instead of m.length here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

m is the result of the regular expression matching something like at /path/to/file.js:123:10 or at functionName /path/to/file.js:123:10. If we're at this point in the code, m.length should always be the same, where m[1] is the portion of the regex matching everything after at.

s is m[1].split(/\s+/) So if s has a length of 1, we know thatm[1] is just case where there's just a path with no function name.

Although in writing this, it now occurs to me that the original m[1].split(/\s+/) code fails to account for directory and file names with spaces ... lemme go fix that.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we could store s.length > 1 in a nicely named variable, for clarity?

@fongandrew
Copy link
Contributor Author

Added test for a file name with a space in it. Rewrote the stack trace regex to handle that scenario. New approach also gets rid of the for ... in loop.

lib/test.js Outdated
at Test.bound [as run] (/path/to/file.js:123:45)
at /path/to/file.js:123:45
*/
var m = new RegExp(
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it would be really nice if we could use a regex literal, and repeat the parts you want to comment, in the comment, instead of using new RegExp

lib/test.js Outdated
res.functionName = callDescription.split(/s+/)[0]
res.file = filePath;
res.line = Number(m[3]);
if (m[4]) res.column = m[4];
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Number(m[4])?

@fongandrew
Copy link
Contributor Author

Comments addressed (amended old commit).

Copy link
Collaborator

@ljharb ljharb left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems straightforward, thanks!

@ljharb ljharb merged commit cc69501 into tape-testing:master Sep 13, 2017
ljharb added a commit that referenced this pull request Feb 19, 2018
 - [New] use `process.env.NODE_TAPE_OBJECT_PRINT_DEPTH` for the default object print depth (#420)
 - [New] Add "onFailure" listener to test harness (#408)
 - [Fix] fix stack where actual is falsy (#400)
 - [Fix] normalize path separators in stacks (#402)
 - [Fix] fix line numbers in stack traces from inside anon functions (#387)
 - [Fix] Fix dirname in stack traces (#388)
 - [Robustness] Use local reference for clearTimeout global (#385)
 - [Deps] update `function-bind`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants