Join GitHub today
GitHub is home to over 20 million developers working together to host and review code, manage projects, and build software together.
(PUP-6452) Allow defaultfor to accept regex #5069
Conversation
petems
added
the
#puppethack
label
Jun 28, 2016
puppetcla
commented
Jun 28, 2016
|
CLA signed by all contributors. |
|
I was just thinking earlier today that this would be super useful (especially for things like yum/dnf and systemd). Thanks for taking the time to open it! I'm not gonna do a serious review at 22:30, but I'll get back to you on this as soon as I have some spare cycles. |
|
Would it be possible to make this more flexible than just regex? AIUI, even for Fedora, it's really F22+, not just F2{2-9}. That is, when F30 comes out, the regex is no longer correct and there would be a regression, right? Similarly for Ubuntu, we want to say 15.04 was the turning point for systemd being the default, and just be able to go off that value as to what the default init-script provider is. |
kylog
commented
Jul 6, 2016
|
Another idea: have
I haven't looked to see if this would fly or not, just tossing out ideas :) |
|
@kylog that's probably the way to go moving forward. In my mind that makes regex not super useful - but I'm also not sure anyone on Client will have the bandwidth to make that change. I don't think we should block small improvements on us having a better idea but not actually doing it @petems I'm happy to merge this as-is, unless you feel like you want to take on the defaultfor accepting a block change, in which case I'd consider that to supersede this PR |
branan
added
the
Waiting on Contributor
label
Jul 12, 2016
|
@branan Right now I wouldn't have the bandwidth without some pairing on how to do that (never really got block argument stuff in Ruby). How do we feel about going for Regex in the mean time? |
branan
added
Blocked
and removed
Waiting on Contributor
labels
Jul 26, 2016
kylog
commented
Jul 26, 2016
|
Yeah, I'm +1. |
|
Ok, I'm cool with getting this merged and opening a ticket for more complex block arguments in the future? |
petems
removed
the
Blocked
label
Dec 14, 2016
| - values.include?(fval) | ||
| + if values.any? {|v| v.is_a? Regexp } | ||
| + regex_match = Regexp.union(values) | ||
| + fval =~ regex_match |
joshcooper
Dec 14, 2016
Member
I think we need to anchor non-regexp values, otherwise we'll match on substrings, e.g.
irb(main):001:0> a = /foo/
=> /foo/
irb(main):002:0> b = "bar"
=> "bar"
irb(main):003:0> Regexp.union(a, b)
=> /(?-mix:foo)|bar/
irb(main):004:0> c = Regexp.union(a, b)
=> /(?-mix:foo)|bar/
irb(main):005:0> c =~ 'foo'
=> 0
irb(main):006:0> c =~ 'bar'
=> 0
irb(main):007:0> c =~ 'oobar'
=> 2
Note we match oobar. I'm thinking the regexp would need to be a union of regexp and strings of the form /^#{value}$/ for each non-regexp value, e.g.
irb(main):011:0> d = Regexp.union(a, /^#{b}$/)
=> /(?-mix:foo)|(?-mix:^bar$)/
irb(main):012:0> d =~ 'oobar'
=> nil
irb(main):013:0> d =~ 'bar'
=> 0
Also if values can contain multiline text (eg user-specified input), I'd recommend using \A and \Z instead.
petems
Dec 14, 2016
•
Contributor
So, I ended up asking a question over on Code Review on Stack Exchange, because I felt this logic was overly complex, any I got a suggestion that would fix the above issue and make he code simpler:
fact_val = Facter.value(fact).to_s.downcase
fact_val != "" and [*values].any? { |v| v === fact_val }
So this works, and all the unit tests pass, except the ones about multiple defaultfor... have you got any ideas why that would be?
petems
Dec 14, 2016
Contributor
Failures:
1) Puppet::Provider default providers should find the default provider
Failure/Error: expect(subject.name).to eq(type.defaultprovider.name)
expected: :nondefault
got: :default
(compared using ==)
Diff:
@@ -1,2 +1,2 @@
-:nondefault
+:default
# ./spec/unit/provider_spec.rb:279:in `block (3 levels) in <top (required)>'
2) Puppet::Provider default providers should consider any true value enough to be default
Failure/Error: expect(subject.name).to eq(type.defaultprovider.name)
expected: :nondefault
got: :default
(compared using ==)
Diff:
@@ -1,2 +1,2 @@
-:nondefault
+:default
# ./spec/unit/provider_spec.rb:381:in `block (3 levels) in <top (required)>'
3) Puppet::Provider default providers when there are multiple defaultfor's of equal specificity should be default for the first defaultfor
Failure/Error: expect(provider).to be_default
expected `Puppet::Type::Test::ProviderDefault.default?` to return true, got false
# ./spec/unit/provider_spec.rb:332:in `block (4 levels) in <top (required)>'
4) Puppet::Provider default providers when there are multiple defaultfor's of equal specificity should be default for the last defaultfor
Failure/Error: expect(provider).to be_default
expected `Puppet::Type::Test::ProviderDefault.default?` to return true, got false
# ./spec/unit/provider_spec.rb:339:in `block (4 levels) in <top (required)>'
5) Puppet::Provider default providers when there are multiple defaultfor's with different specificity should be default for a more specific, but matching, defaultfor
Failure/Error: expect(provider).to be_default
expected `Puppet::Type::Test::ProviderDefault.default?` to return true, got false
# ./spec/unit/provider_spec.rb:357:in `block (4 levels) in <top (required)>'
6) Puppet::Provider default providers when there are multiple defaultfor's with different specificity should be default for a more specific, but matching, defaultfor with regex
Failure/Error: expect(provider).to be_default
expected `Puppet::Type::Test::ProviderDefault.default?` to return true, got false
# ./spec/unit/provider_spec.rb:365:in `block (4 levels) in <top (required)>'
7) Puppet::Provider default providers when there are multiple defaultfor's with different specificity should be default for a less specific, but matching, defaultfor
Failure/Error: expect(provider).to be_default
expected `Puppet::Type::Test::ProviderDefault.default?` to return true, got false
# ./spec/unit/provider_spec.rb:372:in `block (4 levels) in <top (required)>'
Finished in 2.86 seconds (files took 1.4 seconds to load)
233 examples, 7 failures
|
@petems resurrecting this PR - do you want to push up your latest code that caused the test failures you referenced in your last comment? We can help debug it. |
|
@MosesMendoza Done! |
ScottGarman
assigned
Magisus
Jul 18, 2017
|
The spec failures are equality failures: the first group of failures are due to |
|
@petems will you have time to play with this more? Probably going to need a different solution than |
Magisus
added
the
Waiting on Contributor
label
Aug 1, 2017
|
@petems i'm going to push a commit up to your branch that addresses these failures if i have access, otherwise I'll close this and open a new with your changes and mine |
|
ok I don't have push access so i'll open a new branch/pr |
MosesMendoza
referenced this pull request
Oct 19, 2017
Merged
(PUP-6452) Allow defaultfor to accept regex #6292
|
superseded by #6292 |
petems commentedJun 28, 2016
Code is rough right now, but working according to the specs!😄