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

Match file paths with spaces in caller regexp #1358

Merged
merged 1 commit into from
Dec 2, 2015

Conversation

rwstauner
Copy link
Contributor

When matching only \S+ I get warnings like this on my ci server:

Cannot digest non-existent file: '/tmp/space char20151201-28026-1sux619/some_ruby.rb20151201-28026-g481xg:1:in `<top (required)>''.
Please set `::_cache_digest` of the serializer
if you'd like to cache it.

Matching the trailing space after :\d+:in wasn't strictly necessary, but seemed like it would help it to be more accurate.

@bf4
Copy link
Member

bf4 commented Dec 1, 2015

duplicate : #1355

@rwstauner
Copy link
Contributor Author

rats. i searched for the "cache" warning but i guess i didn't search for "spaces".

@bf4
Copy link
Member

bf4 commented Dec 1, 2015

you solved it somewhat differently. would you like to merge in changes from the other (mine), or have me add some changes? I don't have failing tests for the difference between the two regexen

@bf4
Copy link
Member

bf4 commented Dec 1, 2015

(As a maintainer, I'd rather someone else get credit...)

@beauby
Copy link
Contributor

beauby commented Dec 1, 2015

Great, I was hoping someone would fix the spaces situation. Thanks 👍

@rwstauner
Copy link
Contributor Author

After fiddling with some extra tests I think your pattern is more accurate.

My original thinking was that the string after the file path (an error message) could include a substring match.
But in this case you aren't matching errors, you're matching an explicit caller line, so I assume the ending will only be something like <top (required)>'` (or

'`),
in which case the greedy regexp is more correct.

An example that passes for non-greedy and fails for greedy:

def test_serializer_file_path_with_path_in_string
  # $ ruby =(echo $'def foo(s); raise s; end;\nfoo("file:123:in string")')
  # /tmp/zshr2gfAE:1:in `foo': file:123:in string (RuntimeError)
  #         from /tmp/zshr2gfAE:2:in `<main>'
  path = '/Users/git/ember js/ember-crm-backend/app/serializers/lead_serializer.rb'
  caller_line = "#{path}:1:in `foo': file:123:in string (RuntimeError)"
  assert_equal path, caller_line[ActiveModel::Serializer::CALLER_FILE]
end

but as I said, I don't think that's appropriate in this case.

So I adjusted mine, added your test, and added an additional (more convoluted) test to confirm the difference between the two. Use whichever pull req makes sense.


def test_serializer_file_path_with_submatch
# The submatch in the path ensures we're using a correctly greedy regexp.
path = '/Users/git/ember js/ember:123:in x/app/serializers/lead_serializer.rb'
Copy link
Member

Choose a reason for hiding this comment

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

If someone has that in their file path I pity them 😺

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, quite.

In my case the file path was generated by another process with apparently little sanitation on the naming ;-)

@bf4
Copy link
Member

bf4 commented Dec 2, 2015

@rwstauner looks good. Could you add a changelog entry as a Fix?

I have a few other comments but nothing big.

I keep thinking there should be a way for the subclass to calculate its own digest outside of the inherited hook, which would be simpler, no? I think the method would just need to be dynamically defined define_singleton_method(:cache_digest) { MD5::Digest.hexdigest(__FILE__) } or something like that, right?

@rwstauner
Copy link
Contributor Author

Sounds like a good idea, but we'd have to find another way to get the file from where the class is inheriting (the caller) since the value of __FILE__ is based on where the token is present.
And class_eval "..." isn't any better (__FILE__ evaluates to (eval)).

bf4 added a commit that referenced this pull request Dec 2, 2015
Match file paths with spaces in caller regexp
@bf4 bf4 merged commit b3b9a46 into rails-api:master Dec 2, 2015
@bf4
Copy link
Member

bf4 commented Dec 2, 2015

Closes #1355

@rwstauner
Copy link
Contributor Author

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants