jenkins-bot has submitted this change and it was merged.

Change subject: plugin: Default auto-configuration of `vagrant_cores`
......................................................................


plugin: Default auto-configuration of `vagrant_cores`

Support automatic configuration in `Setting` itself instead of as an ad
hoc feature of `vagrant_cores` and `vagrant_ram`.

Settings that specify `:auto`, currently just `vagrant_cores`, for their
default value are automatically configured upon running `vagrant
config`.

Bug: T87684
Change-Id: I6b5e53c5aa1b2177a378bda223476203727df391
---
M Gemfile.lock
M lib/mediawiki-vagrant/config.rb
M lib/mediawiki-vagrant/setting.rb
M lib/mediawiki-vagrant/settings/definitions.rb
M lib/mediawiki-vagrant/version.rb
M spec/mediawiki_vagrant/setting_spec.rb
M spec/mediawiki_vagrant/settings/definitions_spec.rb
7 files changed, 103 insertions(+), 14 deletions(-)

Approvals:
  Dduvall: Looks good to me, approved
  jenkins-bot: Verified



diff --git a/Gemfile.lock b/Gemfile.lock
index b77fb18..1877ac5 100644
--- a/Gemfile.lock
+++ b/Gemfile.lock
@@ -23,7 +23,7 @@
 PATH
   remote: .
   specs:
-    mediawiki-vagrant (0.4.0)
+    mediawiki-vagrant (0.5.0)
 
 GEM
   remote: https://rubygems.org/
diff --git a/lib/mediawiki-vagrant/config.rb b/lib/mediawiki-vagrant/config.rb
index ffd5af6..d4d33bb 100644
--- a/lib/mediawiki-vagrant/config.rb
+++ b/lib/mediawiki-vagrant/config.rb
@@ -114,6 +114,9 @@
           Settings.definitions.include?(name) && !setting.internal?
         end
 
+        # Automatically configure settings that defaults to it
+        scope.each { |_, setting| setting.auto! if setting.default == :auto }
+
         if options[:required]
           scope = scope.reject { |_, setting| setting.default? || setting.set? 
}
         end
diff --git a/lib/mediawiki-vagrant/setting.rb b/lib/mediawiki-vagrant/setting.rb
index ee21afc..510a4e5 100644
--- a/lib/mediawiki-vagrant/setting.rb
+++ b/lib/mediawiki-vagrant/setting.rb
@@ -6,6 +6,7 @@
   # @attr description [String] Brief description of the setting
   # @attr help [String] Additional help text
   # @attr default [Object] Default value
+  # @attr auto [Proc] Used to automatically configure the setting
   # @attr coercion [Proc] Used to process a given setting value
   # @attr internal [true, false] Whether the setting is only used internally
   # @attr allows_empty [true, false] Whether to allow empty string values
@@ -15,7 +16,7 @@
   #
   class Setting
     attr_reader :name
-    attr_accessor :description, :help, :default, :coercion, :internal, 
:allows_empty
+    attr_accessor :description, :help, :default, :auto, :coercion, :internal, 
:allows_empty
 
     def initialize(name, value = nil)
       @name = name
@@ -26,6 +27,10 @@
     end
 
     alias allows_empty? allows_empty
+
+    def auto!
+      self.value = @auto.call unless @auto.nil? || set?
+    end
 
     alias internal? internal
 
@@ -46,6 +51,7 @@
     end
 
     def value=(other)
+      other = @auto.call if @auto && other == 'auto'
       @value = @coercion.call(self, other)
     end
   end
diff --git a/lib/mediawiki-vagrant/settings/definitions.rb 
b/lib/mediawiki-vagrant/settings/definitions.rb
index 6d8a8d7..550ce7d 100644
--- a/lib/mediawiki-vagrant/settings/definitions.rb
+++ b/lib/mediawiki-vagrant/settings/definitions.rb
@@ -12,19 +12,15 @@
       description: "Amount of RAM (in MB) allocated to the guest VM",
       help: "Specify 'auto' to automatically allocate 1/4 of your system's 
memory",
       default: 1536,
-      coercion: ->(setting, new) do
-        new = (new == "auto") ? (Environment.total_memory / 4) : new.to_i
-        [setting.default, new].max
-      end
+      auto: -> { Environment.total_memory / 4 },
+      coercion: ->(setting, new) { [setting.default, new.to_i].max }
 
     setting :vagrant_cores,
       description: "CPU cores allocated to the guest VM",
-      help: "If you're on a single-core system, be sure to enter '1'. Specify 
'auto' to automatically allocate all of your host system's cores.",
-      default: 2,
-      coercion: ->(setting, new) do
-        new = (new == "auto") ? Environment.total_cpus : new.to_i
-        new && new.to_i
-      end
+      help: "All of your host system's cores are allocated by default.",
+      default: :auto,
+      auto: -> { Environment.total_cpus },
+      coercion: ->(setting, new) { new && new.to_i }
 
     setting :static_ip,
       description: "IP address assigned to the guest VM",
diff --git a/lib/mediawiki-vagrant/version.rb b/lib/mediawiki-vagrant/version.rb
index 099c991..65ce434 100644
--- a/lib/mediawiki-vagrant/version.rb
+++ b/lib/mediawiki-vagrant/version.rb
@@ -1,3 +1,3 @@
 module MediaWikiVagrant
-  VERSION = '0.4.0'
+  VERSION = '0.5.0'
 end
diff --git a/spec/mediawiki_vagrant/setting_spec.rb 
b/spec/mediawiki_vagrant/setting_spec.rb
index 2465152..b1a33e0 100644
--- a/spec/mediawiki_vagrant/setting_spec.rb
+++ b/spec/mediawiki_vagrant/setting_spec.rb
@@ -8,6 +8,39 @@
     let(:name) { :foo }
     let(:value) { nil }
 
+    describe '#auto!' do
+      subject { setting.auto! }
+
+      context 'where `auto=` is set' do
+        before { setting.auto = -> { :bar } }
+
+        context 'and no value is set' do
+          it 'sets the value using the auto result' do
+            subject
+            expect(setting.value).to eq(:bar)
+          end
+        end
+
+        context 'but a value is set' do
+          let(:value) { :baz }
+
+          it 'sets the value using the auto result' do
+            subject
+            expect(setting.value).to eq(:baz)
+          end
+        end
+      end
+
+      context 'where `auto=` is not set' do
+        context 'and no value is set' do
+          it 'the value remains unset' do
+            subject
+            expect(setting).not_to be_set
+          end
+        end
+      end
+    end
+
     describe '#set?' do
       subject { setting.set? }
 
@@ -82,6 +115,28 @@
           expect(setting.value).to eq(20)
         end
       end
+
+      context 'given a value of "auto"' do
+        let(:new_value) { 'auto' }
+
+        context 'where an auto configuration is defined' do
+          let(:auto) { -> { 15 } }
+
+          before { setting.auto = auto }
+
+          it 'calls the auto configuration' do
+            subject
+            expect(setting.value).to eq(15)
+          end
+        end
+
+        context 'where an auto configuration is not defined' do
+          it 'sets the value as literally "auto"' do
+            subject
+            expect(setting.value).to eq('auto')
+          end
+        end
+      end
     end
   end
 end
diff --git a/spec/mediawiki_vagrant/settings/definitions_spec.rb 
b/spec/mediawiki_vagrant/settings/definitions_spec.rb
index dd59be1..7643253 100644
--- a/spec/mediawiki_vagrant/settings/definitions_spec.rb
+++ b/spec/mediawiki_vagrant/settings/definitions_spec.rb
@@ -1,4 +1,5 @@
 require 'spec_helper'
+require 'mediawiki-vagrant/environment'
 require 'mediawiki-vagrant/settings/definitions'
 
 module MediaWikiVagrant
@@ -34,12 +35,26 @@
             end
           end
         end
+
+        context 'auto configuration' do
+          before { expect(Environment).to 
receive(:total_memory).and_return(8192) }
+
+          it 'allocates 1/4 of total system memory' do
+            subject.auto!
+            expect(subject.value).to eq(2048)
+          end
+
+          it 'should work when set to "auto"' do
+            subject.value = 'auto'
+            expect(subject.value).to eq(2048)
+          end
+        end
       end
 
       describe 'vagrant_cores' do
         subject { definitions[:vagrant_cores] }
 
-        it { is_expected.to have_attributes(default: 2) }
+        it { is_expected.to have_attributes(default: :auto) }
 
         context 'when a new value is set' do
           before { subject.value = '4' }
@@ -48,6 +63,20 @@
             expect(subject.value).to eq(4)
           end
         end
+
+        context 'auto configuration' do
+          before { expect(Environment).to receive(:total_cpus).and_return(8) }
+
+          it 'uses all available cores/CPUs' do
+            subject.auto!
+            expect(subject.value).to eq(8)
+          end
+
+          it 'should work when set to "auto"' do
+            subject.value = 'auto'
+            expect(subject.value).to eq(8)
+          end
+        end
       end
 
       describe 'static_ip' do

-- 
To view, visit https://gerrit.wikimedia.org/r/194948
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: merged
Gerrit-Change-Id: I6b5e53c5aa1b2177a378bda223476203727df391
Gerrit-PatchSet: 4
Gerrit-Project: mediawiki/vagrant
Gerrit-Branch: master
Gerrit-Owner: Dduvall <[email protected]>
Gerrit-Reviewer: BryanDavis <[email protected]>
Gerrit-Reviewer: Dduvall <[email protected]>
Gerrit-Reviewer: Ori.livneh <[email protected]>
Gerrit-Reviewer: jenkins-bot <>

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to