-
Notifications
You must be signed in to change notification settings - Fork 3.2k
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
Fix bug on phone number generator for en-US
locale caused by incorrect .yml
file structure
#2924
Fix bug on phone number generator for en-US
locale caused by incorrect .yml
file structure
#2924
Conversation
test/test_en_us_locale.rb
Outdated
en_translations = I18n.translate('faker.cell_phone.formats', locale: :en, raise: true) | ||
en_us_translations = I18n.translate('faker.cell_phone.formats', locale: Faker::Config.locale, raise: true) | ||
|
||
refute_equal en_translations, en_us_translations |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I made up this test to try and capture the essence of the problem, but I'm open to writing the test in some other way if there's a better pattern to follow.
It seems that the root cause of the problem is that there was a .yml
file with a structure different than the "schema" that's expected of all files. So in that sense this feels like almost the wrong thing to test because it's too isolated. I'm not familiar with the codebase though, and this seems to cover the broken case.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, I understand the intent, but I'm not sure this is the right test to add either.
I would love to see a test that could catch some obviously wrong en-US numbers. None of the existing tests caught this issue.
My understanding is that test_validity_of_phone_method_output
was supposed to catch things like this, but it did not. It is also very hard to read due to the complex regex.
Maybe we could add a simpler test that does some straightforward sanity checks or rejects impossible area codes, etc.?
If not, that's fine. We should remove this test, and I'll open another issue specific for improving the coverage for en-US phone numbers.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since test_validity_of_phone_method_output
only tests phone_number
and not a method that calls cell_phone
, it doesn't cover the broken case. A test like test_validity_of_phone_method_output
against cell_phone
would cover it, but maybe not deterministically, because I think all the fallback en
value in cell_phone.formats
might look like en-US
's cell_phone.formats
, except for the leading 1
values? That might not be true though.
As it's unclear how exactly to test this, I'll go with removing my test from this PR!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Removed!
a36a8d8
to
24a4b19
Compare
A short-term workaround until this is fixed is to explicitly provide the corrected en-US:
faker:
cell_phone:
formats:
- "#{PhoneNumber.area_code}-#{PhoneNumber.exchange_code}-#{PhoneNumber.subscriber_number}"
- "(#{PhoneNumber.area_code}) #{PhoneNumber.exchange_code}-#{PhoneNumber.subscriber_number}"
- "#{PhoneNumber.area_code}-#{PhoneNumber.exchange_code}-#{PhoneNumber.subscriber_number}"
- "#{PhoneNumber.area_code}.#{PhoneNumber.exchange_code}.#{PhoneNumber.subscriber_number}"
- "#{PhoneNumber.area_code}-#{PhoneNumber.exchange_code}-#{PhoneNumber.subscriber_number}"
- "(#{PhoneNumber.area_code}) #{PhoneNumber.exchange_code}-#{PhoneNumber.subscriber_number}"
- "#{PhoneNumber.area_code}-#{PhoneNumber.exchange_code}-#{PhoneNumber.subscriber_number}"
- "#{PhoneNumber.area_code}.#{PhoneNumber.exchange_code}.#{PhoneNumber.subscriber_number}" |
LGTM! Thanks for investigating. I was banging my head against the wall on that mistagged release during bisect. |
en-US
locale
en-US
localeen-US
locale caused by incorrect .yml
file structure
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nice catch, thanks for fixing this!
I left a suggestion about the test, let me know if it makes sense.
test/test_en_us_locale.rb
Outdated
en_translations = I18n.translate('faker.cell_phone.formats', locale: :en, raise: true) | ||
en_us_translations = I18n.translate('faker.cell_phone.formats', locale: Faker::Config.locale, raise: true) | ||
|
||
refute_equal en_translations, en_us_translations |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, I understand the intent, but I'm not sure this is the right test to add either.
I would love to see a test that could catch some obviously wrong en-US numbers. None of the existing tests caught this issue.
My understanding is that test_validity_of_phone_method_output
was supposed to catch things like this, but it did not. It is also very hard to read due to the complex regex.
Maybe we could add a simpler test that does some straightforward sanity checks or rejects impossible area codes, etc.?
If not, that's fine. We should remove this test, and I'll open another issue specific for improving the coverage for en-US phone numbers.
`PhoneNumber.cell_phone` expects an i18n key of `cell_phone.formats`, but the en-US.yml file currently has `faker.phone_number.cell_phone` instead of `faker.cell_phone`. The result of that mismatch is that `cell_phone` will ignore the defined formats, and potentially generate invalid US numbers (e.g. those with an area code beginning with 1) when the locale is en-US. 31d99d1 reworked YAML structure and appears to have inadvertently moved the key. You can see the change in US number behavior in a console: Faker::Config.locale = "en-US" Faker::PhoneNumber.translate("faker.cell_phone.formats") On 3.3.0 this returns ["###-###-####", "(###) ###-####", "###.###.####", "### ### ####"] because it's the fallback value within the `en` (not `en-US`) locale file. The correct value should be the formats outdented in this commit.
24a4b19
to
c7772c6
Compare
@thdaraujo thanks for the review! Let me know if I need to add a CHANGELOG entry or anything. Happy to make any other revisions. |
Can confirm that this generates mostly valid numbers for Here's an example test run (based on @theycallmeswift 's reproduction script in #2922):
|
Motivation / Background
PhoneNumber.cell_phone
expects an i18n key ofcell_phone.formats
, but the en-US.yml file currently hasfaker.phone_number.cell_phone
instead offaker.cell_phone
.The result of that mismatch is that
cell_phone
will ignore the defined formats, and potentially generate invalid US numbers (e.g. those with an area code beginning with 1) when the locale is en-US.31d99d1 reworked YAML structure and appears to have inadvertently moved the key.
You can see the change in US number behavior in a console:
On 3.3.0 this returns
because it's the fallback value within the
en
(noten-US
) locale file. The correct value should be the formats outdented in this commit.Fixes #2922 (although it's currently closed).
Additional information
See #2922 (comment).
Checklist
Before submitting the PR make sure the following are checked:
[Fix #issue-number]
If you're proposing a new generator or locale: